-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (96 loc) · 3.41 KB
/
main.js
File metadata and controls
115 lines (96 loc) · 3.41 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
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const isDev = process.env.APP_ENV === 'development';
const { screen } = require('electron');
// const { autoUpdater } = require('electron-updater');
// const log = require('electron-log');
// 配置日誌
// log.transports.file.level = 'debug';
// autoUpdater.logger = log;
// autoUpdater.autoDownload = true;
// autoUpdater.autoInstallOnAppQuit = true;
// autoUpdater.channel = 'latest';
// 設置應用程序圖標(僅在 macOS 中有效)
if (process.platform === 'darwin') {
app.dock.setIcon(path.join(__dirname, 'assets', 'logo.png'));
}
function createWindow() {
// 獲取所有螢幕
const displays = screen.getAllDisplays();
// 找出最大寬度和高度
let maxWidth = 0;
let maxHeight = 0;
displays.forEach(display => {
maxWidth = Math.max(maxWidth, display.bounds.width);
maxHeight = Math.max(maxHeight, display.bounds.height);
});
// 設定視窗大小為螢幕最大尺寸的 80%
const windowWidth = Math.floor(maxWidth * 1);
const windowHeight = Math.floor(maxHeight * 1);
// 計算視窗位置使其置中
const windowX = 0;
const windowY = 0;
const mainWindow = new BrowserWindow({
width: windowWidth,
height: windowHeight,
x: windowX,
y: windowY,
icon: path.join(__dirname, 'assets', 'logo.png'), // 設置窗口圖標
webPreferences: {
nodeIntegration: false,
contextIsolation: false,
webSecurity: false // 允許跨域請求
}
});
// 載入 index.html
mainWindow.loadFile('index.html');
// 設置 CORS 頭
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
'Access-Control-Allow-Origin': ['*'],
'Access-Control-Allow-Methods': ['GET, POST, PUT, DELETE, OPTIONS'],
'Access-Control-Allow-Headers': ['Content-Type', 'Authorization']
}
});
});
}
app.whenReady().then(() => {
createWindow();
// 只在生產環境檢查更新
if (isDev === true) { return; }
// autoUpdater.checkForUpdatesAndNotify();
// // 監聽更新事件
// autoUpdater.on('error', (err) => {
// log.error('更新錯誤:', err);
// });
// autoUpdater.on('update-available', (info) => {
// log.info('發現新版本:', info);
// });
// autoUpdater.on('update-downloaded', (info) => {
// log.info('更新已下載:', info);
// // 提示用戶重啟應用
// if (mainWindow) {
// mainWindow.webContents.executeJavaScript(`
// if (confirm('新版本已下載完成,是否立即重啟應用?')) {
// require('electron').ipcRenderer.send('restart-app');
// }
// `).catch(err => log.error('執行重啟提示失敗:', err));
// }
// });
// // 監聽重啟請求
// ipcMain.on('restart-app', () => {
// autoUpdater.quitAndInstall();
// });
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});