繁体   English   中英

电子托盘应用在Mac OS上的位置

[英]Electron tray app position on Mac OS

我正在为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()
})

您可以调用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

暂无
暂无

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

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