|
| 1 | +// Copyright (C) 2022 STMicroelectronics and others. |
| 2 | +// |
| 3 | +// This program and the accompanying materials are made available under the |
| 4 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 5 | +// http://www.eclipse.org/legal/epl-2.0. |
| 6 | +// |
| 7 | +// This Source Code may also be made available under the following Secondary |
| 8 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 9 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 10 | +// with the GNU Classpath Exception which is available at |
| 11 | +// https://www.gnu.org/software/classpath/license.html. |
| 12 | +// |
| 13 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 14 | +// ***************************************************************************** |
| 15 | + |
| 16 | +import { Disposable, Emitter, Event } from '@theia/core'; |
| 17 | + |
| 18 | +const CHECK_INACTIVITY_LIMIT = 2; |
| 19 | +const CHECK_INACTIVITY_INTERVAL = 10000; |
| 20 | + |
| 21 | +const eventListenerOptions: AddEventListenerOptions = { |
| 22 | + passive: true, |
| 23 | + capture: true |
| 24 | +}; |
| 25 | +export class WindowActivityTracker implements Disposable { |
| 26 | + |
| 27 | + private inactivityTime = 0; // number of times inactivity was checked since last reset |
| 28 | + private readonly inactivityLimit = CHECK_INACTIVITY_LIMIT; // number of inactivity checks done before sending inactive signal |
| 29 | + private readonly checkInactivityInterval = CHECK_INACTIVITY_INTERVAL; // check interval in milliseconds |
| 30 | + private interval: NodeJS.Timeout | undefined; |
| 31 | + |
| 32 | + protected readonly onDidChangeActiveStateEmitter = new Emitter<boolean>(); |
| 33 | + private _activeState: boolean = true; |
| 34 | + |
| 35 | + constructor(readonly win: Window) { |
| 36 | + this.initializeListeners(this.win); |
| 37 | + } |
| 38 | + |
| 39 | + get onDidChangeActiveState(): Event<boolean> { |
| 40 | + return this.onDidChangeActiveStateEmitter.event; |
| 41 | + } |
| 42 | + |
| 43 | + private set activeState(newState: boolean) { |
| 44 | + if (this._activeState !== newState) { |
| 45 | + this._activeState = newState; |
| 46 | + this.onDidChangeActiveStateEmitter.fire(this._activeState); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private initializeListeners(win: Window): void { |
| 51 | + // currently assumes activity based on key/mouse/touch pressed, not on mouse move or scrolling. |
| 52 | + win.addEventListener('mousedown', this.resetInactivity, eventListenerOptions); |
| 53 | + win.addEventListener('keydown', this.resetInactivity, eventListenerOptions); |
| 54 | + win.addEventListener('touchstart', this.resetInactivity, eventListenerOptions); |
| 55 | + } |
| 56 | + |
| 57 | + dispose(): void { |
| 58 | + this.stopTracking(); |
| 59 | + this.win.removeEventListener('mousedown', this.resetInactivity); |
| 60 | + this.win.removeEventListener('keydown', this.resetInactivity); |
| 61 | + this.win.removeEventListener('touchstart', this.resetInactivity); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + // Reset inactivity time |
| 66 | + private resetInactivity = (): void => { |
| 67 | + this.inactivityTime = 0; |
| 68 | + if (!this.interval) { |
| 69 | + // it was not active. Set as active and restart tracking inactivity |
| 70 | + this.activeState = true; |
| 71 | + this.startTracking(); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Check inactivity status |
| 76 | + private checkInactivity = (): void => { |
| 77 | + this.inactivityTime++; |
| 78 | + if (this.inactivityTime >= this.inactivityLimit) { |
| 79 | + this.activeState = false; |
| 80 | + this.stopTracking(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + public startTracking(): void { |
| 85 | + this.stopTracking(); |
| 86 | + this.interval = setInterval(this.checkInactivity, this.checkInactivityInterval); |
| 87 | + } |
| 88 | + |
| 89 | + public stopTracking(): void { |
| 90 | + if (this.interval) { |
| 91 | + clearInterval(this.interval); |
| 92 | + this.interval = undefined; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments