diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedBorder.java index add515a24..b47268e72 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedBorder.java @@ -65,6 +65,7 @@ * A client property is set on the component to store the animation state. * * @author Karl Tauber + * @since 1.5 */ public interface AnimatedBorder extends Border @@ -89,6 +90,20 @@ default void paintBorder( Component c, Graphics g, int x, int y, int width, int */ void paintBorderAnimated( Component c, Graphics g, int x, int y, int width, int height, float animatedValue ); + /** + * Repaint the animated part of the border. + *

+ * Useful to limit the repaint region. E.g. if only the bottom border is animated. + * If more than one border side is animated (e.g. bottom and right side), then it + * makes no sense to do separate repaints because the Swing repaint manager unions + * the regions and the whole component is repainted. + *

+ * The default implementation repaints the whole component. + */ + default void repaintBorder( Component c, int x, int y, int width, int height ) { + c.repaint( x, y, width, height ); + } + /** * Gets the value of the component. *

@@ -197,7 +212,7 @@ public static void paintBorder( AnimatedBorder border, Component c, Graphics g, as2.fraction = fraction; // repaint border - c.repaint( as2.x, as2.y, as2.width, as2.height ); + border.repaintBorder( c, as2.x, as2.y, as2.width, as2.height ); }, () -> { as2.startValue = as2.animatedValue = as2.targetValue; as2.animator = null; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatedBorderTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatedBorderTest.java index abd8fcc5d..c24acd688 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatedBorderTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatedBorderTest.java @@ -23,7 +23,7 @@ import java.awt.Insets; import java.awt.geom.Rectangle2D; import javax.swing.*; -import com.formdev.flatlaf.ui.FlatMarginBorder; +import javax.swing.border.AbstractBorder; import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.util.AnimatedBorder; import com.formdev.flatlaf.util.ColorFunctions; @@ -133,7 +133,7 @@ private void initComponents() { * - animates focus indicator color and border width */ private class AnimatedFocusFadeBorder - extends FlatMarginBorder + extends AbstractBorder implements AnimatedBorder { // needed because otherwise the empty paint method in superclass @@ -155,6 +155,13 @@ public void paintBorderAnimated( Component c, Graphics g, int x, int y, int widt FlatUIUtils.paintComponentBorder( (Graphics2D) g, x, y, width, height, 0, lw, 0 ); } + @Override + public Insets getBorderInsets( Component c, Insets insets ) { + insets.top = insets.bottom = UIScale.scale( 3 ); + insets.left = insets.right = UIScale.scale( 7 ); + return insets; + } + @Override public float getValue( Component c ) { return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; @@ -174,7 +181,7 @@ public int getAnimationDuration() { * - animates focus indicator at bottom */ private class AnimatedMaterialBorder - extends FlatMarginBorder + extends AbstractBorder implements AnimatedBorder { // needed because otherwise the empty paint method in superclass @@ -206,6 +213,20 @@ public void paintBorderAnimated( Component c, Graphics g, int x, int y, int widt } ); } + @Override + public void repaintBorder( Component c, int x, int y, int width, int height ) { + // limit repaint to bottom border + int lh = UIScale.scale( 2 ); + c.repaint( x, y + height - lh, width, lh ); + } + + @Override + public Insets getBorderInsets( Component c, Insets insets ) { + insets.top = insets.bottom = UIScale.scale( 3 ); + insets.left = insets.right = UIScale.scale( 7 ); + return insets; + } + @Override public float getValue( Component c ) { return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0; @@ -245,7 +266,7 @@ public int getAnimationDuration() { @Override public Insets getBorderInsets( Component c ) { - return UIScale.scale( new Insets( 4, 4, 4, 4 ) ); + return UIScale.scale( new Insets( 3, 7, 3, 7 ) ); } @Override