-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_v0.5.js
More file actions
266 lines (224 loc) · 6.45 KB
/
main_v0.5.js
File metadata and controls
266 lines (224 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const { app, BrowserWindow, dialog, ipcMain, screen } = require('electron');
const path = require('path');
const fs = require('fs-extra');
let mainWindow;
let petWindow;
// 创建主窗口(设置界面)
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 500,
height: 600,
minWidth: 450,
minHeight: 500,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
resizable: true,
title: 'SAI桌宠设置',
show: false // 先不显示,等加载完成后再显示
});
mainWindow.loadFile('settings_v0.3.html');
// 窗口加载完成后显示
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
// 开发模式下打开开发者工具
if (process.argv.includes('--dev')) {
mainWindow.webContents.openDevTools();
}
}
// 创建桌宠窗口
function createPetWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
petWindow = new BrowserWindow({
width: 200,
height: 200,
x: width - 220,
y: height - 220,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
frame: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
resizable: false,
movable: true,
minimizable: false,
maximizable: false,
closable: false
});
petWindow.loadFile('src/pet_v0.3.html');
// 确保窗口始终在最前面
petWindow.setAlwaysOnTop(true, 'screen-saver');
// 窗口失去焦点时保持显示
petWindow.on('blur', () => {
petWindow.setAlwaysOnTop(true, 'screen-saver');
});
}
// 应用准备就绪
app.whenReady().then(() => {
createMainWindow();
createPetWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
createPetWindow();
}
});
});
// 所有窗口关闭时退出应用
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// 处理文件上传
ipcMain.handle('upload-image', async (event, filePath) => {
try {
const assetsDir = path.join(__dirname, '..', 'assets', 'custom');
await fs.ensureDir(assetsDir);
const fileName = path.basename(filePath);
const destPath = path.join(assetsDir, fileName);
// 复制文件到自定义贴图目录
await fs.copyFile(filePath, destPath);
// 通知桌宠窗口更新贴图
if (petWindow) {
petWindow.webContents.send('image-uploaded', fileName);
}
return { success: true, fileName: fileName };
} catch (error) {
console.error('文件上传失败:', error);
return { success: false, error: error.message };
}
});
// 获取自定义贴图列表
ipcMain.handle('get-custom-images', async () => {
try {
const assetsDir = path.join(__dirname, '..', 'assets', 'custom');
await fs.ensureDir(assetsDir);
const files = await fs.readdir(assetsDir);
const imageFiles = files.filter(file =>
file.toLowerCase().endsWith('.png')
);
return imageFiles;
} catch (error) {
console.error('获取自定义贴图失败:', error);
return [];
}
});
// 选择文件对话框
ipcMain.handle('select-image-file', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'PNG图片', extensions: ['png'] }
]
});
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
}
return null;
});
// 关闭桌宠窗口
ipcMain.handle('close-pet', () => {
if (petWindow) {
petWindow.close();
}
});
// 显示桌宠窗口 - 解决点击开始后桌宠重新显示的问题
ipcMain.handle('show-pet', () => {
if (petWindow) {
petWindow.show();
} else {
createPetWindow();
}
});
// 隐藏桌宠窗口
ipcMain.handle('hide-pet', () => {
if (petWindow) {
petWindow.hide();
}
});
// 移动桌宠窗口
ipcMain.handle('move-pet-window', (event, deltaX, deltaY) => {
if (petWindow) {
const [x, y] = petWindow.getPosition();
petWindow.setPosition(x + deltaX, y + deltaY);
}
});
// 获取桌宠窗口位置
ipcMain.handle('get-pet-position', () => {
if (petWindow) {
return petWindow.getPosition();
}
return [0, 0];
});
// 获取桌宠窗口位置(用于边界检测)
ipcMain.handle('get-pet-window-position', () => {
if (petWindow) {
return petWindow.getPosition();
}
return [0, 0];
});
// 设置桌宠窗口位置(用于边界检测)
ipcMain.handle('set-pet-window-position', (event, x, y) => {
if (petWindow) {
petWindow.setPosition(x, y);
return { success: true };
}
return { success: false, error: '桌宠窗口不存在' };
});
// 通知桌宠重新加载贴图
ipcMain.handle('reload-pet-images', () => {
if (petWindow) {
petWindow.webContents.send('reload-images');
return true;
}
return false;
});
// 删除自定义贴图
ipcMain.handle('delete-image', async (event, fileName) => {
try {
const assetsDir = path.join(__dirname, '..', 'assets', 'custom');
const filePath = path.join(assetsDir, fileName);
if (fs.existsSync(filePath)) {
await fs.unlink(filePath);
// 通知桌宠窗口更新贴图
if (petWindow) {
petWindow.webContents.send('image-deleted', fileName);
}
return { success: true, fileName: fileName };
}
return { success: false, error: '文件不存在' };
} catch (error) {
console.error('删除文件失败:', error);
return { success: false, error: error.message };
}
});
// 获取桌宠窗口引用
ipcMain.handle('get-pet-window', () => {
return petWindow !== null && !petWindow.isDestroyed();
});
// 重置桌宠位置到屏幕中央
ipcMain.handle('reset-pet-position', () => {
if (petWindow) {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
const windowWidth = petWindow.getBounds().width;
const windowHeight = petWindow.getBounds().height;
// 计算屏幕中央位置
const centerX = Math.floor((width - windowWidth) / 2);
const centerY = Math.floor((height - windowHeight) / 2);
// 移动窗口到中央位置
petWindow.setPosition(centerX, centerY);
return { success: true };
}
return { success: false, error: '桌宠窗口不存在' };
});
// 新增:获取屏幕尺寸(用于前端统一图片大小)
ipcMain.handle('get-screen-size', () => {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
return { width, height };
});