-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (153 loc) · 4.38 KB
/
main.js
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
const { app, BrowserWindow, ipcMain, shell, screen} = require('electron')
const path = require('path')
const http = require('http')
const isDevelopment = process.env.NODE_ENV === 'development'
function pollServer(url) {
return new Promise(resolve => {
const request = http.get(url, (res) => {
resolve(true);
request.abort(); // We don't need to read the response, just knowing the server is up is enough
});
request.on('error', (err) => {
setTimeout(() => {
console.log(`Polling for dev server:`, url);
resolve(pollServer(url)); // Retry after a delay
}, 1000);
});
});
}
/**
* Create and return the {@link BrowserWindow} instance.
* @returns {Promise<Electron.CrossProcessExports.BrowserWindow>}
*/
const createWindow = async () => {
const win = new BrowserWindow({
show: false,
width: 700,
height: 420,
minWidth: 700,
minHeight: 420,
backgroundColor: '#161617',
webPreferences: {
contextIsolation: true,
sandbox: false,
preload: isDevelopment
? path.join(__dirname, 'src', 'main', 'preload.js')
: path.join(__dirname, 'preload.js')
}
})
const startUrl =
isDevelopment
? `http://127.0.0.1:3000`
: `file://${path.join(__dirname, 'index.html')}`
win.removeMenu()
win.once('ready-to-show', () => {
win.show()
})
if (isDevelopment) {
await pollServer(startUrl)
}
await win.loadURL(startUrl)
return win
}
/**
* Wait for app ready and initialize the window.
* @returns {Promise<void>}
*/
const start = async () => {
// app.disableHardwareAcceleration()
await app.whenReady()
let window = await createWindow()
let overlay
let activating = false
const closeOverlay = async () => {
if (overlay) {
await new Promise(resolve => {
overlay.once('closed', () => {
console.log('Closed overlay window!')
overlay = null
resolve()
})
console.log('Destroying overlay window...')
overlay.destroy()
})
}
}
window.on('closed', async () => {
await closeOverlay()
})
app.on('window-all-closed', () => {
app.quit()
})
app.on('activate', async () => {
if (!activating && window.isDestroyed()) {
activating = true
window = await createWindow()
activating = false
}
})
ipcMain.on('open-overlay', async (event, props) => {
await closeOverlay()
console.log('Initializing overlay with properties:', props)
const display = screen.getPrimaryDisplay()
const x = display.workArea.x + (props.offsetSize ? 1 : 0)
const y = display.workArea.y + (props.offsetSize ? 1 : 0)
const width = display.workArea.width - (props.offsetSize ? 2 : 0)
const height = display.workArea.height - (props.offsetSize ? 2 : 0)
console.log('Overlay dimensions:', width, height)
overlay = new BrowserWindow({
frame: false,
titleBarStyle: 'hidden',
transparent: true,
skipTaskbar: true,
minimizable: false,
maximizable: false,
closable: false,
fullscreen: false,
movable: false,
resizable: false,
show: !props.delayShow,
backgroundColor: props.backgroundColor,
alwaysOnTop: props.alwaysOnTop ?? true,
hasShadow: props.hasShadow,
backgroundMaterial: props.backgroundMaterial,
thickFrame: props.thickFrame,
x,
y,
width,
height
})
overlay.removeMenu()
overlay.setIgnoreMouseEvents(true)
if (props.delayShow) {
await new Promise(resolve => setTimeout(resolve, 2000))
if (props.controlSizeAndBounds) {
overlay.setContentSize(width, height)
overlay.setBounds({x, y, width, height})
}
if (!props.alwaysOnTop) {
overlay.setAlwaysOnTop(true, 'status')
}
overlay.showInactive()
} else if (props.controlSizeAndBounds) {
overlay.setContentSize(width, height)
overlay.setBounds({x, y, width, height})
}
const overlayHtml = isDevelopment
? path.join(__dirname, 'src', 'renderer', 'overlay.html')
: path.join(__dirname, 'overlay.html')
await overlay.loadURL(`file://${overlayHtml}`)
if (props.setBackgroundColor) {
overlay.setBackgroundColor(props.backgroundColor ?? '#00000000')
}
event.reply('overlay-opened', {
name: props.name,
x,
y,
width,
height
})
})
}
// run the start func
start()