-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathmain.ts
266 lines (237 loc) · 8.11 KB
/
main.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { BrowserWindow, dialog, session } from 'electron'
import path from 'node:path'
import { createTaskBarButtons, getWindowSizeInfo } from './utils'
import { isLinux, isWin } from '@common/utils'
import { openDevTools as handleOpenDevTools } from '@main/utils'
import { mainSend } from '@common/mainIpc'
import { sendFocus, sendTaskbarButtonClick } from './rendererEvent'
import { encodePath } from '@common/utils/electron'
let browserWindow: Electron.BrowserWindow | null = null
const winEvent = () => {
if (!browserWindow) return
browserWindow.on('close', event => {
if (
global.lx.isSkipTrayQuit ||
!global.lx.appSetting['tray.enable'] ||
// linux下,如果不是点击软件上的关闭按钮退出的操作都直接退出
// https://github.com/lyswhut/lx-music-desktop/issues/191
(isLinux && !global.lx.isTrafficLightClose)
) {
browserWindow!.setProgressBar(-1)
// global.lx.mainWindowClosed = true
global.lx.event_app.main_window_close()
return
}
global.lx.isTrafficLightClose &&= false
event.preventDefault()
browserWindow!.hide()
})
browserWindow.on('closed', () => {
// global.lx.mainWindowClosed = true
browserWindow = null
})
// browserWindow.on('restore', () => {
// browserWindow.webContents.send('restore')
// })
browserWindow.on('focus', () => {
sendFocus()
global.lx.event_app.main_window_focus()
})
browserWindow.on('blur', () => {
global.lx.event_app.main_window_blur()
})
browserWindow.once('ready-to-show', () => {
showWindow()
setThumbarButtons()
global.lx.event_app.main_window_ready_to_show()
})
browserWindow.on('show', () => {
global.lx.event_app.main_window_show()
// 修复隐藏窗口后再显示时任务栏按钮丢失的问题
setThumbarButtons()
})
browserWindow.on('hide', () => {
global.lx.event_app.main_window_hide()
})
}
export const createWindow = () => {
closeWindow()
const windowSizeInfo = getWindowSizeInfo(global.lx.appSetting['common.windowSizeId'])
const { shouldUseDarkColors, theme } = global.lx.theme
const ses = session.fromPartition('persist:win-main')
/**
* Initial window options
*/
const options: Electron.BrowserWindowConstructorOptions = {
height: windowSizeInfo.height,
useContentSize: true,
width: windowSizeInfo.width,
frame: false,
transparent: !global.envParams.cmdParams.dt,
hasShadow: global.envParams.cmdParams.dt,
// enableRemoteModule: false,
// icon: join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'),
resizable: false,
maximizable: false,
fullscreenable: true,
roundedCorners: false,
show: false,
webPreferences: {
session: ses,
nodeIntegrationInWorker: true,
contextIsolation: false,
webSecurity: false,
nodeIntegration: true,
sandbox: false,
enableWebSQL: false,
webgl: false,
spellcheck: false, // 禁用拼写检查器
},
}
if (global.envParams.cmdParams.dt) options.backgroundColor = theme.colors['--color-primary-light-1000']
if (global.lx.appSetting['common.startInFullscreen']) {
options.fullscreen = true
if (isLinux) options.resizable = true
}
browserWindow = new BrowserWindow(options)
const winURL = process.env.NODE_ENV !== 'production' ? 'http://localhost:9080' : `file://${path.join(encodePath(__dirname), 'index.html')}`
void browserWindow.loadURL(winURL + `?dt=${!!global.envParams.cmdParams.dt}&dark=${shouldUseDarkColors}&theme=${encodeURIComponent(JSON.stringify(theme))}`)
winEvent()
if (global.envParams.cmdParams.odt) handleOpenDevTools(browserWindow.webContents)
// global.lx.mainWindowClosed = false
// browserWindow.webContents.openDevTools()
}
export const isExistWindow = (): boolean => !!browserWindow
export const isShowWindow = (): boolean => {
if (!browserWindow) return false
return browserWindow.isVisible() && (isWin ? true : browserWindow.isFocused())
}
export const closeWindow = () => {
if (!browserWindow) return
browserWindow.close()
}
export const sendEvent = <T = any>(name: string, params?: T) => {
if (!browserWindow) return
mainSend(browserWindow, name, params)
}
export const showSelectDialog = async(options: Electron.OpenDialogOptions) => {
if (!browserWindow) throw new Error('main window is undefined')
return dialog.showOpenDialog(browserWindow, options)
}
export const showDialog = ({ type, message, detail }: Electron.MessageBoxSyncOptions) => {
if (!browserWindow) return
dialog.showMessageBoxSync(browserWindow, {
type,
message,
detail,
})
}
export const showSaveDialog = async(options: Electron.SaveDialogOptions) => {
if (!browserWindow) throw new Error('main window is undefined')
return dialog.showSaveDialog(browserWindow, options)
}
export const minimize = () => {
if (!browserWindow) return
browserWindow.minimize()
}
export const maximize = () => {
if (!browserWindow) return
browserWindow.maximize()
}
export const unmaximize = () => {
if (!browserWindow) return
browserWindow.unmaximize()
}
export const toggleHide = () => {
if (!browserWindow) return
browserWindow.isVisible()
? browserWindow.hide()
: browserWindow.show()
}
export const toggleMinimize = () => {
if (!browserWindow) return
if (browserWindow.isVisible()) {
if (browserWindow.isMinimized()) browserWindow.restore()
else browserWindow.minimize()
} else browserWindow.show()
}
export const showWindow = () => {
if (!browserWindow) return
if (browserWindow.isVisible()) {
if (browserWindow.isMinimized()) browserWindow.restore()
else browserWindow.focus()
} else browserWindow.show()
}
export const hideWindow = () => {
if (!browserWindow) return
browserWindow.hide()
}
export const setWindowBounds = (options: Partial<Electron.Rectangle>) => {
if (!browserWindow) return
browserWindow.setBounds(options)
}
export const setProgressBar = (progress: number, options?: Electron.ProgressBarOptions) => {
if (!browserWindow) return
browserWindow.setProgressBar(progress, options)
}
export const setIgnoreMouseEvents = (ignore: boolean, options?: Electron.IgnoreMouseEventsOptions) => {
if (!browserWindow) return
browserWindow.setIgnoreMouseEvents(ignore, options)
}
export const toggleDevTools = () => {
if (!browserWindow) return
if (browserWindow.webContents.isDevToolsOpened()) {
browserWindow.webContents.closeDevTools()
} else {
handleOpenDevTools(browserWindow.webContents)
}
}
export const setFullScreen = (isFullscreen: boolean): boolean => {
if (!browserWindow) return false
if (isLinux) { // linux 需要先设置为可调整窗口大小才能全屏
if (isFullscreen) {
browserWindow.setResizable(isFullscreen)
browserWindow.setFullScreen(isFullscreen)
} else {
browserWindow.setFullScreen(isFullscreen)
browserWindow.setResizable(isFullscreen)
}
} else {
browserWindow.setFullScreen(isFullscreen)
}
return isFullscreen
}
const taskBarButtonFlags: LX.TaskBarButtonFlags = {
empty: true,
collect: false,
play: false,
next: true,
prev: true,
}
export const setThumbarButtons = ({ empty, collect, play, next, prev }: LX.TaskBarButtonFlags = taskBarButtonFlags) => {
if (!isWin || !browserWindow) return
taskBarButtonFlags.empty = empty
taskBarButtonFlags.collect = collect
taskBarButtonFlags.play = play
taskBarButtonFlags.next = next
taskBarButtonFlags.prev = prev
browserWindow.setThumbarButtons(createTaskBarButtons(taskBarButtonFlags, action => {
sendTaskbarButtonClick(action)
}))
}
export const setThumbnailClip = (region: Electron.Rectangle) => {
if (!browserWindow) return
browserWindow.setThumbnailClip(region)
}
export const clearCache = async() => {
if (!browserWindow) throw new Error('main window is undefined')
await browserWindow.webContents.session.clearCache()
}
export const getCacheSize = async() => {
if (!browserWindow) throw new Error('main window is undefined')
return browserWindow.webContents.session.getCacheSize()
}
export const getWebContents = (): Electron.WebContents => {
if (!browserWindow) throw new Error('main window is undefined')
return browserWindow.webContents
}