timer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (C) 2013 Borsato Ivano
  3. The JavaScript code in this page is free software: you can
  4. redistribute it and/or modify it under the terms of the GNU
  5. General Public License (GNU GPL) as published by the Free Software
  6. Foundation, either version 3 of the License, or (at your option)
  7. any later version. The code is distributed WITHOUT ANY WARRANTY;
  8. without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  10. */
  11. 'use strict';
  12. import GObject from 'gi://GObject';
  13. import GLib from 'gi://GLib';
  14. import * as Lib from './convenience.js';
  15. /*
  16. DELAY TIMER
  17. */
  18. let DelaySec = 0;
  19. let timerDelayId = null;
  20. let CallbackFuncDelay = null;
  21. let ElapsedSec;
  22. /**
  23. * @type {TimerDelay}
  24. */
  25. export const TimerDelay = GObject.registerClass({
  26. GTypeName: 'EasyScreenCast_TimerDelay',
  27. }, class TimerDelay extends GObject.Object {
  28. /**
  29. * Create a new timer
  30. *
  31. * @param {number} delay delay in seconds
  32. * @param {Function} callback callback function that is called after delay seconds (without arguments)
  33. * @param {*} scope scope for the callback
  34. */
  35. constructor(delay, callback, scope) {
  36. super();
  37. if (isNaN(delay)) {
  38. Lib.TalkativeLog(`-%-delay is NOT a number :${delay}`);
  39. } else {
  40. Lib.TalkativeLog(`-%-init TimerDelay called - sec : ${delay}`);
  41. DelaySec = delay;
  42. ElapsedSec = 1;
  43. this.setCallback(callback);
  44. this.Scope = scope;
  45. }
  46. }
  47. /**
  48. * Set the callback-function
  49. *
  50. * @param {Function} callback callback function that is called after delay seconds (without arguments)
  51. */
  52. setCallback(callback) {
  53. Lib.TalkativeLog('-%-setcallback TimerDelay called');
  54. if (
  55. callback === undefined ||
  56. callback === null ||
  57. typeof callback !== 'function'
  58. )
  59. throw TypeError("'callback' needs to be a function.");
  60. CallbackFuncDelay = callback;
  61. }
  62. /**
  63. * Set the delay time
  64. *
  65. * @param {number} delay delay in seconds
  66. */
  67. setDelay(delay) {
  68. Lib.TalkativeLog(`-%-setdelay TimerDelay called: ${delay}`);
  69. DelaySec = delay;
  70. }
  71. /**
  72. * Start or restart a new timer
  73. */
  74. begin() {
  75. Lib.TalkativeLog('-%-start TimerDelay called');
  76. this.stop();
  77. timerDelayId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () =>
  78. this._callbackInternal()
  79. );
  80. }
  81. /**
  82. * Stop the current timer
  83. */
  84. stop() {
  85. Lib.TalkativeLog('-%-stop TimerDelay called');
  86. if (timerDelayId !== null) {
  87. if (GLib.source_remove(timerDelayId)) {
  88. timerDelayId = null;
  89. ElapsedSec = 1;
  90. }
  91. }
  92. }
  93. /**
  94. * A convenient way to restart the timer.
  95. */
  96. restart() {
  97. this.stop();
  98. this.begin();
  99. }
  100. /**
  101. * The internal callback-function.
  102. *
  103. * @private
  104. * @returns {boolean}
  105. */
  106. _callbackInternal() {
  107. Lib.TalkativeLog(`-%-internalFunction TimerDelay called | Sec = ${ElapsedSec} Sec delay = ${DelaySec}`);
  108. if (ElapsedSec >= DelaySec) {
  109. CallbackFuncDelay.apply(this.Scope, []);
  110. ElapsedSec = 1;
  111. return false;
  112. } else {
  113. ElapsedSec++;
  114. return true;
  115. }
  116. }
  117. });
  118. /*
  119. COUNTING TIMER
  120. */
  121. let timerCountingId = null;
  122. let CallbackFuncCounting = null;
  123. let isRunning = false;
  124. let secpassed = 0;
  125. /**
  126. * @type {TimerCounting}
  127. */
  128. export const TimerCounting = GObject.registerClass({
  129. GTypeName: 'EasyScreenCast_TimerCounting',
  130. }, class TimerCounting extends GObject.Object {
  131. /**
  132. * Callback for the counting timer.
  133. *
  134. * @callback TimerCounting~callback
  135. * @param {number} count seconds passed
  136. * @param {boolean} alertEnd whether the timer is ending
  137. */
  138. /**
  139. * Create a new timer
  140. *
  141. * @param {TimerCounting~callback} callback callback function that is called every second
  142. * @param {EasyScreenCast_Indicator} scope scope for the callback function. This is also used to updateTimeLabel.
  143. */
  144. constructor(callback, scope) {
  145. super();
  146. Lib.TalkativeLog('-%-init TimerCounting called');
  147. this.setCallback(callback);
  148. secpassed = 0;
  149. this.Scope = scope;
  150. }
  151. /**
  152. * Set the callback-function
  153. *
  154. * @param {TimerCounting~callback} callback callback function that is called every second
  155. */
  156. setCallback(callback) {
  157. Lib.TalkativeLog('-%-setcallback TimerCounting called');
  158. if (
  159. callback === undefined ||
  160. callback === null ||
  161. typeof callback !== 'function'
  162. )
  163. throw TypeError("'callback' needs to be a function.");
  164. CallbackFuncCounting = callback;
  165. }
  166. /**
  167. * Start or restart a new timer
  168. */
  169. begin() {
  170. Lib.TalkativeLog('-%-start TimerCounting called');
  171. if (isRunning)
  172. this.stop();
  173. isRunning = true;
  174. timerCountingId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () =>
  175. this._callbackInternal()
  176. );
  177. }
  178. /**
  179. * Stop the current timer
  180. */
  181. stop() {
  182. Lib.TalkativeLog('-%-stop TimerCounting called');
  183. isRunning = false;
  184. if (timerCountingId !== null && GLib.source_remove(timerCountingId))
  185. timerCountingId = null;
  186. }
  187. /**
  188. * A convenient way to stop timer
  189. */
  190. halt() {
  191. isRunning = false;
  192. }
  193. /**
  194. * The internal callback-function. Calls a function that handles
  195. * the desktop notifications and one that sets the time label next
  196. * to the icon.
  197. *
  198. * @private
  199. * @returns {boolean}
  200. */
  201. _callbackInternal() {
  202. if (isRunning === false) {
  203. Lib.TalkativeLog('-%-finish TimerCounting ');
  204. CallbackFuncCounting.apply(this.Scope, [secpassed, true]);
  205. secpassed = 0;
  206. this.stop();
  207. this.Scope.updateTimeLabel('');
  208. return false;
  209. } else {
  210. secpassed++;
  211. Lib.TalkativeLog(`-%-continued TimerCounting | sec: ${secpassed}`);
  212. CallbackFuncCounting.apply(this.Scope, [secpassed, false]);
  213. this.Scope.updateTimeLabel(secpassed);
  214. return true;
  215. }
  216. }
  217. });