Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove linux altGr detection, replace windows altGr with Control + Alt #1548

Merged
merged 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions main/keyboardShortcuts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { globalShortcut } from 'electron'
import log from 'electron-log'

import store from '../store'
import { shortcutKeyMap } from '../../resources/keyboard/mappings'
import type { Shortcut } from '../store/state/types/shortcuts'

Expand All @@ -10,9 +9,6 @@ const stringifyShortcut = ({ modifierKeys, shortcutKey }: Shortcut) => ({
accelerator: [...modifierKeys.slice().sort(), shortcutKeyMap[shortcutKey] || shortcutKey].join('+')
})

const equivalentShortcuts = (shortcut1: Shortcut, shortcut2: Shortcut) =>
shortcut1.modifierKeys === shortcut2.modifierKeys && shortcut1.shortcutKey === shortcut2.shortcutKey

function unregister(shortcut: Shortcut) {
const { shortcutString, accelerator } = stringifyShortcut(shortcut)

Expand Down Expand Up @@ -41,50 +37,6 @@ function register(shortcut: Shortcut, shortcutHandler: (accelerator: string) =>
}

export const registerShortcut = (shortcut: Shortcut, shortcutHandler: (accelerator: string) => void) => {
const isWindows = process.platform === 'win32'
const isMacOS = process.platform === 'darwin'
const keyboardLayout = store('keyboardLayout')
const createAltGrShortcut = () => {
// remove AltGr and Alt from modifiers (Linux)
// remove AltGr, Alt and Control from modifiers (Windows)
const modifierKeys = shortcut.modifierKeys.filter((modifier) =>
isWindows ? !modifier.startsWith('Alt') && modifier !== 'Control' : !modifier.startsWith('Alt')
)

// return new modifiers depending on OS + rest of shortcut - so that AltGr / Right Alt triggers in the same way as Left Alt
return {
...shortcut,
modifierKeys: (isWindows
? [...modifierKeys, 'Control', 'Alt']
: [...modifierKeys, 'AltGr']) as typeof shortcut.modifierKeys
}
}

// Windows & Linux Non-US key layout AltGr / Right Alt fix
if (!isMacOS) {
const altGrShortcut = createAltGrShortcut()

// unregister any existing AltGr shortcut - unless it matches the one we are about to register
if (!keyboardLayout.isUS && !equivalentShortcuts(shortcut, altGrShortcut)) {
unregister(altGrShortcut)
}

if (
shortcut.modifierKeys.includes('AltGr') ||
(shortcut.modifierKeys.includes('Alt') && !keyboardLayout.isUS)
) {
// register the AltGr shortcut
register(altGrShortcut, shortcutHandler)

// replace AltGr with Alt in the main shortcut
shortcut = {
...shortcut,
modifierKeys: shortcut.modifierKeys.map((key) => (key === 'AltGr' ? 'Alt' : key))
}
}
}

// register the shortcut
unregister(shortcut)
register(shortcut, shortcutHandler)
}
11 changes: 11 additions & 0 deletions main/store/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,17 @@ const migrations = {
initial.main.shortcuts.summon.enabled = true
}

return initial
},
37: (initial) => {
const isWindows = process.platform === 'win32'
const { shortcuts } = initial.main || {}
const altGrIndex = shortcuts.summon.modifierKeys.indexOf('AltGr')
goosewobbler marked this conversation as resolved.
Show resolved Hide resolved
if (altGrIndex > -1) {
const altGrReplacement = isWindows ? ['Alt', 'Control'] : ['Alt']
initial.main.shortcuts.summon.modifierKeys.splice(altGrIndex, 1, ...altGrReplacement)
}

return initial
}
}
Expand Down
2 changes: 1 addition & 1 deletion main/store/state/types/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'

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

const supportedShortcutKey = z.enum([
'Comma',
Expand Down
2 changes: 1 addition & 1 deletion resources/Components/KeyboardShortcutConfigurator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const KeyboardShortcutConfigurator = ({ actionText = '', platform, shortcut, sho
hotkeys('*', { capture: true }, (event) => {
event.preventDefault()

const allowedModifierKeys = ['Meta', 'Alt', 'AltGr', 'Control', 'Command']
const allowedModifierKeys = ['Meta', 'Alt', 'Control', 'Command']
const isModifierKey = allowedModifierKeys.includes(event.key)

// ignore modifier key solo keypresses and disabled keys
Expand Down
12 changes: 3 additions & 9 deletions resources/keyboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ function getModifierKey(key: ModifierKey, platform: Platform) {
return isMacOS ? 'Option' : 'Alt'
}

if (key === 'AltGr') {
return 'Alt'
}

if (key === 'Control' || key === 'CommandOrCtrl') {
return isMacOS ? 'Control' : 'Ctrl'
}
Expand All @@ -60,13 +56,11 @@ export const getDisplayShortcut = (platform: Platform, shortcut: Shortcut) => {

export const getShortcutFromKeyEvent = (e: KeyboardEvent, pressedKeyCodes: number[], platform: Platform) => {
const isWindows = platform === 'win32'
const isLinux = platform === 'linux'
const altGrPressed = !e.altKey &&
((pressedKeyCodes.includes(17) && pressedKeyCodes.includes(18) && isWindows) || (pressedKeyCodes.includes(18) && isLinux))
const altGrPressed = !e.altKey && pressedKeyCodes.includes(17) && pressedKeyCodes.includes(18)
const modifierKeys = []

if (altGrPressed) {
modifierKeys.push('AltGr')
if (isWindows && altGrPressed) {
modifierKeys.push('Alt', 'Control')
}
if (e.altKey) {
modifierKeys.push('Alt')
Expand Down
Loading