From 08d24e2be6dff46d0509ea7caa9359ee7a32ad4e 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 | 20 +++++++++++++ src/KeyBindingsManager.ts | 11 +++++++ src/accessibility/KeyboardShortcuts.ts | 13 ++++++++ src/components/structures/LoggedInView.tsx | 30 ++++++++++++++++++- .../tabs/user/KeyboardUserSettingsTab.tsx | 7 +++++ src/i18n/strings/en_EN.json | 5 ++-- 6 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/KeyBindingsDefaults.ts b/src/KeyBindingsDefaults.ts index 468d9989520c..f3d44ea318ce 100644 --- a/src/KeyBindingsDefaults.ts +++ b/src/KeyBindingsDefaults.ts @@ -22,9 +22,11 @@ import { NavigationAction, RoomAction, RoomListAction, + LabsAction, } from "./KeyBindingsManager"; import { isMac, Key } from "./Keyboard"; import SettingsStore from "./settings/SettingsStore"; +import SdkConfig from "./SdkConfig"; const messageComposerBindings = (): KeyBinding[] => { const bindings: KeyBinding[] = [ @@ -411,10 +413,28 @@ const navigationBindings = (): KeyBinding[] => { ]; }; +const labsBindings = (): KeyBinding[] => { + if (!SdkConfig.get()['showLabsSettings']) { + return []; + } + + 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..dbff6e68cbe6 100644 --- a/src/accessibility/KeyboardShortcuts.ts +++ b/src/accessibility/KeyboardShortcuts.ts @@ -24,6 +24,7 @@ export enum Categories { ROOM_LIST = "Room List", ROOM = "Room", AUTOCOMPLETE = "Autocomplete", + LABS = "Labs", } export enum Modifiers { @@ -276,6 +277,18 @@ export const shortcuts: Record = { description: _td("Cancel autocomplete"), }, ], + + // SdkConfig isn't initialized here, so add labs shortcuts unconditionally. The logic whether they should be visible + // and enabled belongs in other places anyway. + [Categories.LABS]: [ + { + keybinds: [{ + modifiers: [CMD_OR_CTRL, Modifiers.SHIFT], + key: Key.H, + }], + description: _td("Toggle visibility of hidden events"), + }, + ], }; export const registerShortcut = (category: Categories, defn: IShortcut) => { diff --git a/src/components/structures/LoggedInView.tsx b/src/components/structures/LoggedInView.tsx index ee25d585895e..14d1e53ab65a 100644 --- a/src/components/structures/LoggedInView.tsx +++ b/src/components/structures/LoggedInView.tsx @@ -29,6 +29,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 +48,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 +532,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 (!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/components/views/settings/tabs/user/KeyboardUserSettingsTab.tsx b/src/components/views/settings/tabs/user/KeyboardUserSettingsTab.tsx index 11ef078424b3..fa2411bc2d32 100644 --- a/src/components/views/settings/tabs/user/KeyboardUserSettingsTab.tsx +++ b/src/components/views/settings/tabs/user/KeyboardUserSettingsTab.tsx @@ -21,6 +21,7 @@ import React from "react"; import { Categories, DIGITS, IShortcut, Modifiers, shortcuts } from "../../../../../accessibility/KeyboardShortcuts"; import { isMac, Key } from "../../../../../Keyboard"; import { _t, _td } from "../../../../../languageHandler"; +import SdkConfig from "../../../../../SdkConfig"; // TS: once languageHandler is TS we can probably inline this into the enum _td("Alt"); @@ -33,6 +34,7 @@ _td("Calls"); _td("Composer"); _td("Room List"); _td("Autocomplete"); +_td("Labs"); const categoryOrder = [ Categories.COMPOSER, @@ -43,6 +45,11 @@ const categoryOrder = [ Categories.CALLS, ]; +// Add the labs category if they're enabled +if (SdkConfig.get()['showLabsSettings']) { + categoryOrder.push(Categories.LABS); +} + const modifierIcon: Record = { [Modifiers.COMMAND]: "⌘", }; diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 83d550a60b58..46ca46504543 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -1441,6 +1441,7 @@ "Composer": "Composer", "Room List": "Room List", "Autocomplete": "Autocomplete", + "Labs": "Labs", "Page Up": "Page Up", "Page Down": "Page Down", "Esc": "Esc", @@ -1449,7 +1450,6 @@ "End": "End", "[number]": "[number]", "Keyboard": "Keyboard", - "Labs": "Labs", "Feeling experimental? Labs are the best way to get things early, test out new features and help shape them before they actually launch. Learn more.": "Feeling experimental? Labs are the best way to get things early, test out new features and help shape them before they actually launch. Learn more.", "Ignored/Blocked": "Ignored/Blocked", "Error adding ignored user/server": "Error adding ignored user/server", @@ -3411,5 +3411,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" }