[英]Electron: How to refocus/restore a minimised DevTools window
I've created a global shortcut where Ctrl+Shift+I
opens a detached DevTools window.我创建了一个全局快捷方式,其中
Ctrl+Shift+I
打开一个分离的 DevTools 窗口。 However, when the DevTools window is minimised, pressing Ctrl+Shift+I
does not do anything.但是,当 DevTools 窗口最小化时,按
Ctrl+Shift+I
不会执行任何操作。
I want to be able to restore/unminimise the minimised DevTools window by using the Ctrl+Shift+I
shortcut.我希望能够使用
Ctrl+Shift+I
快捷方式恢复/取消最小化最小化的 DevTools 窗口。 The .focus
method usually works for things like this but doesn't in this case. .focus
方法通常适用于这样的事情,但在这种情况下不起作用。 I've tried using 'undocked' mode for the window too.我也尝试过对窗口使用“非停靠”模式。
The code I have to implement the current behaviour is this:我必须实现当前行为的代码是这样的:
if (focussedWindow.webContents.isDevToolsOpened()) {
if (focussedWindow.webContents.devToolsWebContents !== null) {
focussedWindow.webContents.devToolsWebContents.focus();
}
}
I should also add that the DevTools window does refocus if it is behind other windows, just not when minimised.我还应该补充一点,如果 DevTools 窗口在其他窗口后面,它会重新聚焦,只是在最小化时不会。
I'm using Electron 9.3.0 on Windows 10 Pro.我在 Windows 10 专业版上使用 Electron 9.3.0。
On Linux, BrowserWindow.webContents.devToolsWebContents.focus()
restores the minimized detached DevTools window – as desired.在 Linux 上,
BrowserWindow.webContents.devToolsWebContents.focus()
根据需要恢复最小化的分离 DevTools 窗口。
On Windows, this does not work (tested with Electron 9.3.0 and 10.1.1 on Win 7).在 Windows 上,这不起作用(在 Win 7 上使用 Electron 9.3.0 和 10.1.1 进行测试)。 The minimized detached DevTools window stays minimized.
最小化的分离 DevTools 窗口保持最小化。
After trying many workarounds that all did not work on Windows, I found the following trick: make a new BrowserWindow for the DevTools to occupy.在尝试了许多在 Windows 上都不起作用的解决方法之后,我发现了以下技巧:为 DevTools 创建一个新的 BrowserWindow。 With this reference, you can now simply call
BrowserWindow.restore()
.有了这个参考,您现在可以简单地调用
BrowserWindow.restore()
。
const {app, BrowserWindow} = require("electron");
const url = require("url");
const path = require("path");
let mainWindow, newDevToolsWindow;
app.on("ready", function() {
mainWindow = new BrowserWindow();
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
}));
console.log("create new window for devtools")
newDevToolsWindow = new BrowserWindow();
mainWindow.webContents.setDevToolsWebContents(newDevToolsWindow.webContents);
mainWindow.webContents.openDevTools({mode: "detach"});
// Simulate OP's keypress
setInterval(() => {
console.log("restore the new window")
newDevToolsWindow.restore();
}, 4000);
});
Notes:笔记:
isDevToolsOpened
will return false
, so you need to adapt your checks as well.isDevToolsOpened
将返回false
,因此您还需要调整您的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.