Slider: replaced Slider.thumbWidth with Slider.thumbSize to support non-square sized thumbs (as used in Windows 10)

This commit is contained in:
Karl Tauber
2020-11-19 16:46:46 +01:00
parent 0101171159
commit 1d3ce76b27
7 changed files with 45 additions and 22 deletions

View File

@@ -53,7 +53,7 @@ import com.formdev.flatlaf.util.UIScale;
* <!-- FlatSliderUI --> * <!-- FlatSliderUI -->
* *
* @uiDefault Slider.trackWidth int * @uiDefault Slider.trackWidth int
* @uiDefault Slider.thumbWidth int * @uiDefault Slider.thumbSize Dimension
* @uiDefault Slider.focusWidth int * @uiDefault Slider.focusWidth int
* @uiDefault Slider.trackValueColor Color optional; defaults to Slider.thumbColor * @uiDefault Slider.trackValueColor Color optional; defaults to Slider.thumbColor
* @uiDefault Slider.trackColor Color * @uiDefault Slider.trackColor Color
@@ -73,7 +73,7 @@ public class FlatSliderUI
extends BasicSliderUI extends BasicSliderUI
{ {
protected int trackWidth; protected int trackWidth;
protected int thumbWidth; protected Dimension thumbSize;
protected int focusWidth; protected int focusWidth;
protected Color trackValueColor; protected Color trackValueColor;
@@ -106,7 +106,12 @@ public class FlatSliderUI
LookAndFeel.installProperty( slider, "opaque", false ); LookAndFeel.installProperty( slider, "opaque", false );
trackWidth = UIManager.getInt( "Slider.trackWidth" ); trackWidth = UIManager.getInt( "Slider.trackWidth" );
thumbWidth = UIManager.getInt( "Slider.thumbWidth" ); thumbSize = UIManager.getDimension( "Slider.thumbSize" );
if( thumbSize == null ) {
// fallback for compatibility with old versions
int thumbWidth = UIManager.getInt( "Slider.thumbWidth" );
thumbSize = new Dimension( thumbWidth, thumbWidth );
}
focusWidth = FlatUIUtils.getUIInt( "Slider.focusWidth", 4 ); focusWidth = FlatUIUtils.getUIInt( "Slider.focusWidth", 4 );
trackValueColor = FlatUIUtils.getUIColor( "Slider.trackValueColor", "Slider.thumbColor" ); trackValueColor = FlatUIUtils.getUIColor( "Slider.trackValueColor", "Slider.thumbColor" );
@@ -187,9 +192,16 @@ public class FlatSliderUI
@Override @Override
protected Dimension getThumbSize() { protected Dimension getThumbSize() {
return calcThumbSize( slider, thumbSize, focusWidth );
}
public static Dimension calcThumbSize( JSlider slider, Dimension thumbSize, int focusWidth ) {
int fw = UIScale.scale( focusWidth ); int fw = UIScale.scale( focusWidth );
int w = UIScale.scale( thumbWidth ) + fw + fw; int w = UIScale.scale( thumbSize.width ) + fw + fw;
return new Dimension( w, w ); int h = UIScale.scale( thumbSize.height ) + fw + fw;
return (slider.getOrientation() == JSlider.HORIZONTAL)
? new Dimension( w, h )
: new Dimension( h, w );
} }
@Override @Override
@@ -285,23 +297,23 @@ debug*/
// paint thumb focus border // paint thumb focus border
if( focused ) { if( focused ) {
g.setColor( focusedColor ); g.setColor( focusedColor );
g.fillOval( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height ); ((Graphics2D)g).fill( createRoundThumbShape( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height ) );
} }
if( thumbBorderColor != null ) { if( thumbBorderColor != null ) {
// paint thumb border // paint thumb border
g.setColor( thumbBorderColor ); g.setColor( thumbBorderColor );
g.fillOval( x, y, width, height ); ((Graphics2D)g).fill( createRoundThumbShape( x, y, width, height ) );
// paint thumb background // paint thumb background
float lw = UIScale.scale( 1f ); float lw = UIScale.scale( 1f );
g.setColor( thumbColor ); g.setColor( thumbColor );
((Graphics2D)g).fill( new Ellipse2D.Float( x + lw, y + lw, ((Graphics2D)g).fill( createRoundThumbShape( x + lw, y + lw,
width - lw - lw, height - lw - lw ) ); width - lw - lw, height - lw - lw ) );
} else { } else {
// paint thumb background // paint thumb background
g.setColor( thumbColor ); g.setColor( thumbColor );
g.fillOval( x, y, width, height ); ((Graphics2D)g).fill( createRoundThumbShape( x, y, width, height ) );
} }
} else { } else {
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
@@ -315,13 +327,17 @@ debug*/
g2.translate( thumbRect.width, 0 ); g2.translate( thumbRect.width, 0 );
g2.rotate( Math.toRadians( 90 ) ); g2.rotate( Math.toRadians( 90 ) );
} }
int temp = width;
width = height;
height = temp;
} }
// paint thumb focus border // paint thumb focus border
if( focused ) { if( focused ) {
g2.setColor( focusedColor ); g2.setColor( focusedColor );
g2.fill( createDirectionalThumbShape( 0, 0, g2.fill( createDirectionalThumbShape( 0, 0,
thumbRect.width, thumbRect.height + (fw * 0.4142f), fw ) ); width + fw + fw, height + fw + fw + (fw * 0.4142f), fw ) );
} }
if( thumbBorderColor != null ) { if( thumbBorderColor != null ) {
@@ -345,8 +361,17 @@ debug*/
} }
} }
public static Shape createDirectionalThumbShape( double x, double y, double w, double h, double arc ) { public static Shape createRoundThumbShape( float x, float y, float w, float h ) {
double wh = w / 2; if( w == h )
return new Ellipse2D.Float( x, y, w, h );
else {
float arc = Math.min( w, h );
return new RoundRectangle2D.Float( x, y, w, h, arc, arc );
}
}
public static Shape createDirectionalThumbShape( float x, float y, float w, float h, float arc ) {
float wh = w / 2;
Path2D path = new Path2D.Float(); Path2D path = new Path2D.Float();
path.moveTo( x + wh, y + h ); path.moveTo( x + wh, y + h );

View File

@@ -507,7 +507,7 @@ Separator.stripeIndent=1
Slider.focusInsets=0,0,0,0 Slider.focusInsets=0,0,0,0
Slider.trackWidth=2 Slider.trackWidth=2
Slider.thumbWidth=12 Slider.thumbSize=12,12
Slider.focusWidth=4 Slider.focusWidth=4

View File

@@ -43,7 +43,7 @@ public class FlatRangeSliderUI
extends BasicRangeSliderUI extends BasicRangeSliderUI
{ {
protected int trackWidth; protected int trackWidth;
protected int thumbWidth; protected Dimension thumbSize;
protected int focusWidth; protected int focusWidth;
protected Color trackValueColor; protected Color trackValueColor;
@@ -105,7 +105,7 @@ public class FlatRangeSliderUI
LookAndFeel.installProperty( slider, "opaque", false ); LookAndFeel.installProperty( slider, "opaque", false );
trackWidth = UIManager.getInt( "Slider.trackWidth" ); trackWidth = UIManager.getInt( "Slider.trackWidth" );
thumbWidth = UIManager.getInt( "Slider.thumbWidth" ); thumbSize = UIManager.getDimension( "Slider.thumbSize" );
focusWidth = FlatUIUtils.getUIInt( "Slider.focusWidth", 4 ); focusWidth = FlatUIUtils.getUIInt( "Slider.focusWidth", 4 );
trackValueColor = FlatUIUtils.getUIColor( "Slider.trackValueColor", "Slider.thumbColor" ); trackValueColor = FlatUIUtils.getUIColor( "Slider.trackValueColor", "Slider.thumbColor" );
@@ -185,9 +185,7 @@ public class FlatRangeSliderUI
@Override @Override
protected Dimension getThumbSize() { protected Dimension getThumbSize() {
int fw = UIScale.scale( focusWidth ); return FlatSliderUI.calcThumbSize( slider, thumbSize, focusWidth );
int w = UIScale.scale( thumbWidth ) + fw + fw;
return new Dimension( w, w );
} }
@Override @Override

View File

@@ -858,7 +858,7 @@ Slider.onlyLeftMouseButtonDrag true
Slider.pressedThumbColor #2e6296 com.formdev.flatlaf.util.DerivedColor [UI] darken(15% autoInverse) Slider.pressedThumbColor #2e6296 com.formdev.flatlaf.util.DerivedColor [UI] darken(15% autoInverse)
Slider.shadow #646464 javax.swing.plaf.ColorUIResource [UI] Slider.shadow #646464 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbColor #4a88c7 javax.swing.plaf.ColorUIResource [UI] Slider.thumbColor #4a88c7 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbWidth 12 Slider.thumbSize 12,12 javax.swing.plaf.DimensionUIResource [UI]
Slider.tickColor #888888 javax.swing.plaf.ColorUIResource [UI] Slider.tickColor #888888 javax.swing.plaf.ColorUIResource [UI]
Slider.trackColor #646464 javax.swing.plaf.ColorUIResource [UI] Slider.trackColor #646464 javax.swing.plaf.ColorUIResource [UI]
Slider.trackValueColor #4a88c7 javax.swing.plaf.ColorUIResource [UI] Slider.trackValueColor #4a88c7 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -863,7 +863,7 @@ Slider.onlyLeftMouseButtonDrag true
Slider.pressedThumbColor #125ca5 com.formdev.flatlaf.util.DerivedColor [UI] lighten(15% autoInverse) Slider.pressedThumbColor #125ca5 com.formdev.flatlaf.util.DerivedColor [UI] lighten(15% autoInverse)
Slider.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Slider.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbColor #1e82e6 javax.swing.plaf.ColorUIResource [UI] Slider.thumbColor #1e82e6 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbWidth 12 Slider.thumbSize 12,12 javax.swing.plaf.DimensionUIResource [UI]
Slider.tickColor #888888 javax.swing.plaf.ColorUIResource [UI] Slider.tickColor #888888 javax.swing.plaf.ColorUIResource [UI]
Slider.trackColor #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Slider.trackColor #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
Slider.trackValueColor #1e82e6 javax.swing.plaf.ColorUIResource [UI] Slider.trackValueColor #1e82e6 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -853,7 +853,7 @@ Slider.pressedTrackColor #88ff88 javax.swing.plaf.ColorUIResource [UI]
Slider.shadow #a0a0a0 javax.swing.plaf.ColorUIResource [UI] Slider.shadow #a0a0a0 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbBorderColor #ff0000 javax.swing.plaf.ColorUIResource [UI] Slider.thumbBorderColor #ff0000 javax.swing.plaf.ColorUIResource [UI]
Slider.thumbColor #ffaaaa javax.swing.plaf.ColorUIResource [UI] Slider.thumbColor #ffaaaa javax.swing.plaf.ColorUIResource [UI]
Slider.thumbWidth 12 Slider.thumbSize 12,12 javax.swing.plaf.DimensionUIResource [UI]
Slider.tickColor #ff0000 javax.swing.plaf.ColorUIResource [UI] Slider.tickColor #ff0000 javax.swing.plaf.ColorUIResource [UI]
Slider.trackColor #88ff88 javax.swing.plaf.ColorUIResource [UI] Slider.trackColor #88ff88 javax.swing.plaf.ColorUIResource [UI]
Slider.trackWidth 2 Slider.trackWidth 2

View File

@@ -600,7 +600,7 @@ Slider.onlyLeftMouseButtonDrag
Slider.pressedThumbColor Slider.pressedThumbColor
Slider.shadow Slider.shadow
Slider.thumbColor Slider.thumbColor
Slider.thumbWidth Slider.thumbSize
Slider.tickColor Slider.tickColor
Slider.trackColor Slider.trackColor
Slider.trackValueColor Slider.trackValueColor