From 889b5ea56aa8cd685d491f8ae3cf1f287f710186 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 25 Jul 2020 19:53:52 +0200 Subject: [PATCH] ScrollBar: fixed smooth scrolling issues when continuously scrolling (issue #50) --- .../formdev/flatlaf/ui/FlatScrollBarUI.java | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java index 301f413d..a0b3c47d 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollBarUI.java @@ -22,6 +22,7 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.beans.PropertyChangeListener; @@ -205,6 +206,16 @@ public class FlatScrollBarUI oldStyleValues = null; } + @Override + protected TrackListener createTrackListener() { + return new FlatTrackListener(); + } + + @Override + protected ScrollListener createScrollListener() { + return new FlatScrollListener(); + } + @Override protected PropertyChangeListener createPropertyChangeListener() { PropertyChangeListener superListener = super.createPropertyChangeListener(); @@ -452,11 +463,13 @@ public class FlatScrollBarUI * and then animate scroll bar value from old value to new value. */ public void runAndSetValueAnimated( Runnable r ) { - if( !isSmoothScrollingEnabled() ) { + if( inRunAndSetValueAnimated || !isSmoothScrollingEnabled() ) { r.run(); return; } + inRunAndSetValueAnimated = true; + if( animator != null ) animator.cancel(); @@ -470,11 +483,16 @@ public class FlatScrollBarUI r.run(); int newValue = scrollbar.getValue(); - scrollbar.setValue( oldValue ); + if( newValue != oldValue ) { + scrollbar.setValue( oldValue ); - setValueAnimated( newValue ); + setValueAnimated( newValue ); + } + + inRunAndSetValueAnimated = false; } + private boolean inRunAndSetValueAnimated; private Animator animator; private int targetValue = Integer.MIN_VALUE; private int delta; @@ -524,6 +542,32 @@ public class FlatScrollBarUI return UIManager.getBoolean( "ScrollPane.smoothScrolling" ); } + //---- class FlatTrackListener -------------------------------------------- + + protected class FlatTrackListener + extends TrackListener + { + @Override + public void mousePressed( MouseEvent e ) { + runAndSetValueAnimated( () -> { + super.mousePressed( e ); + } ); + } + } + + //---- class FlatScrollListener ------------------------------------------- + + protected class FlatScrollListener + extends ScrollListener + { + @Override + public void actionPerformed( ActionEvent e ) { + runAndSetValueAnimated( () -> { + super.actionPerformed( e ); + } ); + } + } + //---- class ScrollBarHoverListener --------------------------------------- // using static field to disabling hover for other scroll bars