typescriptmacoselectronwindow

Electron window flashes white upon showing despite graceful display


I am trying to display a window with black content inside a BrowserWindow:

changeShortcutWindow = new BrowserWindow({
    height: 400,
    width: 600,
    show: false, // will be displayed upon ready-to-show
    resizable: false,
    maximizable: false,
    titleBarStyle: 'hidden', // make the content full-sized, while preserving native controls
    backgroundColor: "#000", // make the window background same as contentview background to avoid visual flash
    webPreferences: {
      preload: CHANGE_SHORTCUT_PRELOAD_WEBPACK_ENTRY,
    }
  });

changeShortcutWindow.loadURL(CHANGE_SHORTCUT_WEBPACK_ENTRY);

However upon loading the local URL, the window flashes white (completely empty, for one frame, I checked a screen recording) before it displays the full black content:

empty content filledContent

Based on the documentation you are supposed to wait for ready-to-show event but I am doing that. I am also setting the background color of the window to black (as seen above) and that doesn't help either.

changeShortcutWindow.once('ready-to-show', () => { // show gracefully, without a visual flash
    console.log("Ready to show");
    changeShortcutWindow.show();
  })

waiting for did-finish-load also did not help

changeShortcutWindow.webContents.on('did-finish-load', () => {
    console.log("finish load");
  })

awaiting loadURL also does not help.

await changeShortcutWindow.loadURL(CHANGE_SHORTCUT_WEBPACK_ENTRY);

NOTE: This seems to only happen during the first show of the window. Subsequent inits and shows of the window respect the background color.

Does anyone know how to prevent this?


Solution

  • I had this problem and fixed it by displaying offscreen then doing setPosition after ~100msec. Maybe a shorter delay could work too. Worked on windows, have not tried mac yet. Create the window with x=-20000, or calculate based on screens. Then just bw.setPosition(actualX, y).