-
Notifications
You must be signed in to change notification settings - Fork 80
/
timer.js
257 lines (217 loc) · 6.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
Copyright (C) 2013 Borsato Ivano
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*/
'use strict';
import GObject from 'gi://GObject';
import GLib from 'gi://GLib';
import * as Lib from './convenience.js';
/*
DELAY TIMER
*/
let DelaySec = 0;
let timerDelayId = null;
let CallbackFuncDelay = null;
let ElapsedSec;
/**
* @type {TimerDelay}
*/
export const TimerDelay = GObject.registerClass({
GTypeName: 'EasyScreenCast_TimerDelay',
}, class TimerDelay extends GObject.Object {
/**
* Create a new timer
*
* @param {number} delay delay in seconds
* @param {Function} callback callback function that is called after delay seconds (without arguments)
* @param {*} scope scope for the callback
*/
constructor(delay, callback, scope) {
super();
if (isNaN(delay)) {
Lib.TalkativeLog(`-%-delay is NOT a number :${delay}`);
} else {
Lib.TalkativeLog(`-%-init TimerDelay called - sec : ${delay}`);
DelaySec = delay;
ElapsedSec = 1;
this.setCallback(callback);
this.Scope = scope;
}
}
/**
* Set the callback-function
*
* @param {Function} callback callback function that is called after delay seconds (without arguments)
*/
setCallback(callback) {
Lib.TalkativeLog('-%-setcallback TimerDelay called');
if (
callback === undefined ||
callback === null ||
typeof callback !== 'function'
)
throw TypeError("'callback' needs to be a function.");
CallbackFuncDelay = callback;
}
/**
* Set the delay time
*
* @param {number} delay delay in seconds
*/
setDelay(delay) {
Lib.TalkativeLog(`-%-setdelay TimerDelay called: ${delay}`);
DelaySec = delay;
}
/**
* Start or restart a new timer
*/
begin() {
Lib.TalkativeLog('-%-start TimerDelay called');
this.stop();
timerDelayId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () =>
this._callbackInternal()
);
}
/**
* Stop the current timer
*/
stop() {
Lib.TalkativeLog('-%-stop TimerDelay called');
if (timerDelayId !== null) {
if (GLib.source_remove(timerDelayId)) {
timerDelayId = null;
ElapsedSec = 1;
}
}
}
/**
* A convenient way to restart the timer.
*/
restart() {
this.stop();
this.begin();
}
/**
* The internal callback-function.
*
* @private
* @returns {boolean}
*/
_callbackInternal() {
Lib.TalkativeLog(`-%-internalFunction TimerDelay called | Sec = ${ElapsedSec} Sec delay = ${DelaySec}`);
if (ElapsedSec >= DelaySec) {
CallbackFuncDelay.apply(this.Scope, []);
ElapsedSec = 1;
return false;
} else {
ElapsedSec++;
return true;
}
}
});
/*
COUNTING TIMER
*/
let timerCountingId = null;
let CallbackFuncCounting = null;
let isRunning = false;
let secpassed = 0;
/**
* @type {TimerCounting}
*/
export const TimerCounting = GObject.registerClass({
GTypeName: 'EasyScreenCast_TimerCounting',
}, class TimerCounting extends GObject.Object {
/**
* Callback for the counting timer.
*
* @callback TimerCounting~callback
* @param {number} count seconds passed
* @param {boolean} alertEnd whether the timer is ending
*/
/**
* Create a new timer
*
* @param {TimerCounting~callback} callback callback function that is called every second
* @param {EasyScreenCast_Indicator} scope scope for the callback function. This is also used to updateTimeLabel.
*/
constructor(callback, scope) {
super();
Lib.TalkativeLog('-%-init TimerCounting called');
this.setCallback(callback);
secpassed = 0;
this.Scope = scope;
}
/**
* Set the callback-function
*
* @param {TimerCounting~callback} callback callback function that is called every second
*/
setCallback(callback) {
Lib.TalkativeLog('-%-setcallback TimerCounting called');
if (
callback === undefined ||
callback === null ||
typeof callback !== 'function'
)
throw TypeError("'callback' needs to be a function.");
CallbackFuncCounting = callback;
}
/**
* Start or restart a new timer
*/
begin() {
Lib.TalkativeLog('-%-start TimerCounting called');
if (isRunning)
this.stop();
isRunning = true;
timerCountingId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () =>
this._callbackInternal()
);
}
/**
* Stop the current timer
*/
stop() {
Lib.TalkativeLog('-%-stop TimerCounting called');
isRunning = false;
if (timerCountingId !== null && GLib.source_remove(timerCountingId))
timerCountingId = null;
}
/**
* A convenient way to stop timer
*/
halt() {
isRunning = false;
}
/**
* The internal callback-function. Calls a function that handles
* the desktop notifications and one that sets the time label next
* to the icon.
*
* @private
* @returns {boolean}
*/
_callbackInternal() {
if (isRunning === false) {
Lib.TalkativeLog('-%-finish TimerCounting ');
CallbackFuncCounting.apply(this.Scope, [secpassed, true]);
secpassed = 0;
this.stop();
this.Scope.updateTimeLabel('');
return false;
} else {
secpassed++;
Lib.TalkativeLog(`-%-continued TimerCounting | sec: ${secpassed}`);
CallbackFuncCounting.apply(this.Scope, [secpassed, false]);
this.Scope.updateTimeLabel(secpassed);
return true;
}
}
});