2e51f39db2a793b295a0cb815a4dea1524c61bbe
[busymon] / java / notzed.busyalert / au / notzed / busyalert / BusyAlert.java
1 /*
2  * Copyright (C) 2019 Michael Zucchi
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package au.notzed.busyalert;
18
19 import java.time.Duration;
20 import java.time.Instant;
21 import java.time.temporal.ChronoUnit;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import javafx.application.Application;
26 import javafx.application.Platform;
27 import javafx.geometry.Insets;
28 import javafx.geometry.Pos;
29 import javafx.scene.Scene;
30 import javafx.scene.control.Label;
31 import javafx.scene.layout.Background;
32 import javafx.scene.layout.BackgroundFill;
33 import javafx.scene.layout.CornerRadii;
34 import javafx.scene.layout.VBox;
35 import javafx.scene.paint.Color;
36 import javafx.scene.text.Font;
37 import javafx.stage.Stage;
38
39 public class BusyAlert extends Application {
40
41         long timeout = 60 * 5;
42
43         ScheduledExecutorService queue = Executors.newSingleThreadScheduledExecutor((r) -> {
44                 Thread t = new Thread(r);
45                 t.setDaemon(true);
46                 return t;
47         });
48
49         @Override
50         public void start(Stage primaryStage) throws Exception {
51                 Stage stage = new Stage();
52
53                 Label time = new Label();
54                 Label text = new Label("BREAK NOW!");
55                 VBox box = new VBox(text, time);
56
57                 box.setAlignment(Pos.CENTER);
58                 box.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
59
60                 VBox.setMargin(time, new Insets(20));
61                 VBox.setMargin(text, new Insets(20));
62
63                 time.setText(String.format("%3d:%02d", timeout / 60, timeout % 60));
64                 time.setFont(Font.font("monspaced", 64));
65
66                 text.setFont(Font.font("monspaced", 96));
67
68                 Instant start = Instant.now();
69
70                 Scene scene = new Scene(box);
71
72                 stage.setFullScreen(true);
73                 stage.setFullScreenExitHint("");
74                 stage.setScene(scene);
75
76                 stage.show();
77
78                 queueForever(() -> {
79                         Duration delta = Duration.between(start, Instant.now());
80                         long waited = delta.get(ChronoUnit.SECONDS);
81                         long left = timeout - waited;
82
83                         time.setText(String.format("%3d:%02d", left / 60, left % 60));
84                 });
85
86                 queue.schedule(() -> Platform.runLater(stage::close),
87                         timeout, TimeUnit.SECONDS);
88         }
89
90         void queueForever(Runnable r) {
91                 queue.schedule(() -> {
92                         Platform.runLater(r);
93                         queueForever(r);
94                 },
95                         1, TimeUnit.SECONDS
96                 );
97         }
98
99         void update() {
100         }
101
102         public static void main(String[] args) {
103                 launch(args);
104         }
105 }