| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- // 使用 CommonJS 格式
- const { app, BrowserWindow, ipcMain, shell, session, Menu, Tray, nativeImage } = require('electron');
- const { join } = require('path');
- const fs = require('fs');
- let mainWindow: typeof BrowserWindow.prototype | null = null;
- let tray: typeof Tray.prototype | null = null;
- let isQuitting = false;
- const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL;
- // 获取图标路径
- function getIconPath() {
- return VITE_DEV_SERVER_URL
- ? join(__dirname, '../public/icons/icon-256.png')
- : join(__dirname, '../dist/icons/icon-256.png');
- }
- // 获取托盘图标路径
- function getTrayIconPath() {
- return VITE_DEV_SERVER_URL
- ? join(__dirname, '../public/icons/tray-icon.png')
- : join(__dirname, '../dist/icons/tray-icon.png');
- }
- // 创建托盘图标
- function createTrayIcon(): typeof nativeImage.prototype {
- const trayIconPath = getTrayIconPath();
- return nativeImage.createFromPath(trayIconPath);
- }
- // 创建系统托盘
- function createTray() {
- const trayIcon = createTrayIcon();
- tray = new Tray(trayIcon);
-
- const contextMenu = Menu.buildFromTemplate([
- {
- label: '显示主窗口',
- click: () => {
- if (mainWindow) {
- mainWindow.show();
- mainWindow.focus();
- }
- }
- },
- {
- label: '最小化到托盘',
- click: () => {
- mainWindow?.hide();
- }
- },
- { type: 'separator' },
- {
- label: '退出',
- click: () => {
- isQuitting = true;
- app.quit();
- }
- }
- ]);
-
- tray.setToolTip('多平台媒体管理系统');
- tray.setContextMenu(contextMenu);
-
- // 点击托盘图标显示窗口
- tray.on('click', () => {
- if (mainWindow) {
- if (mainWindow.isVisible()) {
- mainWindow.focus();
- } else {
- mainWindow.show();
- mainWindow.focus();
- }
- }
- });
-
- // 双击托盘图标显示窗口
- tray.on('double-click', () => {
- if (mainWindow) {
- mainWindow.show();
- mainWindow.focus();
- }
- });
- }
- function createWindow() {
- // 隐藏默认菜单栏
- Menu.setApplicationMenu(null);
-
- const iconPath = getIconPath();
- mainWindow = new BrowserWindow({
- width: 1400,
- height: 900,
- minWidth: 1200,
- minHeight: 700,
- icon: iconPath,
- webPreferences: {
- preload: join(__dirname, 'preload.js'),
- nodeIntegration: false,
- contextIsolation: true,
- webviewTag: true, // 启用 webview 标签
- },
- frame: false, // 无边框窗口,自定义标题栏
- transparent: false,
- backgroundColor: '#f0f2f5',
- show: false,
- });
- // 窗口准备好后再显示,避免白屏
- mainWindow.once('ready-to-show', () => {
- mainWindow?.show();
- setupWindowEvents();
- });
- // 加载页面
- if (VITE_DEV_SERVER_URL) {
- mainWindow.loadURL(VITE_DEV_SERVER_URL);
- mainWindow.webContents.openDevTools();
- } else {
- mainWindow.loadFile(join(__dirname, '../dist/index.html'));
- }
- // 处理外部链接
- mainWindow.webContents.setWindowOpenHandler(({ url }: { url: string }) => {
- shell.openExternal(url);
- return { action: 'deny' };
- });
- // 关闭按钮默认最小化到托盘
- mainWindow.on('close', (event: Event) => {
- if (!isQuitting) {
- event.preventDefault();
- mainWindow?.hide();
-
- // 显示托盘通知(仅首次)
- if (tray && !app.isPackaged) {
- // 开发模式下可以显示通知
- }
- }
- });
- mainWindow.on('closed', () => {
- mainWindow = null;
- });
- }
- // 单实例锁定
- const gotTheLock = app.requestSingleInstanceLock();
- if (!gotTheLock) {
- app.quit();
- } else {
- app.on('second-instance', () => {
- if (mainWindow) {
- mainWindow.show();
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.focus();
- }
- });
- app.whenReady().then(() => {
- createTray();
- createWindow();
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- } else if (mainWindow) {
- mainWindow.show();
- }
- });
- });
- }
- // 阻止默认的 window-all-closed 行为,保持托盘运行
- app.on('window-all-closed', () => {
- // 不退出应用,保持托盘运行
- // 只有在 isQuitting 为 true 时才真正退出
- });
- // 应用退出前清理托盘
- app.on('before-quit', () => {
- isQuitting = true;
- });
- app.on('quit', () => {
- if (tray) {
- tray.destroy();
- tray = null;
- }
- });
- // IPC 处理
- ipcMain.handle('get-app-version', () => {
- return app.getVersion();
- });
- ipcMain.handle('get-platform', () => {
- return process.platform;
- });
- // 窗口控制
- ipcMain.on('window-minimize', () => {
- mainWindow?.minimize();
- });
- ipcMain.on('window-maximize', () => {
- if (mainWindow?.isMaximized()) {
- mainWindow.unmaximize();
- } else {
- mainWindow?.maximize();
- }
- });
- // 关闭窗口(最小化到托盘)
- ipcMain.on('window-close', () => {
- mainWindow?.hide();
- });
- // 真正退出应用
- ipcMain.on('app-quit', () => {
- isQuitting = true;
- app.quit();
- });
- // 获取窗口最大化状态
- ipcMain.handle('window-is-maximized', () => {
- return mainWindow?.isMaximized() || false;
- });
- // 监听窗口最大化/还原事件,通知渲染进程
- function setupWindowEvents() {
- mainWindow?.on('maximize', () => {
- mainWindow?.webContents.send('window-maximized', true);
- });
- mainWindow?.on('unmaximize', () => {
- mainWindow?.webContents.send('window-maximized', false);
- });
- }
- // 获取 webview 的 cookies
- ipcMain.handle('get-webview-cookies', async (_event: unknown, partition: string, url: string) => {
- try {
- const ses = session.fromPartition(partition);
- const cookies = await ses.cookies.get({ url });
- return cookies;
- } catch (error) {
- console.error('获取 cookies 失败:', error);
- return [];
- }
- });
- // 清除 webview 的 cookies
- ipcMain.handle('clear-webview-cookies', async (_event: unknown, partition: string) => {
- try {
- const ses = session.fromPartition(partition);
- await ses.clearStorageData({ storages: ['cookies'] });
- return true;
- } catch (error) {
- console.error('清除 cookies 失败:', error);
- return false;
- }
- });
|