focus indication border (or background) no longer hidden when temporary loosing focus (e.g. showing a popup menu)

This commit is contained in:
Karl Tauber
2020-04-20 11:27:29 +02:00
parent 2e1acb7871
commit a2b615d4a7
14 changed files with 35 additions and 26 deletions

View File

@@ -3,6 +3,8 @@ FlatLaf Change Log
## Unreleased ## Unreleased
- Focus indication border (or background) no longer hidden when temporary
loosing focus (e.g. showing a popup menu).
- List, Table and Tree: Item selection color of focused components no longer - List, Table and Tree: Item selection color of focused components no longer
change from blue to gray when temporary loosing focus (e.g. showing a popup change from blue to gray when temporary loosing focus (e.g. showing a popup
menu). menu).

View File

@@ -96,7 +96,7 @@ public class FlatCheckBoxIcon
boolean selected = indeterminate || (c instanceof AbstractButton && ((AbstractButton)c).isSelected()); boolean selected = indeterminate || (c instanceof AbstractButton && ((AbstractButton)c).isSelected());
// paint focused border // paint focused border
if( c.hasFocus() && focusWidth > 0 ) { if( FlatUIUtils.isPermanentFocusOwner( c ) && focusWidth > 0 ) {
g2.setColor( focusColor ); g2.setColor( focusColor );
paintFocusBorder( g2 ); paintFocusBorder( g2 );
} }

View File

@@ -82,7 +82,7 @@ public class FlatHelpButtonIcon
*/ */
boolean enabled = c.isEnabled(); boolean enabled = c.isEnabled();
boolean focused = c.hasFocus(); boolean focused = FlatUIUtils.isPermanentFocusOwner( c );
// paint focused border // paint focused border
if( focused ) { if( focused ) {

View File

@@ -119,7 +119,7 @@ public class FlatBorder
JViewport viewport = ((JScrollPane)c).getViewport(); JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null; Component view = (viewport != null) ? viewport.getView() : null;
if( view != null ) { if( view != null ) {
if( view.hasFocus() ) if( FlatUIUtils.isPermanentFocusOwner( view ) )
return true; return true;
if( (view instanceof JTable && ((JTable)view).isEditing()) || if( (view instanceof JTable && ((JTable)view).isEditing()) ||
@@ -133,17 +133,17 @@ public class FlatBorder
return false; return false;
} else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) { } else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) {
Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent(); Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent();
return (editorComponent != null) ? editorComponent.hasFocus() : false; return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false;
} else if( c instanceof JSpinner ) { } else if( c instanceof JSpinner ) {
JComponent editor = ((JSpinner)c).getEditor(); JComponent editor = ((JSpinner)c).getEditor();
if( editor instanceof JSpinner.DefaultEditor ) { if( editor instanceof JSpinner.DefaultEditor ) {
JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField(); JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField();
if( textField != null ) if( textField != null )
return textField.hasFocus(); return FlatUIUtils.isPermanentFocusOwner( textField );
} }
return false; return false;
} else } else
return c.hasFocus(); return FlatUIUtils.isPermanentFocusOwner( c );
} }
protected boolean isTableCellEditor( Component c ) { protected boolean isTableCellEditor( Component c ) {

View File

@@ -294,7 +294,9 @@ public class FlatButtonUI
// paint shadow // paint shadow
Color shadowColor = def ? defaultShadowColor : this.shadowColor; Color shadowColor = def ? defaultShadowColor : this.shadowColor;
if( !isToolBarButton && shadowColor != null && shadowWidth > 0 && focusWidth > 0 && !c.hasFocus() && c.isEnabled() ) { if( !isToolBarButton && shadowColor != null && shadowWidth > 0 && focusWidth > 0 &&
!FlatUIUtils.isPermanentFocusOwner( c ) && c.isEnabled() )
{
g2.setColor( shadowColor ); g2.setColor( shadowColor );
g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ), g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ),
width - focusWidth * 2, height - focusWidth * 2, arc, arc ) ); width - focusWidth * 2, height - focusWidth * 2, arc, arc ) );
@@ -382,7 +384,7 @@ public class FlatButtonUI
if( hoverColor != null && b != null && b.getModel().isRollover() ) if( hoverColor != null && b != null && b.getModel().isRollover() )
return hoverColor; return hoverColor;
if( focusedColor != null && c.hasFocus() ) if( focusedColor != null && FlatUIUtils.isPermanentFocusOwner( c ) )
return focusedColor; return focusedColor;
return enabledColor; return enabledColor;

View File

