-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96c7989
commit 2e1a9e0
Showing
1 changed file
with
53 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,62 @@ | ||
import { shutdown } from "./on_shutdown.mjs"; | ||
import { __dirname } from "./server.mjs"; | ||
import path from "node:path"; | ||
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
import os from 'node:os'; | ||
const SysTray = (await import('npm:systray')).default.default //?????? | ||
|
||
async function getBase64Icon(iconPath) { | ||
try { | ||
const iconData = fs.readFileSync(iconPath); | ||
return iconData.toString('base64'); | ||
} catch (err) { | ||
console.error("读取图标文件失败:", err); | ||
return ""; | ||
} | ||
} | ||
|
||
|
||
export async function createTray() { | ||
try { | ||
let Tray = await import('npm:ctray') | ||
let tray = new Tray(path.join(__dirname, "/src/public/favicon.ico")); | ||
tray.tooltip = "Fount" | ||
let iconPath = ''; | ||
const platform = os.platform(); | ||
|
||
tray.menu = [ | ||
{ text: "Exit", callback: _ => tray.close() } | ||
] | ||
|
||
tray.on("close", shutdown); | ||
} | ||
catch (err) { | ||
console.error('创建托盘失败:', err) | ||
if (platform === 'win32') { | ||
iconPath = path.join(__dirname, "/src/public/favicon.ico"); | ||
} else { | ||
iconPath = path.join(__dirname, "/src/public/icon.png"); | ||
} | ||
|
||
const base64Icon = await getBase64Icon(iconPath); | ||
|
||
const systray = new SysTray({ | ||
menu: { | ||
icon: base64Icon, | ||
title: "Fount", | ||
tooltip: "Fount", | ||
items: [ | ||
{ | ||
title: "Exit", | ||
tooltip: "Exit application", | ||
checked: false, | ||
enabled: true | ||
} | ||
] | ||
}, | ||
debug: false, | ||
copyDir: true | ||
}); | ||
|
||
|
||
systray.onClick(action => { | ||
if (action.seq_id === 0) { | ||
systray.kill(); | ||
shutdown(); | ||
} | ||
}); | ||
|
||
} catch (err) { | ||
console.error('创建托盘失败:', err); | ||
} | ||
} |