smoother transition from old to new theme, independent of UI complexity, when using animated theme change

This commit is contained in:
Karl Tauber
2020-08-31 18:10:54 +02:00
parent faffc9393d
commit 5923ac65df
2 changed files with 31 additions and 7 deletions

View File

@@ -3,7 +3,7 @@ FlatLaf Change Log
## Unreleased
#### New features
#### New features and improvements
- Added API to register packages or folders where FlatLaf searches for
application specific properties files with custom UI defaults (see
@@ -12,6 +12,8 @@ FlatLaf Change Log
application.
- Extras: `FlatSVGIcon` now allows specifying `ClassLoader` that is used to load
SVG file. (issue #163)
- Smoother transition from old to new theme, independent of UI complexity, when
using animated theme change (see [FlatLaf Extras](flatlaf-extras)).
#### Fixed bugs

View File

@@ -56,8 +56,10 @@ public class FlatAnimatedLafChange
public static int resolution = 40;
private static Animator animator;
private static final Map<JLayeredPane, JComponent> map = new WeakHashMap<>();
private static final Map<JLayeredPane, JComponent> oldUIsnapshots = new WeakHashMap<>();
private static final Map<JLayeredPane, JComponent> newUIsnapshots = new WeakHashMap<>();
private static float alpha;
private static boolean inShowSnapshot;
/**
* Create a snapshot of the old UI and shows it on top of the UI.
@@ -73,6 +75,13 @@ public class FlatAnimatedLafChange
alpha = 1;
// show snapshot of old UI
showSnapshot( true, oldUIsnapshots );
}
private static void showSnapshot( boolean useAlpha, Map<JLayeredPane, JComponent> map ) {
inShowSnapshot = true;
// create snapshots for all shown windows
Window[] windows = Window.getWindows();
for( Window window : windows ) {
@@ -94,10 +103,11 @@ public class FlatAnimatedLafChange
JComponent snapshotLayer = new JComponent() {
@Override
public void paint( Graphics g ) {
if( snapshot.contentsLost() )
if( inShowSnapshot || snapshot.contentsLost() )
return;
((Graphics2D)g).setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) );
if( useAlpha )
((Graphics2D)g).setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) );
g.drawImage( snapshot, 0, 0, null );
}
@@ -109,12 +119,16 @@ public class FlatAnimatedLafChange
snapshot.flush();
}
};
if( !useAlpha )
snapshotLayer.setOpaque( true );
snapshotLayer.setSize( layeredPane.getSize() );
// add image layer to layered pane
layeredPane.add( snapshotLayer, JLayeredPane.DRAG_LAYER );
layeredPane.add( snapshotLayer, Integer.valueOf( JLayeredPane.DRAG_LAYER + (useAlpha ? 2 : 1) ) );
map.put( layeredPane, snapshotLayer );
}
inShowSnapshot = false;
}
/**
@@ -126,9 +140,12 @@ public class FlatAnimatedLafChange
if( !FlatSystemProperties.getBoolean( "flatlaf.animatedLafChange", true ) )
return;
if( map.isEmpty() )
if( oldUIsnapshots.isEmpty() )
return;
// show snapshot of new UI
showSnapshot( false, newUIsnapshots );
// create animator
animator = new Animator( duration, fraction -> {
if( fraction < 0.1 || fraction > 0.9 )
@@ -137,7 +154,7 @@ public class FlatAnimatedLafChange
alpha = 1f - fraction;
// repaint snapshots
for( Map.Entry<JLayeredPane, JComponent> e : map.entrySet() ) {
for( Map.Entry<JLayeredPane, JComponent> e : oldUIsnapshots.entrySet() ) {
if( e.getKey().isShowing() )
e.getValue().repaint();
}
@@ -151,6 +168,11 @@ public class FlatAnimatedLafChange
}
private static void hideSnapshot() {
hideSnapshot( oldUIsnapshots );
hideSnapshot( newUIsnapshots );
}
private static void hideSnapshot( Map<JLayeredPane, JComponent> map ) {
// remove snapshots
for( Map.Entry<JLayeredPane, JComponent> e : map.entrySet() ) {
e.getKey().remove( e.getValue() );