fix: too many listeners
This commit is contained in:
parent
16d9be4ce4
commit
1a2a382916
@ -8,6 +8,9 @@ import { windowService } from './WindowService'
|
|||||||
let showAppAccelerator: string | null = null
|
let showAppAccelerator: string | null = null
|
||||||
let showMiniWindowAccelerator: string | null = null
|
let showMiniWindowAccelerator: string | null = null
|
||||||
|
|
||||||
|
// store the focus and blur handlers for each window to unregister them later
|
||||||
|
const windowOnHandlers = new Map<BrowserWindow, { onFocusHandler: () => void; onBlurHandler: () => void }>()
|
||||||
|
|
||||||
function getShortcutHandler(shortcut: Shortcut) {
|
function getShortcutHandler(shortcut: Shortcut) {
|
||||||
switch (shortcut.key) {
|
switch (shortcut.key) {
|
||||||
case 'zoom_in':
|
case 'zoom_in':
|
||||||
@ -112,10 +115,6 @@ const convertShortcutRecordedByKeyboardEventKeyValueToElectronGlobalShortcutForm
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerShortcuts(window: BrowserWindow) {
|
export function registerShortcuts(window: BrowserWindow) {
|
||||||
window.once('ready-to-show', () => {
|
|
||||||
window.webContents.setZoomFactor(configManager.getZoomFactor())
|
|
||||||
})
|
|
||||||
|
|
||||||
const register = () => {
|
const register = () => {
|
||||||
if (window.isDestroyed()) return
|
if (window.isDestroyed()) return
|
||||||
|
|
||||||
@ -145,9 +144,10 @@ export function registerShortcuts(window: BrowserWindow) {
|
|||||||
|
|
||||||
case 'mini_window':
|
case 'mini_window':
|
||||||
//available only when QuickAssistant enabled
|
//available only when QuickAssistant enabled
|
||||||
if (configManager.getEnableQuickAssistant()) {
|
if (!configManager.getEnableQuickAssistant()) {
|
||||||
showMiniWindowAccelerator = formatShortcutKey(shortcut.shortcut)
|
return
|
||||||
}
|
}
|
||||||
|
showMiniWindowAccelerator = formatShortcutKey(shortcut.shortcut)
|
||||||
break
|
break
|
||||||
|
|
||||||
//the following ZOOMs will register shortcuts seperately, so will return
|
//the following ZOOMs will register shortcuts seperately, so will return
|
||||||
@ -201,8 +201,12 @@ export function registerShortcuts(window: BrowserWindow) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.on('focus', () => register())
|
// only register the event handlers once
|
||||||
window.on('blur', () => unregister())
|
if (undefined === windowOnHandlers.get(window)) {
|
||||||
|
window.on('focus', register)
|
||||||
|
window.on('blur', unregister)
|
||||||
|
windowOnHandlers.set(window, { onFocusHandler: register, onBlurHandler: unregister })
|
||||||
|
}
|
||||||
|
|
||||||
if (!window.isDestroyed() && window.isFocused()) {
|
if (!window.isDestroyed() && window.isFocused()) {
|
||||||
register()
|
register()
|
||||||
@ -213,6 +217,11 @@ export function unregisterAllShortcuts() {
|
|||||||
try {
|
try {
|
||||||
showAppAccelerator = null
|
showAppAccelerator = null
|
||||||
showMiniWindowAccelerator = null
|
showMiniWindowAccelerator = null
|
||||||
|
windowOnHandlers.forEach((handlers, window) => {
|
||||||
|
window.off('focus', handlers.onFocusHandler)
|
||||||
|
window.off('blur', handlers.onBlurHandler)
|
||||||
|
})
|
||||||
|
windowOnHandlers.clear()
|
||||||
globalShortcut.unregisterAll()
|
globalShortcut.unregisterAll()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error('[ShortcutService] Failed to unregister all shortcuts')
|
Logger.error('[ShortcutService] Failed to unregister all shortcuts')
|
||||||
|
|||||||
@ -145,6 +145,7 @@ export class WindowService {
|
|||||||
|
|
||||||
private setupWindowEvents(mainWindow: BrowserWindow) {
|
private setupWindowEvents(mainWindow: BrowserWindow) {
|
||||||
mainWindow.once('ready-to-show', () => {
|
mainWindow.once('ready-to-show', () => {
|
||||||
|
mainWindow.webContents.setZoomFactor(configManager.getZoomFactor())
|
||||||
mainWindow.show()
|
mainWindow.show()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user