From e5ddb9004f304860d137e2eeb8b1f9540f83b3d7 Mon Sep 17 00:00:00 2001 From: mkpaz Date: Tue, 30 May 2023 20:20:56 +0400 Subject: [PATCH] Apply customized Sampler styles before showing a modal dialog #46 --- .../sampler/page/components/DialogPage.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/sampler/src/main/java/atlantafx/sampler/page/components/DialogPage.java b/sampler/src/main/java/atlantafx/sampler/page/components/DialogPage.java index ecb0004..d2df2d7 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/components/DialogPage.java +++ b/sampler/src/main/java/atlantafx/sampler/page/components/DialogPage.java @@ -16,6 +16,7 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.ChoiceDialog; +import javafx.scene.control.Dialog; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextInputDialog; @@ -59,7 +60,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(FAKER.chuckNorris().fact()); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); var warnBtn = new Button("Click", new FontIcon(Feather.ALERT_TRIANGLE)); @@ -69,7 +70,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(FAKER.chuckNorris().fact()); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); var errorBtn = new Button("Click", new FontIcon(Feather.X_CIRCLE)); @@ -79,7 +80,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(FAKER.chuckNorris().fact()); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); //snippet_1:end @@ -124,7 +125,7 @@ public final class DialogPage extends OutlinePage { alert.getDialogPane().setExpandableContent(content); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); //snippet_2:end @@ -158,7 +159,7 @@ public final class DialogPage extends OutlinePage { alert.getButtonTypes().setAll(yesBtn, noBtn, cancelBtn); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); //snippet_3:end @@ -182,7 +183,7 @@ public final class DialogPage extends OutlinePage { dialog.setHeaderText(FAKER.chuckNorris().fact()); dialog.setContentText("Enter your name:"); dialog.initOwner(getScene().getWindow()); - dialog.showAndWait(); + show(dialog); }); //snippet_4:end @@ -206,7 +207,7 @@ public final class DialogPage extends OutlinePage { dialog.setHeaderText(FAKER.chuckNorris().fact()); dialog.setContentText("Choose your letter:"); dialog.initOwner(getScene().getWindow()); - dialog.showAndWait(); + show(dialog); }); //snippet_5:end @@ -229,7 +230,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(null); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); var warnBtn = new Button("Click", new FontIcon(Feather.ALERT_TRIANGLE)); @@ -239,7 +240,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(null); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); var errorBtn = new Button("Click", new FontIcon(Feather.X_CIRCLE)); @@ -249,7 +250,7 @@ public final class DialogPage extends OutlinePage { alert.setHeaderText(null); alert.setContentText(FAKER.lorem().paragraph(3)); alert.initOwner(getScene().getWindow()); - alert.showAndWait(); + show(alert); }); //snippet_6:end @@ -263,4 +264,18 @@ public final class DialogPage extends OutlinePage { return example; } + + private void show(Dialog alert) { + // copy customized styles, like changed accent color etc + try { + for (var pc : getScene().getRoot().getPseudoClassStates()) { + alert.getDialogPane().pseudoClassStateChanged(pc, true); + } + alert.getDialogPane().getStylesheets().addAll(getScene().getRoot().getStylesheets()); + } catch (Exception ignored) { + // yes, ignored + } + + alert.showAndWait(); + } }