diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ce18399..207b98bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,6 @@ FlatLaf Change Log - Menus: Fixed inconsistent left padding in menu items. (issue #3) - Menus: Fixed: Setting `iconTextGap` property on a menu item did increase left and right margins. (issue #54) -- Show mnemonics always when a menu bar is active or a popup menu is visible. - Hide mnemonics if window is deactivated (e.g. Alt+Tab to another window). (issue #43) - macOS: Fixed NPE if using `JMenuBar` in `JInternalFrame` and macOS screen menu diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index a184f428..1906b330 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -17,6 +17,7 @@ package com.formdev.flatlaf; import java.awt.Color; +import java.awt.Component; import java.awt.EventQueue; import java.awt.Font; import java.awt.Image; @@ -528,6 +529,14 @@ public abstract class FlatLaf return MnemonicHandler.isShowMnemonics(); } + public static void showMnemonics( Component c ) { + MnemonicHandler.showMnemonics( true, c ); + } + + public static void hideMnemonics() { + MnemonicHandler.showMnemonics( false, null ); + } + //---- class ActiveFont --------------------------------------------------- private static class ActiveFont diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java index 8540973c..5e4d98b8 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java @@ -89,16 +89,13 @@ class MnemonicHandler @Override public void stateChanged( ChangeEvent e ) { MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath(); - if( selectedPath.length > 0 ) { - // show mnemonics when a menu item is selected - showMnemonics( true, (Component) selectedPath[0] ); - } else { + if( selectedPath.length == 0 ) { // hide mnemonics when menu selection was canceled showMnemonics( false, null ); } } - private void showMnemonics( boolean show, Component c ) { + static void showMnemonics( boolean show, Component c ) { if( show == showMnemonics ) return; @@ -151,7 +148,7 @@ class MnemonicHandler } } - private void repaintMnemonics( Container container ) { + private static void repaintMnemonics( Container container ) { for( Component c : container.getComponents() ) { if( !c.isVisible() ) continue; @@ -164,7 +161,7 @@ class MnemonicHandler } } - private boolean hasMnemonic( Component c ) { + private static boolean hasMnemonic( Component c ) { if( c instanceof JLabel && ((JLabel)c).getDisplayedMnemonicIndex() >= 0 ) return true; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java index fb399699..0612f26c 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java @@ -28,6 +28,7 @@ import javax.swing.SwingUtilities; import javax.swing.plaf.ActionMapUIResource; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicMenuBarUI; +import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.util.SystemInfo; /** @@ -50,7 +51,7 @@ public class FlatMenuBarUI } /* - * WARNING: This class is not used on macOS. + * WARNING: This class is not used on macOS if screen menu bar is enabled. * Do not add any functionality here. */ @@ -58,21 +59,20 @@ public class FlatMenuBarUI protected void installKeyboardActions() { super.installKeyboardActions(); - if( SystemInfo.IS_WINDOWS ) { - ActionMap map = SwingUtilities.getUIActionMap( menuBar ); - if( map == null ) { - map = new ActionMapUIResource(); - SwingUtilities.replaceUIActionMap( menuBar, map ); - } - map.put( "takeFocus", new TakeFocus() ); + ActionMap map = SwingUtilities.getUIActionMap( menuBar ); + if( map == null ) { + map = new ActionMapUIResource(); + SwingUtilities.replaceUIActionMap( menuBar, map ); } + map.put( "takeFocus", new TakeFocus() ); } //---- class TakeFocus ---------------------------------------------------- /** - * On Windows, activates the menu bar, but does not show the menu popup as - * BasicMenuBarUI.TakeFocus does. + * Activates the menu bar and shows mnemonics. + * On Windows, the popup of the first menu is not shown. + * On other platforms, the popup of the first menu is shown. */ private static class TakeFocus extends AbstractAction @@ -81,8 +81,13 @@ public class FlatMenuBarUI public void actionPerformed( ActionEvent e ) { JMenuBar menuBar = (JMenuBar) e.getSource(); JMenu menu = menuBar.getMenu( 0 ); - if( menu != null ) - MenuSelectionManager.defaultManager().setSelectedPath( new MenuElement[] { menuBar, menu } ); + if( menu != null ) { + MenuSelectionManager.defaultManager().setSelectedPath( SystemInfo.IS_WINDOWS + ? new MenuElement[] { menuBar, menu } + : new MenuElement[] { menuBar, menu, menu.getPopupMenu() } ); + + FlatLaf.showMnemonics( menuBar ); + } } } }