feat(desktop): add tray behavior and packaging config

This commit is contained in:
nav
2026-05-13 06:57:27 +00:00
parent 5843d3f8ca
commit 54f4b46755
5 changed files with 3397 additions and 10 deletions

78
main.js
View File

@@ -1,4 +1,4 @@
const { app, BrowserWindow, Menu, ipcMain, Notification, shell } = require('electron')
const { app, BrowserWindow, Menu, Tray, ipcMain, Notification, nativeImage, shell } = require('electron')
const fs = require('fs')
const path = require('path')
@@ -6,6 +6,10 @@ const isDev = !!process.env.FABRIC_DESKTOP_URL
const DEFAULT_DEV_URL = 'http://localhost:5173'
const DEFAULT_PROD_ENTRY = path.join(__dirname, 'offline.html')
let mainWindow = null
let tray = null
let isQuitting = false
function configPath() {
return path.join(app.getPath('userData'), 'fabric-desktop.config.json')
}
@@ -81,11 +85,72 @@ function createWindow() {
} else {
win.loadFile(DEFAULT_PROD_ENTRY)
}
win.on('close', (event) => {
if (!isQuitting) {
event.preventDefault()
win.hide()
}
})
return win
}
function createTrayIcon() {
if (tray) return tray
const iconDataUrl =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAfElEQVR4AWNw4PjPwMDA8J+BgYHhP4MDA8N/BgYGhv8MDAwM/2dgYGB4R2JgYGB4w8DAwPCfgYGB4R8DAwPDf4aGhob/BgwMDAz/Z2BgYHhHYmBgYHjDwMDA8J+BgYHhHwMDA8N/hoYGhv8GDAwMDP9nYGBgeEdiYGAAAB7RImfVq6X8AAAAAElFTkSuQmCC'
const icon = nativeImage.createFromDataURL(iconDataUrl)
tray = new Tray(icon)
tray.setToolTip('Fabric Desktop')
const buildTrayMenu = () =>
Menu.buildFromTemplate([
{
label: mainWindow?.isVisible() ? '隐藏窗口' : '显示窗口',
click: () => {
if (!mainWindow) {
mainWindow = createWindow()
return
}
if (mainWindow.isVisible()) mainWindow.hide()
else {
mainWindow.show()
mainWindow.focus()
}
tray.setContextMenu(buildTrayMenu())
},
},
{
label: '退出',
click: () => {
isQuitting = true
app.quit()
},
},
])
tray.setContextMenu(buildTrayMenu())
tray.on('double-click', () => {
if (!mainWindow) {
mainWindow = createWindow()
return
}
mainWindow.show()
mainWindow.focus()
tray.setContextMenu(buildTrayMenu())
})
return tray
}
app.whenReady().then(() => {
createMenu()
createWindow()
mainWindow = createWindow()
createTrayIcon()
ipcMain.handle('fabric:config:get', () => readConfig())
ipcMain.handle('fabric:config:set', (_evt, next) => writeConfig(next || {}))
@@ -100,10 +165,15 @@ app.whenReady().then(() => {
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
if (BrowserWindow.getAllWindows().length === 0) mainWindow = createWindow()
else if (mainWindow && !mainWindow.isVisible()) mainWindow.show()
})
app.on('before-quit', () => {
isQuitting = true
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
// 保持后台驻留,由托盘控制退出
})