1
+ import { app , BrowserWindow , globalShortcut , ipcMain , protocol , shell } from 'electron'
2
+ import { release } from 'os'
3
+ import { join } from 'path'
4
+ import Update from "../update" ;
5
+
6
+
7
+
8
+ // Disable GPU Acceleration for Windows 7
9
+ if ( release ( ) . startsWith ( '6.1' ) ) app . disableHardwareAcceleration ( )
10
+
11
+ // Set application name for Windows 10+ notifications
12
+ if ( process . platform === 'win32' ) app . setAppUserModelId ( app . getName ( ) )
13
+
14
+ if ( ! app . requestSingleInstanceLock ( ) ) {
15
+ app . quit ( )
16
+ process . exit ( 0 )
17
+ }
18
+
19
+ process . env [ 'ELECTRON_DISABLE_SECURITY_WARNINGS' ] = 'true'
20
+
21
+ export const ROOT_PATH = {
22
+ // /dist
23
+ dist : join ( __dirname , '../..' ) ,
24
+ // /dist or /public
25
+ public : join ( __dirname , app . isPackaged ? '../..' : '../../../public' ) ,
26
+ }
27
+
28
+ let win : BrowserWindow | null = null
29
+ // Here, you can also use other preload
30
+ const preload = join ( __dirname , '../preload/index.js' )
31
+ log . info ( preload )
32
+ // 🚧 Use ['ENV_NAME'] avoid vite:define plugin
33
+ const url = `http://${ process . env [ 'VITE_DEV_SERVER_HOST' ] } :${ process . env [ 'VITE_DEV_SERVER_PORT' ] } `
34
+ const indexHtml = join ( ROOT_PATH . dist , 'index.html' )
35
+ // 禁用当前应用程序的硬件加速
36
+ app . disableHardwareAcceleration ( )
37
+ async function createWindow ( ) {
38
+ win = new BrowserWindow ( {
39
+ height : 800 ,
40
+ width : 1200 ,
41
+ frame : false , //添加这一行采用无边框窗口
42
+ icon : join ( ROOT_PATH . public , 'favicon.ico' ) ,
43
+ transparent : false , //透明 为true会影响页面缩放
44
+ resizable : true , //可否缩放 为false会影响页面缩放
45
+ movable : true , //可否移动 为false会影响页面缩放
46
+ webPreferences : {
47
+ preload,
48
+ webSecurity : false , //允许跨域请求
49
+ javascript : true ,
50
+ plugins : true ,
51
+ nodeIntegration : true , // 是否集成 Nodejs
52
+ contextIsolation : false ,
53
+ webviewTag : true
54
+ } ,
55
+ } )
56
+ //注册全局快捷键
57
+ globalShortcut . register ( 'ctrl+l' , ( ) => {
58
+ win ?. webContents . send ( 'key-event-l' )
59
+ } )
60
+ win . on ( 'maximize' , ( ) => {
61
+ win . webContents . send ( 'WINDOW_MAXIMIZE' , true )
62
+ } )
63
+ win . on ( 'unmaximize' , ( ) => {
64
+ win . webContents . send ( 'WINDOW_MAXIMIZE' , false )
65
+ } )
66
+
67
+ // win.setMenu(null)
68
+ if ( app . isPackaged ) {
69
+ // win.setMenu(null)
70
+ win . loadFile ( indexHtml )
71
+ } else {
72
+ win . loadURL ( url )
73
+
74
+ }
75
+
76
+
77
+
78
+ // Test actively push message to the Electron-Renderer
79
+ win . webContents . on ( 'did-finish-load' , ( ) => {
80
+ win ?. webContents . send ( 'main-process-message' , new Date ( ) . toLocaleString ( ) )
81
+ } )
82
+
83
+ // Make all links open with the browser, not with the application
84
+ win . webContents . setWindowOpenHandler ( ( { url } ) => {
85
+ if ( url . startsWith ( 'https:' ) ) shell . openExternal ( url )
86
+ return { action : 'deny' }
87
+ } )
88
+ //引用本地图片问题https://blog.csdn.net/Takayamaaren/article/details/107289994
89
+ protocol . interceptFileProtocol ( 'file' , ( req , callback ) => {
90
+ const url = req . url . substr ( 8 ) ;
91
+ callback ( url ) ;
92
+ } ) ;
93
+ }
94
+
95
+ app . whenReady ( ) . then ( ( ) => {
96
+ createWindow ( )
97
+ //自动更新
98
+ Update ( win )
99
+ } )
100
+
101
+ //主进程错误
102
+ process . on ( 'uncaughtException' , function ( error ) {
103
+ console . log ( error )
104
+ } )
105
+ app . on ( 'window-all-closed' , ( ) => {
106
+ win = null
107
+ if ( process . platform !== 'darwin' ) app . quit ( )
108
+ } )
109
+
110
+ app . on ( 'second-instance' , ( ) => {
111
+ if ( win ) {
112
+ // Focus on the main window if the user tried to open another
113
+ if ( win . isMinimized ( ) ) win . restore ( )
114
+ win . focus ( )
115
+ }
116
+ } )
117
+
118
+ app . on ( 'activate' , ( ) => {
119
+ const allWindows = BrowserWindow . getAllWindows ( )
120
+ if ( allWindows . length ) {
121
+ allWindows [ 0 ] . focus ( )
122
+ } else {
123
+ createWindow ( )
124
+
125
+ }
126
+ } )
127
+
128
+ // new window example arg: new windows url
129
+ ipcMain . handle ( 'open-win' , ( event , arg ) => {
130
+ const childWindow = new BrowserWindow ( {
131
+ webPreferences : {
132
+ preload,
133
+ } ,
134
+ } )
135
+
136
+ if ( app . isPackaged ) {
137
+ childWindow . loadFile ( indexHtml , { hash : arg } )
138
+ } else {
139
+ childWindow . loadURL ( `${ url } /#${ arg } ` )
140
+ // childWindow.webContents.openDevTools({ mode: "undocked", activate: true })
141
+ }
142
+ } )
143
+
144
+
145
+ ipcMain . on ( "read-content" , ( event , url ) => {
146
+ //通知content.vue渲染
147
+ win ?. webContents . send ( 'read-content-done' , url )
148
+ } )
149
+ ipcMain . on ( "update-item-list" , ( event , ...args ) => {
150
+
151
+ //通知content.vue渲染
152
+ win ?. webContents . send ( 'update-item-list-done' , args )
153
+ } )
154
+
155
+
156
+ ipcMain . on ( 'refresh-sub-list' , ( event , url ) => {
157
+ win ?. webContents . send ( 'refresh-sub-list-done' )
158
+ } )
159
+ // @ts -ignore
160
+ import { ChannelMessage } from "../../src/domain/enum"
161
+
162
+ ipcMain . on ( ChannelMessage . WINDOW_OPERATION , ( event , operation ) => {
163
+ switch ( operation ) {
164
+ case 'minimize' :
165
+ win . minimize ( ) ;
166
+ break
167
+ case 'maximize' :
168
+ if ( win . isMaximized ( ) ) {
169
+ win . restore ( ) ;
170
+ } else {
171
+ win . maximize ( ) ;
172
+ }
173
+ break
174
+ case 'close' :
175
+ win . destroy ( )
176
+ app . quit ( ) ;
177
+ }
178
+ } )
179
+
180
+ /**
181
+ * 菜单-更新检查
182
+ */
183
+ ipcMain . on ( ChannelMessage . CHECK_UPDATES , ( ) => {
184
+ // const path = require("path")
185
+ // //测试环境下使用自动更新
186
+ // Object.defineProperty(app,"isPackaged",{
187
+ // get(){
188
+ // return true
189
+ // }
190
+ // })
191
+ // if (app.isPackaged) {
192
+ // autoUpdater.updateConfigPath = path.join(__dirname, '../../../config/dev-app-update.yml');
193
+ // }
194
+
195
+ autoUpdater . checkForUpdatesAndNotify ( )
196
+
197
+ } )
198
+
199
+ import log from './../../src/util/log'
200
+ import { autoUpdater } from "electron-updater" ;
201
+ /**
202
+ * 菜单-应用设置
203
+ */
204
+ ipcMain . on ( ChannelMessage . APP_SETTINGS_DIALOG , ( ) => {
205
+
206
+ win . webContents . send ( ChannelMessage . APP_SETTINGS_DIALOG_DONE )
207
+ } )
208
+ ipcMain . on ( 'open-dev-tool' , ( ) => {
209
+ win . webContents . openDevTools ( )
210
+ } )
0 commit comments