-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathmain.js
More file actions
242 lines (211 loc) · 7.42 KB
/
main.js
File metadata and controls
242 lines (211 loc) · 7.42 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
const { app, globalShortcut, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const { spawn } = require("child_process");
// 导入日志管理器
const LogManager = require("./src/helpers/logManager");
// 初始化日志管理器
const logger = new LogManager();
// 添加全局错误处理
process.on("uncaughtException", (error) => {
logger.error("Uncaught Exception:", error);
if (error.code === "EPIPE") {
return;
}
logger.error("Error stack:", error.stack);
});
process.on("unhandledRejection", (reason, promise) => {
logger.error("Unhandled Rejection at:", { promise, reason });
});
// 导入助手模块
const EnvironmentManager = require("./src/helpers/environment");
const WindowManager = require("./src/helpers/windowManager");
const DatabaseManager = require("./src/helpers/database");
const ClipboardManager = require("./src/helpers/clipboard");
const FunASRManager = require("./src/helpers/funasrManager");
const TrayManager = require("./src/helpers/tray");
const HotkeyManager = require("./src/helpers/hotkeyManager");
const IPCHandlers = require("./src/helpers/ipcHandlers");
// 设置生产环境PATH
function setupProductionPath() {
logger.info('设置生产环境PATH', {
platform: process.platform,
nodeEnv: process.env.NODE_ENV,
currentPath: process.env.PATH
});
if (process.platform === 'darwin' && process.env.NODE_ENV !== 'development') {
const commonPaths = [
'/usr/local/bin',
'/opt/homebrew/bin',
'/usr/bin',
'/bin',
'/usr/sbin',
'/sbin',
'/Library/Frameworks/Python.framework/Versions/3.12/bin',
'/Library/Frameworks/Python.framework/Versions/3.11/bin',
'/Library/Frameworks/Python.framework/Versions/3.10/bin',
'/Library/Frameworks/Python.framework/Versions/3.9/bin',
'/Library/Frameworks/Python.framework/Versions/3.8/bin',
// 添加更多可能的Python路径
'/opt/homebrew/opt/python@3.11/bin',
'/opt/homebrew/opt/python@3.10/bin',
'/opt/homebrew/opt/python@3.9/bin',
'/usr/local/opt/python@3.11/bin',
'/usr/local/opt/python@3.10/bin',
'/usr/local/opt/python@3.9/bin'
];
const currentPath = process.env.PATH || '';
const pathsToAdd = commonPaths.filter(p => !currentPath.includes(p));
if (pathsToAdd.length > 0) {
const newPath = `${currentPath}:${pathsToAdd.join(':')}`;
process.env.PATH = newPath;
logger.info('PATH已更新', {
添加的路径: pathsToAdd,
新PATH: newPath
});
} else {
logger.info('PATH无需更新,所有路径已存在');
}
} else if (process.platform === 'win32' && process.env.NODE_ENV !== 'development') {
// Windows平台的Python路径设置
const commonPaths = [
'C:\\Python311\\Scripts',
'C:\\Python311',
'C:\\Python310\\Scripts',
'C:\\Python310',
'C:\\Python39\\Scripts',
'C:\\Python39',
'C:\\Users\\' + require('os').userInfo().username + '\\AppData\\Local\\Programs\\Python\\Python311\\Scripts',
'C:\\Users\\' + require('os').userInfo().username + '\\AppData\\Local\\Programs\\Python\\Python311',
'C:\\Users\\' + require('os').userInfo().username + '\\AppData\\Local\\Programs\\Python\\Python310\\Scripts',
'C:\\Users\\' + require('os').userInfo().username + '\\AppData\\Local\\Programs\\Python\\Python310'
];
const currentPath = process.env.PATH || '';
const pathsToAdd = commonPaths.filter(p => !currentPath.includes(p));
if (pathsToAdd.length > 0) {
const newPath = `${currentPath};${pathsToAdd.join(';')}`;
process.env.PATH = newPath;
logger.info('Windows PATH已更新', {
添加的路径: pathsToAdd,
新PATH: newPath
});
}
}
}
// 在初始化管理器之前设置PATH
setupProductionPath();
// 设置用户数据目录环境变量,供Python脚本使用
process.env.ELECTRON_USER_DATA = app.getPath('userData');
logger.info('设置用户数据目录环境变量', {
ELECTRON_USER_DATA: process.env.ELECTRON_USER_DATA
});
// 初始化管理器
const environmentManager = new EnvironmentManager();
const windowManager = new WindowManager();
const databaseManager = new DatabaseManager();
const clipboardManager = new ClipboardManager(logger); // 传递logger实例
const funasrManager = new FunASRManager(logger); // 传递logger实例
const trayManager = new TrayManager();
const hotkeyManager = new HotkeyManager();
// 初始化数据库
const dataDirectory = environmentManager.ensureDataDirectory();
databaseManager.initialize(dataDirectory);
// 使用所有管理器初始化IPC处理器
const ipcHandlers = new IPCHandlers({
environmentManager,
databaseManager,
clipboardManager,
funasrManager,
windowManager,
hotkeyManager,
logger, // 传递logger实例
});
// 主应用启动函数
async function startApp() {
logger.info('应用启动开始', {
nodeEnv: process.env.NODE_ENV,
platform: process.platform,
arch: process.arch,
electronVersion: process.versions.electron,
appVersion: app.getVersion()
});
// 注释掉 accessibility 支持 - 可能干扰文本插入
// try {
// app.setAccessibilitySupportEnabled(true);
// logger.info('✅ 已启用 Electron accessibility 支持');
// } catch (error) {
// logger.warn('⚠️ 启用 accessibility 支持失败:', error.message);
// }
// 记录系统信息
logger.info('系统信息', logger.getSystemInfo());
// 开发模式下添加小延迟让Vite正确启动
if (process.env.NODE_ENV === "development") {
logger.info('开发模式,等待Vite启动...');
await new Promise((resolve) => setTimeout(resolve, 2000));
}
// 确保macOS上dock可见
if (process.platform === 'darwin' && app.dock) {
app.dock.show();
logger.info('macOS Dock已显示');
}
// 在启动时初始化FunASR管理器(不等待以避免阻塞)
logger.info('开始初始化FunASR管理器...');
funasrManager.initializeAtStartup().catch((err) => {
logger.warn("FunASR在启动时不可用,这不是关键问题", err);
});
// 创建主窗口
try {
logger.info('创建主窗口...');
await windowManager.createMainWindow();
logger.info('主窗口创建成功');
} catch (error) {
logger.error("创建主窗口时出错:", error);
}
// 创建控制面板窗口
try {
logger.info('创建控制面板窗口...');
await windowManager.createControlPanelWindow();
logger.info('控制面板窗口创建成功');
} catch (error) {
logger.error("创建控制面板窗口时出错:", error);
}
// 设置托盘
logger.info('设置系统托盘...');
trayManager.setWindows(
windowManager.mainWindow,
windowManager.controlPanelWindow
);
trayManager.setCreateControlPanelCallback(() =>
windowManager.createControlPanelWindow()
);
await trayManager.createTray();
logger.info('系统托盘设置完成');
logger.info('应用启动完成');
}
// 应用事件处理器
app.whenReady().then(() => {
startApp();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
windowManager.createMainWindow();
}
});
app.on("will-quit", () => {
globalShortcut.unregisterAll();
});
// 导出管理器供其他模块使用
module.exports = {
environmentManager,
windowManager,
databaseManager,
clipboardManager,
funasrManager,
trayManager,
hotkeyManager,
logger
};