我正在为Mac OS构建一个电子托盘应用程序。一切似乎都没问题,但是如果我在一个桌面上运行应用程序然后切换到另一个桌面,当我点击应用程序图标时,应用程序会将我返回到它启动的位置。如何修复它以在每个桌面上打开? 提前致谢
// 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()
})
答案 0 :(得分:0)
您可以拨打win.setVisibleOnAllWorkspaces(true);
或致电app.dock.hide();
win.setVisibleOnAllWorkspaces(true);
说出来并使窗口在所有工作区都可见。
app.dock.hide();
隐藏应用程序的停靠栏图标,但允许应用程序中的所有窗口在所有工作区中都可见。
我认为在您的情况下,您应该使用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