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: 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. <kbd>Alt+Tab</kbd> to another
window). (issue #43)
- macOS: Fixed NPE if using `JMenuBar` in `JInternalFrame` and macOS screen menu

View File

@@ -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

View File

@@ -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;

View File

@@ -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 );
}
}
}
}