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.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
@@ -12,9 +13,13 @@ import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import static Config.SomeCodes.ValidString;
public class AutoCloseAlert {
private static Stage currentAlertStage;
@@ -37,6 +42,22 @@ public class AutoCloseAlert {
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
* If called several times, the previous alert will be closed before showing a new one
@@ -48,8 +69,21 @@ public class AutoCloseAlert {
*/
public static void show(String title, String header, String content, int seconds, Consumer<String> onClose) {
Platform.runLater(()->{
Stage alertStage = _showtext(title, header, content);
closeAlertStage(seconds, onClose, alertStage);
});
}
/**
* Create an alert with a title, header, and content
* @param title the title
* @param header the header
* @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
Optional.ofNullable(currentAlertStage).ifPresent(Stage::close);
close();
Stage alertStage = new Stage();
alertStage.initModality(Modality.APPLICATION_MODAL);
@@ -60,14 +94,22 @@ public class AutoCloseAlert {
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);
headerLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 16px;");
headerLabel.setMinHeight(height*0.25);
children.add(headerLabel);
}
if (ValidString(content)){
Label contentLabel = new Label(content);
contentLabel.setStyle("-fx-font-size: 12px;");
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.setAlignment(Pos.CENTER);
@@ -75,14 +117,23 @@ public class AutoCloseAlert {
alertStage.setScene(scene);
alertStage.setTitle(title);
currentAlertStage = alertStage;
alertStage.show();
currentAlertStage = alertStage;
shownHeader = ValidString(header) ? header : "";
shownContent = ValidString(content) ? content : "";
shownTitle = ValidString(title) ? title : "";
shownHeader = header;
shownContent = content;
shownTitle = title;
return alertStage;
}
/**
* 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 -> {
@@ -95,8 +146,5 @@ public class AutoCloseAlert {
} );
delay.play();
}
});
}
}