-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathsplash-screen.ts
158 lines (150 loc) · 5.33 KB
/
splash-screen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
MIT License
Copyright (c) 2017 Troy McKinnon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Copied from https://raw.githubusercontent.com/trodi/electron-splashscreen/2f5052a133be021cbf9a438d0ef4719cd1796b75/index.ts
/**
* Module handles configurable splashscreen to show while app is loading.
*/
import { Event } from '@theia/core/lib/common/event';
import { BrowserWindow } from 'electron';
/**
* When splashscreen was shown.
* @ignore
*/
let splashScreenTimestamp = 0;
/**
* Splashscreen is loaded and ready to show.
* @ignore
*/
let splashScreenReady = false;
/**
* Main window has been loading for a min amount of time.
* @ignore
*/
let slowStartup = false;
/**
* True when expected work is complete and we've closed splashscreen, else user prematurely closed splashscreen.
* @ignore
*/
let done = false;
/**
* Show splashscreen if criteria are met.
* @ignore
*/
const showSplash = () => {
if (splashScreen && splashScreenReady && slowStartup) {
splashScreen.show();
splashScreenTimestamp = Date.now();
}
};
/**
* Close splashscreen / show main screen. Ensure screen is visible for a min amount of time.
* @ignore
*/
const closeSplashScreen = (main: Electron.BrowserWindow, min: number): void => {
if (splashScreen) {
const timeout = min - (Date.now() - splashScreenTimestamp);
setTimeout(() => {
done = true;
if (splashScreen) {
splashScreen.isDestroyed() || splashScreen.close(); // Avoid `Error: Object has been destroyed` (#19)
splashScreen = null;
}
if (!main.isDestroyed()) {
main.show();
}
}, timeout);
}
};
/** `electron-splashscreen` config object. */
export interface Config {
/** Options for the window that is loading and having a splashscreen tied to. */
windowOpts: Electron.BrowserWindowConstructorOptions;
/**
* URL to the splashscreen template. This is the path to an `HTML` or `SVG` file.
* If you want to simply show a `PNG`, wrap it in an `HTML` file.
*/
templateUrl: string;
/**
* Full set of browser window options for the splashscreen. We override key attributes to
* make it look & feel like a splashscreen; the rest is up to you!
*/
splashScreenOpts: Electron.BrowserWindowConstructorOptions;
/** Number of ms the window will load before splashscreen appears (default: 500ms). */
delay?: number;
/** Minimum ms the splashscreen will be visible (default: 500ms). */
minVisible?: number;
/** Close window that is loading if splashscreen is closed by user (default: true). */
closeWindow?: boolean;
}
/**
* The actual splashscreen browser window.
* @ignore
*/
let splashScreen: Electron.BrowserWindow | null;
/**
* Initializes a splashscreen that will show/hide smartly (and handle show/hiding of main window).
* @param config - Configures splashscreen
* @returns {BrowserWindow} the main browser window ready for loading
*/
export const initSplashScreen = async (
config: Config,
windowFactory: (
options: Electron.BrowserViewConstructorOptions
) => Promise<BrowserWindow>,
onCloseRequested?: Event<void>
): Promise<BrowserWindow> => {
const xConfig: Required<Config> = {
windowOpts: config.windowOpts,
templateUrl: config.templateUrl,
splashScreenOpts: config.splashScreenOpts,
delay: config.delay ?? 500,
minVisible: config.minVisible ?? 500,
closeWindow: config.closeWindow ?? true,
};
xConfig.splashScreenOpts.center = true;
xConfig.splashScreenOpts.frame = false;
xConfig.windowOpts.show = false;
const window = await windowFactory(xConfig.windowOpts);
splashScreen = new BrowserWindow(xConfig.splashScreenOpts);
splashScreen.loadURL(`file://${xConfig.templateUrl}`);
xConfig.closeWindow &&
splashScreen.on('close', () => {
done || window.close();
});
// Splashscreen is fully loaded and ready to view.
splashScreen.webContents.on('did-finish-load', () => {
splashScreenReady = true;
showSplash();
});
// Startup is taking enough time to show a splashscreen.
setTimeout(() => {
slowStartup = true;
showSplash();
}, xConfig.delay);
if (onCloseRequested) {
onCloseRequested(() => closeSplashScreen(window, xConfig.minVisible));
} else {
window.webContents.on('did-finish-load', () => {
closeSplashScreen(window, xConfig.minVisible);
});
}
window.on('closed', () => closeSplashScreen(window, 0)); // XXX: close splash when main window is closed
return window;
};