Skip to content

[Fix] Handle shortcuts from the frontend without depending on menu #2684

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

Merged
merged 1 commit into from
May 6, 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
19 changes: 11 additions & 8 deletions src/backend/api/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ export const clipboardWriteText = async (text: string) =>
export const pathExists = async (path: string) =>
ipcRenderer.invoke('pathExists', path)

export const processShortcut = async (combination: string) =>
ipcRenderer.send('processShortcut', combination)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const handleGoToScreen = (callback: any) => {
ipcRenderer.on('openScreen', callback)
return () => {
ipcRenderer.removeListener('openScreen', callback)
}
}

export const handleShowDialog = (
onMessage: (
e: Electron.IpcRendererEvent,
Expand Down Expand Up @@ -155,11 +166,3 @@ export const getWikiGameInfo = async (
appName: string,
runner: Runner
) => ipcRenderer.invoke('getWikiGameInfo', title, appName, runner)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const handleGoToScreen = (callback: any) => {
ipcRenderer.on('openScreen', callback)
return () => {
ipcRenderer.removeListener('openScreen', callback)
}
}
63 changes: 31 additions & 32 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
powerSaveBlocker,
protocol,
screen,
clipboard,
globalShortcut
clipboard
} from 'electron'
import 'backend/updater'
import { autoUpdater } from 'electron-updater'
Expand Down Expand Up @@ -398,36 +397,6 @@ if (!gotTheLock) {

initTrayIcon(mainWindow)

// hotkey to reload the app
globalShortcut.register('CommandOrControl+R', () => {
mainWindow.reload()
})

// hotkey to quit the app
globalShortcut.register('CommandOrControl+Q', () => {
handleExit()
})

// hotkey to open the dev tools
globalShortcut.register('CommandOrControl+Shift+I', () => {
mainWindow.webContents.openDevTools()
})

// hotkey to open the settings on frontend
globalShortcut.register('CommandOrControl+K', () => {
sendFrontendMessage('openScreen', '/settings/app/default/general')
})

// hotkey to open the library screen on frontend
globalShortcut.register('CommandOrControl+L', () => {
sendFrontendMessage('openScreen', '/library')
})

// hotkey to open the downloads screen on frontend
globalShortcut.register('CommandOrControl+J', () => {
sendFrontendMessage('openScreen', '/download-manager')
})

return
})
}
Expand Down Expand Up @@ -1643,6 +1612,36 @@ ipcMain.handle('pathExists', async (e, path: string) => {
return existsSync(path)
})

ipcMain.on('processShortcut', async (e, combination: string) => {
const mainWindow = getMainWindow()

switch (combination) {
// hotkey to reload the app
case 'ctrl+r':
mainWindow?.reload()
break
// hotkey to quit the app
case 'ctrl+q':
handleExit()
break
// hotkey to open the settings on frontend
case 'ctrl+k':
sendFrontendMessage('openScreen', '/settings/app/default/general')
break
// hotkey to open the downloads screen on frontend
case 'ctrl+j':
sendFrontendMessage('openScreen', '/download-manager')
break
// hotkey to open the library screen on frontend
case 'ctrl+l':
sendFrontendMessage('openScreen', '/library')
break
case 'ctrl+shift+i':
mainWindow?.webContents?.openDevTools()
break
}
})

/*
Other Keys that should go into translation files:
t('box.error.generic.title')
Expand Down
1 change: 1 addition & 0 deletions src/common/typedefs/ipcBridge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ interface SyncIPCFunctions {
logInfo: (message: unknown) => void
showItemInFolder: (item: string) => void
clipboardWriteText: (text: string) => void
processShortcut: (combination: string) => void
addNewApp: (args: SideloadGame) => void
showLogFileInFolder: (args: {
appName: string
Expand Down
16 changes: 16 additions & 0 deletions src/frontend/helpers/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,21 @@ export const initShortcuts = () => {
if ((e.ctrlKey || e.metaKey) && e.key.toLocaleLowerCase() === 'f') {
document.getElementById('search')?.focus()
}

if (e.ctrlKey || e.metaKey) {
// Ctrl+R or Cmd+R, reload
// Ctrl+L or Cmd+L, show library
// Ctrl+J or Cmd+J, download manager
// Ctrl+K or Cmd+K, settings
// Ctrl+Q or Cmd+Q, quit heroic
if (['r', 'j', 'k', 'l', 'q'].includes(e.key)) {
window.api.processShortcut(`ctrl+${e.key}`)
}

// Ctrl+Shift+I or Cmd+Shift+I, open devtools
if (e.key === 'I') {
window.api.processShortcut(`ctrl+shift+${e.key.toLocaleLowerCase()}`)
}
}
})
}