Skip to content

Commit ba8172c

Browse files
committed
AnimatedBorder:
- support repainting only necessary region while animating - use AbstractBorder in test app and fixed insets
1 parent b44c079 commit ba8172c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedBorder.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* A client property is set on the component to store the animation state.
6666
*
6767
* @author Karl Tauber
68+
* @since 1.5
6869
*/
6970
public interface AnimatedBorder
7071
extends Border
@@ -89,6 +90,20 @@ default void paintBorder( Component c, Graphics g, int x, int y, int width, int
8990
*/
9091
void paintBorderAnimated( Component c, Graphics g, int x, int y, int width, int height, float animatedValue );
9192

93+
/**
94+
* Repaint the animated part of the border.
95+
* <p>
96+
* Useful to limit the repaint region. E.g. if only the bottom border is animated.
97+
* If more than one border side is animated (e.g. bottom and right side), then it
98+
* makes no sense to do separate repaints because the Swing repaint manager unions
99+
* the regions and the whole component is repainted.
100+
* <p>
101+
* The default implementation repaints the whole component.
102+
*/
103+
default void repaintBorder( Component c, int x, int y, int width, int height ) {
104+
c.repaint( x, y, width, height );
105+
}
106+
92107
/**
93108
* Gets the value of the component.
94109
* <p>
@@ -197,7 +212,7 @@ public static void paintBorder( AnimatedBorder border, Component c, Graphics g,
197212
as2.fraction = fraction;
198213

199214
// repaint border
200-
c.repaint( as2.x, as2.y, as2.width, as2.height );
215+
border.repaintBorder( c, as2.x, as2.y, as2.width, as2.height );
201216
}, () -> {
202217
as2.startValue = as2.animatedValue = as2.targetValue;
203218
as2.animator = null;

flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatAnimatedBorderTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.awt.Insets;
2424
import java.awt.geom.Rectangle2D;
2525
import javax.swing.*;
26-
import com.formdev.flatlaf.ui.FlatMarginBorder;
26+
import javax.swing.border.AbstractBorder;
2727
import com.formdev.flatlaf.ui.FlatUIUtils;
2828
import com.formdev.flatlaf.util.AnimatedBorder;
2929
import com.formdev.flatlaf.util.ColorFunctions;
@@ -133,7 +133,7 @@ private void initComponents() {
133133
* - animates focus indicator color and border width
134134
*/
135135
private class AnimatedFocusFadeBorder
136-
extends FlatMarginBorder
136+
extends AbstractBorder
137137
implements AnimatedBorder
138138
{
139139
// 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
155155
FlatUIUtils.paintComponentBorder( (Graphics2D) g, x, y, width, height, 0, lw, 0 );
156156
}
157157

158+
@Override
159+
public Insets getBorderInsets( Component c, Insets insets ) {
160+
insets.top = insets.bottom = UIScale.scale( 3 );
161+
insets.left = insets.right = UIScale.scale( 7 );
162+
return insets;
163+
}
164+
158165
@Override
159166
public float getValue( Component c ) {
160167
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0;
@@ -174,7 +181,7 @@ public int getAnimationDuration() {
174181
* - animates focus indicator at bottom
175182
*/
176183
private class AnimatedMaterialBorder
177-
extends FlatMarginBorder
184+
extends AbstractBorder
178185
implements AnimatedBorder
179186
{
180187
// 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
206213
} );
207214
}
208215

216+
@Override
217+
public void repaintBorder( Component c, int x, int y, int width, int height ) {
218+
// limit repaint to bottom border
219+
int lh = UIScale.scale( 2 );
220+
c.repaint( x, y + height - lh, width, lh );
221+
}
222+
223+
@Override
224+
public Insets getBorderInsets( Component c, Insets insets ) {
225+
insets.top = insets.bottom = UIScale.scale( 3 );
226+
insets.left = insets.right = UIScale.scale( 7 );
227+
return insets;
228+
}
229+
209230
@Override
210231
public float getValue( Component c ) {
211232
return FlatUIUtils.isPermanentFocusOwner( c ) ? 1 : 0;
@@ -245,7 +266,7 @@ public int getAnimationDuration() {
245266

246267
@Override
247268
public Insets getBorderInsets( Component c ) {
248-
return UIScale.scale( new Insets( 4, 4, 4, 4 ) );
269+
return UIScale.scale( new Insets( 3, 7, 3, 7 ) );
249270
}
250271

251272
@Override

0 commit comments

Comments
 (0)