簡體   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