From 813d820b85e1b623c50f8e0325ad372bf2f344f9 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Fri, 3 May 2024 12:03:36 +0200 Subject: [PATCH] Fix blank window on load on desktop version #348 This commit updates the application startup behavior to prevent showing a blank window until it's fully loaded on all platforms. This enhancement improves the user experience by ensuring the UI only becomes visible when it is ready to interact with. This fix contributes to a smoother user experience by aligning the window display timing with content readiness, thus avoiding the brief display of an empty screen. Changes: - Set window to initially hide until fully loaded using the `ready-to-show` event. - Show the window, focus on it and bring it front once it is loaded. Windows requires additional logic to put Window to front, see electron/electron#2867. - Parametrize the behavior of opening developer tools for easier configuration during testing. --- src/presentation/electron/main/index.ts | 37 +++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/presentation/electron/main/index.ts b/src/presentation/electron/main/index.ts index c8a9bdd7..bb6d79d4 100644 --- a/src/presentation/electron/main/index.ts +++ b/src/presentation/electron/main/index.ts @@ -14,6 +14,8 @@ import { } from './ElectronConfig'; import { registerAllIpcChannels } from './IpcRegistration'; +const hideWindowUntilLoaded = true; +const openDevToolsOnDevelopment = true; const isDevelopment = !app.isPackaged; // Keep a global reference of the window object, if you don't, the window will @@ -48,12 +50,12 @@ function createWindow() { preload: PRELOADER_SCRIPT_PATH, }, icon: APP_ICON_PATH, + show: !hideWindowUntilLoaded, }); - + focusAndShowOnceLoaded(win); win.setMenuBarVisibility(false); configureExternalsUrlsOpenBrowser(win); loadApplication(win); - win.on('closed', () => { win = null; }); @@ -101,9 +103,8 @@ function loadApplication(window: BrowserWindow) { } else { loadUrlWithNodeWorkaround(window, RENDERER_HTML_PATH); } - if (isDevelopment) { - window.webContents.openDevTools(); - } else { + openDevTools(window); + if (!isDevelopment) { const updater = setupAutoUpdater(); updater.checkForUpdates(); } @@ -165,3 +166,29 @@ function configureAppQuitBehavior() { }); } } + +function focusAndShowOnceLoaded(window: BrowserWindow) { + window.once('ready-to-show', () => { + window.show(); // Shows and focuses + bringToFront(window); + }); +} + +function bringToFront(window: BrowserWindow) { + // Only needed for Windows, tested on GNOME 42.5, Windows 11 23H2 Pro and macOS Sonoma 14.4.1. + // Some report it's also needed for some versions of GNOME. + // - https://github.com/electron/electron/issues/2867#issuecomment-409858459 + // - https://github.com/signalapp/Signal-Desktop/blob/0999df2d6e93da805b2135f788ffc739ba69832d/app/SystemTrayService.ts#L277-L284 + window.setAlwaysOnTop(true); + window.setAlwaysOnTop(false); +} + +function openDevTools(window: BrowserWindow) { + if (!isDevelopment) { + return; + } + if (!openDevToolsOnDevelopment) { + return; + } + window.webContents.openDevTools(); +}