Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add a keyboard shortcut to toggle hidden event visibility when labs a…
Browse files Browse the repository at this point in the history
…re enabled.

Notes: The keyboard shortcut is control (or cmd) shift h.
Signed-off-by: Katarzyna Stachura <uwunyaa@outlook.com>
  • Loading branch information
UwUnyaa committed Jan 19, 2022
1 parent 582a1b0 commit b65298f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/KeyBindingsDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
NavigationAction,
RoomAction,
RoomListAction,
LabsAction,
} from "./KeyBindingsManager";
import { isMac, Key } from "./Keyboard";
import SettingsStore from "./settings/SettingsStore";
Expand Down Expand Up @@ -411,10 +412,24 @@ const navigationBindings = (): KeyBinding<NavigationAction>[] => {
];
};

const labsBindings = (): KeyBinding<LabsAction>[] => {
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,
};
11 changes: 11 additions & 0 deletions src/KeyBindingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -213,6 +219,7 @@ export interface IKeyBindingsProvider {
getRoomListBindings: KeyBindingGetter<RoomListAction>;
getRoomBindings: KeyBindingGetter<RoomAction>;
getNavigationBindings: KeyBindingGetter<NavigationAction>;
getLabsBindings: KeyBindingGetter<LabsAction>;
}

export class KeyBindingsManager {
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/accessibility/KeyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,6 +25,7 @@ export enum Categories {
ROOM_LIST = "Room List",
ROOM = "Room",
AUTOCOMPLETE = "Autocomplete",
LABS = "Labs",
}

export enum Modifiers {
Expand Down Expand Up @@ -276,8 +278,22 @@ export const shortcuts: Record<Categories, IShortcut[]> = {
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);
};
31 changes: 30 additions & 1 deletion src/components/structures/LoggedInView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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';
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";
Expand All @@ -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";
Expand Down Expand Up @@ -531,6 +533,33 @@ class LoggedInView extends React.Component<IProps, IState> {
// 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();
Expand Down

0 comments on commit b65298f

Please sign in to comment.