1+ import { ScreensaverUI } from "./uis/screensaver" ;
2+
13export class Idler {
24 private _idle : boolean = false ;
35 private _lastActivity : number = Date . now ( ) ;
46 private _idleAfter : number = 300000 ; // 5 minutes
5- private _takeActionAfter : number = 2520000 ; // 42 minutes
67 private _isLockScreen : boolean ;
8+ private _screensaverUI : ScreensaverUI ;
79
810 public constructor ( isLockScreen : boolean = false ) {
911 this . _isLockScreen = isLockScreen ;
12+ this . _screensaverUI = new ScreensaverUI ( isLockScreen ) ;
1013
1114 // Listen for keyboard and mouse events
12- window . addEventListener ( "keydown" , this . _unidle . bind ( this ) ) ;
13- window . addEventListener ( "mousemove" , this . _unidle . bind ( this ) ) ;
14- window . addEventListener ( "mousedown" , this . _unidle . bind ( this ) ) ;
15+ window . addEventListener ( "keydown" , this . _stopIdling . bind ( this ) ) ;
16+ window . addEventListener ( "mousemove" , this . _stopIdling . bind ( this ) ) ;
17+ window . addEventListener ( "mousedown" , this . _stopIdling . bind ( this ) ) ;
1518
1619 // Check for idle at a regular interval
1720 setInterval ( this . _checkIdle . bind ( this ) , 1000 ) ;
@@ -25,38 +28,41 @@ export class Idler {
2528 return this . _idle ;
2629 }
2730
28- private _unidle ( ) : void {
29- this . _lastActivity = Date . now ( ) ;
30- this . _idle = false ;
31- }
32-
33- private _action ( ) : void {
34- // TODO: start screensaver?
35- // Warning: this function is called from a setInterval, so it should return if the action is already running
31+ /**
32+ * Start idling and show the screensaver.
33+ */
34+ private _startIdling ( ) : void {
35+ this . _idle = true ;
36+ this . _screensaverUI . start ( ) ;
3637 }
3738
38- private _checkIfActionNeeded ( ) : boolean {
39+ /**
40+ * Stop the screensaver and reset the idle timer.
41+ * @param ev The event that triggered this function.
42+ */
43+ private _stopIdling ( ev : Event | null = null ) : void {
44+ this . _lastActivity = Date . now ( ) ;
3945 if ( this . _idle ) {
40- // Check if we should take action
41- if ( Date . now ( ) - this . _lastActivity >= this . _takeActionAfter ) {
42- this . _action ( ) ;
43- return true ;
46+ if ( ev ) {
47+ ev . preventDefault ( ) ; // Prevent the event from bubbling up and causing unwanted UI interactions
4448 }
49+ this . _screensaverUI . stop ( ) ;
50+ this . _idle = false ;
4551 }
46- return false ;
4752 }
4853
54+ /**
55+ * Check if the computer is idling (e.g. no keyboard or mouse interactions for a certain amount of time).
56+ * @returns True if the computer is idling, false otherwise.
57+ */
4958 private _checkIdle ( ) : boolean {
5059 if ( this . _idle ) {
51- this . _checkIfActionNeeded ( ) ;
5260 return true ;
5361 }
5462
5563 // Check if we're idle
5664 if ( Date . now ( ) - this . _lastActivity >= this . _idleAfter ) {
57- this . _idle = true ;
58- console . log ( "Now idling..." ) ;
59- this . _checkIfActionNeeded ( ) ; // Needed if idleAfter and takeActionAfter share the same value
65+ this . _startIdling ( ) ;
6066 return true ;
6167 }
6268 return false ;
0 commit comments