From fe15f44e967c72244f770a630d0ac604be75e451 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 18 Jun 2020 11:04:38 +0200 Subject: [PATCH] ScrollBar: support pressed track, thumb and button colors (issue #115) --- CHANGELOG.md | 3 + .../formdev/flatlaf/ui/FlatArrowButton.java | 47 +++++++++++++--- .../formdev/flatlaf/ui/FlatScrollBarUI.java | 55 +++++++++++++++---- .../formdev/flatlaf/FlatDarkLaf.properties | 3 + .../com/formdev/flatlaf/FlatLaf.properties | 1 + .../formdev/flatlaf/FlatLightLaf.properties | 3 + .../uidefaults/FlatDarkLaf_1.8.0_202.txt | 4 ++ .../uidefaults/FlatLightLaf_1.8.0_202.txt | 4 ++ 8 files changed, 101 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eda2a6711..910fedb4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatArrowButton.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatArrowButton.java index 5cd440dea..172f07d19 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatArrowButton.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatArrowButton.java @@ -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 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 ) { @@ -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(); + } } ); } } @@ -97,6 +118,10 @@ protected boolean isHover() { return hover; } + protected boolean isPressed() { + return pressed; + } + public int getXOffset() { return xOffset; } @@ -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 @@ -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(); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java index 46fb4b6ba..b62f5f2c5 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java @@ -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 */ @@ -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 @@ 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 @@ -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 @@ -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 @@ -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 ); } @@ -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 ); } @@ -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 @@ -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() ); } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index a3f6ad8f0..ccf68b084 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -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 ---- diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 272535683..65ae790c8 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -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 diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index 938997eaa..b8dd8fe45 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -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 ---- diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt index 0b54cc90a..2e856d91b 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt @@ -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%) diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt index f509c3b11..75227d5fc 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt @@ -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%)