From df55816c04df82dcd81d68d98541f8d41738aeca Mon Sep 17 00:00:00 2001 From: Katarzyna Stachura Date: Wed, 19 Jan 2022 22:51:45 +0100 Subject: [PATCH] Add a keyboard shortcut to toggle hidden event visibility when labs are enabled. Notes: The keyboard shortcut is control (or cmd) shift h. Signed-off-by: Katarzyna Stachura --- src/KeyBindingsDefaults.ts | 15 +++++++++++ src/KeyBindingsManager.ts | 11 ++++++++ src/accessibility/KeyboardShortcuts.ts | 16 +++++++++++ src/components/structures/LoggedInView.tsx | 31 +++++++++++++++++++++- src/i18n/strings/en_EN.json | 3 ++- 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/KeyBindingsDefaults.ts b/src/KeyBindingsDefaults.ts index 468d9989520c..8c506acf1b25 100644 --- a/src/KeyBindingsDefaults.ts +++ b/src/KeyBindingsDefaults.ts @@ -22,6 +22,7 @@ import { NavigationAction, RoomAction, RoomListAction, + LabsAction, } from "./KeyBindingsManager"; import { isMac, Key } from "./Keyboard"; import SettingsStore from "./settings/SettingsStore"; @@ -411,10 +412,24 @@ const navigationBindings = (): KeyBinding[] => { ]; }; +const labsBindings = (): KeyBinding[] => { + return [ + { + action: LabsAction.ToggleHiddenEventVisibility, + keyCombo: { + key: Key.H, + ctrlOrCmd: true, + shiftKey: true, + }, + }, + ]; +}; + export const defaultBindingsProvider: IKeyBindingsProvider = { getMessageComposerBindings: messageComposerBindings, getAutocompleteBindings: autocompleteBindings, getRoomListBindings: roomListBindings, getRoomBindings: roomBindings, getNavigationBindings: navigationBindings, + getLabsBindings: labsBindings, }; diff --git a/src/KeyBindingsManager.ts b/src/KeyBindingsManager.ts index 047e0b74c249..37142baedd28 100644 --- a/src/KeyBindingsManager.ts +++ b/src/KeyBindingsManager.ts @@ -125,6 +125,12 @@ export enum NavigationAction { SelectNextUnreadRoom = 'SelectNextUnreadRoom', } +/** Actions only available when labs are enabled */ +export enum LabsAction { + /** Toggle visibility of hidden events */ + ToggleHiddenEventVisibility = 'ToggleHiddenEventVisibility', +} + /** * Represent a key combination. * @@ -213,6 +219,7 @@ export interface IKeyBindingsProvider { getRoomListBindings: KeyBindingGetter; getRoomBindings: KeyBindingGetter; getNavigationBindings: KeyBindingGetter; + getLabsBindings: KeyBindingGetter; } export class KeyBindingsManager { @@ -264,6 +271,10 @@ export class KeyBindingsManager { getNavigationAction(ev: KeyboardEvent | React.KeyboardEvent): NavigationAction | undefined { return this.getAction(this.bindingsProviders.map(it => it.getNavigationBindings), ev); } + + getLabsAction(ev: KeyboardEvent | React.KeyboardEvent): LabsAction | undefined { + return this.getAction(this.bindingsProviders.map(it => it.getLabsBindings), ev); + } } const manager = new KeyBindingsManager(); diff --git a/src/accessibility/KeyboardShortcuts.ts b/src/accessibility/KeyboardShortcuts.ts index 868277ccc7ba..decc1426a8e8 100644 --- a/src/accessibility/KeyboardShortcuts.ts +++ b/src/accessibility/KeyboardShortcuts.ts @@ -16,6 +16,7 @@ limitations under the License. import { _td } from "../languageHandler"; import { isMac, Key } from "../Keyboard"; +import SdkConfig from "../SdkConfig"; export enum Categories { NAVIGATION = "Navigation", @@ -24,6 +25,7 @@ export enum Categories { ROOM_LIST = "Room List", ROOM = "Room", AUTOCOMPLETE = "Autocomplete", + LABS = "Labs", } export enum Modifiers { @@ -276,8 +278,22 @@ export const shortcuts: Record = { description: _td("Cancel autocomplete"), }, ], + + // Intentionally left blank, as bindings should be only added when the feature is enabled + [Categories.LABS]: [], }; +// Add this short only if labs are enabled, as the feature is only exposed there. +if (SdkConfig.get()['showLabsSettings']) { + shortcuts[Categories.LABS].push({ + keybinds: [{ + modifiers: [CMD_OR_CTRL, Modifiers.SHIFT], + key: Key.H, + }], + description: _td("Toggle visibility of hidden events"), + }); +} + export const registerShortcut = (category: Categories, defn: IShortcut) => { shortcuts[category].push(defn); }; diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index ee25d585895e..468e2b7f3a8d 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -22,6 +22,7 @@ import classNames from 'classnames'; import { ISyncStateData, SyncState } from 'matrix-js-sdk/src/sync'; import { IUsageLimit } from 'matrix-js-sdk/src/@types/partials'; +import SdkConfig from "../../SdkConfig"; import { Key } from '../../Keyboard'; import PageTypes from '../../PageTypes'; import MediaDeviceHandler from '../../MediaDeviceHandler'; @@ -29,6 +30,7 @@ import { fixupColorFonts } from '../../utils/FontManager'; import dis from '../../dispatcher/dispatcher'; import { IMatrixClientCreds } from '../../MatrixClientPeg'; import SettingsStore from "../../settings/SettingsStore"; +import { SettingLevel } from "../../settings/SettingLevel"; import ResizeHandle from '../views/elements/ResizeHandle'; import { CollapseDistributor, Resizer } from '../../resizer'; import MatrixClientContext from "../../contexts/MatrixClientContext"; @@ -47,7 +49,7 @@ import { IOOBData, IThreepidInvite } from "../../stores/ThreepidInviteStore"; import Modal from "../../Modal"; import { ICollapseConfig } from "../../resizer/distributors/collapse"; import HostSignupContainer from '../views/host_signup/HostSignupContainer'; -import { getKeyBindingsManager, NavigationAction, RoomAction } from '../../KeyBindingsManager'; +import { getKeyBindingsManager, NavigationAction, RoomAction, LabsAction } from '../../KeyBindingsManager'; import { IOpts } from "../../createRoom"; import SpacePanel from "../views/spaces/SpacePanel"; import { replaceableComponent } from "../../utils/replaceableComponent"; @@ -531,6 +533,33 @@ class LoggedInView extends React.Component { // if we do not have a handler for it, pass it to the platform which might handled = PlatformPeg.get().onKeyDown(ev); } + + // Handle labs actions here, as they apply within the same scope + if (SdkConfig.get()['showLabsSettings'] && !handled) { + const labsAction = getKeyBindingsManager().getLabsAction(ev); + switch (labsAction) { + case LabsAction.ToggleHiddenEventVisibility: { + const hiddenEventVisibility = SettingsStore.getValueAt( + SettingLevel.DEVICE, + 'showHiddenEventsInTimeline', + undefined, + false, + ); + SettingsStore.setValue( + 'showHiddenEventsInTimeline', + undefined, + SettingLevel.DEVICE, + !hiddenEventVisibility, + ); + handled = true; + break; + } + default: + // if we do not have a handler for it, pass it to the platform which might + handled = PlatformPeg.get().onKeyDown(ev); + } + } + if (handled) { ev.stopPropagation(); ev.preventDefault(); diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 86f2e90efb2d..1c6c12a73f55 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -3408,5 +3408,6 @@ "Open this settings tab": "Open this settings tab", "Go to Home View": "Go to Home View", "Move autocomplete selection up/down": "Move autocomplete selection up/down", - "Cancel autocomplete": "Cancel autocomplete" + "Cancel autocomplete": "Cancel autocomplete", + "Toggle visibility of hidden events": "Toggle visibility of hidden events" }