11const { app } = require ( 'electron' ) ;
2- // yf add
3- const { BrowserWindow, dialog, shell } = require ( 'electron' )
4-
2+ const { BrowserWindow, dialog, shell } = require ( 'electron' ) ;
53const fs = require ( 'fs' ) ;
64const path = require ( 'path' ) ;
75const process = require ( 'child_process' ) . spawn ;
86const portfinder = require ( 'detect-port' ) ;
97let io , browserWindows , ipc , apiProcess , loadURL ;
108let appApi , menu , dialogApi , notification , tray , webContents ;
119let globalShortcut , shellApi , screen , clipboard ;
10+ let splashScreen , mainWindowId ;
1211
13- // yf add
14- let loadingWindow ;
15- let mainWindowId ;
16- let countDownInterval ;
17-
18- // yf add
19- const manifestJsonFile = require ( "./bin/electron.manifest.json" ) ;
12+ const manifestJsonFile = require ( './bin/electron.manifest.json' ) ;
2013if ( manifestJsonFile . singleInstance ) {
2114 const shouldQuit = app . makeSingleInstance ( ( commandLine , workingDirectory ) => {
2215 mainWindowId && BrowserWindow . fromId ( mainWindowId ) && BrowserWindow . fromId ( mainWindowId ) . show ( ) ;
2316 } ) ;
17+
2418 if ( shouldQuit ) {
2519 app . quit ( ) ;
26- return ;
2720 }
2821}
2922
3023app . on ( 'ready' , ( ) => {
31-
32- // yf add
33- startLoadingWindow ( ) ;
24+ if ( isSplashScreenEnabled ( ) ) {
25+ startSplashScreen ( ) ;
26+ }
3427
3528 portfinder ( 8000 , ( error , port ) => {
3629 startSocketApiBridge ( port ) ;
3730 } ) ;
3831} ) ;
3932
33+ function isSplashScreenEnabled ( ) {
34+ return Boolean ( manifestJsonFile . loadingUrl ) ;
35+ }
36+
37+ function startSplashScreen ( ) {
38+ let loadingUrl = manifestJsonFile . loadingUrl ;
39+ let icon = manifestJsonFile . icon ;
40+
41+ if ( loadingUrl ) {
42+ splashScreen = new BrowserWindow ( {
43+ width : manifestJsonFile . width ,
44+ height : manifestJsonFile . height ,
45+ transparent : true ,
46+ frame : false ,
47+ show : false ,
48+ icon : path . join ( __dirname , icon )
49+ } ) ;
50+
51+ if ( manifestJsonFile . devTools ) {
52+ splashScreen . webContents . openDevTools ( ) ;
53+ }
54+
55+ splashScreen . loadURL ( loadingUrl ) ;
56+ splashScreen . once ( 'ready-to-show' , ( ) => {
57+ splashScreen . show ( ) ;
58+ } ) ;
59+
60+ splashScreen . on ( 'closed' , ( ) => {
61+ splashScreen = null ;
62+ } ) ;
63+ }
64+ }
65+
4066function startSocketApiBridge ( port ) {
4167 io = require ( 'socket.io' ) ( port ) ;
42-
43- // yf add
4468 startAspCoreBackend ( port ) ;
4569
4670 io . on ( 'connection' , ( socket ) => {
47-
48- global . elesocket = socket ;
49- global . elesocket . setMaxListeners ( 0 ) ;
50- console . log ( 'ASP.NET Core Application connected...' , 'global.elesocket' , global . elesocket . id , new Date ( ) ) ;
71+ global [ 'electronsocket' ] = socket ;
72+ global [ 'electronsocket' ] . setMaxListeners ( 0 ) ;
73+ console . log ( 'ASP.NET Core Application connected...' , 'global.electronsocket' , global [ 'electronsocket' ] . id , new Date ( ) ) ;
5174
5275 appApi = require ( './api/app' ) ( socket , app ) ;
5376 browserWindows = require ( './api/browserWindows' ) ( socket ) ;
@@ -61,19 +84,21 @@ function startSocketApiBridge(port) {
6184 shellApi = require ( './api/shell' ) ( socket ) ;
6285 screen = require ( './api/screen' ) ( socket ) ;
6386 clipboard = require ( './api/clipboard' ) ( socket ) ;
87+
88+ if ( splashScreen && ! splashScreen . isDestroyed ( ) ) {
89+ splashScreen . close ( ) ;
90+ }
6491 } ) ;
6592}
6693
6794function startAspCoreBackend ( electronPort ) {
6895 portfinder ( 8000 , ( error , electronWebPort ) => {
6996 loadURL = `http://localhost:${ electronWebPort } `
7097 const parameters = [ `/electronPort=${ electronPort } ` , `/electronWebPort=${ electronWebPort } ` ] ;
98+ let binaryFile = manifestJsonFile . executable ;
7199
72- const manifestFile = require ( "./bin/electron.manifest.json" ) ;
73- let binaryFile = manifestFile . executable ;
74-
75- const os = require ( "os" ) ;
76- if ( os . platform ( ) === "win32" ) {
100+ const os = require ( 'os' ) ;
101+ if ( os . platform ( ) === 'win32' ) {
77102 binaryFile = binaryFile + '.exe' ;
78103 }
79104
@@ -82,79 +107,11 @@ function startAspCoreBackend(electronPort) {
82107 apiProcess = process ( binFilePath , parameters , options ) ;
83108
84109 apiProcess . stdout . on ( 'data' , ( data ) => {
85- var text = data . toString ( ) ;
86110 console . log ( `stdout: ${ data . toString ( ) } ` ) ;
87-
88- // yf add
89- if ( text . indexOf ( manifestFile . mainWindowShowed ) > - 1 &&
90- loadingWindow && ! loadingWindow . isDestroyed ( ) ) {
91- loadingWindow . close ( ) ;
92-
93- mainWindowId = parseInt ( text . replace ( `${ manifestFile . mainWindowShowed } :` , "" ) . trim ( ) ) ;
94- }
95111 } ) ;
96112 } ) ;
97113}
98114
99- // yf add
100- function startLoadingWindow ( ) {
101- let loadingUrl = manifestJsonFile . loadingUrl ;
102- let icon = manifestJsonFile . icon ;
103- if ( loadingUrl ) {
104- loadingWindow = new BrowserWindow ( {
105- width : manifestJsonFile . width ,
106- height : manifestJsonFile . height ,
107- transparent : true ,
108- frame : false ,
109- show : false ,
110- devTools : true ,
111- icon : path . join ( __dirname , icon )
112- } )
113- if ( manifestJsonFile . devTools ) {
114- loadingWindow . webContents . openDevTools ( ) ;
115- }
116- loadingWindow . loadURL ( loadingUrl ) ;
117- loadingWindow . once ( 'ready-to-show' , ( ) => {
118- loadingWindow . show ( )
119-
120- // 激活倒计时
121- activeCountDowInterval ( manifestJsonFile )
122- } )
123- loadingWindow . on ( 'closed' , ( ) => {
124- loadingWindow = null
125-
126- clearInterval ( countDownInterval )
127- } )
128- }
129- }
130-
131- function activeCountDowInterval ( manifestJsonFile ) {
132- if ( ! manifestJsonFile . timeout || ! manifestJsonFile . timeout . limit )
133- return
134-
135- let limitSecond = manifestJsonFile . timeout . limit
136- let currentSecond = 0 ;
137- countDownInterval = setInterval ( ( ) => {
138- currentSecond ++ ;
139- if ( currentSecond < limitSecond )
140- return ;
141-
142- clearInterval ( countDownInterval ) ;
143-
144- dialog . showMessageBox ( loadingWindow , {
145- type : manifestJsonFile . timeout . messageBox . type || 'error' ,
146- buttons : manifestJsonFile . timeout . messageBox . buttons || [ "前往安装" ] ,
147- title : manifestJsonFile . timeout . messageBox . title || '文件缺失提示' ,
148- message : manifestJsonFile . timeout . messageBox . message || '计算机缺少组件无法启动该程序,点击前往安装组件后重试' ,
149- } , ( res , isChecked ) => {
150- if ( manifestJsonFile . timeout . help )
151- shell . openExternal ( manifestJsonFile . timeout . help )
152- app . quit ( ) ;
153- } ) ;
154-
155- } , 1000 )
156- }
157-
158115//app.on('activate', () => {
159116 // On macOS it's common to re-create a window in the app when the
160117 // dock icon is clicked and there are no other windows open.
0 commit comments