Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix window maximization when using splash screen #14219

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

tortmayr
Copy link
Contributor

@tortmayr tortmayr commented Sep 25, 2024

What it does

Avoid eager restore of maximized state in TheiaElectronWindow.restoreMaximizeState
Calling window.maximize() implicitly also makes the window visible.
Therefore we have to check if the window is already visibible before restoring t he maximizeState.
If the window is not yet visible yet we restore the state once the window is shown.

This fixes and issue were both the splashscreen and the preload window are be visible at the same time.

Fixes #14218

How to test

  1. Build electron example app
  2. Start electron example app
  3. Maximize window and close app
  4. Start app again

Only the splash screen should be shown, main window should remain invisible until the splash screen is hidden.

Follow-ups

Review checklist

Reminder for reviewers

Comment on lines 199 to 201
if (state === 'ready' && this.options.preventAutomaticShow) {
this.restoreMaximizedState();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that here is the best place to put the maximize logic. I agree that this fixes the issue, however this depends on the fact that we know that the main application will call show once the application state is 'ready'.

What do you think of the following:

  • If a Window is immediately shown (i.e. this.options.show === true || typeof this.options.show === 'undefined'), we directly call the restoreMaximizedState
  • If a Window is not immediately shown we
    • return a Proxy of BrowserWindow
    • The proxy overrides the show of BrowserWindow
    • On the first time show is called, we also implicitly call restoreMaximizedState

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO using a proxy approach here is kind of an overkill. Maybe it would be better to move the maximization logic into the main application entirely. Here we know exactly when the main window will be shown and can invoke the maximization at this point if nencessary.
It could look like this:

diff
diff --git a/packages/core/src/electron-main/electron-main-application.ts b/packages/core/src/electron-main/electron-main-application.ts
index b464c916a84..13f7fe127b7 100644
--- a/packages/core/src/electron-main/electron-main-application.ts
+++ b/packages/core/src/electron-main/electron-main-application.ts
@@ -334,19 +334,22 @@ export class ElectronMainApplication {
                 });
                 if (this.isShowSplashScreen()) {
                     console.log('Showing splash screen');
-                    this.configureAndShowSplashScreen(this.initialWindow);
+                    this.configureAndShowSplashScreen(this.initialWindow, options.isMaximized);
                 }

                 // Show main window early if windows shall be shown early and splash screen is not configured
                 if (this.isShowWindowEarly() && !this.isShowSplashScreen()) {
                     console.log('Showing main window early');
                     this.initialWindow.show();
+                    if (options.isMaximized) {
+                        this.initialWindow.maximize();
+                    }
                 }
             });
         }
     }

-    protected async configureAndShowSplashScreen(mainWindow: BrowserWindow): Promise<BrowserWindow> {
+    protected async configureAndShowSplashScreen(mainWindow: BrowserWindow, isMaximized?: boolean): Promise<BrowserWindow> {
         const splashScreenOptions = this.getSplashScreenOptions()!;
         console.debug('SplashScreen options', splashScreenOptions);

@@ -382,6 +385,9 @@ export class ElectronMainApplication {
             cancelTokenSource.cancel();
             if (!mainWindow.isVisible()) {
                 mainWindow.show();
+                if (isMaximized) {
+                    mainWindow.maximize();
+                }
             }
             splashScreenWindow.close();
         };
diff --git a/packages/core/src/electron-main/theia-electron-window.ts b/packages/core/src/electron-main/theia-electron-window.ts
index 720865f8f59..4faa5852fb7 100644
--- a/packages/core/src/electron-main/theia-electron-window.ts
+++ b/packages/core/src/electron-main/theia-electron-window.ts
@@ -86,7 +86,6 @@ export class TheiaElectronWindow {
         if (!this.options.preventAutomaticShow) {
             this.attachReadyToShow();
         }
-        this.restoreMaximizedState();
         this.attachCloseListeners();
         this.trackApplicationState();
         this.attachReloadListener();
@@ -185,14 +184,6 @@ export class TheiaElectronWindow {
         return TheiaRendererAPI.requestClose(this.window.webContents, reason);
     }

-    protected restoreMaximizedState(): void {
-        if (this.options.isMaximized) {
-            this._window.maximize();
-        } else {
-            this._window.unmaximize();
-        }
-    }
-
     protected trackApplicationState(): void {
         this.toDispose.push(TheiaRendererAPI.onApplicationStateChanged(this.window.webContents, state => {
             this.applicationState = state;

WDYT?

Sidenote: options.show is currently set to false by default in Theia and not considered at all in the initialization logic.
I think we should keep it that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me in general. We just need to make sure that when the window is shown automatically, without the main application calling show itself, we also restore the maximized state.

@tsmaeder
Copy link
Contributor

@sdirix @tortmayr what's the state on this one?

@sdirix
Copy link
Member

sdirix commented Nov 26, 2024

@sdirix @tortmayr what's the state on this one?

@tortmayr Can you update the PR as discussed?

Avoid eager restore of maximized state in `TheiaElectronWindow.restoreMaximizeState`
Calling `window.maximize()` implicitly also makes the window visible.
Therefore we have to check if the window is already visibible before restoring t he maximizeState.
If the window is not yet visible yet we restore the state once the window is shown.

Fixes eclipse-theia#14218
@tortmayr tortmayr force-pushed the tortmayr/issues/14218 branch from d969dc1 to 3a67a32 Compare December 4, 2024 17:32
@tortmayr
Copy link
Contributor Author

tortmayr commented Dec 4, 2024

Sorry, for the long roundtrip. I was busy with other projects and didn't find the time to work on this PR.
I have now pushed an update.
In the end I found a pretty simple solution for this issue.
In the restoreMaximizeState method we simply have to check if the window is already visible.
If so restore the state directly.
If not we have listen for the next show event on the window and restore the state then.

@sdirix Could you please rereview this change?

@tortmayr tortmayr requested a review from sdirix December 4, 2024 17:40
Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! Great final solution.

@sdirix sdirix merged commit 53c09a8 into eclipse-theia:master Dec 12, 2024
11 checks passed
@sdirix sdirix deleted the tortmayr/issues/14218 branch December 12, 2024 09:35
@github-actions github-actions bot added this to the 1.57.0 milestone Dec 12, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[electron] Splash screen behavior is inconsistent with regards to starting maximized/unmaximized
3 participants