Compare commits

..

10 Commits

29 changed files with 12589 additions and 0 deletions

144
.gitignore vendored Normal file
View File

@ -0,0 +1,144 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
core/*build*/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.*
!.env.example
.DS_Store
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist/
renderer/dist/
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Sveltekit cache directory
.svelte-kit/
# vitepress build output
**/.vitepress/dist
# vitepress cache directory
**/.vitepress/cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# Firebase cache directory
.firebase/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
.vscode/
.idea/
# yarn v3
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
# Vite logs files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*

BIN
build/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

102
main/main.js Normal file
View File

@ -0,0 +1,102 @@
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const { spawn } = require('child_process')
// 音效播放器
function playAudioFile(filePath) {
if (process.platform === 'win32') {
spawn('cmd', ['/c', `start "" "${filePath}"`])
} else if (process.platform === 'darwin') {
spawn('afplay', [filePath])
} else {
spawn('aplay', [filePath])
}
}
ipcMain.on('play-sound', (_, soundFile) => {
let soundPath;
// 判断是开发环境还是生产环境
if (process.env.NODE_ENV === 'development') {
// 在开发模式下,直接指向 renderer/public 里的文件
soundPath = path.join(__dirname, '../renderer/public/assets/sounds', soundFile);
} else {
// 在生产模式下Vite 会把 public 里的文件复制到 dist 文件夹
soundPath = path.join(__dirname, '../renderer/dist/assets/sounds', soundFile);
}
console.log('Main process trying to play sound at path:', soundPath);
if (require('fs').existsSync(soundPath)) {
playAudioFile(soundPath);
} else {
console.error('Sound file not found:', soundPath);
}
});
ipcMain.handle('get-sound-path', (_, soundFile) => {
let soundPath;
if (process.env.NODE_ENV === 'development') {
soundPath = path.join(__dirname, '../renderer/public/assets/sounds', soundFile);
} else {
soundPath = path.join(__dirname, '../renderer/dist/assets/sounds', soundFile);
}
if (require('fs').existsSync(soundPath)) {
// 返回一个可供 web 环境使用的 file 协议 URL
return `file://${soundPath}`;
} else {
console.error('Sound file not found:', soundPath);
return null;
}
});
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 300,
height: 300,
transparent: true,
frame: false,
resizable: false, // 固定大小
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
webSecurity: false, // 信任应用,并允许加载本地资源
},
});
// 开发模式加载Vite服务器
if (process.env.NODE_ENV === "development") {
mainWindow.loadURL("http://localhost:5173");
} else {
mainWindow.loadFile(path.join(__dirname, "../renderer/dist/index.html"));
}
// 窗口拖拽功能
let isDragging = false;
mainWindow.webContents.on("before-input-event", (_, input) => {
if (input.type === "mouseDown") {
isDragging = true;
mainWindow.webContents.executeJavaScript(`
window.dragOffset = { x: ${input.x}, y: ${input.y} }
`);
} else if (input.type === "mouseUp") {
isDragging = false;
}
});
mainWindow.on("moved", () => {
if (isDragging) {
mainWindow.webContents.executeJavaScript(`
window.electronAPI.updatePosition()
`);
}
const [x, y] = mainWindow.getPosition();
mainWindow.webContents.send("update-position", { x, y });
});
}
app.whenReady().then(createWindow);

16
main/preload.js Normal file
View File

@ -0,0 +1,16 @@
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
playSound: (soundFile) => {
try {
ipcRenderer.send('play-sound', soundFile)
} catch (err) {
console.error('播放音效失败:', err)
}
},
getSoundPath: (soundFile) => ipcRenderer.invoke('get-sound-path', soundFile),
showTooltip: (text) => ipcRenderer.send('show-tooltip', text),
onUpdatePosition: (callback) => {
ipcRenderer.on('update-position', (_, position) => callback(position))
}
})

6208
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

74
package.json Normal file
View File

