Skip to content

Commit

Permalink
Fix blank window on load on desktop version #348
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
undergroundwires committed May 3, 2024
1 parent 66a5688 commit 813d820
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/presentation/electron/main/index.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

0 comments on commit 813d820

Please sign in to comment.