Skip to content

Commit

Permalink
Settings window: Add interaction mode and tab focus state
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Nonnenberg <scott@signal.org>
  • Loading branch information
automated-signal and scottnonnenberg-signal committed Oct 28, 2021
1 parent 17285d2 commit 63088ed
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 50 deletions.
9 changes: 9 additions & 0 deletions stylesheets/components/Preferences.scss
Expand Up @@ -55,6 +55,15 @@
}
}

&:focus {
@include keyboard-mode {
background: $color-gray-05;
}
@include dark-keyboard-mode {
background: $color-gray-75;
}
}

&::before {
content: '';
display: block;
Expand Down
52 changes: 2 additions & 50 deletions ts/background.ts
Expand Up @@ -121,6 +121,7 @@ import { ToastCaptchaSolved } from './components/ToastCaptchaSolved';
import { ToastConversationArchived } from './components/ToastConversationArchived';
import { ToastConversationUnarchived } from './components/ToastConversationUnarchived';
import { showToast } from './util/showToast';
import { startInteractionMode } from './windows/startInteractionMode';

const MAX_ATTACHMENT_DOWNLOAD_AGE = 3600 * 72 * 1000;

Expand Down Expand Up @@ -440,56 +441,7 @@ export async function startApp(): Promise<void> {
false
);

// Keyboard/mouse mode
let interactionMode: 'mouse' | 'keyboard' = 'mouse';
$(document.body).addClass('mouse-mode');

window.enterKeyboardMode = () => {
if (interactionMode === 'keyboard') {
return;
}

interactionMode = 'keyboard';
$(document.body).addClass('keyboard-mode').removeClass('mouse-mode');
const { userChanged } = window.reduxActions.user;
const { clearSelectedMessage } = window.reduxActions.conversations;
if (clearSelectedMessage) {
clearSelectedMessage();
}
if (userChanged) {
userChanged({ interactionMode });
}
};
window.enterMouseMode = () => {
if (interactionMode === 'mouse') {
return;
}

interactionMode = 'mouse';
$(document.body).addClass('mouse-mode').removeClass('keyboard-mode');
const { userChanged } = window.reduxActions.user;
const { clearSelectedMessage } = window.reduxActions.conversations;
if (clearSelectedMessage) {
clearSelectedMessage();
}
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);

window.getInteractionMode = () => interactionMode;
startInteractionMode();

// Load these images now to ensure that they don't flicker on first use
window.preloadedImages = [];
Expand Down
3 changes: 3 additions & 0 deletions ts/windows/settings/preload.ts
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../types/SystemTraySetting';
import { awaitObject } from '../../util/awaitObject';
import { createSetting, createCallback } from '../../util/preload';
import { startInteractionMode } from '../startInteractionMode';

function doneRendering() {
ipcRenderer.send('settings-done-rendering');
Expand Down Expand Up @@ -124,6 +125,8 @@ function getSystemTraySettingValues(
}

const renderPreferences = async () => {
startInteractionMode();

const {
blockedCount,
deviceName,
Expand Down
71 changes: 71 additions & 0 deletions ts/windows/startInteractionMode.ts
@@ -0,0 +1,71 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

let initialized = false;
let interactionMode: 'mouse' | 'keyboard' = '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);

window.getInteractionMode = () => interactionMode;
}

0 comments on commit 63088ed

Please sign in to comment.