Skip to content

Commit dce5f3d

Browse files
author
unknown
committed
完善单例&图标&通知等
1 parent 72a5a1e commit dce5f3d

File tree

22 files changed

+164
-1894
lines changed

22 files changed

+164
-1894
lines changed

public/iconTransparent.ico

51.1 KB
Binary file not shown.

public/iconTransparent.png

15.2 KB
Loading

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
77
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
8-
<title>electron-vue-test</title>
8+
<title>加菲猫视频</title>
99
<script src="https://cdn.bootcss.com/hls.js/8.0.0-beta.3/hls.min.js"></script>
1010
</head>
1111
<body>

public/logo.png

18.4 KB
Loading

src/background.js

Lines changed: 162 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict'
22

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'
413
// import { establishConnection } from './biz/channel/connection'
514
const path = require('path')
615
import {
@@ -58,30 +67,9 @@ function createWindow() {
5867
ipcMain.on('mainWindow:close', event => {
5968
mainWindow.hide()
6069
mainWindow.setSkipTaskbar(true)
70+
createdNotice()
6171
event.preventDefault()
6272
})
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-
})
8573
}
8674

8775
// Quit when all windows are closed.
@@ -101,27 +89,6 @@ app.on('activate', () => {
10189
}
10290
})
10391

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-
12592
// Exit cleanly on request from parent process in development mode.
12693
if (isDevelopment) {
12794
if (process.platform === 'win32') {
@@ -136,6 +103,154 @@ if (isDevelopment) {
136103
})
137104
}
138105
}
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+
}

src/router/modules/personalizedContent.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/router/modules/recommend.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/router/modules/songListDetail.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/router/router.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import VueRouter from 'vue-router'
44
import MainLayout from '@/views/main/main-layout.vue'
5-
import recommendRoutes from '@/router/modules/recommend.js'
65
import personalFMRoutes from '@/router/modules/personalFM.js'
7-
import personalizedContentRoutes from '@/router/modules/personalizedContent.js'
86
import videoRoutes from '@/router/modules/video.js'
9-
import songListDetailRoutes from '@/router/modules/songListDetail.js'
107
import playRoutes from '@/router/modules/play.js'
118

129
import Login from '@/views/login/login.vue'
@@ -18,14 +15,7 @@ const routes = [
1815
{
1916
path: '/main',
2017
component: MainLayout,
21-
children: [
22-
...recommendRoutes,
23-
...personalFMRoutes,
24-
...personalizedContentRoutes,
25-
...videoRoutes,
26-
...songListDetailRoutes,
27-
...playRoutes
28-
]
18+
children: [...personalFMRoutes, ...videoRoutes, ...playRoutes]
2919
},
3020
{ path: '/login', component: Login }
3121
]

src/views/main/personalizedContent/personalizedContent.vue

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)