Skip to content

Commit

Permalink
AnimatedBorder:
Browse files Browse the repository at this point in the history
- support repainting only necessary region while animating
- use AbstractBorder in test app and fixed insets
  • Loading branch information
DevCharly committed Jul 14, 2021
1 parent b44c079 commit ba8172c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
* <p>
* 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.
* <p>
* 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.
* <p>
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba8172c

Please sign in to comment.