Skip to content

Commit

Permalink
ScrollBar: support pressed track, thumb and button colors (issue #115)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Jun 18, 2020
1 parent 273d762 commit fe15f44
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
Expand Up @@ -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 );

Expand All @@ -64,11 +72,12 @@ public FlatArrowButton( int direction, String type, Color foreground, Color disa
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 ) {
Expand All @@ -81,6 +90,18 @@ public void mouseExited( MouseEvent e ) {
hover = false;
repaint();
}

@Override
public void mousePressed( MouseEvent e ) {
pressed = true;
repaint();
}

@Override
public void mouseReleased( MouseEvent e ) {
pressed = false;
repaint();
}
} );
}
}
Expand All @@ -97,6 +118,10 @@ protected boolean isHover() {
return hover;
}

protected boolean isPressed() {
return pressed;
}

public int getXOffset() {
return xOffset;
}
Expand All @@ -113,8 +138,8 @@ public void setYOffset( int yOffset ) {
this.yOffset = yOffset;
}

protected Color deriveHoverBackground( Color hoverBackground ) {
return hoverBackground;
protected Color deriveBackground( Color background ) {
return background;
}

@Override
Expand All @@ -136,10 +161,18 @@ public void paint( Graphics g ) {
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();
Expand Down
Expand Up @@ -59,13 +59,18 @@
* @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
*/
Expand All @@ -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;
Expand Down Expand Up @@ -122,11 +132,16 @@ protected void installDefaults() {
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
Expand All @@ -137,9 +152,13 @@ protected void uninstallDefaults() {
thumbInsets = null;
hoverTrackColor = null;
hoverThumbColor = null;
pressedTrackColor = null;
pressedThumbColor = null;

buttonArrowColor = null;
buttonDisabledArrowColor = null;
hoverButtonBackground = null;
pressedButtonBackground = null;
}

@Override
Expand Down Expand Up @@ -188,12 +207,12 @@ protected JButton createIncreaseButton( int orientation ) {
}

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
Expand Down Expand Up @@ -236,7 +255,7 @@ public void paint( Graphics g, JComponent c ) {

@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 );
}

Expand All @@ -245,7 +264,8 @@ protected void paintThumb( Graphics g, JComponent c, Rectangle thumbBounds ) {
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 );
}

Expand Down Expand Up @@ -277,15 +297,23 @@ protected void paintIncreaseHighlight( Graphics g ) {
// 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
Expand Down Expand Up @@ -323,11 +351,14 @@ public void mouseMoved( MouseEvent e ) {
@Override
public void mousePressed( MouseEvent e ) {
isPressed = true;
repaint();
}

@Override
public void mouseReleased( MouseEvent e ) {
isPressed = false;
repaint();

update( e.getX(), e.getY() );
}

Expand Down
Expand Up @@ -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 ----
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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 ----
Expand Down
Expand Up @@ -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%)
Expand Down
Expand Up @@ -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%)
Expand Down

0 comments on commit fe15f44

Please sign in to comment.