Skip to content

Commit 7808e25

Browse files
committed
Use the EDT when building Script Editor window
Assisted-by: Claude Code (Opus 5.5)
1 parent adf05cd commit 7808e25

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎src/main/java/org/scijava/ui/swing/script/Icons.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ static Icon magnifier() {
238238
return new MagnifierIcon();
239239
}
240240

241+
/**
242+
* Foreground color with which to paint an icon. Note: The component may be
243+
* null, e.g. when macOS renders icons for its native screen menu bar.
244+
*/
245+
private static Color foreground(final Component c) {
246+
if (c != null) return c.getForeground();
247+
final Color color = UIManager.getColor("MenuItem.foreground");
248+
return color != null ? color : Color.GRAY;
249+
}
250+
241251
/**
242252
* Icon painted from SVG path data (absolute M, L, C and Z commands)
243253
*/
@@ -268,7 +278,7 @@ private static class GlyphIcon extends FlatAbstractIcon {
268278

269279
@Override
270280
protected void paintIcon(final Component c, final Graphics2D g) {
271-
Color color = light == null ? c.getForeground() : FlatLaf.isLafDark() ? dark : light;
281+
Color color = light == null ? foreground(c) : FlatLaf.isLafDark() ? dark : light;
272282
if (disabled) {
273283
final Color d = UIManager.getColor("Button.disabledText");
274284
color = (d != null) ? d : Color.GRAY;
@@ -334,7 +344,7 @@ private abstract static class WindowIcon extends FlatAbstractIcon {
334344

335345
@Override
336346
protected void paintIcon(final Component c, final Graphics2D g) {
337-
g.setColor(c.getForeground());
347+
g.setColor(foreground(c));
338348
g.drawRoundRect(1, 2, 13, 11, 3, 3);
339349
paintContent(g, c instanceof AbstractButton && ((AbstractButton) c).isSelected());
340350
}
@@ -363,7 +373,7 @@ public void paintIcon(final Component c, final Graphics g, final int x, final in
363373
final Graphics2D g2 = (Graphics2D) g.create();
364374
try {
365375
FlatUIUtils.setRenderingHints(g2);
366-
g2.setColor(c.getForeground());
376+
g2.setColor(foreground(c));
367377
g2.setStroke(new BasicStroke(UIScale.scale(1.3f), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
368378
final float s = getIconWidth();
369379
final float a = s * .3f; // inset
@@ -391,7 +401,7 @@ public void paintIcon(final Component c, final Graphics g, final int x, final in
391401
final Graphics2D g2 = (Graphics2D) g.create();
392402
try {
393403
FlatUIUtils.setRenderingHints(g2);
394-
g2.setColor(c.getForeground());
404+
g2.setColor(foreground(c));
395405
g2.setStroke(new BasicStroke(UIScale.scale(1.6f), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
396406
final float s = getIconWidth();
397407
FlatUIUtils.drawShapePure(g2, new Ellipse2D.Float(x + .1f * s, y + .1f * s, .6f * s, .6f * s));

‎src/main/java/org/scijava/ui/swing/script/ScriptEditor.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
package org.scijava.ui.swing.script;
3131

32+
import java.awt.EventQueue;
33+
3234
import org.scijava.Context;
3335
import org.scijava.command.Command;
3436
import org.scijava.plugin.Menu;
@@ -50,7 +52,9 @@ public class ScriptEditor implements Command {
5052

5153
@Override
5254
public void run() {
53-
new TextEditor(context).setVisible(true);
55+
// Note: Swing components must be built and shown on the EDT; otherwise
56+
// layout races with the EDT and can leave the window collapsed.
57+
EventQueue.invokeLater(() -> new TextEditor(context).setVisible(true));
5458
}
5559

5660
}

‎src/main/java/org/scijava/ui/swing/script/TextEditor.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,7 @@ public void windowGainedFocus(final WindowEvent e) {
963963
});
964964
}
965965
catch (final Exception ie) {
966-
/* ignore */
967-
log.debug(ie);
966+
log.error("Failed to lay out Script Editor window", ie);
968967
}
969968
findDialog = new FindAndReplaceDialog(this);
970969

0 commit comments

Comments
 (0)