-
Notifications
You must be signed in to change notification settings - Fork 6k
/
main.ts
227 lines (201 loc) · 7.2 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
import path from 'node:path'
import { BrowserWindow } from 'electron'
import { debounce, isLinux, isWin } from '@common/utils'
import { initWindowSize } from './utils'
import { mainSend } from '@common/mainIpc'
import { encodePath } from '@common/utils/electron'
// require('./event')
// require('./rendererEvent')
let browserWindow: Electron.BrowserWindow | null = null
let isWinBoundsUpdateing = false
const saveBoundsConfig = debounce((config: Partial<LX.AppSetting>) => {
global.lx.event_app.update_config(config)
if (isWinBoundsUpdateing) isWinBoundsUpdateing = false
}, 500)
const winEvent = () => {
if (!browserWindow) return
// browserWindow.on('close', () => {
// if (global.lx.appSetting['desktopLyric.enable'] && !global.lx.mainWindowClosed) {
// browserWindow = null
// global.lx.event_app.update_config({ 'desktopLyric.enable': false })
// }
// })
browserWindow.on('closed', () => {
browserWindow = null
})
browserWindow.on('move', () => {
// bounds = browserWindow.getBounds()
// console.log('move', isWinBoundsUpdateing)
if (isWinBoundsUpdateing) {
const bounds = browserWindow!.getBounds()
saveBoundsConfig({
'desktopLyric.x': bounds.x,
'desktopLyric.y': bounds.y,
'desktopLyric.width': bounds.width,
'desktopLyric.height': bounds.height,
})
} else if (isWin) { // Linux 不允许将窗口设置出屏幕之外,MacOS未知,故只在Windows下执行强制设置
// 非主动调整窗口触发的窗口位置变化将重置回设置值
browserWindow!.setBounds({
x: global.lx.appSetting['desktopLyric.x'] ?? 0,
y: global.lx.appSetting['desktopLyric.y'] ?? 0,
width: global.lx.appSetting['desktopLyric.width'],
height: global.lx.appSetting['desktopLyric.height'],
})
}
})
browserWindow.on('resize', () => {
// bounds = browserWindow.getBounds()
// console.log(bounds)
const bounds = browserWindow!.getBounds()
saveBoundsConfig({
'desktopLyric.x': bounds.x,
'desktopLyric.y': bounds.y,
'desktopLyric.width': bounds.width,
'desktopLyric.height': bounds.height,
})
})
// browserWindow.on('restore', () => {
// browserWindow.webContents.send('restore')
// })
// browserWindow.on('focus', () => {
// browserWindow.webContents.send('focus')
// })
browserWindow.once('ready-to-show', () => {
showWindow()
if (global.lx.appSetting['desktopLyric.isLock']) {
browserWindow!.setIgnoreMouseEvents(true, { forward: !isLinux && global.lx.appSetting['desktopLyric.isHoverHide'] })
}
// linux下每次重开时貌似要重新设置置顶
// if (isLinux && global.lx.appSetting['desktopLyric.isAlwaysOnTop']) {
// browserWindow!.setAlwaysOnTop(global.lx.appSetting['desktopLyric.isAlwaysOnTop'], 'screen-saver')
// }
if (global.lx.appSetting['desktopLyric.isAlwaysOnTop'] && global.lx.appSetting['desktopLyric.isAlwaysOnTopLoop']) alwaysOnTopTools.startLoop()
browserWindow!.blur()
})
}
export const createWindow = () => {
closeWindow()
if (!global.envParams.workAreaSize) return
let x = global.lx.appSetting['desktopLyric.x']
let y = global.lx.appSetting['desktopLyric.y']
let width = global.lx.appSetting['desktopLyric.width']
let height = global.lx.appSetting['desktopLyric.height']
let isAlwaysOnTop = global.lx.appSetting['desktopLyric.isAlwaysOnTop']
// let isLockScreen = global.lx.appSetting['desktopLyric.isLockScreen']
let isShowTaskbar = global.lx.appSetting['desktopLyric.isShowTaskbar']
// let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize
const winSize = initWindowSize(x, y, width, height)
global.lx.event_app.update_config({
'desktopLyric.x': winSize.x,
'desktopLyric.y': winSize.y,
'desktopLyric.width': winSize.width,
'desktopLyric.height': winSize.height,
})
const { shouldUseDarkColors, theme } = global.lx.theme
/**
* Initial window options
*/
browserWindow = new BrowserWindow({
height: winSize.height,
width: winSize.width,
x: winSize.x,
y: winSize.y,
minWidth: 380,
minHeight: 80,
useContentSize: true,
frame: false,
transparent: true,
hasShadow: false,
// enableRemoteModule: false,
// icon: join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'),
resizable: false,
minimizable: false,
maximizable: false,
fullscreenable: false,
show: false,
alwaysOnTop: isAlwaysOnTop,
skipTaskbar: !isShowTaskbar,
webPreferences: {
contextIsolation: false,
webSecurity: false,
sandbox: false,
nodeIntegration: true,
enableWebSQL: false,
webgl: false,
spellcheck: false, // 禁用拼写检查器
},
})
const winURL = process.env.NODE_ENV !== 'production' ? 'http://localhost:9081/lyric.html' : `file://${path.join(encodePath(__dirname), 'lyric.html')}`
void browserWindow.loadURL(winURL + `?dark=${shouldUseDarkColors}&theme=${encodeURIComponent(JSON.stringify(theme))}`)
winEvent()
// browserWindow.webContents.openDevTools()
}
export const isExistWindow = (): boolean => !!browserWindow
export const closeWindow = () => {
if (!browserWindow) return
browserWindow.close()
}
export const showWindow = () => {
if (!browserWindow) return
browserWindow.show()
}
export const sendEvent = <T = any>(name: string, params?: T) => {
if (!browserWindow) return
mainSend(browserWindow, name, params)
}
export const getBounds = (): Electron.Rectangle => {
if (!browserWindow) throw new Error('window is not available')
return browserWindow.getBounds()
}
export const setBounds = (bounds: Electron.Rectangle) => {
if (!browserWindow) return
isWinBoundsUpdateing = true
browserWindow.setBounds(bounds)
}
export const setIgnoreMouseEvents = (ignore: boolean, options?: Electron.IgnoreMouseEventsOptions) => {
if (!browserWindow) return
browserWindow.setIgnoreMouseEvents(ignore, options)
}
export const setSkipTaskbar = (skip: boolean) => {
if (!browserWindow) return
browserWindow.setSkipTaskbar(skip)
}
export const setAlwaysOnTop = (flag: boolean, level?: 'normal' | 'floating' | 'torn-off-menu' | 'modal-panel' | 'main-menu' | 'status' | 'pop-up-menu' | 'screen-saver' | undefined, relativeLevel?: number | undefined) => {
if (!browserWindow) return
browserWindow.setAlwaysOnTop(flag, level, relativeLevel)
}
export const getMainFrame = (): Electron.WebFrameMain | null => {
if (!browserWindow) return null
return browserWindow.webContents.mainFrame
}
interface AlwaysOnTopTools {
timeout: NodeJS.Timeout | null
setAlwaysOnTop: (isLoop: boolean) => void
startLoop: () => void
clearLoop: () => void
}
export const alwaysOnTopTools: AlwaysOnTopTools = {
timeout: null,
setAlwaysOnTop(isLoop) {
this.clearLoop()
setAlwaysOnTop(global.lx.appSetting['desktopLyric.isAlwaysOnTop'], 'screen-saver')
// console.log(isLoop)
if (isLoop) this.startLoop()
},
startLoop() {
this.clearLoop()
this.timeout = setInterval(() => {
if (!isExistWindow()) {
this.clearLoop()
return
}
setAlwaysOnTop(true, 'screen-saver')
}, 1000)
},
clearLoop() {
if (!this.timeout) return
clearInterval(this.timeout)
this.timeout = null
},
}