简体   繁体   English

Electron:如何重新聚焦/恢复最小化的 DevTools 窗口

[英]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。

The Situation情况

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 窗口保持最小化。

Workaround解决方法

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:笔记:

  • The workaround was tested with Electron 9.3.0 and 10.1.1 on Win 7.该解决方法在 Win 7 上使用 Electron 9.3.0 和 10.1.1 进行了测试。
  • You may need to adapt the BrowserWindow a little, if you want to replicate the standard DevTools window behavior in detach mode.如果您想在分离模式下复制标准的 DevTools 窗口行为,您可能需要稍微调整 BrowserWindow。
  • Also note that isDevToolsOpened will return false , so you need to adapt your checks as well.另请注意, isDevToolsOpened将返回false ,因此您还需要调整您的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM