@@ -3,15 +3,9 @@ import { BrowserWindow } from "electron/main";
33import path from "path" ;
44import process from "process" ;
55import { checkForUpdate } from "./helpers/autoUpdate" ;
6- import {
7- IS_DEV ,
8- IS_LINUX ,
9- IS_MAC ,
10- IS_WINDOWS ,
11- RESOURCES_PATH ,
12- } from "./helpers/constants" ;
6+ import { IS_DEV , IS_LINUX , IS_MAC , RESOURCES_PATH } from "./helpers/constants" ;
137import { MenuManager } from "./helpers/menuManager" ;
14- import { settings } from "./helpers/settings" ;
8+ import { setSettingsFlushEnabled , settings } from "./helpers/settings" ;
159import { Conversation , TrayManager } from "./helpers/trayManager" ;
1610import { popupContextMenu } from "./menu/contextMenu" ;
1711
@@ -27,16 +21,18 @@ const {
2721let mainWindow : BrowserWindow ;
2822let trayManager : TrayManager ;
2923
30- app . on ( "second-instance" , ( ) => {
31- if ( mainWindow ) {
32- if ( ! mainWindow . isVisible ( ) ) {
33- mainWindow . show ( ) ;
34- }
35- }
36- } ) ;
24+ const gotTheLock = app . requestSingleInstanceLock ( ) ;
3725
38- if ( ! app . requestSingleInstanceLock ( ) ) {
26+ if ( ! gotTheLock ) {
3927 app . quit ( ) ;
28+ } else {
29+ app . on ( "second-instance" , ( ) => {
30+ if ( mainWindow ) {
31+ if ( ! mainWindow . isVisible ( ) ) {
32+ mainWindow . show ( ) ;
33+ }
34+ }
35+ } ) ;
4036}
4137
4238if ( IS_MAC ) {
@@ -48,141 +44,148 @@ if (IS_MAC) {
4844 } ) ;
4945}
5046
51- app . on ( "ready" , ( ) => app . setAppUserModelId ( "pw.kmr.amd" ) ) ;
52- app . on ( "ready" , ( ) => {
53- trayManager = new TrayManager ( ) ;
54-
55- new MenuManager ( ) ;
56-
57- if ( checkForUpdateOnLaunchEnabled . value && ! IS_DEV ) {
58- checkForUpdate ( true ) ;
59- }
60-
61- const { width, height } = savedWindowSize . value ;
62- const { x, y } = savedWindowPosition . value ?? { } ;
63-
64- mainWindow = new BrowserWindow ( {
65- width,
66- height,
67- x,
68- y,
69- autoHideMenuBar : autoHideMenuEnabled . value ,
70- title : "Android Messages" ,
71- show : false , //don't show window just yet (issue #229)
72- icon : IS_LINUX
73- ? path . resolve ( RESOURCES_PATH , "icons" , "128x128.png" )
74- : undefined ,
75- titleBarStyle : IS_MAC ? "hiddenInset" : "default" ,
76- webPreferences : {
77- nodeIntegration : true ,
78- contextIsolation : false ,
79- preload : IS_DEV
80- ? path . resolve ( app . getAppPath ( ) , "bridge.js" )
81- : path . resolve ( app . getAppPath ( ) , "app" , "bridge.js" ) ,
82- } ,
83- } ) ;
47+ app . on ( "before-quit" , ( ) => {
48+ setSettingsFlushEnabled ( false ) ;
49+ } ) ;
8450
85- process . env . MAIN_WINDOW_ID = mainWindow . id . toString ( ) ;
51+ if ( gotTheLock ) {
52+ app . on ( "ready" , ( ) => app . setAppUserModelId ( "pw.kmr.amd" ) ) ;
53+ app . on ( "ready" , ( ) => {
54+ trayManager = new TrayManager ( ) ;
8655
87- if ( ! ( settings . trayEnabled . value && settings . startInTrayEnabled . value ) ) {
88- mainWindow . show ( ) ;
89- }
90- // set user agent to potentially make google fi work
91- const userAgent =
92- "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0" ;
93-
94- mainWindow . webContents . session . webRequest . onBeforeSendHeaders (
95- {
96- urls : [ "https://accounts.google.com/*" ] ,
97- } ,
98- ( { requestHeaders } , callback ) =>
99- callback ( {
100- requestHeaders : { ...requestHeaders , "User-Agent" : userAgent } ,
101- } )
102- ) ;
103-
104- mainWindow . loadURL ( "https://messages.google.com/web/" ) ;
105-
106- trayManager . startIfEnabled ( ) ;
107- settings . showIconsInRecentConversationTrayEnabled . subscribe ( ( ) =>
108- trayManager . refreshTrayMenu ( )
109- ) ;
110-
111- let quitViaContext = false ;
112- app . on ( "before-quit" , ( ) => {
113- quitViaContext = true ;
114- } ) ;
56+ new MenuManager ( ) ;
11557
116- const shouldExitOnMainWindowClosed = ( ) => {
117- if ( IS_MAC ) {
118- return quitViaContext ;
119- } else {
120- if ( trayEnabled . value ) {
121- return quitViaContext ;
122- }
123- return true ;
124- }
125- } ;
126-
127- mainWindow . on ( "close" , ( event : ElectronEvent ) => {
128- const { x, y, width, height } = mainWindow . getBounds ( ) ;
129- savedWindowPosition . next ( { x, y } ) ;
130- savedWindowSize . next ( { width, height } ) ;
131- if ( ! shouldExitOnMainWindowClosed ( ) ) {
132- event . preventDefault ( ) ;
133- mainWindow . hide ( ) ;
134- trayManager ?. showMinimizeToTrayWarning ( ) ;
135- } else {
136- app . quit ( ) ; // If we don't explicitly call this, the webview and mainWindow get destroyed but background process still runs.
58+ if ( checkForUpdateOnLaunchEnabled . value && ! IS_DEV ) {
59+ checkForUpdate ( true ) ;
13760 }
138- } ) ;
139-
140- mainWindow . webContents . on ( "new-window" , ( e , url ) => {
141- e . preventDefault ( ) ;
142- shell . openExternal ( url ) ;
143- } ) ;
14461
145- mainWindow . webContents . on ( "context-menu" , popupContextMenu ) ;
146-
147- // block Google collecting data
148- mainWindow . webContents . session . webRequest . onBeforeRequest (
149- {
150- urls : [
151- "https://messages.google.com/web/jserror?*" ,
152- "https://play.google.com/log?*" ,
153- "https://www.google-analytics.com/analytics.js" ,
154- ] ,
155- } ,
156- ( details , callback ) => {
157- callback ( { cancel : true } ) ;
62+ const { width, height } = savedWindowSize . value ;
63+ const { x, y } = savedWindowPosition . value ?? { } ;
64+
65+ mainWindow = new BrowserWindow ( {
66+ width,
67+ height,
68+ x,
69+ y,
70+ autoHideMenuBar : autoHideMenuEnabled . value ,
71+ title : "Android Messages" ,
72+ show : false , //don't show window just yet (issue #229)
73+ icon : IS_LINUX
74+ ? path . resolve ( RESOURCES_PATH , "icons" , "128x128.png" )
75+ : undefined ,
76+ titleBarStyle : IS_MAC ? "hiddenInset" : "default" ,
77+ webPreferences : {
78+ nodeIntegration : true ,
79+ contextIsolation : false ,
80+ preload : IS_DEV
81+ ? path . resolve ( app . getAppPath ( ) , "bridge.js" )
82+ : path . resolve ( app . getAppPath ( ) , "app" , "bridge.js" ) ,
83+ } ,
84+ } ) ;
85+
86+ process . env . MAIN_WINDOW_ID = mainWindow . id . toString ( ) ;
87+
88+ if ( ! ( settings . trayEnabled . value && settings . startInTrayEnabled . value ) ) {
89+ mainWindow . show ( ) ;
15890 }
159- ) ;
160- } ) ; //onready
16191
162- ipcMain . on ( "should-hide-notification-content" , ( event ) => {
163- event . returnValue = settings . hideNotificationContentEnabled . value ;
164- } ) ;
92+ // set user agent to potentially make google fi work
93+ const userAgent =
94+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0" ;
95+
96+ mainWindow . webContents . session . webRequest . onBeforeSendHeaders (
97+ {
98+ urls : [ "https://accounts.google.com/*" ] ,
99+ } ,
100+ ( { requestHeaders } , callback ) =>
101+ callback ( {
102+ requestHeaders : { ...requestHeaders , "User-Agent" : userAgent } ,
103+ } )
104+ ) ;
105+
106+ mainWindow . loadURL ( "https://messages.google.com/web/" ) ;
107+
108+ trayManager . startIfEnabled ( ) ;
109+ settings . showIconsInRecentConversationTrayEnabled . subscribe ( ( ) =>
110+ trayManager . refreshTrayMenu ( )
111+ ) ;
112+
113+ let quitViaContext = false ;
114+ app . on ( "before-quit" , ( ) => {
115+ quitViaContext = true ;
116+ } ) ;
117+
118+ const shouldExitOnMainWindowClosed = ( ) => {
119+ if ( IS_MAC ) {
120+ return quitViaContext ;
121+ } else {
122+ if ( trayEnabled . value ) {
123+ return quitViaContext ;
124+ }
125+ return true ;
126+ }
127+ } ;
128+
129+ mainWindow . on ( "close" , ( event : ElectronEvent ) => {
130+ const { x, y, width, height } = mainWindow . getBounds ( ) ;
131+ savedWindowPosition . next ( { x, y } ) ;
132+ savedWindowSize . next ( { width, height } ) ;
133+ if ( ! shouldExitOnMainWindowClosed ( ) ) {
134+ event . preventDefault ( ) ;
135+ mainWindow . hide ( ) ;
136+ trayManager ?. showMinimizeToTrayWarning ( ) ;
137+ } else {
138+ app . quit ( ) ; // If we don't explicitly call this, the webview and mainWindow get destroyed but background process still runs.
139+ }
140+ } ) ;
141+
142+ mainWindow . webContents . on ( "new-window" , ( e , url ) => {
143+ e . preventDefault ( ) ;
144+ shell . openExternal ( url ) ;
145+ } ) ;
146+
147+ mainWindow . webContents . on ( "context-menu" , popupContextMenu ) ;
148+
149+ // block Google collecting data
150+ mainWindow . webContents . session . webRequest . onBeforeRequest (
151+ {
152+ urls : [
153+ "https://messages.google.com/web/jserror?*" ,
154+ "https://play.google.com/log?*" ,
155+ "https://www.google-analytics.com/analytics.js" ,
156+ ] ,
157+ } ,
158+ ( details , callback ) => {
159+ callback ( { cancel : true } ) ;
160+ }
161+ ) ;
162+ } ) ; //onready
165163
166- ipcMain . on ( "show-main-window" , ( ) => {
167- mainWindow . show ( ) ;
168- if ( IS_MAC ) {
169- app . dock . setBadge ( "" ) ;
170- }
171- } ) ;
164+ ipcMain . on ( "should-hide-notification-content" , ( event ) => {
165+ event . returnValue = settings . hideNotificationContentEnabled . value ;
166+ } ) ;
172167
173- ipcMain . on ( "flash-main-window-if-not-focused" , ( ) => {
174- if ( ! mainWindow . isFocused ( ) && taskbarFlashEnabled . value ) {
175- mainWindow . flashFrame ( true ) ;
168+ ipcMain . on ( "show-main-window" , ( ) => {
169+ mainWindow . show ( ) ;
176170 if ( IS_MAC ) {
177- app . dock . setBadge ( "• " ) ;
171+ app . dock . setBadge ( "" ) ;
178172 }
179- }
180- } ) ;
173+ } ) ;
181174
182- ipcMain . on ( "set-unread-status" , ( _event , unreadStatus : boolean ) => {
183- trayManager . setUnread ( unreadStatus ) ;
184- } ) ;
175+ ipcMain . on ( "flash-main-window-if-not-focused" , ( ) => {
176+ if ( ! mainWindow . isFocused ( ) && taskbarFlashEnabled . value ) {
177+ mainWindow . flashFrame ( true ) ;
178+ if ( IS_MAC ) {
179+ app . dock . setBadge ( "•" ) ;
180+ }
181+ }
182+ } ) ;
185183
186- ipcMain . on ( "set-recent-conversations" , ( _event , data : Conversation [ ] ) => {
187- trayManager . setRecentConversations ( data ) ;
188- } ) ;
184+ ipcMain . on ( "set-unread-status" , ( _event , unreadStatus : boolean ) => {
185+ trayManager . setUnread ( unreadStatus ) ;
186+ } ) ;
187+
188+ ipcMain . on ( "set-recent-conversations" , ( _event , data : Conversation [ ] ) => {
189+ trayManager . setRecentConversations ( data ) ;
190+ } ) ;
191+ }
0 commit comments