@ -0,0 +1,74 @@
{
"name": "shuodedaoli-deskpet",
"version": "0.1.0",
"description": "A cute desktop pet of 'Shuodedaoli' built with Electron and Vue 3.",
"main": "main/main.js",
"scripts": {
"dev": "concurrently \"cd renderer && npm run dev\" \"wait-on http://localhost:5173 && cross-env NODE_ENV=development electron .\"",
"build": "npm run build:renderer && electron-builder",
"start": "electron .",
"build:renderer": "cd renderer && npm run build",
"build:win": "npm run build:renderer && electron-builder --win",
"build:linux": "npm run build:renderer && electron-builder --linux",
"build:all": "npm run build:renderer && electron-builder --win --linux"
},
"build": {
"appId": "com.kisechan.deskpet",
"productName": "说的道理桌面宠物",
"copyright": "Copyright © 2025 Kisechan",
"directories": {
"output": "out"
},
"files": [
"main/",
"renderer/dist/",
"package.json"
],
"win": {
"target": "nsis",
"icon": "build/icon.png"
},
"mac": {
"target": "dmg",
"icon": "build/icon.png"
},
"linux": {
"target": "AppImage",
"icon": "build/icon.png"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kisechan/Shuodedaoli-Deskpet.git.git"
},
"keywords": [
"electron",
"vue3",
"desktop-pet",
"desktop-widget",
"shuodedaoli",
"meme",
"fun-project",
"opensource"
],
"author": "Kisechan",
"license": "MIT",
"bugs": {
"url": "https://github.com/Kisechan/Shuodedaoli-Deskpet.git/issues"
},
"homepage": "https://github.com/Kisechan/Shuodedaoli-Deskpet.git#readme",
"dependencies": {
"fs-extra": "^11.3.1",
"howler": "^2.2.4",
"vue": "^3.5.18"
},
"devDependencies": {
"@vitejs/plugin-vue": "^6.0.1",
"concurrently": "^9.2.0",
"cross-env": "^10.0.0",
"electron": "^37.2.6",
"electron-builder": "^26.0.12",
"vite": "^7.1.1",
"wait-on": "^8.0.4"
}
}

24
renderer/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

5
renderer/README.md Normal file
View File

@ -0,0 +1,5 @@
# Vue 3 + TypeScript + Vite
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
Learn more about the recommended Project Setup and IDE Support in the [Vue Docs TypeScript Guide](https://vuejs.org/guide/typescript/overview.html#project-setup).

13
renderer/index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

5713
renderer/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
renderer/package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "renderer",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc -b && vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.5.17"
},
"devDependencies": {
"@vitejs/plugin-vue": "^6.0.0",
"@vue/tsconfig": "^0.7.0",
"concurrently": "^9.2.0",
"electron-builder": "^26.0.12",
"typescript": "~5.8.3",
"vite": "^7.0.4",
"vue-tsc": "^2.2.12",
"wait-on": "^8.0.4"
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

118
renderer/src/App.vue Normal file
View File

@ -0,0 +1,118 @@
<script setup>
import { ref, onMounted } from "vue";
import petGif from "./assets/pet.gif";
import { Howl } from 'howler';
// 配置数据
const tooltips = [
"说的道理~",
"尊尼获加",
"为什么不开大!!",
"(凤鸣)",
];
const soundFiles = [
"cnmb.mp3",
"冲刺,冲.mp3",
"哎你怎么死了.mp3",
"哎,猪逼.mp3",
"啊啊啊我草你妈呀.mp3",
"嘟嘟嘟.mp3",
"韭菜盒子.mp3",
"哇袄.mp3"
];
// 状态管理
const showTooltip = ref(false);
const currentTooltip = ref("");
const position = ref({ x: 0, y: 0 });
// 点击事件处理
const handleClick = async () => {
const randomSoundFile = soundFiles[Math.floor(Math.random() * soundFiles.length)];
console.log('请求播放:', randomSoundFile);
try {
const audioUrl = await window.electronAPI?.getSoundPath(randomSoundFile);
if (audioUrl) {
const sound = new Howl({
src: [audioUrl],
format: ['mp3']
});
sound.play();
} else {
console.error('无法获取音频路径:', randomSoundFile);
}
} catch (err) {
console.error('播放失败:', err);
}
currentTooltip.value = tooltips[Math.floor(Math.random() * tooltips.length)];
showTooltip.value = true;
setTimeout(() => (showTooltip.value = false), 2000);
};
// 初始化位置监听
onMounted(() => {
if (window.electronAPI) {
window.electronAPI.onUpdatePosition((pos) => {
position.value = pos;
});
}
});
</script>
<template>
<div
class="pet-container"
:style="{ left: `${position.x}px`, top: `${position.y}px` }"
>
<img :src="petGif" class="pet-gif" @click="handleClick" draggable="false" />
<transition name="fade">
<div v-if="showTooltip" class="tooltip">
{{ currentTooltip }}
</div>
</transition>
</div>
</template>
<style scoped>
.pet-container {
position: absolute;
width: 300px;
height: 300px;
-webkit-app-region: no-drag;
}
.pet-gif {
width: 100%;
height: 100%;
cursor: pointer;
user-select: none;
-webkit-user-drag: none;
}
.tooltip {
position: absolute;
bottom: -40px;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 8px 16px;
border-radius: 20px;
font-size: 14px;
white-space: nowrap;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>

BIN
renderer/src/assets/pet.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 MiB

5
renderer/src/main.ts Normal file
View File

@ -0,0 +1,5 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
createApp(App).mount('#app')

5
renderer/src/shims.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}

79
renderer/src/style.css Normal file
View File

@ -0,0 +1,79 @@
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

1
renderer/src/vite-env.d.ts vendored Normal file
View File

@ -0,0 +1 @@
/// <reference types="vite/client" />

View File

@ -0,0 +1,15 @@
{
"extends": "@vue/tsconfig/tsconfig.dom.json",
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}

7
renderer/tsconfig.json Normal file
View File

@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" }
]
}

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "ES2023",
"lib": ["ES2023"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["vite.config.ts"]
}

11
renderer/vite.config.ts Normal file
View File

@ -0,0 +1,11 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
base: './',
build: {
assetsInlineLimit: 0
// 强制所有资源作为文件输出
}
})