mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 14:30:56 +03:00
Styling: support ScrollBar
This commit is contained in:
@@ -39,13 +39,13 @@ public class FlatArrowButton
|
|||||||
{
|
{
|
||||||
public static final int DEFAULT_ARROW_WIDTH = 8;
|
public static final int DEFAULT_ARROW_WIDTH = 8;
|
||||||
|
|
||||||
protected final boolean chevron;
|
protected boolean chevron;
|
||||||
protected final Color foreground;
|
protected Color foreground;
|
||||||
protected final Color disabledForeground;
|
protected Color disabledForeground;
|
||||||
protected final Color hoverForeground;
|
protected Color hoverForeground;
|
||||||
protected final Color hoverBackground;
|
protected Color hoverBackground;
|
||||||
protected final Color pressedForeground;
|
protected Color pressedForeground;
|
||||||
protected final Color pressedBackground;
|
protected Color pressedBackground;
|
||||||
|
|
||||||
private int arrowWidth = DEFAULT_ARROW_WIDTH;
|
private int arrowWidth = DEFAULT_ARROW_WIDTH;
|
||||||
private int xOffset = 0;
|
private int xOffset = 0;
|
||||||
@@ -58,14 +58,8 @@ public class FlatArrowButton
|
|||||||
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
|
Color hoverForeground, Color hoverBackground, Color pressedForeground, Color pressedBackground )
|
||||||
{
|
{
|
||||||
super( direction, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
|
super( direction, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE );
|
||||||
|
update( type, foreground, disabledForeground, hoverForeground, hoverBackground,
|
||||||
this.chevron = FlatUIUtils.isChevron( type );
|
pressedForeground, pressedBackground );
|
||||||
this.foreground = foreground;
|
|
||||||
this.disabledForeground = disabledForeground;
|
|
||||||
this.hoverForeground = hoverForeground;
|
|
||||||
this.hoverBackground = hoverBackground;
|
|
||||||
this.pressedForeground = pressedForeground;
|
|
||||||
this.pressedBackground = pressedBackground;
|
|
||||||
|
|
||||||
setOpaque( false );
|
setOpaque( false );
|
||||||
setBorder( null );
|
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() {
|
public int getArrowWidth() {
|
||||||
return arrowWidth;
|
return arrowWidth;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import java.awt.Rectangle;
|
|||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.beans.PropertyChangeListener;
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import javax.swing.InputMap;
|
import javax.swing.InputMap;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@@ -35,6 +36,7 @@ import javax.swing.UIManager;
|
|||||||
import javax.swing.plaf.ComponentUI;
|
import javax.swing.plaf.ComponentUI;
|
||||||
import javax.swing.plaf.basic.BasicScrollBarUI;
|
import javax.swing.plaf.basic.BasicScrollBarUI;
|
||||||
import com.formdev.flatlaf.FlatClientProperties;
|
import com.formdev.flatlaf.FlatClientProperties;
|
||||||
|
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||||
import com.formdev.flatlaf.util.UIScale;
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,32 +77,44 @@ import com.formdev.flatlaf.util.UIScale;
|
|||||||
public class FlatScrollBarUI
|
public class FlatScrollBarUI
|
||||||
extends BasicScrollBarUI
|
extends BasicScrollBarUI
|
||||||
{
|
{
|
||||||
protected Insets trackInsets;
|
// overrides BasicScrollBarUI.supportsAbsolutePositioning (which is private)
|
||||||
protected Insets thumbInsets;
|
@Styleable protected boolean allowsAbsolutePositioning;
|
||||||
protected int trackArc;
|
|
||||||
protected int thumbArc;
|
|
||||||
protected Color hoverTrackColor;
|
|
||||||
protected Color hoverThumbColor;
|
|
||||||
protected boolean hoverThumbWithTrack;
|
|
||||||
protected Color pressedTrackColor;
|
|
||||||
protected Color pressedThumbColor;
|
|
||||||
protected boolean pressedThumbWithTrack;
|
|
||||||
|
|
||||||
protected boolean showButtons;
|
@Styleable protected Insets trackInsets;
|
||||||
protected String arrowType;
|
@Styleable protected Insets thumbInsets;
|
||||||
protected Color buttonArrowColor;
|
@Styleable protected int trackArc;
|
||||||
protected Color buttonDisabledArrowColor;
|
@Styleable protected int thumbArc;
|
||||||
protected Color hoverButtonBackground;
|
@Styleable protected Color hoverTrackColor;
|
||||||
protected Color pressedButtonBackground;
|
@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;
|
private MouseAdapter hoverListener;
|
||||||
protected boolean hoverTrack;
|
protected boolean hoverTrack;
|
||||||
protected boolean hoverThumb;
|
protected boolean hoverThumb;
|
||||||
|
|
||||||
|
private Map<String, Object> oldStyleValues;
|
||||||
|
|
||||||
public static ComponentUI createUI( JComponent c ) {
|
public static ComponentUI createUI( JComponent c ) {
|
||||||
return new FlatScrollBarUI();
|
return new FlatScrollBarUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void installUI( JComponent c ) {
|
||||||
|
super.installUI( c );
|
||||||
|
|
||||||
|
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void installListeners() {
|
protected void installListeners() {
|
||||||
super.installListeners();
|
super.installListeners();
|
||||||
@@ -123,6 +137,8 @@ public class FlatScrollBarUI
|
|||||||
protected void installDefaults() {
|
protected void installDefaults() {
|
||||||
super.installDefaults();
|
super.installDefaults();
|
||||||
|
|
||||||
|
allowsAbsolutePositioning = super.getSupportsAbsolutePositioning();
|
||||||
|
|
||||||
trackInsets = UIManager.getInsets( "ScrollBar.trackInsets" );
|
trackInsets = UIManager.getInsets( "ScrollBar.trackInsets" );
|
||||||
thumbInsets = UIManager.getInsets( "ScrollBar.thumbInsets" );
|
thumbInsets = UIManager.getInsets( "ScrollBar.thumbInsets" );
|
||||||
trackArc = UIManager.getInt( "ScrollBar.trackArc" );
|
trackArc = UIManager.getInt( "ScrollBar.trackArc" );
|
||||||
@@ -177,6 +193,12 @@ public class FlatScrollBarUI
|
|||||||
scrollbar.repaint();
|
scrollbar.repaint();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case FlatClientProperties.COMPONENT_STYLE:
|
||||||
|
applyStyle( e.getNewValue() );
|
||||||
|
scrollbar.revalidate();
|
||||||
|
scrollbar.repaint();
|
||||||
|
break;
|
||||||
|
|
||||||
case "componentOrientation":
|
case "componentOrientation":
|
||||||
// this is missing in BasicScrollBarUI.Handler.propertyChange()
|
// this is missing in BasicScrollBarUI.Handler.propertyChange()
|
||||||
InputMap inputMap = (InputMap) UIManager.get( "ScrollBar.ancestorInputMap" );
|
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
|
@Override
|
||||||
public Dimension getPreferredSize( JComponent c ) {
|
public Dimension getPreferredSize( JComponent c ) {
|
||||||
return UIScale.scale( super.getPreferredSize( c ) );
|
return UIScale.scale( super.getPreferredSize( c ) );
|
||||||
@@ -295,6 +346,11 @@ public class FlatScrollBarUI
|
|||||||
return UIScale.scale( FlatUIUtils.addInsets( super.getMaximumThumbSize(), thumbInsets ) );
|
return UIScale.scale( FlatUIUtils.addInsets( super.getMaximumThumbSize(), thumbInsets ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getSupportsAbsolutePositioning() {
|
||||||
|
return allowsAbsolutePositioning;
|
||||||
|
}
|
||||||
|
|
||||||
//---- class ScrollBarHoverListener ---------------------------------------
|
//---- class ScrollBarHoverListener ---------------------------------------
|
||||||
|
|
||||||
// using static field to disabling hover for other scroll bars
|
// using static field to disabling hover for other scroll bars
|
||||||
@@ -368,6 +424,11 @@ public class FlatScrollBarUI
|
|||||||
setRequestFocusEnabled( false );
|
setRequestFocusEnabled( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void update() {
|
||||||
|
update( arrowType, buttonArrowColor, buttonDisabledArrowColor,
|
||||||
|
null, hoverButtonBackground, null, pressedButtonBackground );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Color deriveBackground( Color background ) {
|
protected Color deriveBackground( Color background ) {
|
||||||
return FlatUIUtils.deriveColor( background, scrollbar.getBackground() );
|
return FlatUIUtils.deriveColor( background, scrollbar.getBackground() );
|
||||||
|
|||||||
@@ -126,6 +126,36 @@ public class FlatStylingTests
|
|||||||
ui.applyStyle( "icon.selectedPressedBackground: #fff" );
|
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
|
@Test
|
||||||
void slider() {
|
void slider() {
|
||||||
FlatSliderUI ui = new FlatSliderUI();
|
FlatSliderUI ui = new FlatSliderUI();
|
||||||
|
|||||||
Reference in New Issue
Block a user