show mnemonics always when a menu bar is active or a popup menu is visible

This commit is contained in:
Karl Tauber
2020-04-29 23:56:15 +02:00
parent 976353d770
commit e3ed47b37c
4 changed files with 33 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ FlatLaf Change Log
- Menus: On Windows, pressing <kbd>F10</kbd> now activates the menu bar without
showing a menu popup (as usual on Windows platform). On other platforms the
first menu popup is shown.
- 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)

View File

@@ -31,8 +31,12 @@ import javax.swing.AbstractButton;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.formdev.flatlaf.util.SystemInfo;
/**
@@ -41,7 +45,7 @@ import com.formdev.flatlaf.util.SystemInfo;
* @author Karl Tauber
*/
class MnemonicHandler
implements KeyEventPostProcessor
implements KeyEventPostProcessor, ChangeListener
{
private static boolean showMnemonics;
private static WeakReference<Window> lastShowMnemonicWindow;
@@ -53,10 +57,12 @@ class MnemonicHandler
void install() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( this );
MenuSelectionManager.defaultManager().addChangeListener( this );
}
void uninstall() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor( this );
MenuSelectionManager.defaultManager().removeChangeListener( this );
}
@Override
@@ -65,16 +71,33 @@ class MnemonicHandler
if( SystemInfo.IS_MAC ) {
// Ctrl+Alt keys must be pressed on Mac
if( keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_ALT )
showMnemonics( e.getID() == KeyEvent.KEY_PRESSED && e.isControlDown() && e.isAltDown(), e.getComponent() );
showMnemonics( shouldShowMnemonics( e ) && e.isControlDown() && e.isAltDown(), e.getComponent() );
} else {
// Alt key must be pressed on Windows and Linux
if( keyCode == KeyEvent.VK_ALT )
showMnemonics( e.getID() == KeyEvent.KEY_PRESSED, e.getComponent() );
showMnemonics( shouldShowMnemonics( e ), e.getComponent() );
}
return false;
}
private boolean shouldShowMnemonics( KeyEvent e ) {
return e.getID() == KeyEvent.KEY_PRESSED ||
MenuSelectionManager.defaultManager().getSelectedPath().length > 0;
}
@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 {
// hide mnemonics when menu selection was canceled
showMnemonics( false, null );
}
}
private void showMnemonics( boolean show, Component c ) {
if( show == showMnemonics )
return;

View File

@@ -602,14 +602,17 @@ class BasicComponentsPanel
//---- cutMenuItem ----
cutMenuItem.setText("Cut");
cutMenuItem.setMnemonic('C');
popupMenu1.add(cutMenuItem);
//---- copyMenuItem ----
copyMenuItem.setText("Copy");
copyMenuItem.setMnemonic('O');
popupMenu1.add(copyMenuItem);
//---- pasteMenuItem ----
pasteMenuItem.setText("Paste");
pasteMenuItem.setMnemonic('P');
popupMenu1.add(pasteMenuItem);
}
// JFormDesigner - End of component initialization //GEN-END:initComponents

View File

@@ -601,14 +601,17 @@ new FormModel {
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "cutMenuItem"
"text": "Cut"
"mnemonic": 67
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "copyMenuItem"
"text": "Copy"
"mnemonic": 79
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "pasteMenuItem"
"text": "Paste"
"mnemonic": 80
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 500 )