From 731f18c57eb62824760f3f845659f3b57924e979 Mon Sep 17 00:00:00 2001 From: yuantao Date: Wed, 10 Sep 2025 18:23:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + index.html | 125 ++++++++++++++++++++++++++++++++++++++++++++++ main.js | 125 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 6 +++ package.json | 44 ++++++++++++++++ 5 files changed, 301 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 main.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..bf21ddf --- /dev/null +++ b/index.html @@ -0,0 +1,125 @@ + + + + + 性能监控 + + + +
+
性能监控
+
+ + +
+
+ +
+
CPU: 0%
+
内存: 0%
+
网络: 0 KB/s
+
+ +
+ +
+ + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..48a73c0 --- /dev/null +++ b/main.js @@ -0,0 +1,125 @@ +const { app, BrowserWindow, Tray, Menu, nativeImage, ipcMain } = require('electron'); +const path = require('path'); +const AutoLaunch = require('auto-launch'); + +// 保持对窗口对象的全局引用,如果不这样做,窗口将会在JavaScript垃圾回收时自动关闭 +let mainWindow; +let tray = null; + +// 创建开机启动管理器 +let autoLauncher = new AutoLaunch({ + name: 'Motioner', + path: process.execPath, +}); + +// 创建窗口的函数 +function createWindow() { + // 创建浏览器窗口 + mainWindow = new BrowserWindow({ + width: 300, + height: 200, + webPreferences: { + nodeIntegration: true, + contextIsolation: false + }, + // 创建无边框窗口 + frame: false, + // 保持窗口始终在最前面 + alwaysOnTop: true, + // 窗口透明 + transparent: true + }); + + // 加载应用的index.html + mainWindow.loadFile('index.html'); + + // 处理最小化事件 + ipcMain.on('window-minimize', () => { + mainWindow.hide(); + }); + + // 处理关闭事件 + ipcMain.on('window-close', () => { + app.quit(); + }); + + // 处理开机启动设置事件 + ipcMain.on('set-auto-launch', (event, enable) => { + if (enable) { + autoLauncher.enable().then(() => { + console.log('开机启动已启用'); + }).catch(err => { + console.error('启用开机启动失败:', err); + }); + } else { + autoLauncher.disable().then(() => { + console.log('开机启动已禁用'); + }).catch(err => { + console.error('禁用开机启动失败:', err); + }); + } + }); + + // 当窗口关闭时触发 + mainWindow.on('closed', function () { + // 取消对窗口对象的引用,通常会存储窗口在数组中,这是删除相应元素的时候 + mainWindow = null; + }); +} + +// 当Electron完成初始化并准备创建浏览器窗口时调用此方法 +app.whenReady().then(() => { + createWindow(); + + // 创建系统托盘 + createTray(); + + app.on('activate', function () { + // 在macOS上,当单击dock图标并且没有其他窗口打开时,通常在应用程序中重新创建一个窗口 + if (BrowserWindow.getAllWindows().length === 0) createWindow(); + }); +}); + +// 当所有窗口都关闭时退出应用 +app.on('window-all-closed', function () { + // 在macOS上,应用程序及其菜单栏通常会保持活动状态,直到用户明确退出 + if (process.platform !== 'darwin') app.quit(); +}); + +// 创建系统托盘 +function createTray() { + // 创建托盘图标 + const iconPath = path.join(__dirname, 'assets/icon.png'); + let icon; + try { + icon = nativeImage.createFromPath(iconPath); + } catch (error) { + // 如果找不到图标文件,使用默认图标 + icon = nativeImage.createEmpty(); + } + + tray = new Tray(icon); + + // 创建上下文菜单 + const contextMenu = Menu.buildFromTemplate([ + { + label: '显示', + click: () => { + mainWindow.show(); + } + }, + { + label: '退出', + click: () => { + app.quit(); + } + } + ]); + + tray.setContextMenu(contextMenu); + + // 点击托盘图标显示窗口 + tray.on('click', () => { + mainWindow.show(); + }); +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..97b7b7e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "motioner", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..1b319b5 --- /dev/null +++ b/package.json @@ -0,0 +1,44 @@ +{ + "name": "motioner", + "version": "0.0.1", + "description": "基于Electron构建的性能监控应用,功能包括全局悬浮窗口、可以最小化到托盘并保持后台运行、可以设置是否开机启动", + "main": "main.js", + "scripts": { + "start": "electron .", + "test": "echo \"Error: no test specified\" && exit 1", + "postinstall": "electron-builder install-app-deps", + "pack": "electron-builder --dir", + "dist": "electron-builder" + }, + "repository": { + "type": "git", + "url": "https://git.pandorastudio.cn/yuantao/motioner.git" + }, + "author": "袁涛", + "license": "ISC", + "keywords": [], + "dependencies": { + "auto-launch": "*" + }, + "devDependencies": { + "electron": "*", + "electron-builder": "*" + }, + "build": { + "appId": "com.pandorastudio.motioner", + "productName": "Motioner", + "directories": { + "output": "dist" + }, + "win": { + "target": [ + "nsis" + ], + "artifactName": "${productName}-Setup-${version}.${ext}" + }, + "nsis": { + "oneClick": false, + "allowToChangeInstallationDirectory": true + } + } +}