no longer always show mnemonics when a menu bar is active or a popup menu is visible (issue #43)

This commit is contained in:
Karl Tauber
2020-05-01 00:22:04 +02:00
parent 8f10c2d8bf
commit a48713b7ca
4 changed files with 30 additions and 20 deletions

View File

@@ -16,7 +16,6 @@ FlatLaf Change Log
- Menus: Fixed inconsistent left padding in menu items. (issue #3) - Menus: Fixed inconsistent left padding in menu items. (issue #3)
- Menus: Fixed: Setting `iconTextGap` property on a menu item did increase left - Menus: Fixed: Setting `iconTextGap` property on a menu item did increase left
and right margins. (issue #54) 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. <kbd>Alt+Tab</kbd> to another - Hide mnemonics if window is deactivated (e.g. <kbd>Alt+Tab</kbd> to another
window). (issue #43) window). (issue #43)
- macOS: Fixed NPE if using `JMenuBar` in `JInternalFrame` and macOS screen menu - macOS: Fixed NPE if using `JMenuBar` in `JInternalFrame` and macOS screen menu

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf; package com.formdev.flatlaf;
import java.awt.Color; import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.Font; import java.awt.Font;
import java.awt.Image; import java.awt.Image;
@@ -528,6 +529,14 @@ public abstract class FlatLaf
return MnemonicHandler.isShowMnemonics(); return MnemonicHandler.isShowMnemonics();
} }
public static void showMnemonics( Component c ) {
MnemonicHandler.showMnemonics( true, c );
}
public static void hideMnemonics() {
MnemonicHandler.showMnemonics( false, null );
}
//---- class ActiveFont --------------------------------------------------- //---- class ActiveFont ---------------------------------------------------
private static class ActiveFont private static class ActiveFont

View File

@@ -89,16 +89,13 @@ class MnemonicHandler
@Override @Override
public void stateChanged( ChangeEvent e ) { public void stateChanged( ChangeEvent e ) {
MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath(); MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath();
if( selectedPath.length > 0 ) { if( selectedPath.length == 0 ) {
// show mnemonics when a menu item is selected
showMnemonics( true, (Component) selectedPath[0] );
} else {
// hide mnemonics when menu selection was canceled // hide mnemonics when menu selection was canceled
showMnemonics( false, null ); showMnemonics( false, null );
} }
} }
private void showMnemonics( boolean show, Component c ) { static void showMnemonics( boolean show, Component c ) {
if( show == showMnemonics ) if( show == showMnemonics )
return; return;
@@ -151,7 +148,7 @@ class MnemonicHandler
} }
} }
private void repaintMnemonics( Container container ) { private static void repaintMnemonics( Container container ) {
for( Component c : container.getComponents() ) { for( Component c : container.getComponents() ) {
if( !c.isVisible() ) if( !c.isVisible() )
continue; 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 ) if( c instanceof JLabel && ((JLabel)c).getDisplayedMnemonicIndex() >= 0 )
return true; return true;

View File

@@ -28,6 +28,7 @@ import javax.swing.SwingUtilities;
import javax.swing.plaf.ActionMapUIResource; import javax.swing.plaf.ActionMapUIResource;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicMenuBarUI; import javax.swing.plaf.basic.BasicMenuBarUI;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.SystemInfo; 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. * Do not add any functionality here.
*/ */
@@ -58,21 +59,20 @@ public class FlatMenuBarUI
protected void installKeyboardActions() { protected void installKeyboardActions() {
super.installKeyboardActions(); super.installKeyboardActions();
if( SystemInfo.IS_WINDOWS ) { ActionMap map = SwingUtilities.getUIActionMap( menuBar );
ActionMap map = SwingUtilities.getUIActionMap( menuBar ); if( map == null ) {
if( map == null ) { map = new ActionMapUIResource();
map = new ActionMapUIResource(); SwingUtilities.replaceUIActionMap( menuBar, map );
SwingUtilities.replaceUIActionMap( menuBar, map );
}
map.put( "takeFocus", new TakeFocus() );
} }
map.put( "takeFocus", new TakeFocus() );
} }
//---- class TakeFocus ---------------------------------------------------- //---- class TakeFocus ----------------------------------------------------
/** /**
* On Windows, activates the menu bar, but does not show the menu popup as * Activates the menu bar and shows mnemonics.
* BasicMenuBarUI.TakeFocus does. * 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 private static class TakeFocus
extends AbstractAction extends AbstractAction
@@ -81,8 +81,13 @@ public class FlatMenuBarUI
public void actionPerformed( ActionEvent e ) { public void actionPerformed( ActionEvent e ) {
JMenuBar menuBar = (JMenuBar) e.getSource(); JMenuBar menuBar = (JMenuBar) e.getSource();
JMenu menu = menuBar.getMenu( 0 ); JMenu menu = menuBar.getMenu( 0 );
if( menu != null ) if( menu != null ) {
MenuSelectionManager.defaultManager().setSelectedPath( new MenuElement[] { menuBar, menu } ); MenuSelectionManager.defaultManager().setSelectedPath( SystemInfo.IS_WINDOWS
? new MenuElement[] { menuBar, menu }
: new MenuElement[] { menuBar, menu, menu.getPopupMenu() } );
FlatLaf.showMnemonics( menuBar );
}
} }
} }
} }