1
1
package com .JayPi4c .RobbiSimulator .controller ;
2
2
3
- import javafx .concurrent . Service ;
4
- import javafx .concurrent . Task ;
3
+ import javafx .animation . KeyFrame ;
4
+ import javafx .animation . Timeline ;
5
5
import javafx .scene .Node ;
6
6
import javafx .scene .Parent ;
7
+ import javafx .util .Duration ;
7
8
import lombok .extern .slf4j .Slf4j ;
8
9
import org .controlsfx .control .NotificationPane ;
9
10
10
- import java .util .concurrent .*;
11
-
12
11
import static com .JayPi4c .RobbiSimulator .utils .I18nUtils .i18n ;
13
12
14
13
@ Slf4j
15
14
public class NotificationController {
16
15
17
16
private final NotificationPane notificationPane ;
18
- private final ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor (r -> {
19
- Thread thread = Executors .defaultThreadFactory ().newThread (r );
20
- thread .setDaemon (true );
21
- return thread ;
22
- });
23
- private Future <?> schedulerFuture ;
17
+ private final Timeline hideTimeline = new Timeline ();
24
18
25
19
public NotificationController (Node node ) {
26
20
notificationPane = new NotificationPane (node );
27
21
notificationPane .setShowFromTop (false );
28
22
notificationPane .getStyleClass ().add (NotificationPane .STYLE_CLASS_DARK );
23
+ hideTimeline .setCycleCount (1 );
29
24
}
30
25
31
26
public Parent getScene () {
@@ -41,12 +36,14 @@ public Parent getScene() {
41
36
* @param args the arguments for the message
42
37
*/
43
38
public void showMessage (int timeout , String key , Object ... args ) {
44
- if (schedulerFuture != null && !schedulerFuture .isDone ()) {
45
- schedulerFuture .cancel (true );
46
- }
47
- logger .info ("Showing snackbar-message: {}; {}" , key , i18n (key , args ));
39
+ hideTimeline .stop ();
48
40
notificationPane .show (i18n (key , args ));
49
- schedulerFuture = scheduler .schedule (notificationPane ::hide , timeout , TimeUnit .MILLISECONDS );
41
+ KeyFrame kf = new KeyFrame (Duration .millis (timeout ), e -> {
42
+ if (notificationPane .isShowing ())
43
+ notificationPane .hide ();
44
+ });
45
+ hideTimeline .getKeyFrames ().setAll (kf );
46
+ hideTimeline .play ();
50
47
}
51
48
52
49
}
0 commit comments