-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
232 lines (177 loc) · 5.37 KB
/
timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//window
const electron = require("electron");
const remote = electron.remote;
const BrowserWindow = remote.BrowserWindow;
//configration constants
const SPEECH_VOLUME = 0.9;
const MINUTES_BETWEEN_REMINDER = 5;
const SHAKE_FRAME_LENGTH = 50; //how long each frame lasts in shake()
const SHAKE_FRAME_SKIP = 3; //how many frames to skip before toggling flash
//global bars
var timerLength, countDownDate, interval;
//elements
const minutesInput = document.getElementById('minutes');
const taskInput = document.getElementById('taskname');
const start = document.getElementById('start');
const timer = document.getElementById('timer');
const setup = document.getElementById('setup');
const timeleft = document.getElementById('timeleft');
const currenttask = document.getElementById('currenttask');
const backbutton = document.getElementById('back');
//audio
var doneSound = new Audio('done.mp3');
var startSound = new Audio('start.mp3');
//windows
const timerWindow = remote.getCurrentWindow();
const borderWindow = new BrowserWindow({
width: screen.width,
height: screen.height,
x: 0,
y: 0,
webPreferences: {
nodeIntegration: true
},
frame: false,
resizable: false,
alwaysOnTop: true,
hasShadow: false,
parent: timerWindow,
transparent: true,
skipTaskbar: true,
show: false,
});
borderWindow.loadFile('border.htm');
borderWindow.setIgnoreMouseEvents(true);
var PAUSED = false;
function update() {
if (PAUSED) return;
// Get todays date and time
var now = new Date().getTime();
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
timeleft.innerHTML = minutes.toString().padStart(2, '00') + ":" + seconds.toString().padStart(2, '00');
//countdown finished
if (distance < 0) {
//reset timer
clearInterval(interval);
timeleft.innerHTML = "00:00";
shake();
//play sound
doneSound.play();
say(currenttask.innerHTML + ' complete');
//show main screen
start.innerHTML = 'Start';
setup.style.visibility = 'visible';
timer.style.visibility = 'hidden';
borderWindow.hide();
}
//at the end of every minute
else if (seconds == 0) {
shake();
startSound.play();
if (minutes % MINUTES_BETWEEN_REMINDER == 0) {
//say current task
say(currenttask.innerHTML);
shake();
}
}
}
start.addEventListener('click', function () {
// Set the date we're counting down to
countDownDate = new Date().getTime() + minutesInput.value * 60 * 1000;
//set start time
var now = new Date().getTime();
var distance = countDownDate - now;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
timeleft.innerHTML = minutes.toString().padStart(2, '00') + ":" + seconds.toString().padStart(2, '00');
// Update the count down every 1 second
interval = setInterval(update, 1000);
say(start.innerHTML+ 'ing task ' + taskInput.value);
//set task name
currenttask.innerHTML = taskInput.value || 'Work';
//show timer screen
setup.style.visibility = 'hidden';
timer.style.visibility = 'visible';
//show border
borderWindow.show();
//play sound
startSound.play();
});
//pause / resume timer by clicking on time left
timeleft.addEventListener('click', function () {
console.log('toggling pause to ' + !PAUSED);
if (PAUSED) {
let minutesLeft = parseInt(timeleft.innerHTML.split(':')[0]);
let secondsLeft = parseInt(timeleft.innerHTML.split(':')[1]);
countDownDate = new Date().getTime() + (minutesLeft * 60 * 1000) + (secondsLeft * 1000);
timeleft.classList.remove('paused');
say('Resuming task '+ taskInput.value);
}
else {
timeleft.classList.add('paused');
say('Pausing task');
}
PAUSED = !PAUSED;
});
//click x button
backbutton.addEventListener('click', function () {
let minutesLeft = parseInt(timeleft.innerHTML.split(':')[0]);
let secondsLeft = parseInt(timeleft.innerHTML.split(':')[1]);
//set minutes to minutes plus seconds divided by 60, and round to 2 decimal places
minutesInput.value = Math.round((minutesLeft + (secondsLeft/60)) * 100) / 100;
//show start screen
setup.style.visibility = 'visible';
timer.style.visibility = 'hidden';
clearInterval(interval);
start.innerHTML = 'Resume';
say('Pausing task');
});
minutes.addEventListener('change', function () {
if (start.innerHTML=='Resume') {
start.innerHTML='Start';
}
});
function say(text) {
var speech = new SpeechSynthesisUtterance(text);
speech.volume = SPEECH_VOLUME;
speechSynthesis.speak(speech);
}
function shake() {
var x = timerWindow.getPosition()[0];
var y = timerWindow.getPosition()[1];
var frames = [
{ x: -5, y: -5 },
{ x: -5, y: +5 },
{ x: +5, y: +5 },
{ x: +5, y: -5 },
{ x: -5, y: -5 },
{ x: -5, y: +5 },
{ x: +5, y: +5 },
{ x: +5, y: -5 },
{ x: -5, y: -5 },
{ x: -5, y: +5 },
{ x: +5, y: +5 },
{ x: +5, y: -5 },
{ x: -5, y: -5 },
{ x: -5, y: +5 },
{ x: +5, y: +5 },
{ x: +5, y: -5 },
]
//schedule each frame
frames.forEach((frame, i) => {
setTimeout(() => {
//flash frame
if (i % SHAKE_FRAME_SKIP == 0) timer.classList.toggle('flash');
//move window
timerWindow.setPosition(x + frame.x, y + frame.y);
}, SHAKE_FRAME_LENGTH * i);
});
//reset at end
setTimeout(() => {
timer.classList.remove('flash');
timerWindow.setPosition(x, y);
}, SHAKE_FRAME_LENGTH * frames.length);
}