This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmain.js
309 lines (268 loc) · 7.06 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
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
'use strict'
// Handle the Squirrel.Windows install madnesss
/* eslint global-require: "off" */
if (require('electron-squirrel-startup')) {
return
}
const { app, BrowserWindow, globalShortcut, Menu } = require('electron')
const autoUpdater = require('./auto-updater')
const checkAccessibilityPermissions = require('./check-accessibility-permissions')
const contextMenu = require('./context-menu')
const darkMode = require('./dark-mode')
const dockMenu = require('./dock-menu')
const errorHandlers = require('./error-handlers')
const mainMenu = require('./menu')
const options = require('./options')
const SoundCloud = require('./soundcloud')
const touchBarMenu = require('./touch-bar-menu')
const windowOpenPolicy = require('./window-open-policy')
const windowState = require('electron-window-state')
let mainWindow = null
let aboutWindow = null
const {
autoUpdaterBaseUrl,
baseUrl,
checkPermissions,
developerTools,
launchUrl,
profile,
quitAfterLastWindow,
useAutoUpdater,
userData,
useMediaKeys
} = options(process)
if (userData) {
app.setPath('userData', userData)
} else if (profile) {
app.setPath('userData', app.getPath('userData') + ' ' + profile)
}
let quitting = false
app.on('before-quit', () => {
quitting = true
})
app.requestSingleInstanceLock()
app.on('ready', (event, argv) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.show()
mainWindow.focus()
const { launchUrl: loadUrl } = options(process, argv)
if (loadUrl) {
mainWindow.loadURL(loadUrl)
}
}
})
if (useAutoUpdater) {
autoUpdater(autoUpdaterBaseUrl)
}
windowOpenPolicy(app)
app.on('activate', () => {
if (mainWindow) {
mainWindow.show()
}
})
app.on('ready', () => {
const menu = mainMenu({ developerTools })
Menu.setApplicationMenu(menu)
const mainWindowState = windowState({
defaultWidth: 1024,
defaultHeight: 640
})
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 640,
minHeight: 320,
webPreferences: {
nodeIntegration: false,
preload: `${__dirname}/preload.js`
}
})
const soundcloud = new SoundCloud(mainWindow)
contextMenu(mainWindow, soundcloud)
errorHandlers(mainWindow)
darkMode(mainWindow)
if (process.platform == 'darwin') {
dockMenu(soundcloud)
touchBarMenu(mainWindow, soundcloud)
}
mainWindowState.manage(mainWindow)
mainWindow.on('close', (event) => {
// Due to (probably) a bug in Spectron this prevents quitting
// the app in tests:
// https://github.com/electron/spectron/issues/137
if (!quitting && !quitAfterLastWindow) {
// Do not quit
event.preventDefault()
// Hide the window instead of quitting
if (mainWindow.isFullScreen()) {
// Avoid blank black screen when closing a fullscreen window on macOS
// by only hiding when the leave-fullscreen animation finished.
// See https://github.com/electron/electron/issues/6033
mainWindow.once('leave-full-screen', () => mainWindow.hide())
mainWindow.setFullScreen(false)
} else {
mainWindow.hide()
}
}
})
// Only send commands from menu accelerators when the app is not focused.
// This avoids double-triggering actions and triggering actions during text
// is entered into the search input or in other places.
function isNotFocused() {
return !mainWindow || !mainWindow.isFocused()
}
mainWindow.on('closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
mainWindow = null
})
if (useMediaKeys) {
globalShortcut.register('MediaPlayPause', () => {
soundcloud.playPause()
})
globalShortcut.register('MediaNextTrack', () => {
soundcloud.nextTrack()
})
globalShortcut.register('MediaPreviousTrack', () => {
soundcloud.previousTrack()
})
} else {
console.log('Not using media keys')
}
menu.events.on('playPause', () => {
if (isNotFocused()) {
soundcloud.playPause()
}
})
menu.events.on('likeUnlike', () => {
if (isNotFocused()) {
soundcloud.likeUnlike()
}
})
menu.events.on('repost', () => {
if (isNotFocused()) {
soundcloud.repost()
}
})
menu.events.on('nextTrack', () => {
if (isNotFocused()) {
soundcloud.nextTrack()
}
})
menu.events.on('previousTrack', () => {
if (isNotFocused()) {
soundcloud.previousTrack()
}
})
// The shortcuts *not* handled by SoundCloud itself
// don't need the isNotFocused() check to avoid double triggering
menu.events.on('home', () => {
soundcloud.goHome()
})
menu.events.on('back', () => {
soundcloud.goBack()
})
menu.events.on('forward', () => {
soundcloud.goForward()
})
menu.events.on('main-window', () => {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
mainWindow.show()
}
})
menu.events.on('about', () => {
showAbout()
})
mainWindow.on('app-command', (e, cmd) => {
switch (cmd) {
case 'media-play':
soundcloud.play()
break
case 'media-pause':
soundcloud.pause()
break
case 'media-play-pause':
soundcloud.playPause()
break
case 'browser-backward':
soundcloud.goBack()
break
case 'browser-forward':
soundcloud.goForward()
break
case 'browser-home':
soundcloud.goHome()
break
default:
}
})
// "You cannot require or use this module until the `ready` event of the
// `app` module is emitted."
/* eslint global-require: off */
require('electron').powerMonitor.on('suspend', () => {
soundcloud.pause()
})
soundcloud.on('play-new-track', ({ title, subtitle, artworkURL }) => {
mainWindow.webContents.send('notification', {
title,
body: subtitle,
icon: artworkURL
})
})
mainWindow.webContents.once('did-start-loading', () => {
mainWindow.setTitle('Loading soundcloud.com...')
})
mainWindow.loadURL(getUrl())
// Make sure we can use media keys on macOS
// Doing this after creating the main window to ensure the dialogs do not
// end up behind the main window.
if (checkPermissions && useMediaKeys) {
checkAccessibilityPermissions()
}
})
function getUrl() {
if (launchUrl) {
return launchUrl
}
if (baseUrl) {
return baseUrl
}
return 'https://soundcloud.com'
}
function showAbout() {
if (aboutWindow) {
aboutWindow.show()
} else {
aboutWindow = new BrowserWindow({
fullscreen: false,
fullscreenable: false,
height: 520,
maximizable: false,
resizable: false,
show: false,
skipTaskbar: true,
width: 385,
modal: true,
parent: mainWindow,
webPreferences: {
nodeIntegration: true
}
})
aboutWindow.setMenu(null)
aboutWindow.once('ready-to-show', () => {
aboutWindow.show()
})
aboutWindow.on('close', () => {
aboutWindow = null
})
aboutWindow.loadURL(`file://${__dirname}/about.html`)
}
}