目前在一个艰难的问题上挠头。我刚刚开始使用电子,到目前为止还不错。但是,当窗口被隐藏时(它是一个带有快捷方式的弹出窗口,当您按 Enter 时它会消失),我想将焦点返回到上一个窗口。
我使用的是 Mac,菜单栏显示了我以前的应用程序的名称,因此看起来焦点已返回给应用程序,但并非完全如此,因为未选择窗口。
知道如何解决这个问题吗?
谢谢!
jun*_*var 12
对于 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();
}
Run Code Online (Sandbox Code Playgroud)
小智 4
我刚刚在 Github 上发布了一个名为Popup Window的 Electron 测试应用程序,它展示了如何正确地将焦点返回到上一个窗口。它是我之前项目之一的简化版本,并且仅限 macOS。我遇到了与您完全相同的问题,我记得我是通过隐藏应用程序而不是窗口来解决它的,并处理窗口模糊事件以实际隐藏它......
哈...
main.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 ();
Run Code Online (Sandbox Code Playgroud)
索引.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>
Run Code Online (Sandbox Code Playgroud)
渲染器.js:
const { ipcRenderer } = require ('electron');
document.addEventListener
(
'keydown',
(event) =>
{
if (event.key === 'Enter')
{
event.preventDefault ();
ipcRenderer.send ('dismiss');
}
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3032 次 |
| 最近记录: |