From a075282a26c8e37b5ac3297bc9979c98edf704dd Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Fri, 25 May 2018 19:15:45 +0300 Subject: [PATCH] ui: fix rendering of separators with text on toolbar * `Component.getGraphics()`` can be `null` follow-up: 7f56a9dbd0fbff807cb2eafe3bf4ec0ee9f543d3 --- .../actionSystem/impl/ActionToolbarImpl.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionToolbarImpl.java b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionToolbarImpl.java index 4062e9a18990..9c98133f694a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionToolbarImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/ActionToolbarImpl.java @@ -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