This commit is contained in:
2026-05-21 00:30:05 +08:00
parent 4ba2b730de
commit 18a14111f8
13 changed files with 345 additions and 7 deletions
+1
View File
@@ -1,3 +1,4 @@
package-lock.json package-lock.json
node_modules node_modules
.idea .idea
out
+40
View File
@@ -0,0 +1,40 @@
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
main: {
plugins: [externalizeDepsPlugin()],
build: {
rollupOptions: {
input: {
index: resolve(__dirname, 'src/main/index.ts')
}
}
}
},
preload: {
plugins: [externalizeDepsPlugin()],
build: {
rollupOptions: {
input: {
index: resolve(__dirname, 'src/preload/index.ts')
}
}
}
},
renderer: {
root: 'src/renderer',
build: {
rollupOptions: {
input: resolve(__dirname, 'src/renderer/index.html')
}
},
plugins: [vue()],
resolve: {
alias: {
'@': resolve(__dirname, 'src/renderer/src')
}
}
}
})
+26 -7
View File
@@ -1,18 +1,37 @@
{ {
"name": "electron-counter", "name": "electron-vue-counter",
"version": "1.0.0", "version": "1.0.0",
"description": "Electron app with live counter", "description": "Electron + Vue3 + TypeScript 计数器应用",
"main": "main.js", "main": "./out/main/index.js",
"type": "module",
"scripts": { "scripts": {
"start": "electron ." "dev": "electron-vite dev",
"build": "electron-vite build",
"preview": "electron-vite preview",
"start": "electron .",
"package": "electron-vite build && electron-builder"
},
"dependencies": {
"@electron-toolkit/preload": "^3.0.2",
"@electron-toolkit/utils": "^4.0.0",
"vue": "^3.4.21"
}, },
"devDependencies": { "devDependencies": {
"electron": "^28.3.3" "@vitejs/plugin-vue": "^5.0.4",
"electron": "^28.3.3",
"electron-builder": "^24.13.3",
"electron-vite": "^2.1.0",
"typescript": "^5.4.3",
"vite": "^5.2.6",
"vue-tsc": "^2.0.7"
}, },
"build": { "build": {
"appId": "com.example.electron-counter", "appId": "com.example.electron-vue-counter",
"win": { "win": {
"target": "nsis" "target": "nsis"
} },
"files": [
"out/**/*"
]
} }
} }
+68
View File
@@ -0,0 +1,68 @@
import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
let mainWindow: BrowserWindow | null = null
function createWindow(): void {
if (mainWindow) return
mainWindow = new BrowserWindow({
width: 500,
height: 400,
show: false,
autoHideMenuBar: false,
webPreferences: {
preload: join(__dirname, '../preload/index.mjs'),
sandbox: false,
contextIsolation: true,
nodeIntegration: false
}
})
mainWindow.on('ready-to-show', () => {
mainWindow?.show()
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
app.whenReady().then(() => {
electronApp.setAppUserModelId('com.example.electron-vue-counter')
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
// IPC 处理程序示例
ipcMain.handle('get-platform-info', () => {
return {
platform: process.platform,
arch: process.arch,
version: process.versions.electron
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
+17
View File
@@ -0,0 +1,17 @@
import { ElectronAPI } from '@electron-toolkit/preload'
interface Api {
platform: string
getPlatformInfo: () => Promise<{
platform: string
arch: string
version: string
}>
}
declare global {
interface Window {
electron: ElectronAPI
api: Api
}
}
+21
View File
@@ -0,0 +1,21 @@
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
const api = {
platform: process.platform,
getPlatformInfo: () => ipcRenderer.invoke('get-platform-info')
}
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('api', api)
} catch (error) {
console.error('preload error:', error)
}
} else {
// @ts-ignore
window.electron = electronAPI
// @ts-ignore
window.api = api
}
+29
View File
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>实时计数器</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Arial, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="module" src="./src/main.ts"></script>
</body>
</html>
+46
View File
@@ -0,0 +1,46 @@
<script setup lang="ts">
import Counter from './components/Counter.vue'
</script>
<template>
<div class="container">
<h1>实时计数器</h1>
<Counter />
<div class="info">
<span class="status">运行中</span>
</div>
</div>
</template>
<style>
.container {
text-align: center;
padding: 40px;
background: rgba(255, 255, 255, 0.1);
border-radius: 20px;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
h1 {
font-size: 28px;
margin-bottom: 30px;
color: #00d9ff;
}
.info {
margin-top: 30px;
font-size: 14px;
color: #888;
}
.status {
display: inline-block;
padding: 5px 15px;
background: #00ff88;
color: #1a1a2e;
border-radius: 20px;
font-weight: bold;
font-size: 12px;
}
</style>
+37
View File
@@ -0,0 +1,37 @@
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
const count = ref(0)
let timer: ReturnType<typeof setTimeout> | null = null
function updateCounter(): void {
count.value++
timer = setTimeout(updateCounter, 1000)
}
onMounted(() => {
updateCounter()
console.log('计数器已启动')
})
onUnmounted(() => {
if (timer) {
clearTimeout(timer)
}
})
</script>
<template>
<div class="counter-display">{{ count.toLocaleString() }}</div>
</template>
<style scoped>
.counter-display {
font-size: 72px;
font-weight: bold;
color: #00ff88;
text-shadow: 0 0 20px rgba(0, 255, 136, 0.5);
margin: 20px 0;
font-variant-numeric: tabular-nums;
}
</style>
+4
View File
@@ -0,0 +1,4 @@
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
+19
View File
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src/**/*", "electron.vite/**/*"],
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.web.json" }
]
}
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"composite": true,
"target": "ES2022",
"lib": ["ES2022"],
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"outDir": "./out/types"
},
"include": ["src/main/**/*", "src/preload/**/*", "electron.vite/**/*"]
}
+21
View File
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"composite": true,
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"jsx": "preserve",
"jsxImportSource": "vue",
"outDir": "./out/types",
"paths": {
"@renderer/*": ["./src/renderer/src/*"]
}
},
"include": ["src/renderer/**/*"]
}