* 注册当前焦点的 DevTools 快捷键 * @param target 需要打开开发者工具的 WebContents
(target: Electron.WebContents)
| 30 | * @param target 需要打开开发者工具的 WebContents |
| 31 | */ |
| 32 | public register(target: Electron.WebContents): void { |
| 33 | // 如果已经注册且目标相同,无需重复注册 |
| 34 | if (this.currentTarget?.id === target.id && globalShortcut.isRegistered(this.shortcut)) { |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | // 先注销可能存在的旧注册 |
| 39 | this.unregister() |
| 40 | |
| 41 | this.currentTarget = target |
| 42 | |
| 43 | // 注册全局快捷键 |
| 44 | const ret = globalShortcut.register(this.shortcut, async () => { |
| 45 | if (this.currentTarget && !this.currentTarget.isDestroyed()) { |
| 46 | console.log(`[DevTools] 触发开发者工具快捷键,目标: ${this.currentTarget.id}`) |
| 47 | if (this.currentTarget.isDevToolsOpened()) { |
| 48 | this.currentTarget.closeDevTools() |
| 49 | } else { |
| 50 | const mode = getDevToolsMode() |
| 51 | this.currentTarget.openDevTools({ mode }) |
| 52 | } |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | if (!ret) { |
| 57 | console.error(`[DevTools] 开发者工具快捷键注册失败: ${this.shortcut}`) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * 注销快捷键 |
nothing calls this directly
no test coverage detected