ui: fix rendering of separators with text on toolbar

* `Component.getGraphics()`` can be `null`

follow-up: 7f56a9dbd0
This commit is contained in:
Aleksey Pivovarov
2018-05-25 19:15:45 +03:00
parent 4e76d05f86
commit a075282a26
@@ -991,16 +991,9 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar, QuickAct
if (myText != null) {
FontMetrics fontMetrics = getFontMetrics(getFont());
Graphics g = getGraphics().create();
try {
UISettings.setupAntialiasing(g);
int textWidth = fontMetrics.getStringBounds(myText, g).getBounds().width;
return new JBDimension(width + gap * 2 + textWidth,
Math.max(fontMetrics.getHeight(), height), true);
}
finally {
g.dispose();
}
int textWidth = getTextWidth(fontMetrics, myText, getGraphics());
return new JBDimension(width + gap * 2 + textWidth,
Math.max(fontMetrics.getHeight(), height), true);
}
else {
return new JBDimension(width, height, true);
@@ -1042,6 +1035,22 @@ public class ActionToolbarImpl extends JPanel implements ActionToolbar, QuickAct
LinePainter2D.paint((Graphics2D)g, gap, center, ActionToolbarImpl.this.getWidth() - gap * 2 - offset, center);
}
}
private int getTextWidth(@NotNull FontMetrics fontMetrics, @NotNull String text, @Nullable Graphics graphics) {
if (graphics == null) {
return fontMetrics.stringWidth(text);
}
else {
Graphics g = graphics.create();
try {
UISettings.setupAntialiasing(g);
return fontMetrics.getStringBounds(text, g).getBounds().width;
}
finally {
g.dispose();
}
}
}
}
@Override