From 5a9e620c17b06a2c6dafa797be7957cd076f03e3 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 25 Jul 2020 10:53:06 +0200 Subject: [PATCH] Animator: added constructor that allows passing a runnable that is invoked at the end of the animation, which allows using lambdas in most cases --- .../com/formdev/flatlaf/util/Animator.java | 19 ++++++++++++- .../flatlaf/extras/FlatAnimatedLafChange.java | 28 ++++++++----------- .../flatlaf/testing/FlatAnimatorTest.java | 8 +++--- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/Animator.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/Animator.java index 6eac10d0..1bb72bdb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/Animator.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/Animator.java @@ -31,6 +31,7 @@ public class Animator private int resolution = 10; private Interpolator interpolator; private final ArrayList targets = new ArrayList<>(); + private final Runnable endRunnable; private boolean running; private boolean hasBegun; @@ -46,7 +47,7 @@ public class Animator * @param duration the duration of the animation in milliseconds */ public Animator( int duration ) { - this( duration, null ); + this( duration, null, null ); } /** @@ -57,8 +58,21 @@ public class Animator * @param target the target that receives timing events */ public Animator( int duration, TimingTarget target ) { + this( duration, target, null ); + } + + /** + * Creates an animation that runs duration milliseconds. + * Use {@link #start()} to start the animation. + * + * @param duration the duration of the animation in milliseconds + * @param target the target that receives timing events + * @param endRunnable a runnable invoked when the animation ends; or {@code null} + */ + public Animator( int duration, TimingTarget target, Runnable endRunnable ) { setDuration( duration ); addTarget( target ); + this.endRunnable = endRunnable; } /** @@ -249,6 +263,9 @@ public class Animator for( TimingTarget target : targets ) target.end(); } + + if( endRunnable != null ) + endRunnable.run(); } private void throwExceptionIfRunning() { diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatAnimatedLafChange.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatAnimatedLafChange.java index e7689bb1..b0010dea 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatAnimatedLafChange.java +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatAnimatedLafChange.java @@ -115,26 +115,20 @@ public class FlatAnimatedLafChange return; // create animator - animator = new Animator( duration, new Animator.TimingTarget() { - @Override - public void timingEvent( float fraction ) { - if( fraction < 0.1 || fraction > 0.9 ) - return; // ignore initial and last events + animator = new Animator( duration, fraction -> { + if( fraction < 0.1 || fraction > 0.9 ) + return; // ignore initial and last events - alpha = 1f - fraction; + alpha = 1f - fraction; - // repaint snapshots - for( Map.Entry e : map.entrySet() ) { - if( e.getKey().isShowing() ) - e.getValue().repaint(); - } - } - - @Override - public void end() { - hideSnapshot(); - animator = null; + // repaint snapshots + for( Map.Entry e : map.entrySet() ) { + if( e.getKey().isShowing() ) + e.getValue().repaint(); } + }, () -> { + hideSnapshot(); + animator = null; } ); animator.setResolution( resolution ); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatorTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatorTest.java index 5aacd963..831a132d 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatorTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatorTest.java @@ -52,8 +52,8 @@ public class FlatAnimatorTest linearAnimator.stop(); linearAnimator.start(); } else { - linearAnimator = new Animator( 1000, t -> { - linearScrollBar.setValue( Math.round( t * linearScrollBar.getMaximum() ) ); + linearAnimator = new Animator( 1000, fraction -> { + linearScrollBar.setValue( Math.round( fraction * linearScrollBar.getMaximum() ) ); } ); linearAnimator.start(); } @@ -64,8 +64,8 @@ public class FlatAnimatorTest easeInOutAnimator.stop(); easeInOutAnimator.start(); } else { - easeInOutAnimator = new Animator( 1000, t -> { - easeInOutScrollBar.setValue( Math.round( t * easeInOutScrollBar.getMaximum() ) ); + easeInOutAnimator = new Animator( 1000, fraction -> { + easeInOutScrollBar.setValue( Math.round( fraction * easeInOutScrollBar.getMaximum() ) ); } ); easeInOutAnimator.setInterpolator( CubicBezierEasing.EASE_IN_OUT ); easeInOutAnimator.start();