ScrollBar: made styling more flexible by supporting insets and arc for track and thumb (issue #103)

This commit is contained in:
Karl Tauber
2020-05-23 12:05:36 +02:00
parent 10b131e111
commit fd208a3879
5 changed files with 65 additions and 10 deletions

View File

@@ -3,6 +3,8 @@ FlatLaf Change Log
## Unreleased
- ScrollBar: Made styling more flexible by supporting insets and arc for track
and thumb. (issue #103)
- Ubuntu Linux: Fixed poorly rendered font. (issue #105)

View File

@@ -19,6 +19,8 @@ package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@@ -52,8 +54,13 @@ import com.formdev.flatlaf.util.UIScale;
*
* <!-- FlatScrollBarUI -->
*
* @uiDefault ScrollBar.trackInsets Insets
* @uiDefault ScrollBar.thumbInsets Insets
* @uiDefault ScrollBar.trackArc int
* @uiDefault ScrollBar.thumbArc int
* @uiDefault ScrollBar.hoverTrackColor Color
* @uiDefault ScrollBar.hoverThumbColor Color
* @uiDefault ScrollBar.hoverThumbWithTrack boolean
* @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault ScrollBar.showButtons boolean
* @uiDefault ScrollBar.buttonArrowColor Color
@@ -64,8 +71,13 @@ 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 boolean showButtons;
protected String arrowType;
@@ -102,8 +114,13 @@ public class FlatScrollBarUI
protected void installDefaults() {
super.installDefaults();
trackInsets = UIManager.getInsets( "ScrollBar.trackInsets" );
thumbInsets = UIManager.getInsets( "ScrollBar.thumbInsets" );
trackArc = UIManager.getInt( "ScrollBar.trackArc" );
thumbArc = UIManager.getInt( "ScrollBar.thumbArc" );
hoverTrackColor = UIManager.getColor( "ScrollBar.hoverTrackColor" );
hoverThumbColor = UIManager.getColor( "ScrollBar.hoverThumbColor" );
hoverThumbWithTrack = UIManager.getBoolean( "ScrollBar.hoverThumbWithTrack" );
showButtons = UIManager.getBoolean( "ScrollBar.showButtons" );
arrowType = UIManager.getString( "Component.arrowType" );
@@ -115,6 +132,8 @@ public class FlatScrollBarUI
protected void uninstallDefaults() {
super.uninstallDefaults();
trackInsets = null;
thumbInsets = null;
hoverTrackColor = null;
hoverThumbColor = null;
@@ -204,19 +223,15 @@ public class FlatScrollBarUI
}
@Override
protected void paintDecreaseHighlight( Graphics g ) {
// do not paint
}
@Override
protected void paintIncreaseHighlight( Graphics g ) {
// do not paint
public void paint( Graphics g, JComponent c ) {
FlatUIUtils.setRenderingHints( (Graphics2D) g );
super.paint( g, c );
}
@Override
protected void paintTrack( Graphics g, JComponent c, Rectangle trackBounds ) {
g.setColor( hoverTrack ? hoverTrackColor : trackColor );
g.fillRect( trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height );
paintTrackOrThumb( g, c, trackBounds, trackInsets, trackArc );
}
@Override
@@ -225,7 +240,30 @@ public class FlatScrollBarUI
return;
g.setColor( hoverThumb ? hoverThumbColor : thumbColor );
g.fillRect( thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height );
paintTrackOrThumb( g, c, thumbBounds, thumbInsets, thumbArc );
}
protected void paintTrackOrThumb( Graphics g, JComponent c, Rectangle bounds, Insets insets, int arc ) {
bounds = FlatUIUtils.subtractInsets( bounds, UIScale.scale( insets ) );
if( arc <= 0 ) {
// paint rectangle
g.fillRect( bounds.x, bounds.y, bounds.width, bounds.height );
} else {
// paint round rectangle
arc = Math.min( UIScale.scale( arc ), Math.min( bounds.width, bounds.height ) );
g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, arc, arc );
}
}
@Override
protected void paintDecreaseHighlight( Graphics g ) {
// do not paint
}
@Override
protected void paintIncreaseHighlight( Graphics g ) {
// do not paint
}
@Override
@@ -276,7 +314,7 @@ public class FlatScrollBarUI
boolean inThumb = getThumbBounds().contains( x, y );
if( inTrack != hoverTrack || inThumb != hoverThumb ) {
hoverTrack = inTrack;
hoverThumb = inThumb;
hoverThumb = inThumb || (hoverThumbWithTrack && inTrack);
repaint();
}
}

View File

@@ -424,6 +424,11 @@ RadioButtonMenuItem.background=@menuBackground
#---- ScrollBar ----
ScrollBar.width=10
ScrollBar.trackInsets=0,0,0,0
ScrollBar.thumbInsets=0,0,0,0
ScrollBar.trackArc=0
ScrollBar.thumbArc=0
ScrollBar.hoverThumbWithTrack=false
ScrollBar.showButtons=false
ScrollBar.squareButtons=false
ScrollBar.buttonArrowColor=$ComboBox.buttonArrowColor

View File

@@ -761,17 +761,22 @@ 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.hoverThumbColor #717678 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.hoverThumbWithTrack false
ScrollBar.hoverTrackColor #494c4f javax.swing.plaf.ColorUIResource [UI]
ScrollBar.maximumThumbSize 4096,4096 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.minimumThumbSize 8,8 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.showButtons false
ScrollBar.squareButtons false
ScrollBar.thumb #585c5e javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbArc 0
ScrollBar.thumbDarkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbHighlight #242424 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbInsets 0,0,0,0 javax.swing.plaf.InsetsUIResource [UI]
ScrollBar.thumbShadow #646464 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.track #3f4244 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.trackArc 0
ScrollBar.trackHighlight #7e7e7e javax.swing.plaf.ColorUIResource [UI]
ScrollBar.trackInsets 0,0,0,0 javax.swing.plaf.InsetsUIResource [UI]
ScrollBar.width 10
ScrollBarUI com.formdev.flatlaf.ui.FlatScrollBarUI

View File

@@ -763,17 +763,22 @@ 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.hoverThumbColor #c3c3c3 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.hoverThumbWithTrack false
ScrollBar.hoverTrackColor #ededed javax.swing.plaf.ColorUIResource [UI]
ScrollBar.maximumThumbSize 4096,4096 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.minimumThumbSize 8,8 javax.swing.plaf.DimensionUIResource [UI]
ScrollBar.showButtons false
ScrollBar.squareButtons false
ScrollBar.thumb #dcdcdc javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbArc 0
ScrollBar.thumbDarkShadow #9e9e9e javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbHighlight #ffffff javax.swing.plaf.ColorUIResource [UI]
ScrollBar.thumbInsets 0,0,0,0 javax.swing.plaf.InsetsUIResource [UI]
ScrollBar.thumbShadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.track #f5f5f5 javax.swing.plaf.ColorUIResource [UI]
ScrollBar.trackArc 0
ScrollBar.trackHighlight #9e9e9e javax.swing.plaf.ColorUIResource [UI]
ScrollBar.trackInsets 0,0,0,0 javax.swing.plaf.InsetsUIResource [UI]
ScrollBar.width 10
ScrollBarUI com.formdev.flatlaf.ui.FlatScrollBarUI