[MM 19649] Fix blank window when auto-starting the app on login (#1106)

* Windows: fix hidden/shown/maximized window on start

* allow for some overlap with the top/left edge of the screen before resetting

* support maximizing directly from app tray icon after initial startup

* inline comments

* alternative to prevent minimize effect on startup

* default hideOnStartup to false

* update comment
This commit is contained in:
Dean Whillier 2019-11-19 17:02:49 -05:00 committed by Guillermo Vayá
parent 395cbf9c9e
commit d23c8cf48a
3 changed files with 43 additions and 22 deletions

View file

@ -544,6 +544,7 @@ function initializeAfterAppReady() {
mainWindow = createMainWindow(config.data, { mainWindow = createMainWindow(config.data, {
hideOnStartup, hideOnStartup,
trayIconShown: process.platform === 'win32' || config.showTrayIcon,
linuxAppIcon: path.join(assetsDir, 'appicon.png'), linuxAppIcon: path.join(assetsDir, 'appicon.png'),
deeplinkingUrl, deeplinkingUrl,
}); });

View file

@ -8,6 +8,8 @@ const defaultOptions = {
stripUnknown: true, stripUnknown: true,
}; };
const defaultMinWindowXPos = -100;
const defaultMinWindowYPos = -100;
const defaultWindowWidth = 1000; const defaultWindowWidth = 1000;
const defaultWindowHeight = 700; const defaultWindowHeight = 700;
const minWindowWidth = 400; const minWindowWidth = 400;
@ -23,8 +25,8 @@ const argsSchema = Joi.object({
}); });
const boundsInfoSchema = Joi.object({ const boundsInfoSchema = Joi.object({
x: Joi.number().integer().min(0), x: Joi.number().integer().min(defaultMinWindowXPos).default(0),
y: Joi.number().integer().min(0), y: Joi.number().integer().min(defaultMinWindowYPos).default(0),
width: Joi.number().integer().min(minWindowWidth).required().default(defaultWindowWidth), width: Joi.number().integer().min(minWindowWidth).required().default(defaultWindowWidth),
height: Joi.number().integer().min(minWindowHeight).required().default(defaultWindowHeight), height: Joi.number().integer().min(minWindowHeight).required().default(defaultWindowHeight),
maximized: Joi.boolean().default(false), maximized: Joi.boolean().default(false),

View file

@ -39,13 +39,16 @@ function createMainWindow(config, options) {
windowOptions = {width: defaultWindowWidth, height: defaultWindowHeight}; windowOptions = {width: defaultWindowWidth, height: defaultWindowHeight};
} }
const {hideOnStartup, trayIconShown} = options;
const {maximized: windowIsMaximized} = windowOptions;
if (process.platform === 'linux') { if (process.platform === 'linux') {
windowOptions.icon = options.linuxAppIcon; windowOptions.icon = options.linuxAppIcon;
} }
Object.assign(windowOptions, { Object.assign(windowOptions, {
title: app.getName(), title: app.getName(),
fullscreenable: true, fullscreenable: true,
show: false, show: hideOnStartup || false,
minWidth: minimumWindowWidth, minWidth: minimumWindowWidth,
minHeight: minimumWindowHeight, minHeight: minimumWindowHeight,
fullscreen: false, fullscreen: false,
@ -63,34 +66,49 @@ function createMainWindow(config, options) {
const indexURL = global.isDev ? 'http://localhost:8080/browser/index.html' : `file://${app.getAppPath()}/browser/index.html`; const indexURL = global.isDev ? 'http://localhost:8080/browser/index.html' : `file://${app.getAppPath()}/browser/index.html`;
mainWindow.loadURL(indexURL); mainWindow.loadURL(indexURL);
// This section should be called after loadURL() #570 // handle hiding the app when launched by auto-start
if (options.hideOnStartup) { if (hideOnStartup) {
if (windowOptions.maximized) { if (trayIconShown && process.platform !== 'darwin') {
mainWindow.maximize(); mainWindow.hide();
} } else {
// on MacOS, the window is already hidden until 'ready-to-show'
if (process.platform !== 'darwin') {
mainWindow.minimize(); mainWindow.minimize();
} }
} else if (windowOptions.maximized) {
mainWindow.maximize();
} }
mainWindow.once('ready-to-show', () => {
mainWindow.webContents.setZoomLevel(0);
// handle showing the window when not launched by auto-start
// - when not configured to auto-start, immediately show contents and optionally maximize as needed
if (!hideOnStartup) {
mainWindow.show();
if (windowIsMaximized) {
mainWindow.maximize();
}
}
});
mainWindow.once('show', () => {
// handle showing the app when hidden to the tray icon by auto-start
// - optionally maximize the window as needed
if (hideOnStartup && windowIsMaximized) {
mainWindow.maximize();
}
});
mainWindow.once('restore', () => {
// handle restoring the window when minimized to the app icon by auto-start
// - optionally maximize the window as needed
if (hideOnStartup && windowIsMaximized) {
mainWindow.maximize();
}
});
mainWindow.webContents.on('will-attach-webview', (event, webPreferences) => { mainWindow.webContents.on('will-attach-webview', (event, webPreferences) => {
webPreferences.nodeIntegration = false; webPreferences.nodeIntegration = false;
webPreferences.contextIsolation = true; webPreferences.contextIsolation = true;
}); });
mainWindow.once('ready-to-show', () => {
mainWindow.webContents.setZoomLevel(0);
if (process.platform !== 'darwin') {
mainWindow.show();
} else if (options.hideOnStartup !== true) {
mainWindow.show();
}
});
// App should save bounds when a window is closed. // App should save bounds when a window is closed.
// However, 'close' is not fired in some situations(shutdown, ctrl+c) // However, 'close' is not fired in some situations(shutdown, ctrl+c)
// because main process is killed in such situations. // because main process is killed in such situations.