mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
hide mnemonics if window is deactivated (e.g. Alt+Tab to another window) (issue #43)
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
FlatLaf Change Log
|
FlatLaf Change Log
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
- Hide mnemonics if window is deactivated (e.g. <kbd>Alt+Tab</kbd> to another
|
||||||
|
window). (issue #43)
|
||||||
|
|
||||||
|
|
||||||
## 0.33
|
## 0.33
|
||||||
|
|
||||||
- Improved creation of disabled grayscale icons used in disabled buttons, labels
|
- Improved creation of disabled grayscale icons used in disabled buttons, labels
|
||||||
|
|||||||
@@ -18,10 +18,14 @@ package com.formdev.flatlaf;
|
|||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Container;
|
import java.awt.Container;
|
||||||
|
import java.awt.EventQueue;
|
||||||
import java.awt.KeyEventPostProcessor;
|
import java.awt.KeyEventPostProcessor;
|
||||||
import java.awt.KeyboardFocusManager;
|
import java.awt.KeyboardFocusManager;
|
||||||
import java.awt.Window;
|
import java.awt.Window;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.awt.event.WindowListener;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import javax.swing.AbstractButton;
|
import javax.swing.AbstractButton;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
@@ -41,6 +45,7 @@ class MnemonicHandler
|
|||||||
{
|
{
|
||||||
private static boolean showMnemonics;
|
private static boolean showMnemonics;
|
||||||
private static WeakReference<Window> lastShowMnemonicWindow;
|
private static WeakReference<Window> lastShowMnemonicWindow;
|
||||||
|
private static WindowListener windowListener;
|
||||||
|
|
||||||
static boolean isShowMnemonics() {
|
static boolean isShowMnemonics() {
|
||||||
return showMnemonics || !UIManager.getBoolean( "Component.hideMnemonics" );
|
return showMnemonics || !UIManager.getBoolean( "Component.hideMnemonics" );
|
||||||
@@ -94,12 +99,31 @@ class MnemonicHandler
|
|||||||
// repaint components with mnemonics in focused window
|
// repaint components with mnemonics in focused window
|
||||||
repaintMnemonics( window );
|
repaintMnemonics( window );
|
||||||
|
|
||||||
|
// hide mnemonics if window is deactivated (e.g. Alt+Tab to another window)
|
||||||
|
windowListener = new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowDeactivated( WindowEvent e ) {
|
||||||
|
// use invokeLater() to avoid that the listener is removed
|
||||||
|
// while the listener queue is iterated to fire this event
|
||||||
|
EventQueue.invokeLater( () -> {
|
||||||
|
showMnemonics( false, c );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addWindowListener( windowListener );
|
||||||
|
|
||||||
lastShowMnemonicWindow = new WeakReference<>( window );
|
lastShowMnemonicWindow = new WeakReference<>( window );
|
||||||
} else if( lastShowMnemonicWindow != null ) {
|
} else if( lastShowMnemonicWindow != null ) {
|
||||||
Window window = lastShowMnemonicWindow.get();
|
Window window = lastShowMnemonicWindow.get();
|
||||||
if( window != null )
|
if( window != null ) {
|
||||||
repaintMnemonics( window );
|
repaintMnemonics( window );
|
||||||
|
|
||||||
|
if( windowListener != null ) {
|
||||||
|
window.removeWindowListener( windowListener );
|
||||||
|
windowListener = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lastShowMnemonicWindow = null;
|
lastShowMnemonicWindow = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user