ScrollBar: hover support

This commit is contained in:
Karl Tauber
2019-09-11 14:00:33 +02:00
parent 78d94885c5
commit 3505d2dfd5
4 changed files with 107 additions and 3 deletions

View File

@@ -16,11 +16,15 @@
package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicScrollBarUI;
import com.formdev.flatlaf.util.UIScale;
@@ -28,15 +32,67 @@ import com.formdev.flatlaf.util.UIScale;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JScrollBar}.
*
* @uiDefault ScrollBar.background Color
* @uiDefault ScrollBar.foreground Color
* @uiDefault ScrollBar.track Color
* @uiDefault ScrollBar.thumb Color
* @uiDefault ScrollBar.hoverTrackColor Color
* @uiDefault ScrollBar.hoverThumbColor Color
* @uiDefault ScrollBar.width int
* @uiDefault ScrollBar.minimumThumbSize Insets
* @uiDefault ScrollBar.maximumThumbSize Insets
* @uiDefault ScrollBar.allowsAbsolutePositioning boolean
*
* @author Karl Tauber
*/
public class FlatScrollBarUI
extends BasicScrollBarUI
{
protected Color hoverTrackColor;
protected Color hoverThumbColor;
private MouseAdapter hoverListener;
private boolean hoverTrack;
private boolean hoverThumb;
public static ComponentUI createUI( JComponent c ) {
return new FlatScrollBarUI();
}
@Override
protected void installListeners() {
super.installListeners();
hoverListener = new ScrollBarHoverListener();
scrollbar.addMouseListener( hoverListener );
scrollbar.addMouseMotionListener( hoverListener );
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
scrollbar.removeMouseListener( hoverListener );
scrollbar.removeMouseMotionListener( hoverListener );
hoverListener = null;
}
@Override
protected void installDefaults() {
super.installDefaults();
hoverTrackColor = UIManager.getColor( "ScrollBar.hoverTrackColor" );
hoverThumbColor = UIManager.getColor( "ScrollBar.hoverThumbColor" );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
hoverTrackColor = null;
hoverThumbColor = null;
}
@Override
public Dimension getPreferredSize( JComponent c ) {
return UIScale.scale( super.getPreferredSize( c ) );
@@ -72,12 +128,18 @@ public class FlatScrollBarUI
// do not paint
}
@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 );
}
@Override
protected void paintThumb( Graphics g, JComponent c, Rectangle thumbBounds ) {
if( thumbBounds.isEmpty() || !scrollbar.isEnabled() )
return;
g.setColor( thumbColor );
g.setColor( hoverThumb ? hoverThumbColor : thumbColor );
g.fillRect( thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height );
}
@@ -90,4 +152,40 @@ public class FlatScrollBarUI
protected Dimension getMaximumThumbSize() {
return UIScale.scale( super.getMaximumThumbSize() );
}
//---- class ScrollBarHoverListener ---------------------------------------
private class ScrollBarHoverListener
extends MouseAdapter
{
@Override
public void mouseEntered( MouseEvent e ) {
hoverTrack = true;
repaint();
}
@Override
public void mouseExited( MouseEvent e ) {
hoverTrack = hoverThumb = false;
repaint();
}
@Override
public void mouseMoved( MouseEvent e ) {
update( e.getX(), e.getY() );
}
private void update( int x, int y ) {
boolean inThumb = thumbRect.contains( x, y );
if( inThumb != hoverThumb ) {
hoverThumb = inThumb;
repaint();
}
}
private void repaint() {
if( scrollbar.isEnabled() )
scrollbar.repaint();
}
}
}

View File

@@ -156,7 +156,9 @@ ProgressBar.selectionBackground=@foreground
#---- ScrollBar ----
ScrollBar.track=3F4244
ScrollBar.thumb=47A6A6A6
ScrollBar.thumb=5B5E5F
ScrollBar.hoverTrackColor=434647
ScrollBar.hoverThumbColor=666868
#---- Separator ----

View File

@@ -161,7 +161,9 @@ ProgressBar.selectionBackground=@foreground
#---- ScrollBar ----
ScrollBar.track=F5F5F5
ScrollBar.thumb=33737373
ScrollBar.thumb=DBDBDB
ScrollBar.hoverTrackColor=e6e6e6
ScrollBar.hoverThumbColor=c6c6c6
#---- Separator ----

View File

@@ -165,6 +165,8 @@ ProgressBar.cycleTime=10000
ScrollBar.track=88ff88
ScrollBar.thumb=33737373
ScrollBar.hoverTrackColor=00ff00
ScrollBar.hoverThumbColor=ff0000
#---- Separator ----