Skip to content

Commit 3778960

Browse files
committed
fix keyboard shortcut for tab switching
1 parent 9276654 commit 3778960

File tree

9 files changed

+58
-26
lines changed

9 files changed

+58
-26
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"node": ">=14.17.0"
1818
},
1919
"dependencies": {
20+
"@hfelix/electron-localshortcut": "^4.0.1",
2021
"@octokit/rest": "^19.0.3",
2122
"argon2": "^0.28.7",
2223
"axios": "^0.27.2",
@@ -33,6 +34,7 @@
3334
"js-beautify": "^1.14.3",
3435
"mobx": "^6.6.0",
3536
"mobx-state-tree": "^5.1.5",
37+
"native-keymap": "^3.3.0",
3638
"object-hash": "^3.0.0",
3739
"rxjs": "^7.5.5",
3840
"typesafe-i18n": "^5.5.2",

packages/main/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { Application } from './application'
44
import { setupRootStore } from './store'
55
import { setupTitlebar } from 'custom-electron-titlebar/main'
66
import { logger } from './logger'
7+
import { electronLocalshortcut } from '@hfelix/electron-localshortcut'
8+
import { getCurrentKeyboardLayout, getKeyMap } from 'native-keymap'
79

810
app.commandLine.appendSwitch('disable-site-isolation-trials')
911
app.commandLine.appendSwitch('disable-renderer-backgrounding')
1012
app.commandLine.appendSwitch('disable-background-timer-throttling')
1113

14+
electronLocalshortcut.setKeyboardLayout(getCurrentKeyboardLayout(), getKeyMap())
15+
1216
// Disable GPU Acceleration for Windows 7
1317
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
1418

packages/main/windows/game-window.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { EventEmitter } from 'stream'
66
import TypedEmitter from 'typed-emitter'
77
import { generateUserArgent } from '../utils'
88
import { logger } from '../logger'
9+
import { observe } from 'mobx'
10+
import { electronLocalshortcut } from '@hfelix/electron-localshortcut'
911

1012
type GameWindowEvents = {
1113
close: (event: Event) => void
@@ -16,6 +18,7 @@ export class GameWindow extends (EventEmitter as new () => TypedEmitter<GameWind
1618
private readonly _teamWindow?: GameTeamWindow
1719
private readonly _team?: GameTeam
1820
private _isMuted = false
21+
private readonly _shortcutStoreDisposer: () => void
1922

2023
get id() {
2124
return this._win.webContents.id!
@@ -102,6 +105,19 @@ export class GameWindow extends (EventEmitter as new () => TypedEmitter<GameWind
102105
}
103106
})
104107

108+
this._shortcutStoreDisposer = observe(
109+
this._store.hotkeyStore.window.tabs,
110+
() => {
111+
electronLocalshortcut.unregisterAll(this._win)
112+
this._store.hotkeyStore.window.tabs.forEach((tab, index) => {
113+
electronLocalshortcut.register(this._win, tab, () => {
114+
this._win.webContents.send(IPCEvents.SELECT_TAB, index)
115+
})
116+
})
117+
},
118+
true
119+
)
120+
105121
if (app.isPackaged) {
106122
this._win.loadFile(join(__dirname, '../renderer/index.html'))
107123
} else {
@@ -131,6 +147,8 @@ export class GameWindow extends (EventEmitter as new () => TypedEmitter<GameWind
131147

132148
private _close(event: Event) {
133149
this._win.removeAllListeners()
150+
electronLocalshortcut.unregisterAll(this._win)
151+
this._shortcutStoreDisposer()
134152
this.emit('close', event)
135153
}
136154

packages/preload/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ const subscribeToNewTab = (callback: () => void): (() => void) => {
7878
}
7979
}
8080

81+
const subscribeToSelectTab = (callback: (tabIndex: number) => void): (() => void) => {
82+
const listener = (e: IpcRendererEvent, tabIndex: number) => {
83+
callback(tabIndex)
84+
}
85+
ipcRenderer.on(IPCEvents.SELECT_TAB, listener)
86+
87+
return () => {
88+
ipcRenderer.removeListener(IPCEvents.SELECT_TAB, listener)
89+
}
90+
}
91+
8192
const subscribeToNextTab = (callback: () => void): (() => void) => {
8293
const listener = () => {
8394
callback()
@@ -260,6 +271,7 @@ const lindoApi: LindoAPI = {
260271
forwardPatchToMain,
261272
subscribeToIPCPatch,
262273
subscribeToNewTab,
274+
subscribeToSelectTab,
263275
subscribeToNextTab,
264276
subscribeToPrevTab,
265277
subscribeToCloseTab,

packages/renderer/src/screens/main-screen/tab-manager/TabManager.tsx

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { useConst } from '@/hooks'
21
import { useStores } from '@/store'
3-
import { reaction } from 'mobx'
42
import React, { useEffect } from 'react'
5-
import { Shortcuts } from 'shortcuts'
63
export interface TabManagerProps {
74
children: React.ReactNode
85
}
@@ -11,8 +8,7 @@ export interface TabManagerProps {
118
* Manage hotkeys and action for game tabs
129
**/
1310
export const TabManager = ({ children }: TabManagerProps) => {
14-
const { gameStore, hotkeyStore } = useStores()
15-
const shortcuts = useConst(new Shortcuts())
11+
const { gameStore } = useStores()
1612

1713
useEffect(
1814
() =>
@@ -46,26 +42,11 @@ export const TabManager = ({ children }: TabManagerProps) => {
4642
[]
4743
)
4844

49-
useEffect(() => {
50-
const setTabHotKeys = () => {
51-
shortcuts.reset()
52-
shortcuts.add(
53-
hotkeyStore.window.tabs.map((tab, index) => ({
54-
shortcut: tab,
55-
handler: () => {
56-
gameStore.selectGameIndex(index)
57-
}
58-
}))
59-
)
60-
}
61-
setTabHotKeys()
62-
return reaction(
63-
() => hotkeyStore.window.tabs,
64-
() => {
65-
setTabHotKeys()
66-
}
67-
)
68-
}, [hotkeyStore.window.tabs])
45+
useEffect(() =>
46+
window.lindoAPI.subscribeToSelectTab((tabIndex: number) => {
47+
gameStore.selectGameIndex(tabIndex)
48+
})
49+
)
6950

7051
return <>{children}</>
7152
}

packages/renderer/src/store/game-store/game-store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const GameStoreModel = types
5151
selectGameIndex(index: number) {
5252
if (self.gamesOrder[index]) {
5353
self.selectedGame = self.gamesOrder[index]
54+
self.selectedGame.setHasNotification(false)
5455
}
5556
}
5657
}))

packages/shared/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export enum IPCEvents {
1111
APP_READY_TO_SHOW = 'APP_READY_TO_SHOW',
1212
FETCH_GAME_CONTEXT = 'FETCH_GAME_CONTEXT',
1313
NEW_TAB = 'NEW_TAB',
14+
SELECT_TAB = 'SELECT_TAB',
1415
CLOSE_TAB = 'CLOSE_TAB',
1516
PREV_TAB = 'PREV_TAB',
1617
NEXT_TAB = 'NEXT_TAB',

packages/shared/types/lindo-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface LindoAPI {
2525
resetStore: () => void
2626
// hotkeys
2727
subscribeToNewTab: (callback: () => void) => () => void
28+
subscribeToSelectTab: (callback: (tabIndex: number) => void) => () => void
2829
subscribeToNextTab: (callback: () => void) => () => void
2930
subscribeToPrevTab: (callback: () => void) => () => void
3031
subscribeToCloseTab: (callback: () => void) => () => void

yarn.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,13 @@
485485
minimatch "^3.1.2"
486486
strip-json-comments "^3.1.1"
487487

488+
"@hfelix/electron-localshortcut@^4.0.1":
489+
version "4.0.1"
490+
resolved "https://registry.yarnpkg.com/@hfelix/electron-localshortcut/-/electron-localshortcut-4.0.1.tgz#6ef31284c11e1388f2dab88ed8e71ed86b974c5d"
491+
integrity sha512-tfmRcokNSGuMaOH67/acYQjtUHKGwsKrLrzYn14LD2lw4N69rdD2qH4f9trJLj7P2yyx90Ll9jP8jr1Jq7B6jA==
492+
dependencies:
493+
debug "^4.3.3"
494+
488495
"@humanwhocodes/config-array@^0.9.2":
489496
version "0.9.5"
490497
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"
@@ -2245,7 +2252,7 @@ [email protected], debug@^2.6.8, debug@^2.6.9:
22452252
dependencies:
22462253
ms "2.0.0"
22472254

2248-
debug@4, debug@^4.0.0, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
2255+
debug@4, debug@^4.0.0, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.3, debug@^4.3.4:
22492256
version "4.3.4"
22502257
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
22512258
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
@@ -4593,6 +4600,11 @@ nanoid@^3.3.4:
45934600
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
45944601
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
45954602

4603+
native-keymap@^3.3.0:
4604+
version "3.3.0"
4605+
resolved "https://registry.yarnpkg.com/native-keymap/-/native-keymap-3.3.0.tgz#927ca6afcf4ebd986b5dc1bddfab4270e750915d"
4606+
integrity sha512-nvhI1Cdr+ywhpqqGdM+JM8EjMYA1s6SW8EqhWNKtffHU07JcTKDhd8N3o0TLSFn8y2YuRFoOwfAIzdTU6TVhDA==
4607+
45964608
natural-compare@^1.4.0:
45974609
version "1.4.0"
45984610
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"

0 commit comments

Comments
 (0)