Skip to content

Commit

Permalink
Improved pointer location for ToolTip.
Browse files Browse the repository at this point in the history
  • Loading branch information
weisJ committed Oct 19, 2019
1 parent 887bbf8 commit 7c8d603
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 51 deletions.
81 changes: 33 additions & 48 deletions src/main/java/com/weis/darklaf/components/border/BubbleBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
public class BubbleBorder extends AbstractBorder {

private final static double PAD_OFFSET = 0.2;
private final Insets insets;
private Alignment pointerSide = Alignment.NORTH;
private Color color;
Expand All @@ -25,7 +24,6 @@ public class BubbleBorder extends AbstractBorder {
private int pointerSize;
private int pointerWidth;
private BasicStroke stroke;
private double pointerPadPercent = 0.5;


/**
Expand Down Expand Up @@ -212,28 +210,11 @@ public Alignment getPointerSide() {
public BubbleBorder setPointerSide(final Alignment side) {
this.pointerSide = side;
setPointerSize(pointerSize);
switch (pointerSide) {
case NORTH:
case SOUTH:
case EAST:
case WEST:
case CENTER:
pointerPadPercent = 0.5;
break;
case NORTH_EAST:
case SOUTH_EAST:
pointerPadPercent = 1.0 - PAD_OFFSET;
break;
case NORTH_WEST:
case SOUTH_WEST:
pointerPadPercent = PAD_OFFSET;
break;
}
return this;
}

public int getOffset(final int w, final int h) {
return (int) calculatePointerPad(w, h, PAD_OFFSET) + 2 * thickness;
return (int) calculatePointerPad(w, h, Alignment.NORTH_WEST);
}


Expand All @@ -256,15 +237,31 @@ public Insets getBorderInsets(final Component c, final Insets insets) {
return getBorderInsets(c);
}

public Area getInnerArea(final int x, final int y, final int width, final int height) {
var bubble = calculateBubbleRect(x, y, width, height);
final Area area = new Area(bubble);
if (pointerSide != Alignment.CENTER) {
double pointerPad = calculatePointerPad(width, height, pointerPadPercent);
Path2D pointer = creatPointerShape(pointerPad, bubble);
area.add(new Area(pointer));
@Contract(pure = true)
private double calculatePointerPad(final int width, final int height, final Alignment side) {
double pointerPad;
switch (side) {
case WEST:
case EAST:
pointerPad = radius + (height - insets.top - insets.bottom - 2 * radius) / 2.0;
break;
case NORTH_WEST:
case SOUTH_WEST:
pointerPad = radius + insets.left + pointerWidth;
break;
case NORTH_EAST:
case SOUTH_EAST:
pointerPad = width - radius - insets.right - pointerWidth;
break;
case SOUTH:
case NORTH:
pointerPad = radius + (0.5 * (width - insets.left - insets.right - 2 * radius));
break;
default:
pointerPad = 0;
break;
}
return area;
return pointerPad;
}

public void paintBorder(final Graphics g, final Area innerArea) {
Expand All @@ -283,27 +280,15 @@ public RoundRectangle2D.Double calculateBubbleRect(final int x, final int y,
height - insets.top - insets.bottom, radius, radius);
}

@Contract(pure = true)
private double calculatePointerPad(final int width, final int height, final double percent) {
double pointerPad;
switch (pointerSide) {
case WEST:
case EAST:
pointerPad = radius + (height - insets.top - insets.bottom - 2 * radius) / 2.0;
break;
case NORTH:
case NORTH_WEST:
case NORTH_EAST:
case SOUTH:
case SOUTH_WEST:
case SOUTH_EAST:
pointerPad = radius + (percent * (width - insets.left - insets.right - 2 * radius));
break;
default:
pointerPad = 0;
break;
public Area getInnerArea(final int x, final int y, final int width, final int height) {
var bubble = calculateBubbleRect(x, y, width, height);
final Area area = new Area(bubble);
if (pointerSide != Alignment.CENTER) {
double pointerPad = calculatePointerPad(width, height, pointerSide);
Path2D pointer = creatPointerShape(pointerPad, bubble);
area.add(new Area(pointer));
}
return pointerPad;
return area;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,9 @@ private Point alignCenter(final Dimension dim, @NotNull final Rectangle rect) {
private Point adjustPoint(final Point p, final Alignment align, final Dimension dim, final boolean outside) {
int factor = outside ? 1 : -1;
if (align == Alignment.NORTH_EAST || align == Alignment.SOUTH_EAST) {
p.x -= factor * ((DarkTooltipBorder) toolTip.getBorder()).getPointerOffset(toolTip, dim);
p.x -= factor * ((DarkTooltipBorder) toolTip.getBorder()).getPointerOffset(toolTip, dim) + 2;
} else if (align == Alignment.NORTH_WEST || align == Alignment.SOUTH_WEST) {
p.x += factor * ((DarkTooltipBorder) toolTip.getBorder()).getPointerOffset(toolTip, dim);
p.x += factor * ((DarkTooltipBorder) toolTip.getBorder()).getPointerOffset(toolTip, dim) - 1;
}
return p;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ToolTipDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(final String[] args) {
var p = new JPanel(new GridBagLayout());
p.add(new JButton("Button with very very long text") {
private final ToolTipContext context = new ToolTipContext(this).setAlignment(Alignment.CENTER)
.setCenterAlignment(Alignment.SOUTH);
.setCenterAlignment(Alignment.SOUTH_EAST);

{
setToolTipText("ToolTip \n multiline \n third line's a charm");
Expand Down

0 comments on commit 7c8d603

Please sign in to comment.