Commit 21032025

This commit is contained in:
2025-03-27 10:05:47 +07:00
parent 71ecbe0c3e
commit 4535fe0aec

View File

@@ -3,6 +3,7 @@ package id.co.gtc.erhacam;
import javafx.animation.PauseTransition; import javafx.animation.PauseTransition;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
@@ -12,9 +13,13 @@ import javafx.stage.Stage;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
import javafx.util.Duration; import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import static Config.SomeCodes.ValidString;
public class AutoCloseAlert { public class AutoCloseAlert {
private static Stage currentAlertStage; private static Stage currentAlertStage;
@@ -37,6 +42,22 @@ public class AutoCloseAlert {
clear(); clear();
} }
/**
* Show an alert with a title, content, and automatically close after a few seconds
* @param title the title of the alert
* @param content the content of the alert
* @param seconds the number of seconds before the alert is closed, or put 0 to keep it open
* @param onClose What to do after auto close
*/
public static void show(String title, String content, int seconds, Consumer<String> onClose){
Platform.runLater(()->{
Stage alertStage = _showtext(title, "", content);
closeAlertStage(seconds, onClose, alertStage);
});
}
/** /**
* Show an alert with a title, header, content, and automatically close after a few seconds * Show an alert with a title, header, content, and automatically close after a few seconds
* If called several times, the previous alert will be closed before showing a new one * If called several times, the previous alert will be closed before showing a new one
@@ -48,55 +69,82 @@ public class AutoCloseAlert {
*/ */
public static void show(String title, String header, String content, int seconds, Consumer<String> onClose) { public static void show(String title, String header, String content, int seconds, Consumer<String> onClose) {
Platform.runLater(()->{ Platform.runLater(()->{
// close previous alert before showing a new one Stage alertStage = _showtext(title, header, content);
Optional.ofNullable(currentAlertStage).ifPresent(Stage::close); closeAlertStage(seconds, onClose, alertStage);
});
}
Stage alertStage = new Stage(); /**
alertStage.initModality(Modality.APPLICATION_MODAL); * Create an alert with a title, header, and content
alertStage.initStyle(StageStyle.UTILITY); * @param title the title
alertStage.setAlwaysOnTop(true); * @param header the header
alertStage.setResizable(false); * @param content the content
* @return the alert stage
*/
private static Stage _showtext(String title, String header, String content){
// close previous alert before showing a new one
close();
double screenwidth = Screen.getPrimary().getVisualBounds().getWidth(); Stage alertStage = new Stage();
double width = screenwidth/4; alertStage.initModality(Modality.APPLICATION_MODAL);
double height = width * 9 / 16; alertStage.initStyle(StageStyle.UTILITY);
alertStage.setAlwaysOnTop(true);
alertStage.setResizable(false);
double screenwidth = Screen.getPrimary().getVisualBounds().getWidth();
double width = screenwidth/4;
double height = width * 9 / 16;
List<Node> children = new ArrayList<>();
if (ValidString(header)){
Label headerLabel = new Label(header); Label headerLabel = new Label(header);
headerLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 16px;"); headerLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 16px;");
headerLabel.setMinHeight(height*0.25); headerLabel.setMinHeight(height*0.25);
children.add(headerLabel);
}
if (ValidString(content)){
Label contentLabel = new Label(content); Label contentLabel = new Label(content);
contentLabel.setStyle("-fx-font-size: 12px;"); contentLabel.setStyle("-fx-font-size: 12px;");
contentLabel.setMinHeight(height*0.75); contentLabel.setMinHeight(height*0.75);
children.add(contentLabel);
}
VBox root = new VBox(10, headerLabel, contentLabel); VBox root = new VBox(10, children.toArray(new Node[0]));
root.setPrefSize(width, height); root.setPrefSize(width, height);
root.setAlignment(Pos.CENTER); root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root); Scene scene = new Scene(root);
alertStage.setScene(scene); alertStage.setScene(scene);
alertStage.setTitle(title); alertStage.setTitle(title);
currentAlertStage = alertStage;
alertStage.show(); alertStage.show();
currentAlertStage = alertStage;
shownHeader = ValidString(header) ? header : "";
shownContent = ValidString(content) ? content : "";
shownTitle = ValidString(title) ? title : "";
shownHeader = header; return alertStage;
shownContent = content; }
shownTitle = title;
if (seconds>0){
PauseTransition delay = new PauseTransition(Duration.seconds(seconds));
delay.setOnFinished(e -> {
alertStage.close();
if (currentAlertStage == alertStage) {
currentAlertStage = null;
}
if (onClose!=null) onClose.accept(shownTitle);
clear();
} );
delay.play();
}
});
/**
* Close the alert after a few seconds
* @param seconds the number of seconds before the alert is closed, if 0, the alert will not be closed
* @param onClose What to do after auto close
* @param alertStage the alert stage to be closed
*/
private static void closeAlertStage(int seconds, Consumer<String> onClose, Stage alertStage) {
if (seconds>0){
PauseTransition delay = new PauseTransition(Duration.seconds(seconds));
delay.setOnFinished(e -> {
alertStage.close();
if (currentAlertStage == alertStage) {
currentAlertStage = null;
}
if (onClose!=null) onClose.accept(shownTitle);
clear();
} );
delay.play();
}
} }
} }