# twinkle-settings **Repository Path**: iplayaball/twinkle-settings ## Basic Information - **Project Name**: twinkle-settings - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-04-20 - **Last Updated**: 2026-05-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: Electron ## README # 项目介绍 显示多种天气的twinkle工具的定时调整显示器亮度的配置 页面点击切换天气,更新twinkle的定时配置 # todo # 技术 ## 页面绑定快捷键 App.vue 页面绑定 `f5` 刷新页面 # 创建项目 ```shell pnpm create vite -t vue twinkle-settings cd twinkle-settings ``` ```shell pnpm add -D electron electron-builder electron-devtools-installer vite-plugin-electron ``` # electron 配置 新建主进程js文件 electron_main.js ```js import {app, BrowserWindow, globalShortcut, Menu} from 'electron' import { fileURLToPath } from 'url' import { dirname, join } from 'path' const __dirname = dirname(fileURLToPath(import.meta.url)) // 屏蔽安全警告 // ectron Security Warning (Insecure Content-Security-Policy) process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true"; let win const createWindow = () => { win = new BrowserWindow({ // 窗口图标 icon: join(__dirname, "resource/shortcut.ico"), // width: 800, // height: 600, frame: false, webPreferences: { nodeIntegration: true, contextIsolation: false, webSecurity: false, // preload: path.join(__dirname, 'preload.js') }, show: false // fullscreen: true }); win.maximize() if (process.env.VITE_DEV_SERVER_URL) { win.loadURL(process.env.VITE_DEV_SERVER_URL); // win.webContents.openDevTools(); } else { win.loadFile(join(__dirname, "dist/index.html")); } createMenu(); }; // 设置菜单栏 function createMenu() { // darwin表示macOS,针对macOS的设置 if (process.platform !== "darwin") { // windows及linux系统 Menu.setApplicationMenu(null); } } app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); app.whenReady().then(() => { // globalShortcut.register("CommandOrControl+Shift+i", function () { // 隐藏菜单栏后,需要在这里注册打开 dev tools 的快捷键 。f12 注册不了,官网说不能注册其它应用已经注册了的快捷键 globalShortcut.register('CommandOrControl+f12', function () { win.webContents.openDevTools(); }); globalShortcut.register('f4', () => { win.close() }) createWindow(); }); app.on("window-all-closed", () => { if (process.platform !== "darwin") app.quit(); }); app.on('will-quit', () => { // 注销快捷键 // globalShortcut.unregister('CommandOrControl+f12') // 注销所有快捷键 globalShortcut.unregisterAll() }) ``` vite.config.js 中引入 vite-plugin-electron 插件,配置主进程入口,将 electron 和 vite 项目结合起来。 vite.config.js 整个内容替换 ```js import {defineConfig} from 'vite' import vue from '@vitejs/plugin-vue' import electron from 'vite-plugin-electron' import path from "path"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), electron({ // 主进程入口文件 entry: 'electron_main.js' }) ], /*开发服务器选项*/ server: { // 端口 port: 30000, }, base: "./", resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, }) ``` 修改 package.json "main": "electron_main.js" 加入 # 打包 package.json scripts 里手动添加启动和打包命令 ```json { "electron:serve": "vite --host", "electron:build": "vite build && electron-builder" } ``` package.json 末尾添加 appId 必须填写 output 打包输出目录。如果不填会默认输出到 dist 目录,虽然能打包成功,但是试过打开的界面是空白的 ```json { "build": { "appId": "twinkle-settings", "directories": { "output": "release" } } } ``` .gitignore 添加排除 ```.gitignore release/ dist-electron ``` 试着修改下 App.vue HelloWorld.vue 内容 再运行或者打包看看效果是否正常 # 基础依赖 ## lowdb vue-router element-plus keymaster ```shell pnpm i lowdb@1.0.0 vue-router element-plus @element-plus/icons-vue keymaster ``` ## stylus sass ```shell pnpm i -D stylus sass @types/keymaster ``` ## element-plus 配置 main.ts ```ts import { createApp } from 'vue' import App from './App.vue' import './style.css' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import * as ElementPlusIconsVue from '@element-plus/icons-vue' const app = createApp(App) app.use(ElementPlus) for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component) } app.mount('#app') ``` ## tsconfig.json ```json5 { "compilerOptions": { "target": "ES2020", "useDefineForClassFields": true, "module": "ESNext", "lib": ["ES2020", "DOM", "DOM.Iterable"], "skipLibCheck": true, /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "preserve", /* Linting */ "strict": false, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, // 配置 @ "baseUrl": ".", "paths": { "@/*": ["src/*"] }, "allowSyntheticDefaultImports": true, // 不允许使用隐式的 any 类型 "noImplicitAny": false }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.node.json" }] } ``` ## tsx ```shell pnpm install -D @vitejs/plugin-vue-jsx ``` [template setup 和 tsx 的混合开发实践](https://juejin.cn/post/7282692088016437307#heading-3) ## 打包 打包后 Uncaught Error:Cannot find module'lodash' 删除 node_modules lock.json 后用 npm i 可能尝试也图标时,用了npm,pnpm 和npm 混用出了问题,用了 lowdb ,pnpm 在别的项目打包是没有问题的 修改图标 ```shell npm install -g electron-icon-builder ```