signal-desktop/ts/services/InteractionMode.ts

76 lines
1.8 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2023-03-14 15:55:31 +00:00
type InteractionModeType = 'mouse' | 'keyboard';
let initialized = false;
2023-03-14 15:55:31 +00:00
let interactionMode: InteractionModeType = 'mouse';
export function startInteractionMode(): void {
if (initialized) {
return;
}
initialized = true;
document.body.classList.add('mouse-mode');
window.enterKeyboardMode = () => {
if (interactionMode === 'keyboard') {
return;
}
interactionMode = 'keyboard';
document.body.classList.add('keyboard-mode');
document.body.classList.remove('mouse-mode');
const clearSelectedMessage =
window.reduxActions?.conversations?.clearSelectedMessage;
if (clearSelectedMessage) {
clearSelectedMessage();
}
const userChanged = window.reduxActions?.user?.userChanged;
if (userChanged) {
userChanged({ interactionMode });
}
};
window.enterMouseMode = () => {
if (interactionMode === 'mouse') {
return;
}
interactionMode = 'mouse';
document.body.classList.add('mouse-mode');
document.body.classList.remove('keyboard-mode');
const clearSelectedMessage =
window.reduxActions?.conversations?.clearSelectedMessage;
if (clearSelectedMessage) {
clearSelectedMessage();
}
const userChanged = window.reduxActions?.user?.userChanged;
if (userChanged) {
userChanged({ interactionMode });
}
};
document.addEventListener(
'keydown',
event => {
if (event.key === 'Tab') {
window.enterKeyboardMode();
}
},
true
);
document.addEventListener('wheel', window.enterMouseMode, true);
document.addEventListener('mousedown', window.enterMouseMode, true);
2023-03-14 15:55:31 +00:00
}
2023-03-14 15:55:31 +00:00
export function getInteractionMode(): InteractionModeType {
return interactionMode;
}