Skip to content

Commit

Permalink
show mnemonics if the active window does not have a focused component…
Browse files Browse the repository at this point in the history
…; ignore invisible components (issue #43)
  • Loading branch information
DevCharly committed Jan 17, 2020
1 parent 499c4da commit e675d1b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.swing.AbstractButton;
import javax.swing.InputMap;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
Expand Down Expand Up @@ -430,15 +431,15 @@ private static void checkShowMnemonics( KeyEvent e ) {
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() );
showMnemonics( e.getID() == KeyEvent.KEY_PRESSED && 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 );
showMnemonics( e.getID() == KeyEvent.KEY_PRESSED, e.getComponent() );
}
}

private static void showMnemonics( boolean show ) {
private static void showMnemonics( boolean show, Component c ) {
if( show == showMnemonics )
return;

Expand All @@ -449,13 +450,13 @@ private static void showMnemonics( boolean show ) {
return;

if( show ) {
// get focus owner
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if( focusOwner == null )
// get root pane
JRootPane rootPane = SwingUtilities.getRootPane( c );
if( rootPane == null )
return;

// get focused window
Window window = SwingUtilities.windowForComponent( focusOwner );
// get window
Window window = SwingUtilities.getWindowAncestor( rootPane );
if( window == null )
return;

Expand All @@ -474,6 +475,9 @@ private static void showMnemonics( boolean show ) {

private static void repaintMnemonics( Container container ) {
for( Component c : container.getComponents() ) {
if( !c.isVisible() )
continue;

if( hasMnemonic( c ) )
c.repaint();

Expand Down

0 comments on commit e675d1b

Please sign in to comment.