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