hide focus indicator when the containing window became inactive

This commit is contained in:
Karl Tauber
2020-07-01 00:21:22 +02:00
parent 19fcb6a82c
commit 41da023bdd
4 changed files with 25 additions and 3 deletions

View File

@@ -1,6 +1,11 @@
FlatLaf Change Log
==================
## Unreleased
- Hide focus indicator when window is inactive.
## 0.37
- Custom window decorations (Windows 10 only; PR #108; issues #47 and #82)

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComponent;
@@ -106,7 +107,11 @@ public class FlatListUI
@Override
public void focusLost( FocusEvent e ) {
super.focusLost( e );
// use invokeLater for the case that the window is deactivated
EventQueue.invokeLater( () -> {
toggleSelectionColors();
} );
}
};
}

View File

@@ -18,6 +18,7 @@ package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JCheckBox;
@@ -174,7 +175,11 @@ public class FlatTableUI
@Override
public void focusLost( FocusEvent e ) {
super.focusLost( e );
// use invokeLater for the case that the window is deactivated
EventQueue.invokeLater( () -> {
toggleSelectionColors();
} );
}
};
}

View File

@@ -38,6 +38,7 @@ import java.awt.geom.RoundRectangle2D;
import java.util.function.Consumer;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
@@ -139,8 +140,14 @@ public class FlatUIUtils
return c instanceof JComponent && Boolean.TRUE.equals( ((JComponent)c).getClientProperty( "JComboBox.isTableCellEditor" ) );
}
/**
* Returns whether the given component is the permanent focus owner and
* is in the active window. Used to paint focus indicators.
*/
public static boolean isPermanentFocusOwner( Component c ) {
return (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == c);
KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
return keyboardFocusManager.getPermanentFocusOwner() == c &&
keyboardFocusManager.getActiveWindow() == SwingUtilities.windowForComponent( c );
}
public static boolean isRoundRect( Component c ) {