33
44import static com .esotericsoftware .minlog .Log .*;
55
6- import java .awt .EventQueue ;
76import java .io .BufferedInputStream ;
87import java .io .FileInputStream ;
98import java .io .IOException ;
109import java .io .InputStream ;
10+ import java .util .Timer ;
1111import java .util .TimerTask ;
1212
1313import javax .sound .sampled .AudioInputStream ;
1616import javax .sound .sampled .FloatControl ;
1717
1818import com .esotericsoftware .clippy .Win .LASTINPUTINFO ;
19+ import com .esotericsoftware .clippy .util .EventQueueRepeat ;
1920import com .esotericsoftware .clippy .util .Util ;
2021
2122public class BreakWarning {
@@ -27,9 +28,11 @@ public class BreakWarning {
2728 volatile ProgressBar progressBar ;
2829 Clip startClip , flashClip , endClip ;
2930 volatile boolean disabled ;
31+ Timer timer ;
3032
3133 public BreakWarning () {
3234 if (clippy .config .breakWarningMinutes <= 0 ) return ;
35+ timer = new Timer ("BreakWarning" , true );
3336
3437 if (clippy .config .breakStartSound != null ) startClip = loadClip (clippy .config .breakStartSound );
3538 if (clippy .config .breakFlashSound != null ) flashClip = loadClip (clippy .config .breakFlashSound );
@@ -59,76 +62,73 @@ public void run () {
5962 }
6063
6164 void showBreakDialog () {
62- clippy .tray .updateTooltip ("Clippy - Take a break!" );
6365 if (INFO ) info ("Break needed." );
6466
65- EventQueue .invokeLater (new Runnable () {
66- public void run () {
67+ new EventQueueRepeat () {
68+ float indeterminateMillis = 5000 ;
69+ float volume = 0.05f ;
70+
71+ protected void start () {
6772 progressBar = new ProgressBar ("" );
6873 progressBar .clickToDispose = false ;
6974 progressBar .red ("" );
75+ clippy .tray .updateTooltip ("Clippy - Take a break!" );
7076 if (clippy .config .breakReminderMinutes > 0 )
7177 clippy .tray .balloon ("Clippy" , "Take a break!" , 30000 );
7278 else {
7379 playClip (startClip , 1 );
7480 progressBar .setVisible (true );
7581 }
76- new Thread ("BreakWarning Dialog" ) {
77- {
78- setDaemon (true );
79- }
82+ }
83+
84+ protected boolean repeat () {
85+ long inactiveMillis = getInactiveMillis (false );
86+ long inactiveMinutes = inactiveMillis / 1000 / 60 ;
87+ if (inactiveMinutes >= clippy .config .breakResetMinutes ) return true ;
8088
81- public void run () {
82- float indeterminateMillis = 5000 ;
83- float volume = 0.05f ;
84- while (true ) {
85- long inactiveMillis = getInactiveMillis (false );
86- long inactiveMinutes = inactiveMillis / 1000 / 60 ;
87- if (inactiveMinutes >= clippy .config .breakResetMinutes ) break ;
88-
89- float percent = 1 - inactiveMillis / (float )(clippy .config .breakResetMinutes * 60 * 1000 );
90- String message ;
91- if (percent < 0.75f ) {
92- indeterminateMillis = 0 ;
93- message = "Break: " + formatTimeSeconds (clippy .config .breakResetMinutes * 60 * 1000 - inactiveMillis );
94- progressBar .setVisible (true );
95- } else
96- message = "Active: " + formatTimeMinutes (System .currentTimeMillis () - lastBreakTime );
97- progressBar .progressBar .setString (message );
98-
99- indeterminateMillis -= 100 ;
100- if (indeterminateMillis > 0 ) {
101- if (!progressBar .progressBar .isIndeterminate ()) {
102- if (!progressBar .isVisible ()) {
103- // First time after balloon.
104- playClip (startClip , 1 );
105- progressBar .setVisible (true );
106- } else {
107- // Every breakReminderMinutes.
108- playClip (flashClip , volume );
109- volume += 0.1f ;
110- }
111- progressBar .progressBar .setIndeterminate (true );
112- if (INFO ) info ("Break reminder." );
113- }
114- } else {
115- if (clippy .config .breakReminderMinutes > 0 && percent >= 0.99f
116- && indeterminateMillis < -clippy .config .breakReminderMinutes * 60 * 1000 ) indeterminateMillis = 5000 ;
117- progressBar .setProgress (percent ); // Sets indeterminate to false.
118- progressBar .toFront ();
119- progressBar .setAlwaysOnTop (true );
120- }
121- Util .sleep (100 );
89+ float percent = 1 - inactiveMillis / (float )(clippy .config .breakResetMinutes * 60 * 1000 );
90+ String message ;
91+ if (percent < 0.75f ) {
92+ indeterminateMillis = 0 ;
93+ message = "Break: " + formatTimeSeconds (clippy .config .breakResetMinutes * 60 * 1000 - inactiveMillis );
94+ progressBar .setVisible (true );
95+ } else
96+ message = "Active: " + formatTimeMinutes (System .currentTimeMillis () - lastBreakTime );
97+ progressBar .progressBar .setString (message );
98+
99+ indeterminateMillis -= 100 ;
100+ if (indeterminateMillis > 0 ) {
101+ if (!progressBar .progressBar .isIndeterminate ()) {
102+ if (!progressBar .isVisible ()) {
103+ // First time after balloon.
104+ playClip (startClip , 1 );
105+ progressBar .setVisible (true );
106+ } else {
107+ // Every breakReminderMinutes.
108+ playClip (flashClip , volume );
109+ volume += 0.1f ;
122110 }
123- lastBreakTime = System .currentTimeMillis ();
124- playClip (endClip , 1 );
125- progressBar .done ("Break complete!" , 2000 );
126- progressBar = null ;
127- if (INFO ) info ("Break complete!" );
111+ progressBar .progressBar .setIndeterminate (true );
112+ if (INFO ) info ("Break reminder." );
128113 }
129- }.start ();
114+ } else {
115+ if (clippy .config .breakReminderMinutes > 0 && percent >= 0.99f
116+ && indeterminateMillis < -clippy .config .breakReminderMinutes * 60 * 1000 ) indeterminateMillis = 5000 ;
117+ progressBar .setProgress (percent ); // Sets indeterminate to false.
118+ progressBar .toFront ();
119+ progressBar .setAlwaysOnTop (true );
120+ }
121+ return false ;
122+ }
123+
124+ protected void end () {
125+ lastBreakTime = System .currentTimeMillis ();
126+ playClip (endClip , 1 );
127+ progressBar .done ("Break complete!" , 2000 );
128+ progressBar = null ;
129+ if (INFO ) info ("Break complete!" );
130130 }
131- });
131+ }. run ( 100 );
132132 }
133133
134134 void playClip (Clip clip , float volume ) {
0 commit comments