1
- const { app} = require ( 'electron' )
1
+ const { app, powerMonitor } = require ( 'electron' ) ;
2
+ const path = require ( 'path' )
2
3
3
- app . on ( 'ready' , global . p3x . onenote . createWindow . onenote ) ;
4
+ let isInSuspended = false ;
4
5
5
- app . on ( 'window-all-closed' , function ( ) {
6
+ const { net } = require ( 'electron' ) ;
7
+ function waitForNetworkConnectivity ( callback , retries = 60 , interval = 1000 ) {
8
+ let attempts = 0 ;
9
+
10
+ function checkNetwork ( ) {
11
+ const request = net . request ( 'https://www.bing.com' ) ; // Use any lightweight URL
12
+ request . on ( 'response' , ( ) => {
13
+ console . log ( 'Network is available' ) ;
14
+ callback ( ) ;
15
+ } ) ;
16
+ request . on ( 'error' , ( ) => {
17
+ if ( attempts < retries ) {
18
+ attempts ++ ;
19
+ console . log ( `Waiting for network (${ attempts } /${ retries } )...` ) ;
20
+ setTimeout ( checkNetwork , interval ) ;
21
+ } else {
22
+ console . error ( 'Network unavailable after retries.' ) ;
23
+ callback ( ) ;
24
+ }
25
+ } ) ;
26
+ request . end ( ) ;
27
+ }
28
+
29
+ checkNetwork ( ) ;
30
+ }
31
+
32
+ app . on ( 'ready' , ( ) => {
33
+ console . log ( 'P3X-OneNote is ready' ) ;
34
+
35
+ // Create the main window
36
+ global . p3x . onenote . createWindow . onenote ( ) ;
37
+
38
+
39
+ /*
40
+ // Handle power events
41
+ powerMonitor.on('suspend', () => {
42
+ if (isInSuspended) {
43
+ return
44
+ }
45
+ isInSuspended = true;
46
+ console.log('System is suspending...');
47
+ // Close the window when the system goes to sleep
48
+ //if (global.p3x.onenote.window.onenote) {
49
+ // global.p3x.onenote.window.onenote.loadURL('about:blank');
50
+ // global.p3x.onenote.window.onenote.hide();
51
+ //}
52
+ });
6
53
7
- // if (process.platform !== 'darwin') {
54
+ powerMonitor.on('resume', () => {
55
+ if (!isInSuspended) {
56
+ return
57
+ }
58
+ isInSuspended = false;
59
+ console.log('System has resumed...');
60
+
61
+ global.p3x.onenote.window.onenote.loadURL(`about:blank`);
62
+ waitForNetworkConnectivity(() => {
63
+ const url = path.join(app.getAppPath(), 'src/electron/window/onenote/index.html');
64
+ console.log('resume url', url)
65
+ global.p3x.onenote.window.onenote.loadURL(`file://${url}`);
66
+ });
67
+ });
68
+ */
69
+ } ) ;
70
+
71
+ app . on ( 'window-all-closed' , function ( ) {
72
+ if ( ! isInSuspended ) {
8
73
app . quit ( ) ;
9
- // }
74
+ }
10
75
} ) ;
11
76
12
77
app . on ( 'activate' , function ( ) {
@@ -15,13 +80,10 @@ app.on('activate', function () {
15
80
}
16
81
} ) ;
17
82
18
-
19
- //FIXME: webview new-window changed
20
83
app . on ( 'web-contents-created' , function ( webContentsCreatedEvent , contents ) {
21
84
if ( contents . getType ( ) === 'webview' ) {
22
85
contents . on ( 'new-window' , function ( newWindowEvent , url ) {
23
86
newWindowEvent . preventDefault ( ) ;
24
87
} ) ;
25
88
}
26
89
} ) ;
27
-
0 commit comments