1
1
'use strict'
2
2
3
- import { app , protocol , BrowserWindow , ipcMain , Menu , Tray } from 'electron'
3
+ import {
4
+ app ,
5
+ protocol ,
6
+ BrowserWindow ,
7
+ ipcMain ,
8
+ Menu ,
9
+ Tray ,
10
+ shell ,
11
+ Notification
12
+ } from 'electron'
4
13
// import { establishConnection } from './biz/channel/connection'
5
14
const path = require ( 'path' )
6
15
import {
@@ -58,30 +67,9 @@ function createWindow() {
58
67
ipcMain . on ( 'mainWindow:close' , event => {
59
68
mainWindow . hide ( )
60
69
mainWindow . setSkipTaskbar ( true )
70
+ createdNotice ( )
61
71
event . preventDefault ( )
62
72
} )
63
-
64
- // 托盘
65
- let tray = null
66
- //创建系统通知区菜单
67
- tray = new Tray ( path . join ( __dirname , '../public/favicon.ico' ) )
68
- const contextMenu = Menu . buildFromTemplate ( [
69
- {
70
- label : '退出' ,
71
- click : ( ) => {
72
- mainWindow . destroy ( )
73
- }
74
- } //我们需要在这里有一个真正的退出(这里直接强制退出)
75
- ] )
76
- tray . setToolTip ( '加菲猫视频' )
77
- tray . setContextMenu ( contextMenu )
78
- tray . on ( 'click' , ( ) => {
79
- //我们这里模拟桌面程序点击通知区图标实现打开关闭应用的功能
80
- mainWindow . isVisible ( ) ? mainWindow . hide ( ) : mainWindow . show ( )
81
- mainWindow . isVisible ( )
82
- ? mainWindow . setSkipTaskbar ( false )
83
- : mainWindow . setSkipTaskbar ( true )
84
- } )
85
73
}
86
74
87
75
// Quit when all windows are closed.
@@ -101,27 +89,6 @@ app.on('activate', () => {
101
89
}
102
90
} )
103
91
104
- // This method will be called when Electron has finished
105
- // initialization and is ready to create browser windows.
106
- // Some APIs can only be used after this event occurs.
107
- app . on ( 'ready' , async ( ) => {
108
- if ( isDevelopment && ! process . env . IS_TEST ) {
109
- // Install Vue Devtools
110
- // Devtools extensions are broken in Electron 6.0.0 and greater
111
- // See https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/378 for more info
112
- // Electron will not launch with Devtools extensions installed on Windows 10 with dark mode
113
- // If you are not using Windows 10 dark mode, you may uncomment these lines
114
- // In addition, if the linked issue is closed, you can upgrade electron and uncomment these lines
115
- try {
116
- await installVueDevtools ( )
117
- } catch ( e ) {
118
- console . error ( 'Vue Devtools failed to install:' , e . toString ( ) )
119
- }
120
- }
121
- createWindow ( )
122
- // establishConnection()
123
- } )
124
-
125
92
// Exit cleanly on request from parent process in development mode.
126
93
if ( isDevelopment ) {
127
94
if ( process . platform === 'win32' ) {
@@ -136,6 +103,154 @@ if (isDevelopment) {
136
103
} )
137
104
}
138
105
}
139
- let __static = require ( 'path' )
140
- . join ( __dirname , '/static' )
141
- . replace ( / \\ / g, '\\\\' )
106
+
107
+ /**
108
+ * 创建通知
109
+ */
110
+ let minNum = 0
111
+ function createdNotice ( ) {
112
+ if ( minNum > 0 ) return
113
+ minNum ++
114
+ // 通知图标
115
+ const iconNoticePath = `${ __static } /logo.png`
116
+ let notification = new Notification ( {
117
+ title : '加菲猫' , // 通知的标题, 将在通知窗口的顶部显示
118
+ body : '已将加菲猫放入此处,可以在右下角找到我!' , // 通知的正文文本, 将显示在标题或副标题下面
119
+ icon : iconNoticePath , // 用于在该通知上显示的图标
120
+ silent : true // 在显示通知时是否发出系统提示音
121
+ } )
122
+ notification . show ( )
123
+ notification . on ( 'click' , ( ) => {
124
+ notification . close ( )
125
+ } )
126
+ }
127
+
128
+ // 托盘对象
129
+ let appTray = null
130
+ // 是否可以退出
131
+ let trayClose = false
132
+ // 系统托盘右键菜单
133
+ let trayMenuTemplate
134
+ // 系统托盘图标
135
+ let iconPath
136
+ // 图标的上上下文
137
+ let contextMenu
138
+ // 图标闪烁定时器
139
+ let flashTrayTimer
140
+ // 单一实例
141
+ const gotTheLock = app . requestSingleInstanceLock ( )
142
+ /**
143
+ * 设置系统托盘
144
+ */
145
+ function createTray ( ) {
146
+ // 是否可以退出
147
+ trayClose = false
148
+
149
+ // 系统托盘图标
150
+ iconPath = `${ __static } /logo.png`
151
+ let iconMessagePath = `${ __static } /logo.png`
152
+ let iconTransparentPath = `${ __static } /iconTransparent.png`
153
+
154
+ if ( process . platform === 'win32' ) {
155
+ iconPath = `${ __static } /favicon.ico`
156
+ iconMessagePath = `${ __static } /favicon.ico`
157
+ iconTransparentPath = `${ __static } /iconTransparent.ico`
158
+ }
159
+
160
+ // 系统托盘右键菜单
161
+ trayMenuTemplate = [
162
+ {
163
+ label : '崩溃报告测试 process.crash()' ,
164
+ click : function ( ) {
165
+ process . crash ( )
166
+ }
167
+ } ,
168
+ {
169
+ label : '崩溃报告测试throw new Error' ,
170
+ click : function ( ) {
171
+ throw new Error ( 'Error test in main progress' )
172
+ }
173
+ } ,
174
+ {
175
+ label : '托盘闪烁' ,
176
+ click : function ( ) {
177
+ // 判断如果上一个定时器是否执行完
178
+ if ( flashTrayTimer ) {
179
+ return
180
+ }
181
+
182
+ //系统托盘图标闪烁
183
+ appTray . setImage ( iconMessagePath )
184
+ let count = 0
185
+ flashTrayTimer = setInterval ( function ( ) {
186
+ count ++
187
+ if ( count % 2 == 0 ) {
188
+ appTray . setImage ( iconTransparentPath )
189
+ } else {
190
+ appTray . setImage ( iconMessagePath )
191
+ }
192
+ } , 600 )
193
+ }
194
+ } ,
195
+ // {
196
+ // label: '关于项目',
197
+ // click: function() {
198
+ // shell.openExternal('')
199
+ // }
200
+ // },
201
+ {
202
+ label : '退出' ,
203
+ click : function ( ) {
204
+ // 退出
205
+ trayClose = true
206
+ app . quit ( )
207
+ }
208
+ }
209
+ ]
210
+
211
+ appTray = new Tray ( iconPath )
212
+ // 图标的上上下文
213
+ contextMenu = Menu . buildFromTemplate ( trayMenuTemplate )
214
+ // 设置此托盘图标的悬停提示内容
215
+ appTray . setToolTip ( '加菲猫 - for freedom and fun' )
216
+ // 设置此图标的上下文菜单
217
+ appTray . setContextMenu ( contextMenu )
218
+ // 主窗口显示隐藏切换
219
+ appTray . on ( 'click' , ( ) => {
220
+ // 清楚图标闪烁定时器
221
+ clearInterval ( flashTrayTimer )
222
+ flashTrayTimer = null
223
+ // 还原图标
224
+ appTray . setImage ( iconPath )
225
+ mainWindow . isVisible ( ) ? mainWindow . hide ( ) : mainWindow . show ( )
226
+ } )
227
+ }
228
+
229
+ /**
230
+ * 单一实例
231
+ */
232
+ if ( ! gotTheLock ) {
233
+ app . quit ( )
234
+ } else {
235
+ app . on ( 'second-instance' , ( event , commandLine , workingDirectory ) => {
236
+ // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
237
+ if ( mainWindow ) {
238
+ if ( mainWindow . isMinimized ( ) ) mainWindow . restore ( )
239
+ mainWindow . focus ( )
240
+ mainWindow . show ( )
241
+ }
242
+ } )
243
+
244
+ // 创建 mainWindow, 加载应用的其余部分, etc...
245
+ app . on ( 'ready' , async ( ) => {
246
+ if ( isDevelopment && ! process . env . IS_TEST ) {
247
+ try {
248
+ await installVueDevtools ( )
249
+ } catch ( e ) {
250
+ console . error ( 'Vue Devtools failed to install:' , e . toString ( ) )
251
+ }
252
+ }
253
+ createWindow ( )
254
+ createTray ( )
255
+ } )
256
+ }
0 commit comments