mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-08 06:50:56 +03:00
show mnemonics always when a menu bar is active or a popup menu is visible
This commit is contained in:
@@ -7,6 +7,7 @@ FlatLaf Change Log
|
|||||||
- Menus: On Windows, pressing <kbd>F10</kbd> now activates the menu bar without
|
- 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
|
showing a menu popup (as usual on Windows platform). On other platforms the
|
||||||
first menu popup is shown.
|
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
|
- Hide mnemonics if window is deactivated (e.g. <kbd>Alt+Tab</kbd> to another
|
||||||
window). (issue #43)
|
window). (issue #43)
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,12 @@ import javax.swing.AbstractButton;
|
|||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JRootPane;
|
import javax.swing.JRootPane;
|
||||||
import javax.swing.JTabbedPane;
|
import javax.swing.JTabbedPane;
|
||||||
|
import javax.swing.MenuElement;
|
||||||
|
import javax.swing.MenuSelectionManager;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
|
import javax.swing.event.ChangeEvent;
|
||||||
|
import javax.swing.event.ChangeListener;
|
||||||
import com.formdev.flatlaf.util.SystemInfo;
|
import com.formdev.flatlaf.util.SystemInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,7 +45,7 @@ import com.formdev.flatlaf.util.SystemInfo;
|
|||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
*/
|
*/
|
||||||
class MnemonicHandler
|
class MnemonicHandler
|
||||||
implements KeyEventPostProcessor
|
implements KeyEventPostProcessor, ChangeListener
|
||||||
{
|
{
|
||||||
private static boolean showMnemonics;
|
private static boolean showMnemonics;
|
||||||
private static WeakReference<Window> lastShowMnemonicWindow;
|
private static WeakReference<Window> lastShowMnemonicWindow;
|
||||||
@@ -53,10 +57,12 @@ class MnemonicHandler
|
|||||||
|
|
||||||
void install() {
|
void install() {
|
||||||
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( this );
|
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( this );
|
||||||
|
MenuSelectionManager.defaultManager().addChangeListener( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
void uninstall() {
|
void uninstall() {
|
||||||
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor( this );
|
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor( this );
|
||||||
|
MenuSelectionManager.defaultManager().removeChangeListener( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -65,16 +71,33 @@ class MnemonicHandler
|
|||||||
if( SystemInfo.IS_MAC ) {
|
if( SystemInfo.IS_MAC ) {
|
||||||
// Ctrl+Alt keys must be pressed on Mac
|
// Ctrl+Alt keys must be pressed on Mac
|
||||||
if( keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_ALT )
|
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 {
|
} else {
|
||||||
// Alt key must be pressed on Windows and Linux
|
// Alt key must be pressed on Windows and Linux
|
||||||
if( keyCode == KeyEvent.VK_ALT )
|
if( keyCode == KeyEvent.VK_ALT )
|
||||||
showMnemonics( e.getID() == KeyEvent.KEY_PRESSED, e.getComponent() );
|
showMnemonics( shouldShowMnemonics( e ), e.getComponent() );
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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 ) {
|
private void showMnemonics( boolean show, Component c ) {
|
||||||
if( show == showMnemonics )
|
if( show == showMnemonics )
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -602,14 +602,17 @@ class BasicComponentsPanel
|
|||||||
|
|
||||||
//---- cutMenuItem ----
|
//---- cutMenuItem ----
|
||||||
cutMenuItem.setText("Cut");
|
cutMenuItem.setText("Cut");
|
||||||
|
cutMenuItem.setMnemonic('C');
|
||||||
popupMenu1.add(cutMenuItem);
|
popupMenu1.add(cutMenuItem);
|
||||||
|
|
||||||
//---- copyMenuItem ----
|
//---- copyMenuItem ----
|
||||||
copyMenuItem.setText("Copy");
|
copyMenuItem.setText("Copy");
|
||||||
|
copyMenuItem.setMnemonic('O');
|
||||||
popupMenu1.add(copyMenuItem);
|
popupMenu1.add(copyMenuItem);
|
||||||
|
|
||||||
//---- pasteMenuItem ----
|
//---- pasteMenuItem ----
|
||||||
pasteMenuItem.setText("Paste");
|
pasteMenuItem.setText("Paste");
|
||||||
|
pasteMenuItem.setMnemonic('P');
|
||||||
popupMenu1.add(pasteMenuItem);
|
popupMenu1.add(pasteMenuItem);
|
||||||
}
|
}
|
||||||
// JFormDesigner - End of component initialization //GEN-END:initComponents
|
// JFormDesigner - End of component initialization //GEN-END:initComponents
|
||||||
|
|||||||
@@ -601,14 +601,17 @@ new FormModel {
|
|||||||
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
||||||
name: "cutMenuItem"
|
name: "cutMenuItem"
|
||||||
"text": "Cut"
|
"text": "Cut"
|
||||||
|
"mnemonic": 67
|
||||||
} )
|
} )
|
||||||
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
||||||
name: "copyMenuItem"
|
name: "copyMenuItem"
|
||||||
"text": "Copy"
|
"text": "Copy"
|
||||||
|
"mnemonic": 79
|
||||||
} )
|
} )
|
||||||
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
add( new FormComponent( "javax.swing.JMenuItem" ) {
|
||||||
name: "pasteMenuItem"
|
name: "pasteMenuItem"
|
||||||
"text": "Paste"
|
"text": "Paste"
|
||||||
|
"mnemonic": 80
|
||||||
} )
|
} )
|
||||||
}, new FormLayoutConstraints( null ) {
|
}, new FormLayoutConstraints( null ) {
|
||||||
"location": new java.awt.Point( 0, 500 )
|
"location": new java.awt.Point( 0, 500 )
|
||||||
|
|||||||
Reference in New Issue
Block a user