Animate transition between pages

This commit is contained in:
mkpaz 2022-08-25 20:47:00 +04:00
parent 5996323899
commit fc5619c24e
2 changed files with 24 additions and 5 deletions

@ -3,10 +3,14 @@ package atlantafx.sampler.layout;
import atlantafx.sampler.page.Page; import atlantafx.sampler.page.Page;
import atlantafx.sampler.page.components.OverviewPage; import atlantafx.sampler.page.components.OverviewPage;
import javafx.animation.FadeTransition;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.util.Duration;
import java.util.Objects;
import static javafx.scene.layout.Priority.ALWAYS; import static javafx.scene.layout.Priority.ALWAYS;
@ -21,13 +25,27 @@ public class ApplicationWindow extends BorderPane {
sidebar.setOnSelect(pageClass -> { sidebar.setOnSelect(pageClass -> {
try { try {
// reset previous page, e.g. to free resources final Page prevPage = (!pageContainer.getChildren().isEmpty() && pageContainer.getChildren().get(0) instanceof Page page) ? page : null;
if (!pageContainer.getChildren().isEmpty() && pageContainer.getChildren().get(0) instanceof Page page) { final Page nextPage = pageClass.getDeclaredConstructor().newInstance();
page.reset();
// startup, no animation
if (getScene() == null) {
pageContainer.getChildren().add(nextPage.getView());
return;
} }
Page page = pageClass.getDeclaredConstructor().newInstance(); Objects.requireNonNull(prevPage);
pageContainer.getChildren().setAll(page.getView());
// reset previous page, e.g. to free resources
prevPage.reset();
// animate switching between pages
pageContainer.getChildren().add(nextPage.getView());
FadeTransition transition = new FadeTransition(Duration.millis(300), nextPage.getView());
transition.setFromValue(0.0);
transition.setToValue(1.0);
transition.setOnFinished(t -> pageContainer.getChildren().remove(prevPage.getView()));
transition.play();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

@ -64,6 +64,7 @@
.page > .header { .page > .header {
-fx-padding: 10px 20px 14px 20px; -fx-padding: 10px 20px 14px 20px;
-fx-spacing: 10px; -fx-spacing: 10px;
-fx-background-color: -color-bg-default;
} }
.page > .stack > .scroll-pane { .page > .stack > .scroll-pane {
-fx-background-color: -color-bg-default; -fx-background-color: -color-bg-default;