From d23c8cf48a4c90b371919f2b367e232dd2c206f1 Mon Sep 17 00:00:00 2001 From: Dean Whillier Date: Tue, 19 Nov 2019 17:02:49 -0500 Subject: [PATCH] [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 --- src/main.js | 1 + src/main/Validator.js | 6 +++-- src/main/mainWindow.js | 58 +++++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/main.js b/src/main.js index 01e41b4e..1fa0b44f 100644 --- a/src/main.js +++ b/src/main.js @@ -544,6 +544,7 @@ function initializeAfterAppReady() { mainWindow = createMainWindow(config.data, { hideOnStartup, + trayIconShown: process.platform === 'win32' || config.showTrayIcon, linuxAppIcon: path.join(assetsDir, 'appicon.png'), deeplinkingUrl, }); diff --git a/src/main/Validator.js b/src/main/Validator.js index 01abe01f..e756b5a1 100644 --- a/src/main/Validator.js +++ b/src/main/Validator.js @@ -8,6 +8,8 @@ const defaultOptions = { stripUnknown: true, }; +const defaultMinWindowXPos = -100; +const defaultMinWindowYPos = -100; const defaultWindowWidth = 1000; const defaultWindowHeight = 700; const minWindowWidth = 400; @@ -23,8 +25,8 @@ const argsSchema = Joi.object({ }); const boundsInfoSchema = Joi.object({ - x: Joi.number().integer().min(0), - y: Joi.number().integer().min(0), + x: Joi.number().integer().min(defaultMinWindowXPos).default(0), + y: Joi.number().integer().min(defaultMinWindowYPos).default(0), width: Joi.number().integer().min(minWindowWidth).required().default(defaultWindowWidth), height: Joi.number().integer().min(minWindowHeight).required().default(defaultWindowHeight), maximized: Joi.boolean().default(false), diff --git a/src/main/mainWindow.js b/src/main/mainWindow.js index 8aa74a0b..890835ca 100644 --- a/src/main/mainWindow.js +++ b/src/main/mainWindow.js @@ -39,13 +39,16 @@ function createMainWindow(config, options) { windowOptions = {width: defaultWindowWidth, height: defaultWindowHeight}; } + const {hideOnStartup, trayIconShown} = options; + const {maximized: windowIsMaximized} = windowOptions; + if (process.platform === 'linux') { windowOptions.icon = options.linuxAppIcon; } Object.assign(windowOptions, { title: app.getName(), fullscreenable: true, - show: false, + show: hideOnStartup || false, minWidth: minimumWindowWidth, minHeight: minimumWindowHeight, 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`; mainWindow.loadURL(indexURL); - // This section should be called after loadURL() #570 - if (options.hideOnStartup) { - if (windowOptions.maximized) { - mainWindow.maximize(); - } - - // on MacOS, the window is already hidden until 'ready-to-show' - if (process.platform !== 'darwin') { + // handle hiding the app when launched by auto-start + if (hideOnStartup) { + if (trayIconShown && process.platform !== 'darwin') { + mainWindow.hide(); + } else { 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) => { webPreferences.nodeIntegration = false; 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. // However, 'close' is not fired in some situations(shutdown, ctrl+c) // because main process is killed in such situations.