-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainWindow.js
328 lines (273 loc) · 8.5 KB
/
mainWindow.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"use strict"
const path = require('path')
const { app, screen, shell, Menu, BrowserWindow } = require('electron')
const PluginManager = require('./pluginManager.js')
const NORMAL_WINDOW_SIZE = { width: 1778, height: 1000 }
const PIP_WINDOW_SIZE = { width: 640, height: 360 }
const PIP_INSETS = { x: 25, y: 25 }
class MainWindow {
constructor() {
this.browser = null
this.pluginManager = new PluginManager()
this.teleprompterMode = false
this.pictureInPicture = false
}
initialize() {
this.browser = new BrowserWindow({
width: NORMAL_WINDOW_SIZE.width,
height: NORMAL_WINDOW_SIZE.height,
title: app.name,
titleBarStyle: 'hidden',
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
this.updateWindowPosition()
screen.on('display-added', () => this.updateWindowPosition())
screen.on('display-removed', () => this.updateWindowPosition())
screen.on('display-metrics-changed', () => this.updateWindowPosition())
this.browser.webContents.on('did-finish-load', () => {
if (this.teleprompterMode) {
this.browser.webContents.insertCSS('html { transform: scaleX(-1) }')
}
if (this.hasActiveCall()) {
this.pluginManager.beginActiveCall()
return
}
this.pluginManager.endActiveCall()
})
this.browser.webContents.on('new-window', (err, url) => {
err.preventDefault()
shell.openExternal(url)
})
this.browser.on('close', () => {
this.pluginManager.appClosed()
})
this.browser.on('blur', () => {
if (this.teleprompterMode) {
this.updateWindowPosition()
return
}
// Animate to PIP only if there's an active call
if (this.hasActiveCall() && !this.isDevToolsOpened()) {
this.presentPictureInPicture()
}
})
this.browser.on('focus', () => {
if (this.teleprompterMode) {
this.updateWindowPosition()
return
}
this.dismissPictureInPicture()
})
this.createMenu()
this.home()
}
createMenu() {
let template = [
{ role: 'appMenu' },
{
label: 'Call',
submenu: [
{
label: 'Toggle Mute',
accelerator: 'CmdOrCtrl+Shift+D',
click: () => this.toggleMute()
},
{
label: 'Toggle Camera',
accelerator: 'CmdOrCtrl+Shift+E',
click: () => this.toggleCamera()
},
{ type: 'separator' },
{
label: 'Leave Call',
accelerator: 'CmdOrCtrl+L',
click: () => {
if (!this.hasActiveCall()) {
this.home()
return
}
this.leaveCall()
setTimeout(() => this.home(), 2000)
}
},
]
},
{ role: 'editMenu' },
{
label: 'View',
submenu: [
{
label: 'Open Attachment',
accelerator: 'CmdOrCtrl+A',
click: () => this.openFirstAttachment()
},
{
label: 'Toggle Grid',
accelerator: 'CmdOrCtrl+G',
click: () => this.toggleGrid()
},
{ type: 'separator' },
{
label: 'Open Developer Tools',
accelerator: 'CmdOrCtrl+Option+I',
click: () => this.browser.webContents.openDevTools()
}
]
}
]
var pluginMenuItems = this.pluginManager.menuItems()
if (pluginMenuItems && pluginMenuItems.length > 0) {
template.push({ label: 'Plugins', submenu: pluginMenuItems })
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
}
hasActiveCall() {
return this.browser.getURL()
.match(/https:\/\/meet\.google\.com\/(_meet\/)?\w+-\w+-\w+/)
}
isDevToolsOpened() {
return this.browser.webContents.isDevToolsOpened()
}
toggleMute() {
this.browser.webContents.executeJavaScript(`
var onButton = document.querySelector('[aria-label="Turn on microphone (⌘ + d)"]')
var offButton = document.querySelector('[aria-label="Turn off microphone (⌘ + d)"]')
if (onButton) {
onButton.click()
} else if (offButton) {
offButton.click()
}
`)
}
toggleCamera() {
this.browser.webContents.executeJavaScript(`
var onButton = document.querySelector('[aria-label="Turn on camera (⌘ + e)"]')
var offButton = document.querySelector('[aria-label="Turn off camera (⌘ + e)"]')
if (onButton) {
onButton.click()
} else if (offButton) {
offButton.click()
}
`)
}
leaveCall() {
this.browser.webContents.executeJavaScript(`
var leaveCallButton = document.querySelector('[aria-label="Leave call"]')
if (leaveCallButton) {
leaveCallButton.click()
}
`)
}
toggleGrid() {
this.browser.webContents.executeJavaScript(`
var toggleGridButton = document.querySelector('[aria-label="Toggle grid"]')
if (toggleGridButton) {
toggleGridButton.click()
}
`)
}
openFirstAttachment() {
this.browser.webContents.executeJavaScript(`
new Promise((resolve) => {
var detailsButton = document.querySelector('[aria-label$="This meeting has attachments."]')
if (!detailsButton) {
resolve()
return
}
detailsButton.click()
setTimeout(() => resolve(), 200)
}).then(() => {
return new Promise((resolve) => {
var attachmentsContainer = document.querySelector('[aria-label="Attachments"]')
if (!attachmentsContainer) {
resolve()
return
}
var attachment = attachmentsContainer.querySelector('[data-file-url]')
var fileURL = attachment.getAttribute('data-file-url')
setTimeout(() => resolve(fileURL), 300)
})
}).then((fileURL) => {
var detailsButton = document.querySelector('[aria-label$="This meeting has attachments."]')
if (detailsButton) {
detailsButton.click()
}
return fileURL;
})
`).then((fileURL) => {
if (!fileURL) {
return
}
shell.openExternal(fileURL)
})
}
home() {
this.browser.loadURL('https://meet.google.com')
}
updateWindowPosition() {
const displays = screen.getAllDisplays()
const teleprompter = displays.find((d) => {
return d.bounds.width === 1116 && d.bounds.height === 756
})
if (teleprompter) {
this.teleprompterMode = true
this.pictureInPicture = false
this.browser.setBounds(teleprompter.bounds)
this.browser.setAlwaysOnTop(false)
this.browser.setVisibleOnAllWorkspaces(false)
this.browser.setFullScreenable(true)
this.browser.setWindowButtonVisibility(true)
} else {
this.teleprompterMode = false
if (this.pictureInPicture) {
this.presentPictureInPicture()
} else {
this.dismissPictureInPicture()
}
}
// If we're not in a call, reload the page
// so that we can flip the screen if needed
if (!this.hasActiveCall()) {
this.browser.webContents.reload()
}
}
presentPictureInPicture() {
if (this.teleprompterMode) {
return
}
let display = screen.getDisplayNearestPoint(this.browser.getBounds())
let workArea = display.workArea
this.browser.setBounds({
x: Math.floor(workArea.x + PIP_INSETS.x),
y: Math.floor(workArea.y + PIP_INSETS.y),
width: PIP_WINDOW_SIZE.width,
height: PIP_WINDOW_SIZE.height
}, true)
this.browser.setAlwaysOnTop(true, 'floating')
this.browser.setVisibleOnAllWorkspaces(true)
this.browser.setFullScreenable(false)
this.browser.setWindowButtonVisibility(false);
this.pictureInPicture = true
}
dismissPictureInPicture() {
if (this.teleprompterMode) {
return
}
let display = screen.getDisplayNearestPoint(this.browser.getBounds())
let workArea = display.workArea
this.browser.setBounds({
x: Math.round((workArea.width / 2) - (NORMAL_WINDOW_SIZE.width / 2) + workArea.x),
y: Math.round((workArea.height / 2) - (NORMAL_WINDOW_SIZE.height / 2) + workArea.y - 13),
width: NORMAL_WINDOW_SIZE.width,
height: NORMAL_WINDOW_SIZE.height
}, true)
this.browser.setAlwaysOnTop(false)
this.browser.setVisibleOnAllWorkspaces(false)
this.browser.setFullScreenable(true)
this.browser.setWindowButtonVisibility(true)
this.pictureInPicture = false
}
}
module.exports = MainWindow