Skip to content

Commit

Permalink
Choose summon keybinding (#1494)
Browse files Browse the repository at this point in the history
Co-authored-by: goosewobbler <goosewobbler@pm.me>
Co-authored-by: goosewobbler <goosewobbler@protonmail.com>
  • Loading branch information
3 people committed Apr 5, 2023
1 parent ab8ca0c commit 29646be
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 45,920 deletions.
7 changes: 7 additions & 0 deletions app/dash/Main/style/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@
border 1px solid var(--outerspace05)
color var(--outerspace)
height 30px
cursor pointer

.keyCommand:hover
background var(--ghostD)

.keyCommandCancel
margin-left 130px

.nodeProviderStatus
display flex
Expand Down
31 changes: 17 additions & 14 deletions app/dash/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Restore from 'react-restore'

import link from '../../../resources/link'
import Dropdown from '../../../resources/Components/Dropdown'
import { getSummonShortcut } from '../../../resources/app'
import KeyboardShortcutConfigurator from '../../../resources/Components/KeyboardShortcutConfigurator'

class Settings extends Component {
constructor(props, context) {
Expand All @@ -30,7 +30,9 @@ class Settings extends Component {
}

render() {
const { modifierKey, summonKey } = getSummonShortcut(this.store('platform'))
const summonShortcut = this.store('main.shortcuts.summon')
const platform = this.store('platform')

return (
<div className={'localSettings cardShow'}>
<div className='localSettingsWrap'>
Expand All @@ -39,26 +41,27 @@ class Settings extends Component {
<div className='signerPermissionSetting'>Summon Shortcut</div>
<div
className={
this.store('main.shortcuts.altSlash')
summonShortcut.enabled
? 'signerPermissionToggle signerPermissionToggleOn'
: 'signerPermissionToggle'
}
onClick={(_) =>
link.send('tray:action', 'setAltSpace', !this.store('main.shortcuts.altSlash'))
}
onClick={() => {
link.send('tray:action', 'setShortcut', 'summon', {
...summonShortcut,
enabled: !summonShortcut.enabled
})
}}
>
<div className='signerPermissionToggleSwitch' />
</div>
</div>
<div className='signerPermissionDetails'>
<span>
Summon Frame by pressing{' '}
<span className='keyCommand'>
{modifierKey}
<span style={{ padding: '0px 3px' }}>+</span>
{summonKey}
</span>
</span>
<KeyboardShortcutConfigurator
actionText='Summon Frame'
shortcut={summonShortcut}
shortcutName='summon'
platform={platform}
/>
</div>
</div>
<div className='signerPermission localSetting' style={{ zIndex: 213 }}>
Expand Down
4 changes: 2 additions & 2 deletions app/onboard/App/Slides/Access/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Slide, SlideBody, SlideItem, Shortcut } from '../../styled'

import link from '../../../../../resources/link'

import { getSummonShortcut } from '../../../../../resources/app'
import { getDisplayShortcut } from '../../../../../resources/app'

const Access = ({ setTitle, setProceed, platform }) => {
const { modifierKey, summonKey } = getSummonShortcut(platform)
const { modifierKey, summonKey } = getDisplayShortcut(platform, store('main.shortcuts.summon'))
const keyboardShortcut = `${modifierKey} + ${summonKey}`
const [shortcutActivated, setShortcutActivated] = useState(false)
const [trayOpen, setTrayOpen] = useState(store('tray.open'))
Expand Down
9 changes: 7 additions & 2 deletions main/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,13 @@ module.exports = {
toggleSignerCompatibilityWarning: (u) => {
u('main.mute.signerCompatibilityWarning', (v) => !v)
},
setAltSpace: (u, v) => {
u('main.shortcuts.altSlash', () => v)
setShortcut: (u, name, shortcut) => {
u('main.shortcuts', name, (existingShortcut = {}) => ({
modifierKeys: shortcut.modifierKeys || existingShortcut.modifierKeys,
shortcutKey: shortcut.shortcutKey || existingShortcut.shortcutKey,
configuring: shortcut.configuring ?? existingShortcut.configuring,
enabled: shortcut.enabled ?? existingShortcut.enabled
}))
},
setAutohide: (u, v) => {
u('main.autohide', () => v)
Expand Down
38 changes: 38 additions & 0 deletions main/store/migrate/migrations/37/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import log from 'electron-log'

import { v35StateSchema } from '../35/schema'

import type { v37State } from './schema'

const migrate = (initial: unknown) => {
try {
const state = v35StateSchema.parse(initial)
const summonShortcutEnabled = state.main.shortcuts.altSlash

const migratedState: v37State = {
...state,
main: {
...state.main,
shortcuts: {
summon: {
modifierKeys: ['Alt'],
shortcutKey: 'Slash',
enabled: summonShortcutEnabled,
configuring: false
}
}
}
}

return migratedState
} catch (e) {
log.error('Migration 37: could not parse state', e)
}

return initial
}

export default {
version: 37,
migrate
}
18 changes: 18 additions & 0 deletions main/store/migrate/migrations/37/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from 'zod'
import { v35MainSchema, v35StateSchema } from '../35/schema'

const v37ShortcutsSchema = z.object({
summon: z.object({
modifierKeys: z.array(z.enum(['Alt', 'Ctrl', 'Meta', 'Cmd'])),
shortcutKey: z.string(),
enabled: z.boolean(),
configuring: z.boolean()
})
})

const v37MainSchema = v35MainSchema.merge(z.object({ shortcuts: v37ShortcutsSchema }))
const mainUpdates = z.object({ main: v37MainSchema }).passthrough()

export const v37StateSchema = v35StateSchema.merge(mainUpdates).passthrough()

export type v37State = z.infer<typeof v37StateSchema>
91 changes: 91 additions & 0 deletions main/store/state/types/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { z } from 'zod'

const supportedModifierKey = z.enum(['Alt', 'Control', 'Meta', 'Super', 'CommandOrCtrl'])

const supportedShortcutKey = z.enum([
'Comma',
'Period',
'Forwardslash',
'Slash',
'Tab',
'Space',
'Enter',
'Escape',
'ArrowUp',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'F1',
'F2',
'F3',
'F4',
'F5',
'F6',
'F7',
'F8',
'F9',
'F10',
'F11',
'Digit9',
'Digit8',
'Digit7',
'Digit6',
'Digit5',
'Digit4',
'Digit3',
'Digit2',
'Digit1',
'Digit0',
'KeyA',
'KeyB',
'KeyC',
'KeyD',
'KeyE',
'KeyF',
'KeyG',
'KeyH',
'KeyI',
'KeyJ',
'KeyK',
'KeyL',
'KeyM',
'KeyN',
'KeyO',
'KeyP',
'KeyQ',
'KeyR',
'KeyS',
'KeyT',
'KeyU',
'KeyV',
'KeyW',
'KeyX',
'KeyY',
'KeyZ',
'NumpadDivide',
'NumpadMultiply',
'NumpadSubtract',
'NumpadAdd',
'NumpadDecimal',
'Numpad9',
'Numpad8',
'Numpad7',
'Numpad6',
'Numpad5',
'Numpad4',
'Numpad3',
'Numpad2',
'Numpad1',
'Numpad0'
])

export const ShortcutSchema = z.object({
modifierKeys: z.array(supportedModifierKey).default([]),
shortcutKey: supportedShortcutKey,
enabled: z.boolean().default(true),
configuring: z.boolean().default(false)
})

export type ModifierKey = z.infer<typeof supportedModifierKey>
export type ShortcutKey = z.infer<typeof supportedShortcutKey>
export type Shortcut = z.infer<typeof ShortcutSchema>
40 changes: 28 additions & 12 deletions main/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import store from '../store'
import FrameManager from './frames'
import { createWindow } from './window'
import { SystemTray, SystemTrayEventHandlers } from './systemTray'
import { getAcceleratorFromShortcut } from '../../resources/app'

type Windows = { [key: string]: BrowserWindow }

Expand All @@ -38,6 +39,7 @@ let dash: Dash
let onboard: Onboard
let mouseTimeout: NodeJS.Timeout
let glide = false
let summonShortcutAccelerator = 'Alt+/'

const app = {
hide: () => {
Expand Down Expand Up @@ -552,20 +554,34 @@ const init = () => {

store.observer(() => broadcast('permissions', JSON.stringify(store('permissions'))))
store.observer(() => {
const displaySummonShortcut = store('main.shortcuts.altSlash')
if (displaySummonShortcut) {
globalShortcut.unregister('Alt+/')
globalShortcut.register('Alt+/', () => {
app.toggle()
if (store('windows.onboard.showing')) {
send('onboard', 'main:flex', 'shortcutActivated')
}
})
} else {
globalShortcut.unregister('Alt+/')
const summonShortcut = store('main.shortcuts.summon')
const accelerator = getAcceleratorFromShortcut(summonShortcut)
try {
globalShortcut.unregister(accelerator)
if (summonShortcutAccelerator) {
globalShortcut.unregister(summonShortcutAccelerator)
}
if (summonShortcut.enabled && !summonShortcut.configuring) {
globalShortcut.register(accelerator, () => {
app.toggle()
if (store('windows.onboard.showing')) {
send('onboard', 'main:flex', 'shortcutActivated')
}
})
summonShortcutAccelerator = accelerator
}
} catch (e) {
const summonShortcutStr = [...summonShortcut.modifierKeys, summonShortcut.shortcutKey].join('+')
log.error(
new Error(`Could not set accelerator "${accelerator}" for summon shortcut: ${summonShortcutStr}`)
)
}

if (tray?.isReady()) {
systemTray.setContextMenu(tray.isVisible() ? 'hide' : 'show', { displaySummonShortcut })
systemTray.setContextMenu(tray.isVisible() ? 'hide' : 'show', {
displaySummonShortcut: summonShortcut.enabled,
accelerator
})
}
})
}
Expand Down
5 changes: 2 additions & 3 deletions main/windows/systemTray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { app, screen, BrowserWindow, Menu, KeyboardEvent, Rectangle, Tray as ElectronTray } from 'electron'
import path from 'path'
import { capitalize } from '../../resources/utils'
import store from '../store'

const isMacOS = process.platform === 'darwin'

Expand Down Expand Up @@ -35,7 +34,7 @@ export class SystemTray {

setContextMenu(
type: string,
{ displaySummonShortcut = store('main.shortcuts.altSlash'), switchScreen = false }
{ displaySummonShortcut = false, accelerator = 'Alt+/', switchScreen = false }
) {
const separatorMenuItem = {
label: 'Frame',
Expand All @@ -59,7 +58,7 @@ export class SystemTray {
}

if (displaySummonShortcut) {
actionMenuItem.accelerator = 'Alt+/'
actionMenuItem.accelerator = accelerator
actionMenuItem.registerAccelerator = false
}

Expand Down
Loading

0 comments on commit 29646be

Please sign in to comment.