You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/04/19 09:19:38 UTC

(groovy) branch master updated: GROOVY-11932: Allow theme switching light/dark/system in groovyConsole (tweaks)

This is an automated email from the ASF dual-hosted git repository.

paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0bc137e0f0 GROOVY-11932: Allow theme switching light/dark/system in groovyConsole (tweaks)
0bc137e0f0 is described below

commit 0bc137e0f0bf270ce6b53ad3fed8805aea641b15
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Apr 19 19:19:25 2026 +1000

    GROOVY-11932: Allow theme switching light/dark/system in groovyConsole (tweaks)
---
 .../main/groovy/groovy/console/ui/Console.groovy   | 21 +++++++++
 .../groovy/groovy/console/ui/ConsoleActions.groovy | 50 +++++++++++-----------
 .../src/main/groovy/groovy/console/ui/Icons.groovy | 40 +++++++++++------
 .../groovy/groovy/console/ui/ThemeManager.groovy   | 46 +++++++++++++++++++-
 4 files changed, 118 insertions(+), 39 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index 9a56ffb465..b7fa9c115d 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -96,6 +96,8 @@ import java.awt.event.ComponentEvent
 import java.awt.event.ComponentListener
 import java.awt.event.FocusEvent
 import java.awt.event.FocusListener
+import java.awt.event.WindowEvent
+import java.awt.event.WindowFocusListener
 import java.util.logging.Logger
 import java.util.prefs.Preferences
 
@@ -471,6 +473,7 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener, Fo
 
         if (swing.consoleFrame instanceof Window) {
             nativeFullScreenForMac(swing.consoleFrame)
+            installSystemThemeWatcher(swing.consoleFrame as Window)
             swing.consoleFrame.pack()
             swing.consoleFrame.show()
         }
@@ -497,6 +500,24 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener, Fo
         }
     }
 
