电子托盘应用程序在Mac OS上的位置 [英] Electron tray app position on Mac OS
问题描述
我正在为Mac OS开发一个电子托盘应用程序.一切似乎都很好,但是如果我在一个桌面上运行该应用程序然后切换到另一个桌面,则当我单击应用程序图标时,应用程序会将我返回到启动它的位置.我该如何修复它才能在每个台式机上打开?预先感谢
I am building an electron tray app for Mac OS. Everything seems okay, but if I run the app on one desktop and then switch to another, when I click on app icon, app returns me back to place where it was launched. How can I fix it to be open on every desktop? Thanks in advance
// Sets variables (const)
const {app, BrowserWindow, ipcMain, Tray} = require('electron')
const path = require('path')
const assetsDirectory = path.join(__dirname, 'img')
let tray = undefined
let window = undefined
// Don't show the app in the doc
app.dock.hide()
// Creates tray & window
app.on('ready', () => {
createTray()
createWindow()
})
// Quit the app when the window is closed
app.on('window-all-closed', () => {
app.quit()
})
// Creates tray image & toggles window on click
const createTray = () => {
tray = new Tray(path.join(assetsDirectory, 'icon.png'))
tray.on('click', function (event) {
toggleWindow()
})
}
const getWindowPosition = () => {
const windowBounds = window.getBounds()
const trayBounds = tray.getBounds()
// Center window horizontally below the tray icon
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))
// Position window 4 pixels vertically below the tray icon
const y = Math.round(trayBounds.y + trayBounds.height + 3)
return {x: x, y: y}
}
// Creates window & specifies its values
const createWindow = () => {
window = new BrowserWindow({
width: 250,
height: 310,
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
'node-integration': false
})
// This is where the index.html file is loaded into the window
window.loadURL('file://' + __dirname + '/index.html');
// Hide the window when it loses focus
window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide()
}
})
}
const toggleWindow = () => {
if (window.isVisible()) {
window.hide()
} else {
showWindow()
}
}
const showWindow = () => {
const position = getWindowPosition()
window.setPosition(position.x, position.y, false)
window.show()
window.focus()
}
ipcMain.on('show-window', () => {
showWindow()
})
推荐答案
您可以调用 app.dock.hide();
You can either call win.setVisibleOnAllWorkspaces(true);
or call app.dock.hide();
win.setVisibleOnAllWorkspaces(true);
照其说的做,并使该窗口在所有工作区上可见.
win.setVisibleOnAllWorkspaces(true);
Does what it says and makes the window visible on all workspaces.
app.dock.hide();
隐藏应用程序的停靠图标,但将允许应用程序中的所有窗口在所有工作区上可见.
app.dock.hide();
Hides the dock icon for your application but will allow all windows in your application to be visible on all workspaces.
我认为在您的情况下,您应该使用 win.setVisibleOnAllWorkspaces(true);
I think in your case you should go with win.setVisibleOnAllWorkspaces(true);
win.setVisibleOnAllWorkspaces();
https://electron.atom.io/docs/api/browser-window/#winsetvisibleonallworkspacesvisible
app.dock.hide();
https://electron.atom.io/docs/api/app/#appdockhide-macos
这篇关于电子托盘应用程序在Mac OS上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!