@@ -26,6 +26,22 @@ unhandled({
2626process . env . NODE_OPTIONS = "" ;
2727
2828const app = electron . app ;
29+ // Prevent window being garbage collected
30+ let mainWindow ;
31+ autoUpdater . autoDownload = false ;
32+
33+ if ( config . get ( "options.singleInstanceLock" ) ) {
34+ const gotTheLock = app . requestSingleInstanceLock ( ) ;
35+ if ( ! gotTheLock ) app . quit ( ) ;
36+
37+ app . on ( 'second-instance' , ( ) => {
38+ if ( ! mainWindow ) return ;
39+ if ( mainWindow . isMinimized ( ) ) mainWindow . restore ( ) ;
40+ if ( ! mainWindow . isVisible ( ) ) mainWindow . show ( ) ;
41+ mainWindow . focus ( ) ;
42+ } ) ;
43+ }
44+
2945app . commandLine . appendSwitch (
3046 "js-flags" ,
3147 // WebAssembly flags
@@ -54,10 +70,6 @@ require("electron-debug")({
5470 showDevTools : false //disable automatic devTools on new window
5571} ) ;
5672
57- // Prevent window being garbage collected
58- let mainWindow ;
59- autoUpdater . autoDownload = false ;
60-
6173let icon = "assets/youtube-music.png" ;
6274if ( process . platform == "win32" ) {
6375 icon = "assets/generated/icon.ico" ;
@@ -160,22 +172,39 @@ function createMainWindow() {
160172 win . on ( "closed" , onClosed ) ;
161173
162174 win . on ( "move" , ( ) => {
175+ if ( win . isMaximized ( ) ) return ;
163176 let position = win . getPosition ( ) ;
164- config . set ( "window-position" , { x : position [ 0 ] , y : position [ 1 ] } ) ;
177+ lateSave ( "window-position" , { x : position [ 0 ] , y : position [ 1 ] } ) ;
165178 } ) ;
166179
180+ let winWasMaximized ;
181+
167182 win . on ( "resize" , ( ) => {
168183 const windowSize = win . getSize ( ) ;
169184
170- config . set ( "window-maximized" , win . isMaximized ( ) ) ;
171- if ( ! win . isMaximized ( ) ) {
172- config . set ( "window-size" , {
185+ const isMaximized = win . isMaximized ( ) ;
186+ if ( winWasMaximized !== isMaximized ) {
187+ winWasMaximized = isMaximized ;
188+ config . set ( "window-maximized" , isMaximized ) ;
189+ }
190+ if ( ! isMaximized ) {
191+ lateSave ( "window-size" , {
173192 width : windowSize [ 0 ] ,
174193 height : windowSize [ 1 ] ,
175194 } ) ;
176195 }
177196 } ) ;
178197
198+ let savedTimeouts = { } ;
199+ function lateSave ( key , value ) {
200+ if ( savedTimeouts [ key ] ) clearTimeout ( savedTimeouts [ key ] ) ;
201+
202+ savedTimeouts [ key ] = setTimeout ( ( ) => {
203+ config . set ( key , value ) ;
204+ savedTimeouts [ key ] = undefined ;
205+ } , 1000 )
206+ }
207+
179208 win . webContents . on ( "render-process-gone" , ( event , webContents , details ) => {
180209 showUnresponsiveDialog ( win , details ) ;
181210 } ) ;
@@ -192,30 +221,31 @@ function createMainWindow() {
192221}
193222
194223app . once ( "browser-window-created" , ( event , win ) => {
195- // User agents are from https://developers.whatismybrowser.com/useragents/explore/
196- const originalUserAgent = win . webContents . userAgent ;
197- const userAgents = {
198- mac : "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.1; rv:95.0) Gecko/20100101 Firefox/95.0" ,
199- windows : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0" ,
200- linux : "Mozilla/5.0 (Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0" ,
201- }
224+ if ( config . get ( "options.overrideUserAgent" ) ) {
225+ // User agents are from https://developers.whatismybrowser.com/useragents/explore/
226+ const originalUserAgent = win . webContents . userAgent ;
227+ const userAgents = {
228+ mac : "Mozilla/5.0 (Macintosh; Intel Mac OS X 12.1; rv:95.0) Gecko/20100101 Firefox/95.0" ,
229+ windows : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0" ,
230+ linux : "Mozilla/5.0 (Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0" ,
231+ }
202232
203- const updatedUserAgent =
204- is . macOS ( ) ? userAgents . mac :
205- is . windows ( ) ? userAgents . windows :
206- userAgents . linux ;
233+ const updatedUserAgent =
234+ is . macOS ( ) ? userAgents . mac :
235+ is . windows ( ) ? userAgents . windows :
236+ userAgents . linux ;
207237
208- win . webContents . userAgent = updatedUserAgent ;
209- app . userAgentFallback = updatedUserAgent ;
210-
211- win . webContents . session . webRequest . onBeforeSendHeaders ( ( details , cb ) => {
212- // this will only happen if login failed, and "retry" was pressed
213- if ( win . webContents . getURL ( ) . startsWith ( "https://accounts.google.com" ) && details . url . startsWith ( "https://accounts.google.com" ) ) {
214- details . requestHeaders [ "User-Agent" ] = originalUserAgent ;
215- }
216- cb ( { requestHeaders : details . requestHeaders } ) ;
217- } ) ;
238+ win . webContents . userAgent = updatedUserAgent ;
239+ app . userAgentFallback = updatedUserAgent ;
218240
241+ win . webContents . session . webRequest . onBeforeSendHeaders ( ( details , cb ) => {
242+ // this will only happen if login failed, and "retry" was pressed
243+ if ( win . webContents . getURL ( ) . startsWith ( "https://accounts.google.com" ) && details . url . startsWith ( "https://accounts.google.com" ) ) {
244+ details . requestHeaders [ "User-Agent" ] = originalUserAgent ;
245+ }
246+ cb ( { requestHeaders : details . requestHeaders } ) ;
247+ } ) ;
248+ }
219249
220250 setupSongInfo ( win ) ;
221251 loadPlugins ( win ) ;
@@ -325,12 +355,6 @@ app.on("ready", () => {
325355
326356 mainWindow = createMainWindow ( ) ;
327357 setApplicationMenu ( mainWindow ) ;
328- if ( config . get ( "options.restartOnConfigChanges" ) ) {
329- config . watch ( ( ) => {
330- app . relaunch ( ) ;
331- app . exit ( ) ;
332- } ) ;
333- }
334358 setUpTray ( app , mainWindow ) ;
335359
336360 // Autostart at login
0 commit comments