@trodi/electron-splashscreen
Version:
Simple splashscreen for electron applications
113 lines (112 loc) • 3.83 kB
JavaScript
;
/**
* Module handles configurable splashscreen to show while app is loading.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
/**
* When splashscreen was shown.
* @ignore
*/
var splashScreenTimestamp = 0;
/**
* Splashscreen is loaded and ready to show.
* @ignore
*/
var splashScreenReady = false;
/**
* Main window has been loading for a min amount of time.
* @ignore
*/
var slowStartup = false;
/**
* True when expected work is complete and we've closed splashscreen, else user prematurely closed splashscreen.
* @ignore
*/
var done = false;
/**
* Show splashscreen if criteria are met.
* @ignore
*/
var showSplash = function () {
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
*/
var closeSplashScreen = function (main, min) {
if (splashScreen) {
var timeout = min - (Date.now() - splashScreenTimestamp);
setTimeout(function () {
done = true;
if (splashScreen) {
splashScreen.isDestroyed() || splashScreen.close(); // Avoid `Error: Object has been destroyed` (#19)
splashScreen = null;
}
main.show();
}, timeout);
}
};
/**
* The actual splashscreen browser window.
* @ignore
*/
var splashScreen;
/**
* Initializes a splashscreen that will show/hide smartly (and handle show/hiding of main window).
* @param config - Configures splashscren
* @returns {BrowserWindow} the main browser window ready for loading
*/
exports.initSplashScreen = function (config) {
var _a, _b, _c;
var xConfig = {
windowOpts: config.windowOpts,
templateUrl: config.templateUrl,
splashScreenOpts: config.splashScreenOpts,
delay: (_a = config.delay, (_a !== null && _a !== void 0 ? _a : 500)),
minVisible: (_b = config.minVisible, (_b !== null && _b !== void 0 ? _b : 500)),
closeWindow: (_c = config.closeWindow, (_c !== null && _c !== void 0 ? _c : true)),
};
xConfig.splashScreenOpts.frame = false;
xConfig.splashScreenOpts.center = true;
xConfig.splashScreenOpts.show = false;
xConfig.windowOpts.show = false;
var window = new electron_1.BrowserWindow(xConfig.windowOpts);
splashScreen = new electron_1.BrowserWindow(xConfig.splashScreenOpts);
splashScreen.loadURL("file://" + xConfig.templateUrl);
xConfig.closeWindow && splashScreen.on("close", function () {
done || window.close();
});
// Splashscreen is fully loaded and ready to view.
splashScreen.webContents.on("did-finish-load", function () {
splashScreenReady = true;
showSplash();
});
// Startup is taking enough time to show a splashscreen.
setTimeout(function () {
slowStartup = true;
showSplash();
}, xConfig.delay);
window.webContents.on("did-finish-load", function () {
closeSplashScreen(window, xConfig.minVisible);
});
return window;
};
/**
* Initializes a splashscreen that will show/hide smartly (and handle show/hiding of main window).
* Use this function if you need to send/receive info to the splashscreen (e.g., you want to send
* IPC messages to the splashscreen to inform the user of the app's loading state).
* @param config - Configures splashscren
* @returns {DynamicSplashScreen} the main browser window and the created splashscreen
*/
exports.initDynamicSplashScreen = function (config) {
return {
main: exports.initSplashScreen(config),
// initSplashScreen initializes splashscreen so this is a safe cast.
splashScreen: splashScreen,
};
};