2015-10-26 14:54:54 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2019-2024 New Vector Ltd.
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-10-26 14:54:54 +01:00
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2015-10-26 14:54:54 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
|
|
|
import Timer from "./utils/Timer";
|
2015-10-26 14:54:54 +01:00
|
|
|
|
2019-03-12 10:40:17 +01:00
|
|
|
// important these are larger than the timeouts of timers
|
|
|
|
// used with UserActivity.timeWhileActive*,
|
|
|
|
// such as READ_MARKER_INVIEW_THRESHOLD_MS (timeWhileActiveRecently),
|
|
|
|
// READ_MARKER_OUTOFVIEW_THRESHOLD_MS (timeWhileActiveRecently),
|
|
|
|
// READ_RECEIPT_INTERVAL_MS (timeWhileActiveNow) in TimelinePanel
|
2019-03-08 18:41:04 +01:00
|
|
|
|
2019-03-12 10:37:00 +01:00
|
|
|
// 'Under a few seconds'. Must be less than 'RECENTLY_ACTIVE_THRESHOLD_MS'
|
2019-03-08 13:46:38 +01:00
|
|
|
const CURRENTLY_ACTIVE_THRESHOLD_MS = 700;
|
2019-03-08 18:41:04 +01:00
|
|
|
|
|
|
|
// 'Under a few minutes'.
|
2019-03-12 10:37:00 +01:00
|
|
|
const RECENTLY_ACTIVE_THRESHOLD_MS = 2 * 60 * 1000;
|
2015-10-26 14:54:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class watches for user activity (moving the mouse or pressing a key)
|
2018-12-11 15:45:11 +01:00
|
|
|
* and starts/stops attached timers while the user is active.
|
2019-03-08 13:46:38 +01:00
|
|
|
*
|
2019-03-11 12:38:54 +01:00
|
|
|
* There are two classes of 'active': 'active now' and 'active recently'
|
|
|
|
* see doc on the userActive* functions for what these mean.
|
2015-10-26 14:54:54 +01:00
|
|
|
*/
|
2019-03-08 13:46:38 +01:00
|
|
|
export default class UserActivity {
|
2020-10-13 18:38:49 +02:00
|
|
|
private readonly activeNowTimeout: Timer;
|
|
|
|
private readonly activeRecentlyTimeout: Timer;
|
|
|
|
private attachedActiveNowTimers: Timer[] = [];
|
|
|
|
private attachedActiveRecentlyTimers: Timer[] = [];
|
|
|
|
private lastScreenX = 0;
|
|
|
|
private lastScreenY = 0;
|
|
|
|
|
2024-01-02 19:56:39 +01:00
|
|
|
public constructor(
|
|
|
|
private readonly window: Window,
|
|
|
|
private readonly document: Document,
|
|
|
|
) {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.activeNowTimeout = new Timer(CURRENTLY_ACTIVE_THRESHOLD_MS);
|
|
|
|
this.activeRecentlyTimeout = new Timer(RECENTLY_ACTIVE_THRESHOLD_MS);
|
2018-12-11 15:45:11 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public static sharedInstance(): UserActivity {
|
2020-10-13 19:48:02 +02:00
|
|
|
if (window.mxUserActivity === undefined) {
|
|
|
|
window.mxUserActivity = new UserActivity(window, document);
|
2019-03-08 13:46:38 +01:00
|
|
|
}
|
2020-10-13 19:48:02 +02:00
|
|
|
return window.mxUserActivity;
|
2019-03-08 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 15:45:11 +01:00
|
|
|
/**
|
2019-03-12 10:37:00 +01:00
|
|
|
* Runs the given timer while the user is 'active now', aborting when the user is no longer
|
2019-03-11 12:38:54 +01:00
|
|
|
* considered currently active.
|
|
|
|
* See userActiveNow() for what it means for a user to be 'active'.
|
2018-12-11 15:45:11 +01:00
|
|
|
* Can be called multiple times with the same already running timer, which is a NO-OP.
|
|
|
|
* Can be called before the user becomes active, in which case it is only started
|
|
|
|
* later on when the user does become active.
|
2019-01-08 12:10:42 +01:00
|
|
|
* @param {Timer} timer the timer to use
|
2018-12-11 15:45:11 +01:00
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public timeWhileActiveNow(timer: Timer): void {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.timeWhile(timer, this.attachedActiveNowTimers);
|
2019-03-11 12:38:54 +01:00
|
|
|
if (this.userActiveNow()) {
|
2019-03-08 13:46:38 +01:00
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-11 12:38:54 +01:00
|
|
|
* Runs the given timer while the user is 'active' now or recently,
|
|
|
|
* aborting when the user becomes inactive.
|
|
|
|
* See userActiveRecently() for what it means for a user to be 'active recently'.
|
2019-03-08 13:46:38 +01:00
|
|
|
* Can be called multiple times with the same already running timer, which is a NO-OP.
|
|
|
|
* Can be called before the user becomes active, in which case it is only started
|
|
|
|
* later on when the user does become active.
|
|
|
|
* @param {Timer} timer the timer to use
|
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public timeWhileActiveRecently(timer: Timer): void {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.timeWhile(timer, this.attachedActiveRecentlyTimers);
|
2019-03-11 12:38:54 +01:00
|
|
|
if (this.userActiveRecently()) {
|
2019-03-08 13:46:38 +01:00
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private timeWhile(timer: Timer, attachedTimers: Timer[]): void {
|
2018-12-11 15:45:11 +01:00
|
|
|
// important this happens first
|
2019-03-08 13:46:38 +01:00
|
|
|
const index = attachedTimers.indexOf(timer);
|
2018-12-11 15:45:11 +01:00
|
|
|
if (index === -1) {
|
2019-03-08 13:46:38 +01:00
|
|
|
attachedTimers.push(timer);
|
2018-12-11 15:45:11 +01:00
|
|
|
// remove when done or aborted
|
2022-12-12 12:24:14 +01:00
|
|
|
timer
|
|
|
|
.finished()
|
|
|
|
.finally(() => {
|
|
|
|
const index = attachedTimers.indexOf(timer);
|
|
|
|
if (index !== -1) {
|
|
|
|
// should never be -1
|
|
|
|
attachedTimers.splice(index, 1);
|
|
|
|
}
|
|
|
|
// as we fork the promise here,
|
|
|
|
// avoid unhandled rejection warnings
|
|
|
|
})
|
|
|
|
.catch((err) => {});
|
2018-12-11 15:45:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:54:54 +01:00
|
|
|
/**
|
|
|
|
* Start listening to user activity
|
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public start(): void {
|
2022-12-12 12:24:14 +01:00
|
|
|
this.document.addEventListener("mousedown", this.onUserActivity);
|
|
|
|
this.document.addEventListener("mousemove", this.onUserActivity);
|
|
|
|
this.document.addEventListener("keydown", this.onUserActivity);
|
2020-10-13 18:38:49 +02:00
|
|
|
this.document.addEventListener("visibilitychange", this.onPageVisibilityChanged);
|
|
|
|
this.window.addEventListener("blur", this.onWindowBlurred);
|
|
|
|
this.window.addEventListener("focus", this.onUserActivity);
|
2016-01-08 23:19:31 +01:00
|
|
|
// can't use document.scroll here because that's only the document
|
|
|
|
// itself being scrolled. Need to use addEventListener's useCapture.
|
2016-01-09 01:06:54 +01:00
|
|
|
// also this needs to be the wheel event, not scroll, as scroll is
|
|
|
|
// fired when the view scrolls down for a new message.
|
2022-12-12 12:24:14 +01:00
|
|
|
this.window.addEventListener("wheel", this.onUserActivity, {
|
2020-10-13 18:38:49 +02:00
|
|
|
passive: true,
|
|
|
|
capture: true,
|
2019-03-08 18:41:04 +01:00
|
|
|
});
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public stop(): void {
|
2022-12-12 12:24:14 +01:00
|
|
|
this.document.removeEventListener("mousedown", this.onUserActivity);
|
|
|
|
this.document.removeEventListener("mousemove", this.onUserActivity);
|
|
|
|
this.document.removeEventListener("keydown", this.onUserActivity);
|
|
|
|
this.window.removeEventListener("wheel", this.onUserActivity, {
|
2020-10-13 18:38:49 +02:00
|
|
|
capture: true,
|
2019-03-08 22:51:14 +01:00
|
|
|
});
|
2020-10-13 18:38:49 +02:00
|
|
|
this.document.removeEventListener("visibilitychange", this.onPageVisibilityChanged);
|
|
|
|
this.window.removeEventListener("blur", this.onWindowBlurred);
|
|
|
|
this.window.removeEventListener("focus", this.onUserActivity);
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 17:01:31 +01:00
|
|
|
/**
|
2019-03-08 13:46:38 +01:00
|
|
|
* Return true if the user is currently 'active'
|
|
|
|
* A user is 'active' while they are interacting with the app and for a very short (<1s)
|
|
|
|
* time after that. This is intended to give a strong indication that the app has the
|
|
|
|
* user's attention at any given moment.
|
|
|
|
* @returns {boolean} true if user is currently 'active'
|
2016-01-14 17:01:31 +01:00
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public userActiveNow(): boolean {
|
2020-10-13 18:38:49 +02:00
|
|
|
return this.activeNowTimeout.isRunning();
|
2019-03-08 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-11 12:38:54 +01:00
|
|
|
* Return true if the user is currently active or has been recently
|
|
|
|
* A user is 'active recently' for a longer period of time (~2 mins) after
|
|
|
|
* they have been 'active' and while the app still has the focus. This is
|
|
|
|
* intended to indicate when the app may still have the user's attention
|
|
|
|
* (or they may have gone to make tea and left the window focused).
|
|
|
|
* @returns {boolean} true if user has been active recently
|
2019-03-08 13:46:38 +01:00
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public userActiveRecently(): boolean {
|
2020-10-13 18:38:49 +02:00
|
|
|
return this.activeRecentlyTimeout.isRunning();
|
2018-12-11 15:45:11 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
private onPageVisibilityChanged = (e: Event): void => {
|
2020-10-13 18:38:49 +02:00
|
|
|
if (this.document.visibilityState === "hidden") {
|
|
|
|
this.activeNowTimeout.abort();
|
|
|
|
this.activeRecentlyTimeout.abort();
|
2018-12-11 15:45:11 +01:00
|
|
|
} else {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.onUserActivity(e);
|
2018-12-11 15:45:11 +01:00
|
|
|
}
|
2020-10-13 18:38:49 +02:00
|
|
|
};
|
2018-12-11 15:45:11 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private onWindowBlurred = (): void => {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.activeNowTimeout.abort();
|
|
|
|
this.activeRecentlyTimeout.abort();
|
|
|
|
};
|
2016-01-14 17:01:31 +01:00
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
// XXX: exported for tests
|
|
|
|
public onUserActivity = (event: Event): void => {
|
2019-03-08 13:46:38 +01:00
|
|
|
// ignore anything if the window isn't focused
|
2020-10-13 18:38:49 +02:00
|
|
|
if (!this.document.hasFocus()) return;
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2023-02-10 19:11:57 +01:00
|
|
|
if (event.type === "mousemove" && this.isMouseEvent(event)) {
|
2017-07-01 15:50:22 +02:00
|
|
|
if (event.screenX === this.lastScreenX && event.screenY === this.lastScreenY) {
|
2015-11-29 16:39:10 +01:00
|
|
|
// mouse hasn't actually moved
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.lastScreenX = event.screenX;
|
|
|
|
this.lastScreenY = event.screenY;
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
dis.dispatch({ action: "user_activity" });
|
2020-10-13 18:38:49 +02:00
|
|
|
if (!this.activeNowTimeout.isRunning()) {
|
|
|
|
this.activeNowTimeout.start();
|
2022-12-12 12:24:14 +01:00
|
|
|
dis.dispatch({ action: "user_activity_start" });
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2020-10-13 18:38:49 +02:00
|
|
|
UserActivity.runTimersUntilTimeout(this.attachedActiveNowTimers, this.activeNowTimeout);
|
2016-01-14 17:01:31 +01:00
|
|
|
} else {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.activeNowTimeout.restart();
|
2015-10-26 14:54:54 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:38:49 +02:00
|
|
|
if (!this.activeRecentlyTimeout.isRunning()) {
|
|
|
|
this.activeRecentlyTimeout.start();
|
2019-03-08 13:46:38 +01:00
|
|
|
|
2020-10-13 18:38:49 +02:00
|
|
|
UserActivity.runTimersUntilTimeout(this.attachedActiveRecentlyTimers, this.activeRecentlyTimeout);
|
2019-03-08 13:46:38 +01:00
|
|
|
} else {
|
2020-10-13 18:38:49 +02:00
|
|
|
this.activeRecentlyTimeout.restart();
|
2019-03-08 13:46:38 +01:00
|
|
|
}
|
2020-10-13 18:38:49 +02:00
|
|
|
};
|
2018-12-11 15:45:11 +01:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
private static async runTimersUntilTimeout(attachedTimers: Timer[], timeout: Timer): Promise<void> {
|
2019-03-08 13:46:38 +01:00
|
|
|
attachedTimers.forEach((t) => t.start());
|
|
|
|
try {
|
|
|
|
await timeout.finished();
|
2024-10-16 17:43:07 +02:00
|
|
|
} catch {
|
2022-12-12 12:24:14 +01:00
|
|
|
/* aborted */
|
|
|
|
}
|
2019-03-08 13:46:38 +01:00
|
|
|
attachedTimers.forEach((t) => t.abort());
|
|
|
|
}
|
2023-02-10 19:11:57 +01:00
|
|
|
|
|
|
|
private isMouseEvent(event: Event): event is MouseEvent {
|
|
|
|
return event.type.startsWith("mouse");
|
|
|
|
}
|
2019-03-08 13:46:38 +01:00
|
|
|
}
|