Animate Sampler theme change

This commit is contained in:
mkpaz 2023-02-22 20:30:08 +04:00
parent 5add5ce137
commit 318cc43894

@ -29,10 +29,18 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application; import javafx.application.Application;
import javafx.css.PseudoClass; import javafx.css.PseudoClass;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.util.Duration;
public final class ThemeManager { public final class ThemeManager {
@ -99,6 +107,10 @@ public final class ThemeManager {
public void setTheme(SamplerTheme theme) { public void setTheme(SamplerTheme theme) {
Objects.requireNonNull(theme); Objects.requireNonNull(theme);
if (currentTheme != null) {
animateThemeChange(Duration.millis(750));
}
Application.setUserAgentStylesheet(Objects.requireNonNull(theme.getUserAgentStylesheet())); Application.setUserAgentStylesheet(Objects.requireNonNull(theme.getUserAgentStylesheet()));
getScene().getStylesheets().setAll(theme.getAllStylesheets()); getScene().getStylesheets().setAll(theme.getAllStylesheets());
getScene().getRoot().pseudoClassStateChanged(DARK, theme.isDarkMode()); getScene().getRoot().pseudoClassStateChanged(DARK, theme.isDarkMode());
@ -183,6 +195,8 @@ public final class ThemeManager {
public void setAccentColor(AccentColor color) { public void setAccentColor(AccentColor color) {
Objects.requireNonNull(color); Objects.requireNonNull(color);
animateThemeChange(Duration.millis(350));
if (accentColor != null) { if (accentColor != null) {
getScene().getRoot().pseudoClassStateChanged(accentColor.pseudoClass(), false); getScene().getRoot().pseudoClassStateChanged(accentColor.pseudoClass(), false);
} }
@ -194,6 +208,8 @@ public final class ThemeManager {
} }
public void resetAccentColor() { public void resetAccentColor() {
animateThemeChange(Duration.millis(350));
if (accentColor != null) { if (accentColor != null) {
getScene().getRoot().pseudoClassStateChanged(accentColor.pseudoClass(), false); getScene().getRoot().pseudoClassStateChanged(accentColor.pseudoClass(), false);
accentColor = null; accentColor = null;
@ -268,6 +284,21 @@ public final class ThemeManager {
} }
} }
private void animateThemeChange(Duration duration) {
Image snapshot = scene.snapshot(null);
Pane root = (Pane) scene.getRoot();
ImageView imageView = new ImageView(snapshot);
root.getChildren().add(imageView); // add snapshot on top
var transition = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(imageView.opacityProperty(), 1, Interpolator.EASE_OUT)),
new KeyFrame(duration, new KeyValue(imageView.opacityProperty(), 0, Interpolator.EASE_OUT))
);
transition.setOnFinished(e -> root.getChildren().remove(imageView));
transition.play();
}
private void reloadCustomCSS() { private void reloadCustomCSS() {
Objects.requireNonNull(scene); Objects.requireNonNull(scene);
StringBuilder css = new StringBuilder(); StringBuilder css = new StringBuilder();