1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/mouseManager.js

173 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import inputManager from './inputManager';
import focusManager from '../components/focusManager';
import browser from './browser';
2020-08-14 08:46:34 +02:00
import layoutManager from '../components/layoutManager';
import dom from './dom';
import Events from '../utils/events.ts';
2023-04-19 01:56:05 -04:00
const self = {};
2023-04-19 01:56:05 -04:00
let lastMouseInputTime = new Date().getTime();
let isMouseIdle;
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function mouseIdleTime() {
return new Date().getTime() - lastMouseInputTime;
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function notifyApp() {
inputManager.notifyMouseMove();
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function removeIdleClasses() {
const classList = document.body.classList;
2023-04-19 01:56:05 -04:00
classList.remove('mouseIdle');
classList.remove('mouseIdle-tv');
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function addIdleClasses() {
const classList = document.body.classList;
2023-04-19 01:56:05 -04:00
classList.add('mouseIdle');
2023-04-19 01:56:05 -04:00
if (layoutManager.tv) {
classList.add('mouseIdle-tv');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
export function showCursor() {
if (isMouseIdle) {
isMouseIdle = false;
removeIdleClasses();
Events.trigger(self, 'mouseactive');
2020-07-30 10:51:53 +02:00
}
2023-04-19 01:56:05 -04:00
}
2020-07-30 10:51:53 +02:00
2023-04-19 01:56:05 -04:00
export function hideCursor() {
if (!isMouseIdle) {
isMouseIdle = true;
addIdleClasses();
Events.trigger(self, 'mouseidle');
2020-07-30 10:51:53 +02:00
}
2023-04-19 01:56:05 -04:00
}
2020-07-30 10:51:53 +02:00
2023-04-19 01:56:05 -04:00
let lastPointerMoveData;
function onPointerMove(e) {
const eventX = e.screenX || e.clientX;
const eventY = e.screenY || e.clientY;
2023-04-19 01:56:05 -04:00
// if coord don't exist how could it move
if (typeof eventX === 'undefined' && typeof eventY === 'undefined') {
return;
}
2023-04-19 01:56:05 -04:00
const obj = lastPointerMoveData;
if (!obj) {
lastPointerMoveData = {
x: eventX,
y: eventY
};
return;
}
2023-04-19 01:56:05 -04:00
// if coord are same, it didn't move
if (Math.abs(eventX - obj.x) < 10 && Math.abs(eventY - obj.y) < 10) {
return;
}
2023-04-19 01:56:05 -04:00
obj.x = eventX;
obj.y = eventY;
2023-04-19 01:56:05 -04:00
lastMouseInputTime = new Date().getTime();
notifyApp();
2023-04-19 01:56:05 -04:00
showCursor();
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onPointerEnter(e) {
const pointerType = e.pointerType || (layoutManager.mobile ? 'touch' : 'mouse');
2023-04-19 01:56:05 -04:00
if (pointerType === 'mouse' && !isMouseIdle) {
const parent = focusManager.focusableParent(e.target);
if (parent) {
focusManager.focus(parent);
2018-10-23 01:05:09 +03:00
}
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function enableFocusWithMouse() {
if (!layoutManager.tv) {
return false;
}
2023-04-19 01:56:05 -04:00
if (browser.web0s) {
return false;
}
2023-04-19 01:56:05 -04:00
return !!browser.tv;
}
function onMouseInterval() {
if (!isMouseIdle && mouseIdleTime() >= 5000) {
hideCursor();
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
let mouseInterval;
function startMouseInterval() {
if (!mouseInterval) {
mouseInterval = setInterval(onMouseInterval, 5000);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function stopMouseInterval() {
const interval = mouseInterval;
if (interval) {
clearInterval(interval);
mouseInterval = null;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
removeIdleClasses();
}
2023-04-19 01:56:05 -04:00
function initMouse() {
stopMouseInterval();
2023-04-19 01:56:05 -04:00
/* eslint-disable-next-line compat/compat */
dom.removeEventListener(document, (window.PointerEvent ? 'pointermove' : 'mousemove'), onPointerMove, {
passive: true
});
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
if (!layoutManager.mobile) {
startMouseInterval();
2023-04-19 01:56:05 -04:00
dom.addEventListener(document, (window.PointerEvent ? 'pointermove' : 'mousemove'), onPointerMove, {
passive: true
});
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
/* eslint-disable-next-line compat/compat */
dom.removeEventListener(document, (window.PointerEvent ? 'pointerenter' : 'mouseenter'), onPointerEnter, {
capture: true,
passive: true
});
2023-04-19 01:56:05 -04:00
if (enableFocusWithMouse()) {
dom.addEventListener(document, (window.PointerEvent ? 'pointerenter' : 'mouseenter'), onPointerEnter, {
capture: true,
passive: true
});
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
initMouse();
2023-04-19 01:56:05 -04:00
Events.on(layoutManager, 'modechange', initMouse);
2020-08-01 05:46:31 +02:00
export default {
hideCursor,
showCursor
};