+    // Re-probes the OS appearance when the console regains focus so that
+    // SYSTEM mode tracks live OS theme changes, and so that menu-bar icons
+    // on macOS (drawn against the OS-themed screen menu bar) stay legible
+    // when the user flips the OS setting in another app.
+    private void installSystemThemeWatcher(Window frame) {
+        frame.addWindowFocusListener(new WindowFocusListener() {
+            @Override void windowGainedFocus(WindowEvent e) {
+                if (!ThemeManager.refreshSystemDarkMode()) return
+                if (ThemeManager.currentMode == ThemeManager.ThemeMode.SYSTEM) {
+                    switchTheme(ThemeManager.ThemeMode.SYSTEM)
+                } else if (ThemeManager.isMenuDrawnByOS()) {
+                    Icons.refreshAll()
+                }
+            }
+            @Override void windowLostFocus(WindowEvent e) {}
+        })
+    }
+
     void installInterceptor() {
         systemOutInterceptor = new SystemOutputInterceptor(this.&notifySystemOut, true)
         systemOutInterceptor.start()
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
index 2f2fcca7fa..cc642a83bb 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
@@ -30,7 +30,7 @@ newFileAction = action(
         closure: controller.&fileNewFile,
         mnemonic: 'N',
         accelerator: shortcut('N'),
-        smallIcon: Icons.load('note_add'),
+        smallIcon: Icons.menu('note_add'),
         shortDescription: 'New Groovy Script'
 )
 
@@ -46,7 +46,7 @@ openAction = action(
         closure: controller.&fileOpen,
         mnemonic: 'O',
         accelerator: shortcut('O'),
-        smallIcon: Icons.load('folder_open'),
+        smallIcon: Icons.menu('folder_open'),
         shortDescription: 'Open Groovy Script'
 )
 
@@ -55,7 +55,7 @@ saveAction = action(
         closure: controller.&fileSave,
         mnemonic: 'S',
         accelerator: shortcut('S'),
-        smallIcon: Icons.load('save'),
+        smallIcon: Icons.menu('save'),
         shortDescription: 'Save Groovy Script',
         enabled: false // controller will enable as needed
 )
@@ -70,7 +70,7 @@ printAction = action(
         name: 'Print...',
         closure: controller.&print,
         mnemonic: 'P',
-        smallIcon: Icons.load('print'),
+        smallIcon: Icons.menu('print'),
         accelerator: shortcut('P')
 )
 
@@ -89,7 +89,7 @@ undoAction = action(
         closure: controller.&undo,
         mnemonic: 'U',
         accelerator: shortcut('Z'),
-        smallIcon: Icons.load('undo'),
+        smallIcon: Icons.menu('undo'),
         shortDescription: 'Undo'
 )
 
@@ -98,7 +98,7 @@ redoAction = action(
         closure: controller.&redo,
         mnemonic: 'R',
         accelerator: shortcut('shift Z'), // is control-shift-Z or control-Y more common?
-        smallIcon: Icons.load('redo'),
+        smallIcon: Icons.menu('redo'),
         shortDescription: 'Redo'
 )
 
@@ -107,7 +107,7 @@ findAction = action(
         closure: controller.&find,
         mnemonic: 'F',
         accelerator: shortcut('F'),
-        smallIcon: Icons.load('search'),
+        smallIcon: Icons.menu('search'),
         shortDescription: 'Find'
 )
 
@@ -130,7 +130,7 @@ replaceAction = action(
         closure: controller.&replace,
         mnemonic: 'E',
         accelerator: shortcut('H'),
-        smallIcon: Icons.load('find_replace'),
+        smallIcon: Icons.menu('find_replace'),
         shortDescription: 'Replace'
 )
 
@@ -139,7 +139,7 @@ cutAction = action(
         closure: controller.&cut,
         mnemonic: 'T',
         accelerator: shortcut('X'),
-        smallIcon: Icons.load('content_cut'),
+        smallIcon: Icons.menu('content_cut'),
         shortDescription: 'Cut'
 )
 
@@ -148,7 +148,7 @@ copyAction = action(
         closure: controller.&copy,
         mnemonic: 'C',
         accelerator: shortcut('C'),
-        smallIcon: Icons.load('content_copy'),
+        smallIcon: Icons.menu('content_copy'),
         shortDescription: 'Copy'
 )
 
@@ -157,7 +157,7 @@ pasteAction = action(
         closure: controller.&paste,
         mnemonic: 'P',
         accelerator: shortcut('V'),
-        smallIcon: Icons.load('content_paste'),
+        smallIcon: Icons.menu('content_paste'),
         shortDescription: 'Paste'
 )
 
@@ -165,7 +165,7 @@ selectAllAction = action(
         name: 'Select All',
         closure: controller.&selectAll,
         mnemonic: 'A',
-        smallIcon: Icons.load('select_all'),
+        smallIcon: Icons.menu('select_all'),
         accelerator: shortcut('A')
 )
 
@@ -174,7 +174,7 @@ historyPrevAction = action(
         closure: controller.&historyPrev,
         mnemonic: 'P',
         accelerator: shortcut(KeyEvent.VK_COMMA),
-        smallIcon: Icons.load('chevron_left'),
+        smallIcon: Icons.menu('chevron_left'),
         shortDescription: 'Previous Groovy Script',
         enabled: false // controller will enable as needed
 )
@@ -184,7 +184,7 @@ historyNextAction = action(
         closure: controller.&historyNext,
         mnemonic: 'N',
         accelerator: shortcut(KeyEvent.VK_PERIOD),
-        smallIcon: Icons.load('chevron_right'),
+        smallIcon: Icons.menu('chevron_right'),
         shortDescription: 'Next Groovy Script',
         enabled: false // controller will enable as needed
 )
@@ -194,7 +194,7 @@ clearOutputAction = action(
         closure: controller.&clearOutput,
         mnemonic: 'C',
         accelerator: shortcut('W'),
-        smallIcon: Icons.load('delete_sweep'),
+        smallIcon: Icons.menu('delete_sweep'),
         shortDescription: 'Clear Output Area'
 )
 
@@ -204,7 +204,7 @@ runAction = action(
         mnemonic: 'R',
         keyStroke: shortcut('ENTER'),
         accelerator: shortcut('R'),
-        smallIcon: Icons.green('play_arrow'),
+        smallIcon: Icons.menuGreen('play_arrow'),
         shortDescription: 'Execute Groovy Script'
 )
 
@@ -213,7 +213,7 @@ runJavaAction = action(
         closure: controller.&runJava,
         mnemonic: 'J',
         accelerator: shortcut('alt R'),
-        smallIcon: Icons.load('coffee'),
+        smallIcon: Icons.menu('coffee'),
         shortDescription: 'Execute Java Code'
 )
 
@@ -246,14 +246,14 @@ setScriptArgsAction = action(
 addClasspathJar = action(
         name: 'Add Jar(s) to ClassPath',
         closure: controller.&addClasspathJar,
-        smallIcon: Icons.load('library_add'),
+        smallIcon: Icons.menu('library_add'),
         mnemonic: 'J',
 )
 
 addClasspathDir = action(
         name: 'Add Directory to ClassPath',
         closure: controller.&addClasspathDir,
-        smallIcon: Icons.load('create_new_folder'),
+        smallIcon: Icons.menu('create_new_folder'),
         mnemonic: 'D',
 )
 
@@ -265,7 +265,7 @@ listClasspath = action(
 clearClassloader = action(
         name: 'Clear Script Context',
         closure: controller.&clearContext,
-        smallIcon: Icons.load('bolt'),
+        smallIcon: Icons.menu('bolt'),
         mnemonic: 'C',
 )
 
@@ -393,7 +393,7 @@ largerFontAction = action(
         name: 'Larger Font',
         closure: controller.&largerFont,
         mnemonic: 'L',
-        smallIcon: Icons.load('text_increase'),
+        smallIcon: Icons.menu('text_increase'),
         accelerator: shortcut('shift L')
 )
 
@@ -401,7 +401,7 @@ smallerFontAction = action(
         name: 'Smaller Font',
         closure: controller.&smallerFont,
         mnemonic: 'S',
-        smallIcon: Icons.load('text_decrease'),
+        smallIcon: Icons.menu('text_decrease'),
         accelerator: shortcut('shift S')
 )
 
@@ -413,7 +413,7 @@ smartHighlighterAction = action(
 aboutAction = action(
         name: 'About',
         closure: controller.&showAbout,
-        smallIcon: Icons.load('info'),
+        smallIcon: Icons.menu('info'),
         mnemonic: 'A'
 )
 
@@ -427,7 +427,7 @@ interruptAction = action(
         name: 'Interrupt',
         closure: controller.&doInterrupt,
         mnemonic: 'T',
-        smallIcon: Icons.red('stop_circle'),
+        smallIcon: Icons.menuRed('stop_circle'),
         shortDescription: 'Interrupt Running Script',
         enabled: false // controller will enable as needed
 )
@@ -491,7 +491,7 @@ systemThemeAction = action(
 cycleThemeAction = action(
         name: 'Theme',
         closure: controller.&cycleTheme,
-        smallIcon: Icons.load('refresh'),
+        smallIcon: Icons.menu('refresh'),
         shortDescription: 'Cycle theme (' + groovy.console.ui.ThemeManager.themeLabel + ')'
 )
 
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Icons.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Icons.groovy
index b3692499b6..600166c636 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Icons.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Icons.groovy
@@ -44,10 +44,10 @@ import java.util.function.Function
  * forces every icon (toolbar and menu) to re-render — use after a LaF switch.
  */
 class Icons {
-    static final int SIZE_SMALL = 14
-    static final int SIZE_NORMAL = 16
-    static final int SIZE_LARGE = 22
-    static final int MENU_SIZE = 16
+    static final int SIZE_SMALL = 16
+    static final int SIZE_NORMAL = 18
+    static final int SIZE_LARGE = 28
+    static final int MENU_SIZE = 18
 
     private static final String PATH = 'groovy/console/ui/icons/'
     private static int currentSize = SIZE_NORMAL
@@ -55,18 +55,32 @@ class Icons {
     private static final List<DynamicSVGIcon> allIcons = []
     private static final List<DynamicSVGIcon> toolbarIcons = []
 
-    // ---- menu / fixed-size ----
+    // ---- fixed-size icons (in-window menus, popups, AstBrowser/ObjectBrowser toolbars) ----
 
     static DynamicSVGIcon load(String name) {
         menuIcon(name, { Color c -> UIManager.getColor('Label.foreground') ?: c })
     }
 
     static DynamicSVGIcon green(String name) {
-        menuIcon(name, greenMapper())
+        menuIcon(name, greenMapper({ ThemeManager.isDark() }))
     }
 
     static DynamicSVGIcon red(String name) {
-        menuIcon(name, redMapper())
+        menuIcon(name, redMapper({ ThemeManager.isDark() }))
+    }
+
+    // ---- main menu-bar icons (tint follows the menu-bar background, which is OS-drawn on macOS) ----
+
+    static DynamicSVGIcon menu(String name) {
+        menuIcon(name, { Color c -> ThemeManager.menuIconForeground ?: c })
+    }
+
+    static DynamicSVGIcon menuGreen(String name) {
+        menuIcon(name, greenMapper({ ThemeManager.isMenuDark() }))
+    }
+
+    static DynamicSVGIcon menuRed(String name) {
+        menuIcon(name, redMapper({ ThemeManager.isMenuDark() }))
     }
 
     // ---- toolbar / resizable ----
@@ -76,23 +90,23 @@ class Icons {
     }
 
     static DynamicSVGIcon toolbarGreen(String name) {
-        toolbarIcon(name, greenMapper())
+        toolbarIcon(name, greenMapper({ ThemeManager.isDark() }))
     }
 
     static DynamicSVGIcon toolbarRed(String name) {
-        toolbarIcon(name, redMapper())
+        toolbarIcon(name, redMapper({ ThemeManager.isDark() }))
     }
 
-    private static Closure<Color> greenMapper() {
+    private static Closure<Color> greenMapper(Closure<Boolean> darkCheck) {
         Color light = new Color(0x2E7D32)
         Color dark = new Color(0x81C784)
-        return { Color c -> ThemeManager.isDark() ? dark : light }
+        return { Color c -> darkCheck() ? dark : light }
     }
 
-    private static Closure<Color> redMapper() {
+    private static Closure<Color> redMapper(Closure<Boolean> darkCheck) {
         Color light = new Color(0xC62828)
         Color dark = new Color(0xEF5350)
-        return { Color c -> ThemeManager.isDark() ? dark : light }
+        return { Color c -> darkCheck() ? dark : light }
     }
 
     private static DynamicSVGIcon menuIcon(String name, Closure<Color> mapper) {
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ThemeManager.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ThemeManager.groovy
index d73fead4f0..2a3c2627ca 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ThemeManager.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ThemeManager.groovy
@@ -22,6 +22,7 @@ import com.formdev.flatlaf.FlatDarkLaf
 import com.formdev.flatlaf.FlatLightLaf
 import groovy.console.ui.text.GroovyFilter
 
+import javax.swing.UIManager
 import javax.swing.text.StyleConstants
 import javax.swing.text.StyleContext
 import java.awt.Color
@@ -38,6 +39,10 @@ class ThemeManager {
 
     private static final Preferences prefs = Preferences.userNodeForPackage(Console)
 
+    // cached result of the OS-appearance probe; isSystemDarkMode is on hot paths
+    // (icon color filters), so we avoid shelling out per call
+    private static volatile Boolean cachedSystemDark = null
+
     static ThemeMode getCurrentMode() {
         try {
             ThemeMode.valueOf(prefs.get('theme', 'SYSTEM').toUpperCase())
@@ -53,6 +58,7 @@ class ThemeManager {
 
     static void applyTheme(ThemeMode mode) {
         prefs.put('theme', mode.name())
+        refreshSystemDarkMode()
         if (mode == ThemeMode.DARK || (mode == ThemeMode.SYSTEM && isSystemDarkMode())) {
             FlatDarkLaf.setup()
         } else {
@@ -60,6 +66,44 @@ class ThemeManager {
         }
     }
 
+    /** Re-probe OS appearance, update the cache, and report whether it changed. */
+    static boolean refreshSystemDarkMode() {
+        def prior = cachedSystemDark
+        cachedSystemDark = probeSystemDarkMode()
+        prior == null || prior != cachedSystemDark
+    }
+
+    static boolean isSystemDarkMode() {
+        if (cachedSystemDark == null) {
+            cachedSystemDark = probeSystemDarkMode()
+        }
+        cachedSystemDark
+    }
+
+    /**
+     * On macOS with {@code apple.laf.useScreenMenuBar=true} the menu bar is drawn
+     * by the OS rather than FlatLaf, so menu-item icons need to track the OS
+     * appearance instead of the app theme to stay legible.
+     */
+    static boolean isMenuDrawnByOS() {
+        String os = System.getProperty('os.name', '').toLowerCase()
+        os.contains('mac') && Boolean.parseBoolean(System.getProperty('apple.laf.useScreenMenuBar', 'false'))
+    }
+
+    /** True iff menu-item icons will paint against a dark background. */
+    static boolean isMenuDark() {
+        isMenuDrawnByOS() ? isSystemDarkMode() : isDark()
+    }
+
+    /** Foreground color for menu-item icons — OS-tinted on the mac screen menu bar, app-tinted otherwise. */
+    static Color getMenuIconForeground() {
+        if (isMenuDrawnByOS()) {
+            isSystemDarkMode() ? new Color(204, 204, 204) : Color.BLACK
+        } else {
+            UIManager.getColor('Label.foreground')
+        }
+    }
+
     static ThemeMode cycleMode() {
         switch (currentMode) {
             case ThemeMode.LIGHT: return ThemeMode.DARK
@@ -216,7 +260,7 @@ class ThemeManager {
         ]
     }
 
-    private static boolean isSystemDarkMode() {
+    private static boolean probeSystemDarkMode() {
         String os = System.getProperty('os.name', '').toLowerCase()
         if (os.contains('mac')) {
             try {