Skip to content

Commit

Permalink
Electron: Handle all keyboard shortcuts that are in the menu, from th…
Browse files Browse the repository at this point in the history
…e menu (#2370)

* separate listeners

* restore shift+cmd+g

* extra whitespace
  • Loading branch information
codebykat authored Sep 30, 2020
1 parent 386c322 commit aebfc22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion desktop/menus/edit-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const buildEditMenu = (settings, isAuthenticated, editMode) => {
{
label: '&Find in Note',
visible: isAuthenticated,
click: editorCommandSender({ action: 'find' }),
click: appCommandSender({ action: 'focusSearchField' }),
accelerator: 'CommandOrControl+F',
},
{
Expand Down
27 changes: 21 additions & 6 deletions lib/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ class AppComponent extends Component<Props> {
return false;
}

if (('Escape' === key || 'Esc' === key) && this.props.isSearchActive) {
this.props.clearSearch();
}

if (!window.electron) {
this.handleBrowserShortcut(event);
}

return true;
};

// handle all keyboard shortcuts that are duplicated in the Electron menus
// this listener is only called in browsers, as otherwise the
// menu will trigger them via the provided Accelerator, so we don't need a listener
handleBrowserShortcut = (event: KeyboardEvent) => {
const { ctrlKey, metaKey, shiftKey } = event;
const key = event.key.toLowerCase();

// Is either cmd or ctrl pressed? (But not both)
const cmdOrCtrl = (ctrlKey || metaKey) && ctrlKey !== metaKey;

if (
(cmdOrCtrl && shiftKey && 's' === key) ||
(cmdOrCtrl && !shiftKey && 'f' === key)
Expand All @@ -103,10 +124,6 @@ class AppComponent extends Component<Props> {
return false;
}

if (('Escape' === key || 'Esc' === key) && this.props.isSearchActive) {
this.props.clearSearch();
}

if (cmdOrCtrl && shiftKey && 'f' === key) {
this.props.toggleFocusMode();

Expand All @@ -128,8 +145,6 @@ class AppComponent extends Component<Props> {
event.stopPropagation();
event.preventDefault();
}

return true;
};

toggleShortcuts = (doEnable: boolean) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/note-content-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ class NoteContentEditor extends Component<Props> {
return false;
}

if (cmdOrCtrl && !shiftKey && key === 'g') {
// Electron can trigger this from the menu
if (!window.electron && cmdOrCtrl && !shiftKey && key === 'g') {
this.setNextSearchSelection();
event.stopPropagation();
event.preventDefault();
Expand Down

0 comments on commit aebfc22

Please sign in to comment.