From 11be8f08892f1b79c4f1efde8675498d280c2f24 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Thu, 17 Aug 2017 23:44:39 +0200 Subject: [PATCH] =?UTF-8?q?Module=20client=20:=20ajout=20composant=20Toast?= =?UTF-8?q?=20+=20factory=20associ=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/pqt/client/Main.java | 2 + .../components/generics/toast/Toast.java | 54 +++++++++++++++++++ .../generics/toast/ToastFactory.java | 46 ++++++++++++++++ .../client/src/main/resources/dark-theme.css | 14 +++++ 4 files changed, 116 insertions(+) create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/Toast.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/ToastFactory.java diff --git a/Workspace/client/src/main/java/com/pqt/client/Main.java b/Workspace/client/src/main/java/com/pqt/client/Main.java index 8e364c0f..f65aa47c 100644 --- a/Workspace/client/src/main/java/com/pqt/client/Main.java +++ b/Workspace/client/src/main/java/com/pqt/client/Main.java @@ -6,6 +6,7 @@ import com.pqt.client.gui.modules.stat_screen.StatScreen; import com.pqt.client.gui.modules.stock_screen.StockScreen; import com.pqt.client.gui.ressources.components.generics.others.SideBar; import com.pqt.client.gui.ressources.components.generics.others.listeners.ISideBarListener; +import com.pqt.client.gui.ressources.components.generics.toast.ToastFactory; import com.pqt.client.gui.ressources.css.GUICssTool; import com.pqt.client.gui.ressources.strings.GUIStringTool; import com.pqt.client.module.account.AccountService; @@ -38,6 +39,7 @@ public class Main extends Application{ scene.getStylesheets().clear(); scene.getStylesheets().addAll(getClass().getResource(GUICssTool.getCssFilePath()).toExternalForm()); + ToastFactory.init(primaryStage); primaryStage.setTitle(GUIStringTool.getAppTitle()); primaryStage.setScene(scene); primaryStage.show(); diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/Toast.java b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/Toast.java new file mode 100644 index 00000000..051cc5ac --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/Toast.java @@ -0,0 +1,54 @@ +package com.pqt.client.gui.ressources.components.generics.toast; + +import com.pqt.client.gui.ressources.css.GUICssTool; +import javafx.animation.KeyFrame; +import javafx.animation.KeyValue; +import javafx.animation.Timeline; +import javafx.scene.Scene; +import javafx.scene.layout.StackPane; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; +import javafx.stage.Stage; +import javafx.stage.StageStyle; +import javafx.util.Duration; + +final class Toast { + static void toast(Stage ownerStage, String toastMsg, int toastDelay, int fadeInDelay, int fadeOutDelay) { + Stage toastStage = new Stage(); + toastStage.initOwner(ownerStage); + toastStage.setResizable(false); + toastStage.initStyle(StageStyle.TRANSPARENT); + + Text text = new Text(toastMsg); + text.getStyleClass().add("toast-text"); + + StackPane root = new StackPane(text); + root.getStyleClass().add("toast-pane"); + root.setOpacity(0); + + Scene scene = new Scene(root); + scene.setFill(Color.TRANSPARENT); + scene.getStylesheets().addAll(Toast.class.getResource(GUICssTool.getCssFilePath()).toExternalForm()); + toastStage.setScene(scene); + toastStage.show(); + + Timeline fadeInTimeline = new Timeline(); + KeyFrame fadeInKey1 = new KeyFrame(Duration.millis(fadeInDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 1)); + fadeInTimeline.getKeyFrames().add(fadeInKey1); + fadeInTimeline.setOnFinished((ae) -> + new Thread(() -> { + try { + Thread.sleep(toastDelay); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + Timeline fadeOutTimeline = new Timeline(); + KeyFrame fadeOutKey1 = new KeyFrame(Duration.millis(fadeOutDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 0)); + fadeOutTimeline.getKeyFrames().add(fadeOutKey1); + fadeOutTimeline.setOnFinished((aeb) -> toastStage.close()); + fadeOutTimeline.play(); + }).start()); + fadeInTimeline.play(); + } +} \ No newline at end of file diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/ToastFactory.java b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/ToastFactory.java new file mode 100644 index 00000000..d174244e --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/gui/ressources/components/generics/toast/ToastFactory.java @@ -0,0 +1,46 @@ +package com.pqt.client.gui.ressources.components.generics.toast; + +import javafx.stage.Stage; + +public class ToastFactory { + + private static ToastFactory INSTANCE = null; + + private Stage stage; + private int delay, fadeInDelay, fadeOutDelay; + + private ToastFactory(Stage stage){ + this.stage = stage; + delay = 3000; + fadeInDelay = 250; + fadeOutDelay = 250; + } + + public void setToastDelay(int msDelay){ + this.delay = msDelay; + } + + public void setToastFadeInDelay(int msDelay){ + this.fadeInDelay = msDelay; + } + + public void setToastFadeOutDelay(int msDelay){ + this.fadeOutDelay = msDelay; + } + + public void toast(String message){ + Toast.toast(stage, message, delay, fadeInDelay, fadeOutDelay); + } + + public static void init(Stage stage){ + INSTANCE = new ToastFactory(stage); + } + + public static boolean isInitialized(){ + return INSTANCE!=null; + } + + public static ToastFactory getINSTANCE(){ + return INSTANCE; + } +} diff --git a/Workspace/client/src/main/resources/dark-theme.css b/Workspace/client/src/main/resources/dark-theme.css index 4e154cd3..273b47a2 100644 --- a/Workspace/client/src/main/resources/dark-theme.css +++ b/Workspace/client/src/main/resources/dark-theme.css @@ -231,4 +231,18 @@ -fx-padding: 5 5 5 5; -fx-background-radius: 0; -fx-background-insets: 0 0 0 0, 0, 1, 2; + +.toast-text{ + -fx-font: 50px "Impact"; + -fx-fill: whitesmoke; +} + +.toast-pane { + -fx-background-radius: 10px; + -fx-background-color: rgba(0, 0, 0, 0.4); + -fx-border-width: 2px; + -fx-border-color: whitesmoke; + -fx-border-radius: 6px; + -fx-padding: 25px; +} } \ No newline at end of file