Styling: support ScrollBar

This commit is contained in:
Karl Tauber
2021-06-17 13:56:38 +02:00
parent b457fd634e
commit 2542c8bd53
3 changed files with 131 additions and 31 deletions

View File

@@ -39,13 +39,13 @@ public class FlatArrowButton
{
public static final int DEFAULT_ARROW_WIDTH = 8;
protected final boolean chevron;
protected final Color foreground;
protected final Color disabledForeground;
protected final Color hoverForeground;
protected final Color hoverBackground;
protected final Color pressedForeground;
protected final Color pressedBackground;
protected boolean chevron;
protected Color foreground;
protected Color disabledForeground;
protected Color hoverForeground;
protected Color hoverBackground;
protected Color pressedForeground;
protected Color pressedBackground;
private int arrowWidth = DEFAULT_ARROW_WIDTH;
private int xOffset = 0;
@@ -58,14 +58,8 @@ public class FlatArrowButton
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
super( direction, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
this.chevron = FlatUIUtils.isChevron( type );
this.foreground = foreground;
this.disabledForeground = disabledForeground;
this.hoverForeground = hoverForeground;
this.hoverBackground = hoverBackground;
this.pressedForeground = pressedForeground;
this.pressedBackground = pressedBackground;
update( type, foreground, disabledForeground, hoverForeground, hoverBackground,
pressedForeground, pressedBackground );
setOpaque( false );
setBorder( null );
@@ -101,6 +95,21 @@ public class FlatArrowButton
}
}
/**
* @since TODO
*/
public void update( String type, Color foreground, Color disabledForeground,
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
{
this.chevron = FlatUIUtils.isChevron( type );
this.foreground = foreground;
this.disabledForeground = disabledForeground;
this.hoverForeground = hoverForeground;
this.hoverBackground = hoverBackground;
this.pressedForeground = pressedForeground;
this.pressedBackground = pressedBackground;
}
public int getArrowWidth() {
return arrowWidth;
}

View File

@@ -24,6 +24,7 @@ import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;
import java.util.Objects;
import javax.swing.InputMap;
import javax.swing.JButton;
@@ -35,6 +36,7 @@ import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicScrollBarUI;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.util.UIScale;
/**
@@ -75,32 +77,44 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatScrollBarUI
extends BasicScrollBarUI
{
protected Insets trackInsets;
protected Insets thumbInsets;
protected int trackArc;
protected int thumbArc;
protected Color hoverTrackColor;
protected Color hoverThumbColor;
protected boolean hoverThumbWithTrack;
protected Color pressedTrackColor;
protected Color pressedThumbColor;
protected boolean pressedThumbWithTrack;
// overrides BasicScrollBarUI.supportsAbsolutePositioning (which is private)
@Styleable protected boolean allowsAbsolutePositioning;
protected boolean showButtons;
protected String arrowType;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color hoverButtonBackground;
protected Color pressedButtonBackground;
@Styleable protected Insets trackInsets;
@Styleable protected Insets thumbInsets;
@Styleable protected int trackArc;
@Styleable protected int thumbArc;
@Styleable protected Color hoverTrackColor;
@Styleable protected Color hoverThumbColor;
@Styleable protected boolean hoverThumbWithTrack;
@Styleable protected Color pressedTrackColor;
@Styleable protected Color pressedThumbColor;
@Styleable protected boolean pressedThumbWithTrack;
@Styleable protected boolean showButtons;
@Styleable protected String arrowType;
@Styleable protected Color buttonArrowColor;
@Styleable protected Color buttonDisabledArrowColor;
@Styleable protected Color hoverButtonBackground;
@Styleable protected Color pressedButtonBackground;
private MouseAdapter hoverListener;
protected boolean hoverTrack;
protected boolean hoverThumb;
private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) {
return new FlatScrollBarUI();
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
applyStyle( FlatStyleSupport.getStyle( c ) );
}
@Override
protected void installListeners() {
super.installListeners();
@@ -123,6 +137,8 @@ public class FlatScrollBarUI
protected void installDefaults() {
super.installDefaults();
allowsAbsolutePositioning = super.getSupportsAbsolutePositioning();
trackInsets = UIManager.getInsets( "ScrollBar.trackInsets" );
thumbInsets = UIManager.getInsets( "ScrollBar.thumbInsets" );
trackArc = UIManager.getInt( "ScrollBar.trackArc" );
@@ -177,6 +193,12 @@ public class FlatScrollBarUI
scrollbar.repaint();
break;
case FlatClientProperties.COMPONENT_STYLE:
applyStyle( e.getNewValue() );
scrollbar.revalidate();
scrollbar.repaint();
break;
case "componentOrientation":
// this is missing in BasicScrollBarUI.Handler.propertyChange()
InputMap inputMap = (InputMap) UIManager.get( "ScrollBar.ancestorInputMap" );
@@ -193,6 +215,35 @@ public class FlatScrollBarUI
};
}
/**
* @since TODO
*/
protected void applyStyle( Object style ) {
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
if( incrButton instanceof FlatScrollBarButton )
((FlatScrollBarButton)incrButton).update();
if( decrButton instanceof FlatScrollBarButton )
((FlatScrollBarButton)decrButton).update();
}
/**
* @since TODO
*/
protected Object applyStyleProperty( String key, Object value ) {
Object oldValue;
switch( key ) {
// BasicScrollBarUI
case "track": oldValue = trackColor; trackColor = (Color) value; return oldValue;
case "thumb": oldValue = thumbColor; thumbColor = (Color) value; return oldValue;
case "width": oldValue = scrollBarWidth; scrollBarWidth = (int) value; return oldValue;
case "minimumThumbSize": oldValue = minimumThumbSize; minimumThumbSize = (Dimension) value; return oldValue;
case "maximumThumbSize": oldValue = maximumThumbSize; maximumThumbSize = (Dimension) value; return oldValue;
}
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
@Override
public Dimension getPreferredSize( JComponent c ) {
return UIScale.scale( super.getPreferredSize( c ) );
@@ -295,6 +346,11 @@ public class FlatScrollBarUI
return UIScale.scale( FlatUIUtils.addInsets( super.getMaximumThumbSize(), thumbInsets ) );
}
@Override
public boolean getSupportsAbsolutePositioning() {
return allowsAbsolutePositioning;
}
//---- class ScrollBarHoverListener ---------------------------------------
// using static field to disabling hover for other scroll bars
@@ -368,6 +424,11 @@ public class FlatScrollBarUI
setRequestFocusEnabled( false );
}
protected void update() {
update( arrowType, buttonArrowColor, buttonDisabledArrowColor,
null, hoverButtonBackground, null, pressedButtonBackground );
}
@Override
protected Color deriveBackground( Color background ) {
return FlatUIUtils.deriveColor( background, scrollbar.getBackground() );

View File

@@ -126,6 +126,36 @@ public class FlatStylingTests
ui.applyStyle( "icon.selectedPressedBackground: #fff" );
}
@Test
void scrollBar() {
FlatScrollBarUI ui = new FlatScrollBarUI();
ui.applyStyle( "track: #fff" );
ui.applyStyle( "thumb: #fff" );
ui.applyStyle( "width: 10" );
ui.applyStyle( "minimumThumbSize: 1,2" );
ui.applyStyle( "maximumThumbSize: 1,2" );
ui.applyStyle( "allowsAbsolutePositioning: true" );
ui.applyStyle( "trackInsets: 1,2,3,4" );
ui.applyStyle( "thumbInsets: 1,2,3,4" );
ui.applyStyle( "trackArc: 5" );
ui.applyStyle( "thumbArc: 10" );
ui.applyStyle( "hoverTrackColor: #fff" );
ui.applyStyle( "hoverThumbColor: #fff" );
ui.applyStyle( "hoverThumbWithTrack: true" );
ui.applyStyle( "pressedTrackColor: #fff" );
ui.applyStyle( "pressedThumbColor: #fff" );
ui.applyStyle( "pressedThumbWithTrack: true" );
ui.applyStyle( "showButtons: true" );
ui.applyStyle( "arrowType: chevron" );
ui.applyStyle( "buttonArrowColor: #fff" );
ui.applyStyle( "buttonDisabledArrowColor: #fff" );
ui.applyStyle( "hoverButtonBackground: #fff" );
ui.applyStyle( "pressedButtonBackground: #fff" );
}
@Test
void slider() {
FlatSliderUI ui = new FlatSliderUI();