Skip to content

Commit

Permalink
Add desktop only setup of custom keymaps
Browse files Browse the repository at this point in the history
Fix cloud and desktop app global search shortcut
- Add desktop app changeable shortcut in menu
- Fix cloud having multiple global search shortucts bound
  • Loading branch information
Komediruzecki committed Dec 5, 2021
1 parent 7f3a4bc commit 574db47
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 45 deletions.
4 changes: 1 addition & 3 deletions src/cloud/components/Application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ const Application = ({

useEffect(() => {
const handler = () => {
if (usingElectron) {
setShowFuzzyNavigation((prev) => !prev)
}
setShowFuzzyNavigation((prev) => !prev)
}
searchEventEmitter.listen(handler)
return () => {
Expand Down
29 changes: 20 additions & 9 deletions src/cloud/components/molecules/KeymapItemSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import styled from '../../../design/lib/styled'
import { inputStyle } from '../../../design/lib/styled/styleFunctions'
import cc from 'classcat'
import { useToast } from '../../../design/lib/stores/toast'
import { useElectron } from '../../lib/stores/electron'

const invalidShortcutInputs = [' ']
const rejectedShortcutInputs = [' ', 'control', 'alt', 'shift', 'meta']
Expand All @@ -28,6 +29,7 @@ interface KeymapItemSectionProps {
) => Promise<void>
removeKeymap: (key: string) => void
description: string
desktopOnly: boolean
}

const KeymapItemSection = ({
Expand All @@ -36,6 +38,7 @@ const KeymapItemSection = ({
updateKeymap,
removeKeymap,
description,
desktopOnly,
}: KeymapItemSectionProps) => {
const [inputError, setInputError] = useState<boolean>(false)
const [shortcutInputValue, setShortcutInputValue] = useState<string>('')
Expand All @@ -53,6 +56,7 @@ const KeymapItemSection = ({
const shortcutInputRef = useRef<HTMLInputElement>(null)

const { pushMessage } = useToast()
const { usingElectron } = useElectron()

const fetchInputShortcuts: KeyboardEventHandler<HTMLInputElement> = (
event
Expand Down Expand Up @@ -137,6 +141,9 @@ const KeymapItemSection = ({
? getGenericShortcutString(currentKeymapItem)
: ''
}, [currentKeymapItem, currentShortcut])

const isEditableKeymap = !desktopOnly || (desktopOnly && usingElectron)

return (
<KeymapItemSectionContainer>
<div>{description}</div>
Expand All @@ -155,18 +162,22 @@ const KeymapItemSection = ({
onKeyDown={fetchInputShortcuts}
/>
)}
<Button variant={'primary'} onClick={toggleChangingShortcut}>
{currentShortcut == null
? 'Assign'
: changingShortcut
? 'Apply'
: 'Change'}
</Button>
{changingShortcut && (
{!isEditableKeymap ? (
<div>Desktop App Only</div>
) : (
<Button variant={'primary'} onClick={toggleChangingShortcut}>
{currentShortcut == null
? 'Assign'
: changingShortcut
? 'Apply'
: 'Change'}
</Button>
)}
{isEditableKeymap && changingShortcut && (
<Button onClick={handleCancelKeymapChange}>Cancel</Button>
)}

{currentShortcut != null && !changingShortcut && (
{isEditableKeymap && currentShortcut != null && !changingShortcut && (
<Button onClick={handleRemoveKeymap}>Un-assign</Button>
)}
</KeymapItemInputSection>
Expand Down
5 changes: 5 additions & 0 deletions src/cloud/components/settings/KeymapTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const KeymapTab = () => {
description={keymapEntry[1].description}
updateKeymap={updateKeymap}
removeKeymap={removeKeymap}
desktopOnly={
keymapEntry[1].desktopOnly == null
? false
: keymapEntry[1].desktopOnly
}
/>
)
})}
Expand Down
4 changes: 0 additions & 4 deletions src/cloud/lib/stores/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ const useElectronStore = (): ElectronStore => {
event.preventDefault()
toggleSettingsEventEmitter.dispatch()
return
case 'p':
event.preventDefault()
searchEventEmitter.dispatch()
return
case '0':
if (event.shiftKey) {
event.preventDefault()
Expand Down
10 changes: 5 additions & 5 deletions src/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ let ready = false
const singleInstance = app.requestSingleInstanceLock()

export const keymap = new Map<string, string>([
['toggleGlobalSearch', 'CmdOrCtrl + P'],
['toggleGlobalSearch', 'CmdOrCtrl + p'],
['toggleSplitEditMode', 'CmdOrCtrl + \\'],
['togglePreviewMode', 'CmdOrCtrl + E'],
['editorSaveAs', 'CmdOrCtrl + S'],
['createNewDoc', 'CmdOrCtrl + N'],
['createNewFolder', 'CmdOrCtrl + Shift + N'],
['togglePreviewMode', 'CmdOrCtrl + e'],
['editorSaveAs', 'CmdOrCtrl + s'],
['createNewDoc', 'CmdOrCtrl + n'],
// ['createNewFolder', 'CmdOrCtrl + Shift + N'],
['resetZoom', 'CmdOrCtrl + 0'],
['zoomOut', 'CmdOrCtrl + -'],
['zoomIn', 'CmdOrCtrl + Plus'],
Expand Down
6 changes: 3 additions & 3 deletions src/electron/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function getTemplateFromKeymap(
}

menu.push(getFileMenu(keymap))
menu.push(getEditMenu())
menu.push(getEditMenu(keymap))
menu.push(getViewMenu(keymap))
menu.push(getWindowMenu())
menu.push(getCommunityMenu())
Expand Down Expand Up @@ -154,7 +154,7 @@ function getFileMenu(keymap: Map<string, string>): MenuItemConstructorOptions {
}
}

function getEditMenu(): MenuItemConstructorOptions {
function getEditMenu(keymap: Map<string, string>): MenuItemConstructorOptions {
const submenuItems: MenuItemConstructorOptions[] = [
{ role: 'undo' },
{ role: 'redo' },
Expand All @@ -167,7 +167,7 @@ function getEditMenu(): MenuItemConstructorOptions {
type: 'normal',
label: 'Search',
click: createEmitIpcMenuItemHandler('search'),
accelerator: mac ? 'Cmd + P' : 'Ctrl + P',
accelerator: keymap.get('toggleGlobalSearch'),
},
{ type: 'separator' },
{ role: 'delete' },
Expand Down
50 changes: 29 additions & 21 deletions src/lib/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export interface KeymapItem {
shortcutSecondStroke?: KeymapItemEditableProps
description: string
isMenuType?: boolean
desktopOnly?: boolean
}

export const defaultKeymap = new Map<string, KeymapItem>([
[
'createNewDoc',
{
shortcutMainStroke: {
key: 'N',
key: 'n',
keycode: 78,
modifiers:
osName === 'macos'
Expand All @@ -36,26 +37,29 @@ export const defaultKeymap = new Map<string, KeymapItem>([
},
description: 'Create new document',
isMenuType: true,
desktopOnly: true,
},
],
[
'createNewFolder',
{
shortcutMainStroke: {
key: 'N',
keycode: 78,
modifiers:
osName === 'macos'
? { meta: true, shift: true }
: {
ctrl: true,
shift: true,
},
},
description: 'Create new folder',
isMenuType: true,
},
],
// todo: [komediruzecki-2021-12-5] Enable when available
// [
// 'createNewFolder',
// {
// shortcutMainStroke: {
// key: 'N',
// keycode: 78,
// modifiers:
// osName === 'macos'
// ? { meta: true, shift: true }
// : {
// ctrl: true,
// shift: true,
// },
// },
// description: 'Create new folder',
// isMenuType: true,
// desktopOnly: true,
// },
// ],
[
'toggleSideNav',
{
Expand All @@ -77,7 +81,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
'toggleGlobalSearch',
{
shortcutMainStroke: {
key: 'P',
key: 'p',
keycode: 80,
modifiers:
osName === 'macos'
Expand Down Expand Up @@ -105,6 +109,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
},
},
description: 'Toggle in-page search modal dialog',
desktopOnly: true,
},
],
// todo: [komediruzecki-2021-11-7] enable once implemented
Expand Down Expand Up @@ -163,7 +168,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
'togglePreviewMode',
{
shortcutMainStroke: {
key: 'E',
key: 'e',
keycode: 69,
modifiers:
osName === 'macos'
Expand Down Expand Up @@ -208,6 +213,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
},
description: 'Zoom in window',
isMenuType: true,
desktopOnly: true,
},
],
[
Expand All @@ -225,6 +231,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
},
description: 'Zoom out window',
isMenuType: true,
desktopOnly: true,
},
],
[
Expand All @@ -242,6 +249,7 @@ export const defaultKeymap = new Map<string, KeymapItem>([
},
description: 'Reset window zoom',
isMenuType: true,
desktopOnly: true,
},
],
// todo: [komediruzecki-2021-11-7] Enable once implemented
Expand Down

0 comments on commit 574db47

Please sign in to comment.