From 9f41ec39868128549362c6edcb61f2574467d299 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 25 Jul 2020 09:40:57 +0200 Subject: [PATCH] ScrollPane: support disabling smooth scrolling per component via client property "JScrollPane.smoothScrolling" --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/FlatClientProperties.java | 8 ++++++++ .../com/formdev/flatlaf/ui/FlatScrollPaneUI.java | 16 ++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a97ad5..619117b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ FlatLaf Change Log maximizing window. E.g. restoring window state at startup. (issue #129) - InternalFrame: Title pane height was too small when iconify, maximize and close buttons are hidden. (issue #132) +- ScrollPane: Enable/disable smooth scrolling per component if client property + "JScrollPane.smoothScrolling" is set to a `Boolean` on `JScrollPane`. ## 0.38 diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java index 20ee7ab5..79ac1ce1 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java @@ -200,6 +200,14 @@ public interface FlatClientProperties */ String SCROLL_BAR_SHOW_BUTTONS = "JScrollBar.showButtons"; + /** + * Specifies whether the scroll pane uses smooth scrolling. + *

+ * Component {{@link javax.swing.JScrollPane}
+ * Value type {@link java.lang.Boolean} + */ + String SCROLL_PANE_SMOOTH_SCROLLING = "JScrollPane.smoothScrolling"; + /** * Specifies whether separators are shown between tabs. *

diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java index 5c0b72aa..13c3ea8c 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatScrollPaneUI.java @@ -114,10 +114,7 @@ public class FlatScrollPaneUI return new BasicScrollPaneUI.MouseWheelHandler() { @Override public void mouseWheelMoved( MouseWheelEvent e ) { - // Note: Getting UI value "ScrollPane.smoothScrolling" here to allow - // applications to turn smooth scrolling on or off at any time - // (e.g. in application options dialog). - if( UIManager.getBoolean( "ScrollPane.smoothScrolling" ) && + if( isSmoothScrollingEnabled() && scrollpane.isWheelScrollingEnabled() && e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL && e.getPreciseWheelRotation() != 0 && @@ -130,6 +127,17 @@ public class FlatScrollPaneUI }; } + protected boolean isSmoothScrollingEnabled() { + Object smoothScrolling = scrollpane.getClientProperty( FlatClientProperties.SCROLL_PANE_SMOOTH_SCROLLING ); + if( smoothScrolling instanceof Boolean ) + return (Boolean) smoothScrolling; + + // Note: Getting UI value "ScrollPane.smoothScrolling" here to allow + // applications to turn smooth scrolling on or off at any time + // (e.g. in application options dialog). + return UIManager.getBoolean( "ScrollPane.smoothScrolling" ); + } + private static final double EPSILON = 1e-5d; private void mouseWheelMovedSmooth( MouseWheelEvent e ) {