Dock
Electron 具有 API 可在 macOS Dock 中配置應用程式的圖示。存在一個僅限 macOS 的 API 來建立自訂的 Dock 選單,但 Electron 也使用應用程式 Dock 圖示作為跨平台功能(如最近的文件和應用程式進度)的入口點。
自訂 Dock 通常用於將捷徑加入使用者不希望開啟整個應用程式視窗的任務。
Terminal.app 的 Dock 選單
若要設定您的自訂 Dock 選單,您需要使用 app.dock.setMenu
API,該 API 僅在 macOS 上可用。
- main.js
- index.html
const { app, BrowserWindow, Menu } = require('electron/main')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('index.html')
}
const dockMenu = Menu.buildFromTemplate([
{
label: 'New Window',
click () { console.log('New Window') }
}, {
label: 'New Window with Settings',
submenu: [
{ label: 'Basic' },
{ label: 'Pro' }
]
},
{ label: 'New Command...' }
])
app.whenReady().then(() => {
if (process.platform === 'darwin') {
app.dock.setMenu(dockMenu)
}
}).then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<h1>Hello World!</h1>
<p>Right click the dock icon to see the custom menu options.</p>
</body>
</html>
啟動 Electron 應用程式後,右鍵點擊應用程式圖示。您應該會看到您剛才定義的自訂選單