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

feature: 按 F12 可以打开/关闭 开发者工具 了。 #355

Merged
Merged
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
54 changes: 44 additions & 10 deletions packages/gui/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ let hideDockWhenWinClose = DevSidecar.api.config.get().app.dock.hideWhenWinClose
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])

let devToolsStatus = false

function openDevTools () {
try {
log.info('尝试打开 `开发者工具`')
win.webContents.openDevTools()
devToolsStatus = true
log.info('打开 `开发者工具` 成功')
} catch (e) {
log.error('打开 `开发者工具` 失败:', e)
}
}

function closeDevTools () {
try {
log.info('尝试关闭 `开发者工具`')
win.webContents.closeDevTools()
devToolsStatus = false
log.info('关闭 `开发者工具` 成功')
} catch (e) {
log.error('关闭 `开发者工具` 失败:', e)
}
}

function switchDevTools () {
if (devToolsStatus) {
closeDevTools()
} else {
openDevTools()
}
}

// 隐藏主窗口,并创建托盘,绑定关闭事件
function setTray () {
// const topMenu = Menu.buildFromTemplate({})
Expand All @@ -32,17 +65,10 @@ function setTray () {
// 通常被添加到一个 context menu 上.
// 系统托盘右键菜单
const trayMenuTemplate = [

{
// 系统托盘图标目录
label: 'DevTools',
click: () => {
try {
win.webContents.openDevTools()
} catch (e) {
log.error('win.webContents.openDevTools() error:', e)
}
}
label: 'DevTools (F12)',
click: switchDevTools
},
{
// 系统托盘图标目录
Expand Down Expand Up @@ -161,7 +187,7 @@ function createWindow (startHideWindow) {
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
if (!process.env.IS_TEST) openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
Expand Down Expand Up @@ -212,6 +238,14 @@ function createWindow (startHideWindow) {
log.info('session-end:', e)
await quit()
})

// 监听键盘事件
win.webContents.on('before-input-event', (event, input) => {
// 按 F12,打开/关闭 开发者工具
if (input.key === 'F12' && input.type === 'keyUp') {
switchDevTools()
}
})
}

async function beforeQuit () {
Expand Down