ScrollBar: support pressed track, thumb and button colors (issue #115)

This commit is contained in:
Karl Tauber
2020-06-18 11:04:38 +02:00
parent 273d762cd3
commit fe15f44e96
8 changed files with 101 additions and 19 deletions

View File

@@ -6,6 +6,9 @@ FlatLaf Change Log
- Button and ToggleButton: Support disabled background color (use UI values
`Button.disabledBackground` and `ToggleButton.disabledBackground`). (issue
#112)
- ScrollBar: Support pressed track, thumb and button colors (use UI values
`ScrollBar.pressedTrackColor`, `ScrollBar.pressedThumbColor` and
`ScrollBar.pressedButtonBackground`). (issue #115)
- TableHeader: Support top/bottom/left positioned sort arrow when using
[Glazed Lists](https://github.com/glazedlists/glazedlists). (issue #113)

View File

@@ -47,15 +47,23 @@ public class FlatArrowButton
private final Color disabledForeground;
private final Color hoverForeground;
private final Color hoverBackground;
private final Color pressedBackground;
private int arrowWidth = DEFAULT_ARROW_WIDTH;
private int xOffset = 0;
private int yOffset = 0;
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 )
{
super( direction, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
@@ -64,11 +72,12 @@ public class FlatArrowButton
this.disabledForeground = disabledForeground;
this.hoverForeground = hoverForeground;
this.hoverBackground = hoverBackground;
this.pressedBackground = pressedBackground;
setOpaque( false );
setBorder( null );
if( hoverForeground != null || hoverBackground != null ) {
if( hoverForeground != null || hoverBackground != null || pressedBackground != null ) {
addMouseListener( new MouseAdapter() {
@Override
public void mouseEntered( MouseEvent e ) {
@@ -81,6 +90,18 @@ public class FlatArrowButton
hover = false;
repaint();
}
@Override
public void mousePressed( MouseEvent e ) {
pressed = true;
repaint();
}
@Override
public void mouseReleased( MouseEvent e ) {
pressed = false;
repaint();
}
} );
}
}
@@ -97,6 +118,10 @@ public class FlatArrowButton
return hover;
}
protected boolean isPressed() {
return pressed;
}
public int getXOffset() {
return xOffset;
}
@@ -113,8 +138,8 @@ public class FlatArrowButton
this.yOffset = yOffset;
}
protected Color deriveHoverBackground( Color hoverBackground ) {
return hoverBackground;
protected Color deriveBackground( Color background ) {
return background;
}
@Override
@@ -136,10 +161,18 @@ public class FlatArrowButton
int height = getHeight();
boolean enabled = isEnabled();
// paint hover background
if( enabled && isHover() && hoverBackground != null ) {
g.setColor( deriveHoverBackground( hoverBackground ) );
g.fillRect( 0, 0, width, height );
// paint hover or pressed background
if( enabled ) {
Color background = (pressedBackground != null && isPressed())
? deriveBackground( pressedBackground )
: ((hoverBackground != null && isHover())
? deriveBackground( hoverBackground )
: null);
if( background != null ) {
g.setColor( background );
g.fillRect( 0, 0, width, height );
}
}
int direction = getDirection();

View File

@@ -59,13 +59,18 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault ScrollBar.thumbInsets Insets
* @uiDefault ScrollBar.trackArc int
* @uiDefault ScrollBar.thumbArc int
* @uiDefault ScrollBar.hoverTrackColor Color
* @uiDefault ScrollBar.hoverThumbColor Color
* @uiDefault ScrollBar.hoverTrackColor Color optional
* @uiDefault ScrollBar.hoverThumbColor Color optional
* @uiDefault ScrollBar.hoverThumbWithTrack boolean
* @uiDefault ScrollBar.pressedTrackColor Color optional
* @uiDefault ScrollBar.pressedThumbColor Color optional
* @uiDefault ScrollBar.pressedThumbWithTrack boolean
* @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault ScrollBar.showButtons boolean
* @uiDefault ScrollBar.buttonArrowColor Color
* @uiDefault ScrollBar.buttonDisabledArrowColor Color
* @uiDefault ScrollBar.hoverButtonBackground Color optional
* @uiDefault ScrollBar.pressedButtonBackground Color optional
*
* @author Karl Tauber
*/
@@ -79,11 +84,16 @@ public class FlatScrollBarUI
protected Color hoverTrackColor;
protected Color hoverThumbColor;
protected boolean hoverThumbWithTrack;
protected Color pressedTrackColor;
protected Color pressedThumbColor;
protected boolean pressedThumbWithTrack;
protected boolean showButtons;
protected String arrowType;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color hoverButtonBackground;
protected Color pressedButtonBackground;
private MouseAdapter hoverListener;
protected boolean hoverTrack;
@@ -122,11 +132,16 @@ public class FlatScrollBarUI
hoverTrackColor = UIManager.getColor( "ScrollBar.hoverTrackColor" );
hoverThumbColor = UIManager.getColor( "ScrollBar.hoverThumbColor" );
hoverThumbWithTrack = UIManager.getBoolean( "ScrollBar.hoverThumbWithTrack" );
pressedTrackColor = UIManager.getColor( "ScrollBar.pressedTrackColor" );
pressedThumbColor = UIManager.getColor( "ScrollBar.pressedThumbColor" );
pressedThumbWithTrack = UIManager.getBoolean( "ScrollBar.pressedThumbWithTrack" );
showButtons = UIManager.getBoolean( "ScrollBar.showButtons" );
arrowType = UIManager.getString( "Component.arrowType" );
buttonArrowColor = UIManager.getColor( "ScrollBar.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ScrollBar.buttonDisabledArrowColor" );
hoverButtonBackground = UIManager.getColor( "ScrollBar.hoverButtonBackground" );
pressedButtonBackground = UIManager.getColor( "ScrollBar.pressedButtonBackground" );
}
@Override
@@ -137,9 +152,13 @@ public class FlatScrollBarUI
thumbInsets = null;
hoverTrackColor = null;
hoverThumbColor = null;
pressedTrackColor = null;
pressedThumbColor = null;
buttonArrowColor = null;
buttonDisabledArrowColor = null;
hoverButtonBackground = null;
pressedButtonBackground = null;
}
@Override
@@ -188,12 +207,12 @@ public class FlatScrollBarUI
}
private JButton createArrowButton( int orientation ) {
FlatArrowButton button = new FlatArrowButton( orientation,
arrowType, buttonArrowColor, buttonDisabledArrowColor, null, hoverTrackColor )
FlatArrowButton button = new FlatArrowButton( orientation, arrowType, buttonArrowColor,
buttonDisabledArrowColor, null, hoverButtonBackground, pressedButtonBackground )
{
@Override
protected Color deriveHoverBackground( Color hoverBackground ) {
return getTrackColor( scrollbar, true ) ;
protected Color deriveBackground( Color background ) {
return FlatUIUtils.deriveColor( background, scrollbar.getBackground() );
}
@Override
@@ -236,7 +255,7 @@ public class FlatScrollBarUI
@Override
protected void paintTrack( Graphics g, JComponent c, Rectangle trackBounds ) {
g.setColor( getTrackColor( c, hoverTrack ) );
g.setColor( getTrackColor( c, hoverTrack, isPressed && hoverTrack && !hoverThumb ) );
paintTrackOrThumb( g, c, trackBounds, trackInsets, trackArc );
}
@@ -245,7 +264,8 @@ public class FlatScrollBarUI
if( thumbBounds.isEmpty() || !scrollbar.isEnabled() )
return;
g.setColor( getThumbColor( c, hoverThumb || (hoverThumbWithTrack && hoverTrack) ) );
g.setColor( getThumbColor( c, hoverThumb || (hoverThumbWithTrack && hoverTrack),
isPressed && (hoverThumb || (pressedThumbWithTrack && hoverTrack)) ) );
paintTrackOrThumb( g, c, thumbBounds, thumbInsets, thumbArc );
}
@@ -277,15 +297,23 @@ public class FlatScrollBarUI
// do not paint
}
protected Color getTrackColor( JComponent c, boolean hover ) {
protected Color getTrackColor( JComponent c, boolean hover, boolean pressed ) {
Color trackColor = FlatUIUtils.deriveColor( this.trackColor, c.getBackground() );
return hover ? FlatUIUtils.deriveColor( hoverTrackColor, trackColor ) : trackColor;
return (pressed && pressedTrackColor != null)
? FlatUIUtils.deriveColor( pressedTrackColor, trackColor )
: ((hover && hoverTrackColor != null)
? FlatUIUtils.deriveColor( hoverTrackColor, trackColor )
: trackColor);
}
protected Color getThumbColor( JComponent c, boolean hover ) {
protected Color getThumbColor( JComponent c, boolean hover, boolean pressed ) {
Color trackColor = FlatUIUtils.deriveColor( this.trackColor, c.getBackground() );
Color thumbColor = FlatUIUtils.deriveColor( this.thumbColor, trackColor );
return hover ? FlatUIUtils.deriveColor( hoverThumbColor, thumbColor ) : thumbColor;
return (pressed && pressedThumbColor != null)
? FlatUIUtils.deriveColor( pressedThumbColor, thumbColor )
: ((hover && hoverThumbColor != null)
? FlatUIUtils.deriveColor( hoverThumbColor, thumbColor )
: thumbColor);
}
@Override
@@ -323,11 +351,14 @@ public class FlatScrollBarUI
@Override
public void mousePressed( MouseEvent e ) {
isPressed = true;
repaint();
}
@Override
public void mouseReleased( MouseEvent e ) {
isPressed = false;
repaint();
update( e.getX(), e.getY() );
}

View File

@@ -221,6 +221,9 @@ ScrollBar.track=lighten(@background,1%,derived noAutoInverse)
ScrollBar.thumb=lighten($ScrollBar.track,10%,derived noAutoInverse)
ScrollBar.hoverTrackColor=lighten($ScrollBar.track,4%,derived noAutoInverse)
ScrollBar.hoverThumbColor=lighten($ScrollBar.thumb,10%,derived noAutoInverse)
ScrollBar.pressedThumbColor=lighten($ScrollBar.thumb,15%,derived noAutoInverse)
ScrollBar.hoverButtonBackground=lighten(@background,5%,derived noAutoInverse)
ScrollBar.pressedButtonBackground=lighten(@background,10%,derived noAutoInverse)
#---- Separator ----

View File

@@ -431,6 +431,7 @@ ScrollBar.thumbInsets=0,0,0,0
ScrollBar.trackArc=0
ScrollBar.thumbArc=0
ScrollBar.hoverThumbWithTrack=false
ScrollBar.pressedThumbWithTrack=false
ScrollBar.showButtons=false
ScrollBar.squareButtons=false
ScrollBar.buttonArrowColor=$ComboBox.buttonArrowColor

View File

@@ -228,6 +228,9 @@ ScrollBar.track=lighten(@background,1%,derived noAutoInverse)
ScrollBar.thumb=darken($ScrollBar.track,10%,derived noAutoInverse)
ScrollBar.hoverTrackColor=darken($ScrollBar.track,3%,derived noAutoInverse)
ScrollBar.hoverThumbColor=darken($ScrollBar.thumb,10%,derived noAutoInverse)
ScrollBar.pressedThumbColor=darken($ScrollBar.thumb,20%,derived noAutoInverse)
ScrollBar.hoverButtonBackground=darken(@background,5%,derived noAutoInverse)
ScrollBar.pressedButtonBackground=darken(@background,10%,derived noAutoInverse)
#---- Separator ----

View File

@@ -762,11 +762,15 @@ ScrollBar.background #3c3f41 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.buttonArrowColor #9a9da1 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.buttonDisabledArrowColor #585858 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
ScrollBar.hoverButtonBackground #484c4e com.formdev.flatlaf.util.DerivedColor [UI] lighten(5%)
ScrollBar.hoverThumbColor #6e767a com.formdev.flatlaf.util.DerivedColor [UI] lighten(10%)
ScrollBar.hoverThumbWithTrack false
ScrollBar.hoverTrackColor #484c4f com.formdev.flatlaf.util.DerivedColor [UI] lighten(4%)
ScrollBar.maximumThumbSize 4096,4096 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.minimumThumbSize 8,8 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.pressedButtonBackground #54595c com.formdev.flatlaf.util.DerivedColor [UI] lighten(10%)
ScrollBar.pressedThumbColor #7a8387 com.formdev.flatlaf.util.DerivedColor [UI] lighten(15%)
ScrollBar.pressedThumbWithTrack false
ScrollBar.showButtons false
ScrollBar.squareButtons false
ScrollBar.thumb #565c5f com.formdev.flatlaf.util.DerivedColor [UI] lighten(10%)

View File

@@ -764,11 +764,15 @@ ScrollBar.background #f2f2f2 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.buttonArrowColor #666666 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.buttonDisabledArrowColor #ababab javax.swing.plaf.ColorUIResource [UI]
ScrollBar.foreground #000000 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.hoverButtonBackground #e5e5e5 com.formdev.flatlaf.util.DerivedColor [UI] darken(5%)
ScrollBar.hoverThumbColor #c3c3c3 com.formdev.flatlaf.util.DerivedColor [UI] darken(10%)
ScrollBar.hoverThumbWithTrack false
ScrollBar.hoverTrackColor #ededed com.formdev.flatlaf.util.DerivedColor [UI] darken(3%)
ScrollBar.maximumThumbSize 4096,4096 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.minimumThumbSize 8,8 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.pressedButtonBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] darken(10%)
ScrollBar.pressedThumbColor #a9a9a9 com.formdev.flatlaf.util.DerivedColor [UI] darken(20%)
ScrollBar.pressedThumbWithTrack false
ScrollBar.showButtons false
ScrollBar.squareButtons false
ScrollBar.thumb #dcdcdc com.formdev.flatlaf.util.DerivedColor [UI] darken(10%)