1
- import { app , dialog , BrowserWindow , shell , ipcMain } from 'electron'
2
- import * as path from 'path'
1
+ import { app , dialog , BrowserWindow , shell , ipcMain } from 'electron' ;
2
+ import * as path from 'path' ;
3
3
4
- let mainWindow : BrowserWindow | null = null
4
+ let mainWindow : BrowserWindow | null = null ;
5
5
6
6
// Quit when all windows are closed.
7
7
app . on ( 'window-all-closed' , ( ) => {
8
- app . quit ( )
9
- } )
8
+ app . quit ( ) ;
9
+ } ) ;
10
10
11
11
function decorateURL ( url : string ) {
12
12
// safely add `?utm_source=default_app
13
- const parsedUrl = new URL ( url )
14
- parsedUrl . searchParams . append ( 'utm_source' , 'default_app' )
15
- return parsedUrl . toString ( )
13
+ const parsedUrl = new URL ( url ) ;
14
+ parsedUrl . searchParams . append ( 'utm_source' , 'default_app' ) ;
15
+ return parsedUrl . toString ( ) ;
16
16
}
17
17
18
18
// Find the shortest path to the electron binary
19
- const absoluteElectronPath = process . execPath
20
- const relativeElectronPath = path . relative ( process . cwd ( ) , absoluteElectronPath )
19
+ const absoluteElectronPath = process . execPath ;
20
+ const relativeElectronPath = path . relative ( process . cwd ( ) , absoluteElectronPath ) ;
21
21
const electronPath = absoluteElectronPath . length < relativeElectronPath . length
22
22
? absoluteElectronPath
23
- : relativeElectronPath
23
+ : relativeElectronPath ;
24
24
25
- const indexPath = path . resolve ( app . getAppPath ( ) , 'index.html' )
25
+ const indexPath = path . resolve ( app . getAppPath ( ) , 'index.html' ) ;
26
26
27
27
function isTrustedSender ( webContents : Electron . WebContents ) {
28
28
if ( webContents !== ( mainWindow && mainWindow . webContents ) ) {
29
- return false
29
+ return false ;
30
30
}
31
31
32
- const parsedUrl = new URL ( webContents . getURL ( ) )
32
+ const parsedUrl = new URL ( webContents . getURL ( ) ) ;
33
33
const urlPath = process . platform === 'win32'
34
34
// Strip the prefixed "/" that occurs on windows
35
35
? path . resolve ( parsedUrl . pathname . substr ( 1 ) )
36
- : parsedUrl . pathname
37
- return parsedUrl . protocol === 'file:' && urlPath === indexPath
36
+ : parsedUrl . pathname ;
37
+ return parsedUrl . protocol === 'file:' && urlPath === indexPath ;
38
38
}
39
39
40
40
ipcMain . handle ( 'bootstrap' , ( event ) => {
41
- return isTrustedSender ( event . sender ) ? electronPath : null
42
- } )
41
+ return isTrustedSender ( event . sender ) ? electronPath : null ;
42
+ } ) ;
43
43
44
44
async function createWindow ( ) {
45
- await app . whenReady ( )
45
+ await app . whenReady ( ) ;
46
46
47
47
const options : Electron . BrowserWindowConstructorOptions = {
48
48
width : 900 ,
@@ -57,46 +57,46 @@ async function createWindow () {
57
57
} ,
58
58
useContentSize : true ,
59
59
show : false
60
- }
60
+ } ;
61
61
62
62
if ( process . platform === 'linux' ) {
63
- options . icon = path . join ( __dirname , 'icon.png' )
63
+ options . icon = path . join ( __dirname , 'icon.png' ) ;
64
64
}
65
65
66
- mainWindow = new BrowserWindow ( options )
67
- mainWindow . on ( 'ready-to-show' , ( ) => mainWindow ! . show ( ) )
66
+ mainWindow = new BrowserWindow ( options ) ;
67
+ mainWindow . on ( 'ready-to-show' , ( ) => mainWindow ! . show ( ) ) ;
68
68
69
69
mainWindow . webContents . on ( 'new-window' , ( event , url ) => {
70
- event . preventDefault ( )
71
- shell . openExternal ( decorateURL ( url ) )
72
- } )
70
+ event . preventDefault ( ) ;
71
+ shell . openExternal ( decorateURL ( url ) ) ;
72
+ } ) ;
73
73
74
74
mainWindow . webContents . session . setPermissionRequestHandler ( ( webContents , permission , done ) => {
75
- const parsedUrl = new URL ( webContents . getURL ( ) )
75
+ const parsedUrl = new URL ( webContents . getURL ( ) ) ;
76
76
77
77
const options : Electron . MessageBoxOptions = {
78
78
title : 'Permission Request' ,
79
79
message : `Allow '${ parsedUrl . origin } ' to access '${ permission } '?` ,
80
80
buttons : [ 'OK' , 'Cancel' ] ,
81
81
cancelId : 1
82
- }
82
+ } ;
83
83
84
84
dialog . showMessageBox ( mainWindow ! , options ) . then ( ( { response } ) => {
85
- done ( response === 0 )
86
- } )
87
- } )
85
+ done ( response === 0 ) ;
86
+ } ) ;
87
+ } ) ;
88
88
89
- return mainWindow
89
+ return mainWindow ;
90
90
}
91
91
92
92
export const loadURL = async ( appUrl : string ) => {
93
- mainWindow = await createWindow ( )
94
- mainWindow . loadURL ( appUrl )
95
- mainWindow . focus ( )
96
- }
93
+ mainWindow = await createWindow ( ) ;
94
+ mainWindow . loadURL ( appUrl ) ;
95
+ mainWindow . focus ( ) ;
96
+ } ;
97
97
98
98
export const loadFile = async ( appPath : string ) => {
99
- mainWindow = await createWindow ( )
100
- mainWindow . loadFile ( appPath )
101
- mainWindow . focus ( )
102
- }
99
+ mainWindow = await createWindow ( ) ;
100
+ mainWindow . loadFile ( appPath ) ;
101
+ mainWindow . focus ( ) ;
102
+ } ;
0 commit comments