From c529dcb747a548e5a5ec95518624f51277242927 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 27 Aug 2023 14:31:30 +0200 Subject: [PATCH] Smooth Scrolling: - fixed jittery repeating-scrolling with PageUp/Down keys when reaching the top/bottom/left/right of the viewport (see FlatScrollBarUI.setValueAnimated()) - temporary change viewport scroll mode only if it is JViewport.BLIT_SCROLL_MODE - use JViewport.SIMPLE_SCROLL_MODE when temporary disabling blitting --- .../formdev/flatlaf/ui/FlatScrollBarUI.java | 20 ++++++++++--------- .../formdev/flatlaf/ui/FlatScrollPaneUI.java | 17 ++++++---------- 2 files changed, 17 insertions(+), 20 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 7b042e63..0b61fe7b 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 @@ -521,23 +521,25 @@ public class FlatScrollBarUI } public void setValueAnimated( int initialValue, int value ) { + if( useValueIsAdjusting ) + scrollbar.setValueIsAdjusting( true ); + + // (always) set scrollbar value to initial value + scrollbar.setValue( initialValue ); + // do some check if animation already running if( animator != null && animator.isRunning() && targetValue != Integer.MIN_VALUE ) { - // ignore requests if animation still running and scroll direction is the same - // and new value is within currently running animation - // (this may occur when repeat-scrolling via keyboard) + // Ignore requests if animation still running and scroll direction is the same + // and new value is within currently running animation. + // Without this check, repeating-scrolling via keyboard would become + // very slow when reaching the top/bottom/left/right of the viewport, + // because it would start a new 200ms animation to scroll a few pixels. if( value == targetValue || (value > startValue && value < targetValue) || // scroll down/right (value < startValue && value > targetValue) ) // scroll up/left return; } - if( useValueIsAdjusting ) - scrollbar.setValueIsAdjusting( true ); - - // set scrollbar value to initial value - scrollbar.setValue( initialValue ); - startValue = initialValue; targetValue = value; 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 3e42cece..2e5ee728 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 @@ -561,21 +561,16 @@ public class FlatScrollPaneUI */ static void runWithoutBlitting( Container scrollPane, Runnable r ) { // prevent the viewport to immediately repaint using blitting - JViewport viewport = null; - int oldScrollMode = 0; - if( scrollPane instanceof JScrollPane ) { - viewport = ((JScrollPane) scrollPane).getViewport(); - if( viewport != null ) { - oldScrollMode = viewport.getScrollMode(); - viewport.setScrollMode( JViewport.BACKINGSTORE_SCROLL_MODE ); - } - } + JViewport viewport = (scrollPane instanceof JScrollPane) ? ((JScrollPane)scrollPane).getViewport() : null; + boolean isBlitScrollMode = (viewport != null) ? viewport.getScrollMode() == JViewport.BLIT_SCROLL_MODE : false; + if( isBlitScrollMode ) + viewport.setScrollMode( JViewport.SIMPLE_SCROLL_MODE ); try { r.run(); } finally { - if( viewport != null ) - viewport.setScrollMode( oldScrollMode ); + if( isBlitScrollMode ) + viewport.setScrollMode( JViewport.BLIT_SCROLL_MODE ); } }