【问题标题】:Previous Window Focus / Electron以前的窗口焦点/电子
【发布时间】:2021-05-10 03:12:31
【问题描述】:

目前正在为一个艰难的问题挠头。我刚从电子开始,到目前为止一切都很好。但是,当窗口被隐藏时(它是一个带有快捷方式的弹出窗口,当您按下回车键时会消失),我想将焦点返回到前一个窗口。

我使用的是 Mac,菜单栏显示了我以前的应用程序的名称,因此看起来焦点已返回给应用程序,但由于未选择窗口,因此并不完全。

知道如何解决这个问题吗?

谢谢!

【问题讨论】:

  • 你的代码是什么样的?你打电话给win.show()

标签: electron


【解决方案1】:

对于 Linux: 我发现browserWindow.hide() 正确地恢复了焦点。

对于 Windows: browserWindow.minimize() 正确恢复焦点。

对于 Mac: app.hide() 正确恢复焦点。注意:不幸的是,调用app.hide() 会隐藏所有窗口。没有已知的方法可以在不使用app.hide() 隐藏所有窗口的情况下保持某些窗口打开。

这适用于 Mac、Linux 和 Windows:

hide() {
    this.window.minimize();
    this.window.hide();
    if (process.platform == "darwin") this.app.hide()
}

show() {
    this.window.show();
    this.window.restore();
}

【讨论】:

    【解决方案2】:

    我刚刚在 Github 上发布了一个名为 Popup Window 的 Electron 测试应用程序,它展示了如何正确地将焦点返回到上一个窗口。它是我之前的一个项目的简化版本,仅适用于 macOS。我遇到了与您完全相同的问题,我记得我通过隐藏应用程序而不是窗口来解决它,并处理窗口模糊事件以实际隐藏它......

    HTH...

    ma​​in.js

    const { app, BrowserWindow, globalShortcut, ipcMain } = require ('electron');
    let mainWindow = null;
    function onAppReady ()
    {
        mainWindow = new BrowserWindow
        (
            {
                width: 600,
                height: 600,
                show: false,
                frame: false
            }
        );
        mainWindow.loadURL (`file://${__dirname}/index.html`);
        mainWindow.once ('closed', () => { mainWindow = null; });
        mainWindow.on ('blur', () => { mainWindow.hide (); });
        globalShortcut.register ("CommandOrControl+Alt+P", () => { mainWindow.show (); });
        ipcMain.on ('dismiss', () => { app.hide (); });
    }
    app.once ('ready', onAppReady);
    app.once ('window-all-closed', () => { app.quit (); });
    app.dock.hide ();
    


    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
      </head>
      <body>
        <h1>Hello World!</h1>
        <!-- All of the Node.js APIs are available in this renderer process. -->
        We are using Node.js <script>document.write(process.versions.node)</script>,
        Chromium <script>document.write(process.versions.chrome)</script>,
        and Electron <script>document.write(process.versions.electron)</script>.
    
        <script>
          // You can also require other files to run in this process
          require('./renderer.js')
        </script>
      </body>
    </html>
    


    renderer.js

    const { ipcRenderer } = require ('electron');
    document.addEventListener
    (
        'keydown',
        (event) =>
        {
            if (event.key === 'Enter')
            {
                event.preventDefault ();
                ipcRenderer.send ('dismiss');
            }
        }
    );
    

    【讨论】:

    • 这正是我想要的!隐藏应用程序本身是要走的路。太棒了,你让我很开心:)
    【解决方案3】:

    添加这个对我有用:

    mainWindow.on('blur', () => app.hide())
    

    我使用mainWindow.hide() 隐藏应用程序,并在模糊侦听器上添加app.hide() 将焦点返回到上次使用的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2012-02-12
      • 2012-08-18
      • 2018-05-03
      • 2012-08-30
      相关资源
      最近更新 更多