2015-10-26 16:54:54 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-03-08 13:31:30 +03:00
|
|
|
Copyright 2019 New Vector Ltd
|
2015-10-26 16:54:54 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 16:50:22 +03:00
|
|
|
import dis from './dispatcher';
|
2018-12-11 17:45:11 +03:00
|
|
|
import Timer from './utils/Timer';
|
2015-10-26 16:54:54 +03:00
|
|
|
|
2019-03-12 12:40:17 +03: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 20:41:04 +03:00
|
|
|
|
2019-03-12 12:37:00 +03:00
|
|
|
// 'Under a few seconds'. Must be less than 'RECENTLY_ACTIVE_THRESHOLD_MS'
|
2019-03-08 15:46:38 +03:00
|
|
|
const CURRENTLY_ACTIVE_THRESHOLD_MS = 700;
|
2019-03-08 20:41:04 +03:00
|
|
|
|
|
|
|
// 'Under a few minutes'.
|
2019-03-12 12:37:00 +03:00
|
|
|
const RECENTLY_ACTIVE_THRESHOLD_MS = 2 * 60 * 1000;
|
2015-10-26 16:54:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class watches for user activity (moving the mouse or pressing a key)
|
2018-12-11 17:45:11 +03:00
|
|
|
* and starts/stops attached timers while the user is active.
|
2019-03-08 15:46:38 +03:00
|
|
|
*
|
2019-03-11 14:38:54 +03: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 16:54:54 +03:00
|
|
|
*/
|
2019-03-08 15:46:38 +03:00
|
|
|
export default class UserActivity {
|
|
|
|
constructor(windowObj, documentObj) {
|
|
|
|
this._window = windowObj;
|
|
|
|
this._document = documentObj;
|
|
|
|
|
2019-03-12 13:28:47 +03:00
|
|
|
this._attachedActiveNowTimers = [];
|
|
|
|
this._attachedActiveRecentlyTimers = [];
|
2019-03-11 14:38:54 +03:00
|
|
|
this._activeNowTimeout = new Timer(CURRENTLY_ACTIVE_THRESHOLD_MS);
|
2019-03-12 12:37:00 +03:00
|
|
|
this._activeRecentlyTimeout = new Timer(RECENTLY_ACTIVE_THRESHOLD_MS);
|
2018-12-11 17:45:11 +03:00
|
|
|
this._onUserActivity = this._onUserActivity.bind(this);
|
2019-03-08 13:23:18 +03:00
|
|
|
this._onWindowBlurred = this._onWindowBlurred.bind(this);
|
2018-12-11 17:45:11 +03:00
|
|
|
this._onPageVisibilityChanged = this._onPageVisibilityChanged.bind(this);
|
|
|
|
this.lastScreenX = 0;
|
|
|
|
this.lastScreenY = 0;
|
|
|
|
}
|
|
|
|
|
2019-03-08 15:46:38 +03:00
|
|
|
static sharedInstance() {
|
|
|
|
if (global.mxUserActivity === undefined) {
|
|
|
|
global.mxUserActivity = new UserActivity(window, document);
|
|
|
|
}
|
|
|
|
return global.mxUserActivity;
|
|
|
|
}
|
|
|
|
|
2018-12-11 17:45:11 +03:00
|
|
|
/**
|
2019-03-12 12:37:00 +03:00
|
|
|
* Runs the given timer while the user is 'active now', aborting when the user is no longer
|
2019-03-11 14:38:54 +03:00
|
|
|
* considered currently active.
|
|
|
|
* See userActiveNow() for what it means for a user to be 'active'.
|
2018-12-11 17:45:11 +03: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 14:10:42 +03:00
|
|
|
* @param {Timer} timer the timer to use
|
2018-12-11 17:45:11 +03:00
|
|
|
*/
|
2019-03-12 12:37:00 +03:00
|
|
|
timeWhileActiveNow(timer) {
|
2019-03-12 13:28:47 +03:00
|
|
|
this._timeWhile(timer, this._attachedActiveNowTimers);
|
2019-03-11 14:38:54 +03:00
|
|
|
if (this.userActiveNow()) {
|
2019-03-08 15:46:38 +03:00
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-11 14:38:54 +03: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 15:46:38 +03: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
|
|
|
|
*/
|
2019-03-12 12:37:00 +03:00
|
|
|
timeWhileActiveRecently(timer) {
|
2019-03-12 13:28:47 +03:00
|
|
|
this._timeWhile(timer, this._attachedActiveRecentlyTimers);
|
2019-03-11 14:38:54 +03:00
|
|
|
if (this.userActiveRecently()) {
|
2019-03-08 15:46:38 +03:00
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_timeWhile(timer, attachedTimers) {
|
2018-12-11 17:45:11 +03:00
|
|
|
// important this happens first
|
2019-03-08 15:46:38 +03:00
|
|
|
const index = attachedTimers.indexOf(timer);
|
2018-12-11 17:45:11 +03:00
|
|
|
if (index === -1) {
|
2019-03-08 15:46:38 +03:00
|
|
|
attachedTimers.push(timer);
|
2018-12-11 17:45:11 +03:00
|
|
|
// remove when done or aborted
|
|
|
|
timer.finished().finally(() => {
|
2019-03-08 15:46:38 +03:00
|
|
|
const index = attachedTimers.indexOf(timer);
|
2018-12-11 17:45:11 +03:00
|
|
|
if (index !== -1) { // should never be -1
|
2019-03-08 15:46:38 +03:00
|
|
|
attachedTimers.splice(index, 1);
|
2018-12-11 17:45:11 +03:00
|
|
|
}
|
|
|
|
// as we fork the promise here,
|
|
|
|
// avoid unhandled rejection warnings
|
|
|
|
}).catch((err) => {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:54:54 +03:00
|
|
|
/**
|
|
|
|
* Start listening to user activity
|
|
|
|
*/
|
|
|
|
start() {
|
2019-03-08 20:41:04 +03:00
|
|
|
this._document.addEventListener('mousedown', this._onUserActivity);
|
|
|
|
this._document.addEventListener('mousemove', this._onUserActivity);
|
|
|
|
this._document.addEventListener('keydown', this._onUserActivity);
|
2019-03-08 15:46:38 +03:00
|
|
|
this._document.addEventListener("visibilitychange", this._onPageVisibilityChanged);
|
|
|
|
this._window.addEventListener("blur", this._onWindowBlurred);
|
|
|
|
this._window.addEventListener("focus", this._onUserActivity);
|
2016-01-09 01:19:31 +03: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 03:06:54 +03:00
|
|
|
// also this needs to be the wheel event, not scroll, as scroll is
|
|
|
|
// fired when the view scrolls down for a new message.
|
2019-03-08 20:41:04 +03:00
|
|
|
this._window.addEventListener('wheel', this._onUserActivity, {
|
|
|
|
passive: true, capture: true,
|
|
|
|
});
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop tracking user activity
|
|
|
|
*/
|
|
|
|
stop() {
|
2019-03-09 00:51:14 +03: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, {
|
|
|
|
passive: true, capture: true,
|
|
|
|
});
|
2018-12-11 17:45:11 +03:00
|
|
|
|
2019-03-08 15:46:38 +03:00
|
|
|
this._document.removeEventListener("visibilitychange", this._onPageVisibilityChanged);
|
|
|
|
this._window.removeEventListener("blur", this._onWindowBlurred);
|
|
|
|
this._window.removeEventListener("focus", this._onUserActivity);
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
|
|
|
|
2016-01-14 19:01:31 +03:00
|
|
|
/**
|
2019-03-08 15:46:38 +03: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 19:01:31 +03:00
|
|
|
*/
|
2019-03-11 14:38:54 +03:00
|
|
|
userActiveNow() {
|
|
|
|
return this._activeNowTimeout.isRunning();
|
2019-03-08 15:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-11 14:38:54 +03: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 15:46:38 +03:00
|
|
|
*/
|
2019-03-11 14:38:54 +03:00
|
|
|
userActiveRecently() {
|
|
|
|
return this._activeRecentlyTimeout.isRunning();
|
2018-12-11 17:45:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_onPageVisibilityChanged(e) {
|
2019-03-08 15:46:38 +03:00
|
|
|
if (this._document.visibilityState === "hidden") {
|
2019-03-11 14:38:54 +03:00
|
|
|
this._activeNowTimeout.abort();
|
|
|
|
this._activeRecentlyTimeout.abort();
|
2018-12-11 17:45:11 +03:00
|
|
|
} else {
|
|
|
|
this._onUserActivity(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 13:23:18 +03:00
|
|
|
_onWindowBlurred() {
|
2019-03-11 14:38:54 +03:00
|
|
|
this._activeNowTimeout.abort();
|
|
|
|
this._activeRecentlyTimeout.abort();
|
2016-01-14 19:01:31 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 15:46:38 +03:00
|
|
|
_onUserActivity(event) {
|
|
|
|
// ignore anything if the window isn't focused
|
|
|
|
if (!this._document.hasFocus()) return;
|
|
|
|
|
2017-07-01 16:50:22 +03:00
|
|
|
if (event.screenX && event.type === "mousemove") {
|
|
|
|
if (event.screenX === this.lastScreenX && event.screenY === this.lastScreenY) {
|
2015-11-29 18:39:10 +03:00
|
|
|
// mouse hasn't actually moved
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.lastScreenX = event.screenX;
|
|
|
|
this.lastScreenY = event.screenY;
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:25:48 +03:00
|
|
|
dis.dispatch({action: 'user_activity'});
|
2019-03-11 14:38:54 +03:00
|
|
|
if (!this._activeNowTimeout.isRunning()) {
|
|
|
|
this._activeNowTimeout.start();
|
2018-12-11 17:45:11 +03:00
|
|
|
dis.dispatch({action: 'user_activity_start'});
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2019-03-12 13:28:47 +03:00
|
|
|
this._runTimersUntilTimeout(this._attachedActiveNowTimers, this._activeNowTimeout);
|
2016-01-14 19:01:31 +03:00
|
|
|
} else {
|
2019-03-11 14:38:54 +03:00
|
|
|
this._activeNowTimeout.restart();
|
2015-10-26 16:54:54 +03:00
|
|
|
}
|
|
|
|
|
2019-03-11 14:38:54 +03:00
|
|
|
if (!this._activeRecentlyTimeout.isRunning()) {
|
|
|
|
this._activeRecentlyTimeout.start();
|
2019-03-08 15:46:38 +03:00
|
|
|
|
2019-03-12 13:28:47 +03:00
|
|
|
this._runTimersUntilTimeout(this._attachedActiveRecentlyTimers, this._activeRecentlyTimeout);
|
2019-03-08 15:46:38 +03:00
|
|
|
} else {
|
2019-03-11 14:38:54 +03:00
|
|
|
this._activeRecentlyTimeout.restart();
|
2019-03-08 15:46:38 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 17:45:11 +03:00
|
|
|
|
2019-03-08 15:46:38 +03:00
|
|
|
async _runTimersUntilTimeout(attachedTimers, timeout) {
|
|
|
|
attachedTimers.forEach((t) => t.start());
|
|
|
|
try {
|
|
|
|
await timeout.finished();
|
|
|
|
} catch (_e) { /* aborted */ }
|
|
|
|
attachedTimers.forEach((t) => t.abort());
|
|
|
|
}
|
|
|
|
}
|