@@ -81,7 +81,7 @@ public class FlatListUI
selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" ); selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" ); selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" );
toggleSelectionColors( list.hasFocus() ); toggleSelectionColors();
} }
@Override @Override
@@ -100,13 +100,13 @@ public class FlatListUI
@Override @Override
public void focusGained( FocusEvent e ) { public void focusGained( FocusEvent e ) {
super.focusGained( e ); super.focusGained( e );
toggleSelectionColors( true ); toggleSelectionColors();
} }
@Override @Override
public void focusLost( FocusEvent e ) { public void focusLost( FocusEvent e ) {
super.focusLost( e ); super.focusLost( e );
toggleSelectionColors( e.isTemporary() ); toggleSelectionColors();
} }
}; };
} }
@@ -120,8 +120,8 @@ public class FlatListUI
* already used in applications. Then either the inactive colors are not used, * already used in applications. Then either the inactive colors are not used,
* or the application has to be changed to extend a FlatLaf renderer. * or the application has to be changed to extend a FlatLaf renderer.
*/ */
private void toggleSelectionColors( boolean focused ) { private void toggleSelectionColors() {
if( focused ) { if( FlatUIUtils.isPermanentFocusOwner( list ) ) {
if( list.getSelectionBackground() == selectionInactiveBackground ) if( list.getSelectionBackground() == selectionInactiveBackground )
list.setSelectionBackground( selectionBackground ); list.setSelectionBackground( selectionBackground );
if( list.getSelectionForeground() == selectionInactiveForeground ) if( list.getSelectionForeground() == selectionInactiveForeground )

View File

@@ -168,7 +168,7 @@ public class FlatPasswordFieldUI
protected void paintCapsLock( Graphics g ) { protected void paintCapsLock( Graphics g ) {
JTextComponent c = getComponent(); JTextComponent c = getComponent();
if( !c.isFocusOwner() || if( !FlatUIUtils.isPermanentFocusOwner( c ) ||
!Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK ) ) !Toolkit.getDefaultToolkit().getLockingKeyState( KeyEvent.VK_CAPS_LOCK ) )
return; return;

View File

@@ -201,7 +201,7 @@ public class FlatSliderUI
} }
if( coloredTrack != null ) { if( coloredTrack != null ) {
FlatUIUtils.setColor( g, slider.hasFocus() ? focusColor : (hover ? hoverColor : thumbColor), thumbColor ); FlatUIUtils.setColor( g, FlatUIUtils.isPermanentFocusOwner( slider ) ? focusColor : (hover ? hoverColor : thumbColor), thumbColor );
((Graphics2D)g).fill( coloredTrack ); ((Graphics2D)g).fill( coloredTrack );
} }
@@ -212,7 +212,7 @@ public class FlatSliderUI
@Override @Override
public void paintThumb( Graphics g ) { public void paintThumb( Graphics g ) {
FlatUIUtils.setColor( g, slider.isEnabled() FlatUIUtils.setColor( g, slider.isEnabled()
? (slider.hasFocus() ? focusColor : (hover ? hoverColor : thumbColor)) ? (FlatUIUtils.isPermanentFocusOwner( slider ) ? focusColor : (hover ? hoverColor : thumbColor))
: disabledForeground, : disabledForeground,
thumbColor ); thumbColor );

View File

@@ -318,7 +318,7 @@ public class FlatTabbedPaneUI
boolean enabled = tabPane.isEnabled(); boolean enabled = tabPane.isEnabled();
g.setColor( enabled && tabPane.isEnabledAt( tabIndex ) && getRolloverTab() == tabIndex g.setColor( enabled && tabPane.isEnabledAt( tabIndex ) && getRolloverTab() == tabIndex
? hoverColor ? hoverColor
: (enabled && isSelected && tabPane.hasFocus() : (enabled && isSelected && FlatUIUtils.isPermanentFocusOwner( tabPane )
? focusColor ? focusColor
: (selectedBackground != null && enabled && isSelected : (selectedBackground != null && enabled && isSelected
? selectedBackground ? selectedBackground

View File

@@ -113,7 +113,7 @@ public class FlatTableUI
selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" ); selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" ); selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" );
toggleSelectionColors( table.hasFocus() ); toggleSelectionColors();
int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 ); int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 );
if( rowHeight > 0 ) if( rowHeight > 0 )
@@ -160,13 +160,13 @@ public class FlatTableUI
@Override @Override
public void focusGained( FocusEvent e ) { public void focusGained( FocusEvent e ) {
super.focusGained( e ); super.focusGained( e );
toggleSelectionColors( true ); toggleSelectionColors();
} }
@Override @Override
public void focusLost( FocusEvent e ) { public void focusLost( FocusEvent e ) {
super.focusLost( e ); super.focusLost( e );
toggleSelectionColors( e.isTemporary() ); toggleSelectionColors();
} }
}; };
} }
@@ -180,8 +180,8 @@ public class FlatTableUI
* already used in applications. Then either the inactive colors are not used, * already used in applications. Then either the inactive colors are not used,
* or the application has to be changed to extend a FlatLaf renderer. * or the application has to be changed to extend a FlatLaf renderer.
*/ */
private void toggleSelectionColors( boolean focused ) { private void toggleSelectionColors() {
if( focused ) { if( FlatUIUtils.isPermanentFocusOwner( table ) ) {
if( table.getSelectionBackground() == selectionInactiveBackground ) if( table.getSelectionBackground() == selectionInactiveBackground )
table.setSelectionBackground( selectionBackground ); table.setSelectionBackground( selectionBackground );
if( table.getSelectionForeground() == selectionInactiveForeground ) if( table.getSelectionForeground() == selectionInactiveForeground )

View File

@@ -20,7 +20,6 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Insets; import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
@@ -222,7 +221,7 @@ public class FlatTreeUI
TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf ) TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf )
{ {
boolean isEditing = (editingComponent != null && editingRow == row); boolean isEditing = (editingComponent != null && editingRow == row);
boolean hasFocus = (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == tree); boolean hasFocus = FlatUIUtils.isPermanentFocusOwner( tree );
boolean cellHasFocus = hasFocus && (row == getLeadSelectionRow()); boolean cellHasFocus = hasFocus && (row == getLeadSelectionRow());
boolean isSelected = tree.isRowSelected( row ); boolean isSelected = tree.isRowSelected( row );
boolean isDropRow = isDropRow( row ); boolean isDropRow = isDropRow( row );

View File

@@ -24,6 +24,7 @@ import java.awt.Font;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.Shape; import java.awt.Shape;
@@ -136,6 +137,10 @@ public class FlatUIUtils
return c instanceof JComponent && Boolean.TRUE.equals( ((JComponent)c).getClientProperty( "JComboBox.isTableCellEditor" ) ); return c instanceof JComponent && Boolean.TRUE.equals( ((JComponent)c).getClientProperty( "JComboBox.isTableCellEditor" ) );
} }
public static boolean isPermanentFocusOwner( Component c ) {
return (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == c);
}
/** /**
* Sets rendering hints used for painting. * Sets rendering hints used for painting.
*/ */

View File

@@ -216,7 +216,7 @@ public class FlatJideTabbedPaneUI
g.setColor( enabled && _tabPane.isEnabledAt( tabIndex ) && g.setColor( enabled && _tabPane.isEnabledAt( tabIndex ) &&
(_indexMouseOver == tabIndex || (_closeButtons != null && ((JideTabbedPane.NoFocusButton)_closeButtons[tabIndex]).isMouseOver())) (_indexMouseOver == tabIndex || (_closeButtons != null && ((JideTabbedPane.NoFocusButton)_closeButtons[tabIndex]).isMouseOver()))
? hoverColor ? hoverColor
: (enabled && isSelected && _tabPane.hasFocus() : (enabled && isSelected && FlatUIUtils.isPermanentFocusOwner( _tabPane )
? focusColor ? focusColor
: _tabPane.getBackgroundAt( tabIndex )) ); : _tabPane.getBackgroundAt( tabIndex )) );
g.fillRect( x, y, w, h ); g.fillRect( x, y, w, h );

View File

@@ -20,6 +20,7 @@ import java.awt.Component;
import javax.swing.JTable; import javax.swing.JTable;
import org.jdesktop.swingx.JXDatePicker; import org.jdesktop.swingx.JXDatePicker;
import com.formdev.flatlaf.ui.FlatRoundBorder; import com.formdev.flatlaf.ui.FlatRoundBorder;
import com.formdev.flatlaf.ui.FlatUIUtils;
/** /**
* Border for {@link org.jdesktop.swingx.JXDatePicker}. * Border for {@link org.jdesktop.swingx.JXDatePicker}.
@@ -32,7 +33,7 @@ public class FlatDatePickerBorder
@Override @Override
protected boolean isFocused( Component c ) { protected boolean isFocused( Component c ) {
if( c instanceof JXDatePicker ) if( c instanceof JXDatePicker )
return ((JXDatePicker)c).getEditor().hasFocus(); return FlatUIUtils.isPermanentFocusOwner( ((JXDatePicker)c).getEditor() );
return super.isFocused( c ); return super.isFocused( c );
} }