Skip to content

Commit

Permalink
fix tray
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jan 29, 2025
1 parent 96c7989 commit 2e1a9e0
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions src/server/tray.mjs
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);
}
}

0 comments on commit 2e1a9e0

Please sign in to comment.