ComboBox, Spinner and SplitPaneDivider: support "pressed" feedback on arrow buttons

This commit is contained in:
Karl Tauber
2020-12-23 00:02:58 +01:00
parent 073a25f381
commit cbb11ebb03
16 changed files with 119 additions and 80 deletions

View File

@@ -57,18 +57,6 @@ public class FlatArrowButton
private boolean hover;
private boolean pressed;
public FlatArrowButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground )
{
this( direction, type, foreground, disabledForeground, hoverForeground, hoverBackground, null );
}
public FlatArrowButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedBackground )
{
this( direction, type, foreground, disabledForeground, hoverForeground, hoverBackground, null, pressedBackground );
}
public FlatArrowButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
@@ -85,7 +73,9 @@ public class FlatArrowButton
setOpaque( false );
setBorder( null );
if( hoverForeground != null || hoverBackground != null || pressedBackground != null ) {
if( hoverForeground != null || hoverBackground != null ||
pressedForeground != null || pressedBackground != null )
{
addMouseListener( new MouseAdapter() {
@Override
public void mouseEntered( MouseEvent e ) {
@@ -151,7 +141,7 @@ public class FlatArrowButton
}
protected Color deriveForeground( Color foreground ) {
return foreground;
return FlatUIUtils.deriveColor( foreground, this.foreground );
}
@Override

View File

@@ -34,6 +34,8 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
@@ -97,6 +99,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault ComboBox.buttonArrowColor Color
* @uiDefault ComboBox.buttonDisabledArrowColor Color
* @uiDefault ComboBox.buttonHoverArrowColor Color
* @uiDefault ComboBox.buttonPressedArrowColor Color
*
* @author Karl Tauber
*/
@@ -120,9 +123,11 @@ public class FlatComboBoxUI
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor;
protected Color buttonPressedArrowColor;
private MouseListener hoverListener;
protected boolean hover;
protected boolean pressed;
private WeakReference<Component> lastRendererComponent;
@@ -134,13 +139,36 @@ public class FlatComboBoxUI
protected void installListeners() {
super.installListeners();
hoverListener = new FlatUIUtils.HoverListener( null, h -> {
if( !comboBox.isEditable() ) {
hover = h;
if( arrowButton != null )
hoverListener = new MouseAdapter() {
@Override
public void mouseEntered( MouseEvent e ) {
hover = true;
repaintArrowButton();
}
@Override
public void mouseExited( MouseEvent e ) {
hover = false;
repaintArrowButton();
}
@Override
public void mousePressed( MouseEvent e ) {
pressed = true;
repaintArrowButton();
}
@Override
public void mouseReleased( MouseEvent e ) {
pressed = false;
repaintArrowButton();
}
private void repaintArrowButton() {
if( arrowButton != null && !comboBox.isEditable() )
arrowButton.repaint();
}
} );
};
comboBox.addMouseListener( hoverListener );
}
@@ -175,6 +203,7 @@ public class FlatComboBoxUI
buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" );
buttonHoverArrowColor = UIManager.getColor( "ComboBox.buttonHoverArrowColor" );
buttonPressedArrowColor = UIManager.getColor( "ComboBox.buttonPressedArrowColor" );
// set maximumRowCount
int maximumRowCount = UIManager.getInt( "ComboBox.maximumRowCount" );
@@ -203,6 +232,7 @@ public class FlatComboBoxUI
buttonArrowColor = null;
buttonDisabledArrowColor = null;
buttonHoverArrowColor = null;
buttonPressedArrowColor = null;
MigLayoutVisualPadding.uninstall( comboBox );
}
@@ -516,19 +546,26 @@ public class FlatComboBoxUI
extends FlatArrowButton
{
protected FlatComboBoxButton() {
this( SwingConstants.SOUTH, arrowType, buttonArrowColor, buttonDisabledArrowColor, buttonHoverArrowColor, null, null );
this( SwingConstants.SOUTH, arrowType, buttonArrowColor, buttonDisabledArrowColor,
buttonHoverArrowColor, null, buttonPressedArrowColor, null );
}
protected FlatComboBoxButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedBackground )
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
super( direction, type, foreground, disabledForeground, hoverForeground, hoverBackground, pressedBackground );
super( direction, type, foreground, disabledForeground,
hoverForeground, hoverBackground, pressedForeground, pressedBackground );
}
@Override
protected boolean isHover() {
return super.isHover() || (!comboBox.isEditable() ? hover : false);
}
@Override
protected boolean isPressed() {
return super.isPressed() || (!comboBox.isEditable() ? pressed : false);
}
}
//---- class FlatComboPopup -----------------------------------------------

View File

@@ -357,13 +357,14 @@ public class FlatScrollBarUI
{
protected FlatScrollBarButton( int direction ) {
this( direction, arrowType, buttonArrowColor, buttonDisabledArrowColor,
null, hoverButtonBackground, pressedButtonBackground );
null, hoverButtonBackground, null, pressedButtonBackground );
}
protected FlatScrollBarButton( int direction, String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedBackground )
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
super( direction, type, foreground, disabledForeground, hoverForeground, hoverBackground, pressedBackground );
super( direction, type, foreground, disabledForeground,
hoverForeground, hoverBackground, pressedForeground, pressedBackground );
setArrowWidth( FlatArrowButton.DEFAULT_ARROW_WIDTH - 2 );
setFocusable( false );

View File

@@ -69,6 +69,7 @@ import com.formdev.flatlaf.FlatClientProperties;
* @uiDefault Spinner.buttonArrowColor Color
* @uiDefault Spinner.buttonDisabledArrowColor Color
* @uiDefault Spinner.buttonHoverArrowColor Color
* @uiDefault Spinner.buttonPressedArrowColor Color
* @uiDefault Spinner.padding Insets
*
* @author Karl Tauber
@@ -90,6 +91,7 @@ public class FlatSpinnerUI
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor;
protected Color buttonPressedArrowColor;
protected Insets padding;
public static ComponentUI createUI( JComponent c ) {
@@ -114,6 +116,7 @@ public class FlatSpinnerUI
buttonArrowColor = UIManager.getColor( "Spinner.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "Spinner.buttonDisabledArrowColor" );
buttonHoverArrowColor = UIManager.getColor( "Spinner.buttonHoverArrowColor" );
buttonPressedArrowColor = UIManager.getColor( "Spinner.buttonPressedArrowColor" );
padding = UIManager.getInsets( "Spinner.padding" );
// scale
@@ -134,6 +137,7 @@ public class FlatSpinnerUI
buttonArrowColor = null;
buttonDisabledArrowColor = null;
buttonHoverArrowColor = null;
buttonPressedArrowColor = null;
padding = null;
MigLayoutVisualPadding.uninstall( spinner );
@@ -244,7 +248,7 @@ public class FlatSpinnerUI
private Component createArrowButton( int direction, String name ) {
FlatArrowButton button = new FlatArrowButton( direction, arrowType, buttonArrowColor,
buttonDisabledArrowColor, buttonHoverArrowColor, null );
buttonDisabledArrowColor, buttonHoverArrowColor, null, buttonPressedArrowColor, null );
button.setName( name );
button.setYOffset( (direction == SwingConstants.NORTH) ? 1 : -1 );
if( direction == SwingConstants.NORTH )

View File

@@ -52,6 +52,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault SplitPane.continuousLayout boolean
* @uiDefault SplitPaneDivider.oneTouchArrowColor Color
* @uiDefault SplitPaneDivider.oneTouchHoverArrowColor Color
* @uiDefault SplitPaneDivider.oneTouchPressedArrowColor Color
* @uiDefault SplitPaneDivider.style String grip (default) or plain
* @uiDefault SplitPaneDivider.gripColor Color
* @uiDefault SplitPaneDivider.gripDotCount int
@@ -67,6 +68,7 @@ public class FlatSplitPaneUI
private Boolean continuousLayout;
protected Color oneTouchArrowColor;
protected Color oneTouchHoverArrowColor;
protected Color oneTouchPressedArrowColor;
public static ComponentUI createUI( JComponent c ) {
return new FlatSplitPaneUI();
@@ -80,12 +82,22 @@ public class FlatSplitPaneUI
// used in there on LaF switching
oneTouchArrowColor = UIManager.getColor( "SplitPaneDivider.oneTouchArrowColor" );
oneTouchHoverArrowColor = UIManager.getColor( "SplitPaneDivider.oneTouchHoverArrowColor" );
oneTouchPressedArrowColor = UIManager.getColor( "SplitPaneDivider.oneTouchPressedArrowColor" );
super.installDefaults();
continuousLayout = (Boolean) UIManager.get( "SplitPane.continuousLayout" );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
oneTouchArrowColor = null;
oneTouchHoverArrowColor = null;
oneTouchPressedArrowColor = null;
}
@Override
public boolean isContinuousLayout() {
return super.isContinuousLayout() || (continuousLayout != null && Boolean.TRUE.equals( continuousLayout ));
@@ -185,7 +197,8 @@ public class FlatSplitPaneUI
protected final boolean left;
protected FlatOneTouchButton( boolean left ) {
super( SwingConstants.NORTH, arrowType, oneTouchArrowColor, null, oneTouchHoverArrowColor, null );
super( SwingConstants.NORTH, arrowType, oneTouchArrowColor, null,
oneTouchHoverArrowColor, null, oneTouchPressedArrowColor, null );
setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
ToolTipManager.sharedInstance().registerComponent( this );

View File

@@ -33,15 +33,12 @@ import java.awt.Shape;
import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.IdentityHashMap;
import java.util.WeakHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.swing.JComponent;
import javax.swing.JTable;
@@ -680,37 +677,6 @@ public class FlatUIUtils
.computeIfAbsent( key, k -> newInstanceSupplier.get() );
}
//---- class HoverListener ------------------------------------------------
public static class HoverListener
extends MouseAdapter
{
private final Component repaintComponent;
private final Consumer<Boolean> hoverChanged;
public HoverListener( Component repaintComponent, Consumer<Boolean> hoverChanged ) {
this.repaintComponent = repaintComponent;
this.hoverChanged = hoverChanged;
}
@Override
public void mouseEntered( MouseEvent e ) {
hoverChanged.accept( true );
repaint();
}
@Override
public void mouseExited( MouseEvent e ) {
hoverChanged.accept( false );
repaint();
}
private void repaint() {
if( repaintComponent != null && repaintComponent.isEnabled() )
repaintComponent.repaint();
}
}
//---- class RepaintFocusListener -----------------------------------------
public static class RepaintFocusListener

View File

@@ -121,8 +121,9 @@ CheckBox.icon[filled].selectedPressedBackground=darken($CheckBox.icon[filled].se
ComboBox.buttonEditableBackground=#404445
ComboBox.buttonArrowColor=#9A9DA1
ComboBox.buttonDisabledArrowColor=#585858
ComboBox.buttonHoverArrowColor=#bbb
ComboBox.buttonDisabledArrowColor=darken($ComboBox.buttonArrowColor,25%)
ComboBox.buttonHoverArrowColor=lighten($ComboBox.buttonArrowColor,10%,derived noAutoInverse)
ComboBox.buttonPressedArrowColor=lighten($ComboBox.buttonArrowColor,20%,derived noAutoInverse)
#---- Component ----
@@ -251,7 +252,6 @@ Slider.disabledThumbColor=$Slider.disabledTrackColor
#---- SplitPane ----
SplitPaneDivider.draggingColor=#646464
SplitPaneDivider.oneTouchHoverArrowColor=#7A7D81
#---- TabbedPane ----

View File

@@ -519,6 +519,7 @@ Spinner.buttonBackground=$ComboBox.buttonEditableBackground
Spinner.buttonArrowColor=$ComboBox.buttonArrowColor
Spinner.buttonDisabledArrowColor=$ComboBox.buttonDisabledArrowColor
Spinner.buttonHoverArrowColor=$ComboBox.buttonHoverArrowColor
Spinner.buttonPressedArrowColor=$ComboBox.buttonPressedArrowColor
Spinner.padding=@textComponentMargin
Spinner.editorBorderPainted=false
# allowed values: button or none
@@ -536,6 +537,8 @@ SplitPane.oneTouchButtonOffset={scaledInteger}2
SplitPaneDivider.border=null
SplitPaneDivider.oneTouchArrowColor=$ComboBox.buttonArrowColor
SplitPaneDivider.oneTouchHoverArrowColor=$ComboBox.buttonHoverArrowColor
SplitPaneDivider.oneTouchPressedArrowColor=$ComboBox.buttonPressedArrowColor
# allowed values: grip or plain
SplitPaneDivider.style=grip
SplitPaneDivider.gripColor=@icon

View File

@@ -128,8 +128,9 @@ CheckBox.icon[filled].selectedPressedBackground=darken($CheckBox.icon[filled].se
ComboBox.buttonEditableBackground=#fafafa
ComboBox.buttonArrowColor=#666
ComboBox.buttonDisabledArrowColor=#ABABAB
ComboBox.buttonHoverArrowColor=#999
ComboBox.buttonDisabledArrowColor=lighten($ComboBox.buttonArrowColor,25%)
ComboBox.buttonHoverArrowColor=lighten($ComboBox.buttonArrowColor,20%,derived noAutoInverse)
ComboBox.buttonPressedArrowColor=lighten($ComboBox.buttonArrowColor,30%,derived noAutoInverse)
#---- Component ----
@@ -263,7 +264,6 @@ Slider.disabledThumbColor=$Slider.disabledTrackColor
#---- SplitPane ----
SplitPaneDivider.draggingColor=#c4c4c4
SplitPaneDivider.oneTouchHoverArrowColor=#333
#---- TabbedPane ----