Repository: electron/electron (language: cpp)

Issue:
[Bug]: BrowserWindow.showInactive Will Restore Maximized Window
### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

17.0.0-alpha.6

### What operating system are you using?

Windows

### Operating System Version

Windows 10

### What arch are you using?

x64

### Last Known Working Electron version

_No response_

### Expected Behavior

`BrowserWindow.showInactive` should not restore a maximized window

### Actual Behavior

`BrowserWindow.showInactive` does restore a maximized window

### Testcase Gist URL

https://gist.github.com/9f2c69b089f2155859bb0b439aefb668

### Additional Information

The provided gist is a test so it should be runnable by `bugbot`. I've confirmed the behavior on Windows, macOS seems unaffected, I haven't tested Linux.

~@ckerr, could you test Linux? It might be similar to #32350 where it affects both Windows and Linux but not macOS.~

Root cause may be upstream in Chromium, `ShowInactive` in Electron just passes through to `ShowInactive` on the widget, there's really no logic present.


Discussion / hints:
I've confirmed this is a Windows-only issue, and opened a CL on Chromium to fix it: https://chromium-review.googlesource.com/c/chromium/src/+/3371573

I'll see what they say about the CL, and if it is approved I'll make a PR on Electron with a patch.
Hi, Im a beginner in github and was wondering if this is beginner friendly? I have experience in c++ but if this isnt beginner friendly can you direct me to issues in this project that I can help out with? Thanks a lot.
Hi @AbhiramL! This issue isn't very beginner friendly, it involves upstream changes to Chromium, and I'm already working it. Electron has a "beginner friendly" label for issues, you can try looking at the list of issues with that label here: https://github.com/electron/electron/issues?q=is%3Aopen+is%3Aissue+label%3A%22beginner+friendly%22

Fix patch:
diff --git a/patches/chromium/.patches b/patches/chromium/.patches
index c2f0b55b8b8c2..29781b45b7925 100644
--- a/patches/chromium/.patches
+++ b/patches/chromium/.patches
@@ -113,3 +113,4 @@ revert_stop_using_nsrunloop_in_renderer_process.patch
 fix_dont_delete_SerialPortManager_on_main_thread.patch
 feat_add_data_transfer_to_requestsingleinstancelock.patch
 fix_crash_when_saving_edited_pdf_files.patch
+fix_don_t_restore_maximized_windows_when_calling_showinactive.patch
diff --git a/patches/chromium/fix_don_t_restore_maximized_windows_when_calling_showinactive.patch b/patches/chromium/fix_don_t_restore_maximized_windows_when_calling_showinactive.patch
new file mode 100644
index 0000000000000..17f355134d0f0
--- /dev/null
+++ b/patches/chromium/fix_don_t_restore_maximized_windows_when_calling_showinactive.patch
@@ -0,0 +1,42 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: David Sanders <dsanders11@ucsbalum.com>
+Date: Fri, 11 Feb 2022 22:37:39 +0000
+Subject: fix: don't restore maximized windows when calling ShowInactive
+
+This is a backport from Chromium of
+https://chromium-review.googlesource.com/c/chromium/src/+/3371573.
+
+diff --git a/ui/views/win/hwnd_message_handler.cc b/ui/views/win/hwnd_message_handler.cc
+index 067861bb743ee2f3c1916794d45efb7dd591b230..6507557bf5a47492343602607e0dbb7d8f88246d 100644
+--- a/ui/views/win/hwnd_message_handler.cc
++++ b/ui/views/win/hwnd_message_handler.cc
+@@ -664,9 +664,16 @@ void HWNDMessageHandler::Show(ui::WindowShowState show_state,
+     SetWindowPlacement(hwnd(), &placement);
+     native_show_state = SW_SHOWMAXIMIZED;
+   } else {
++    const bool is_maximized = IsMaximized();
++
++    // Use SW_SHOW/SW_SHOWNA instead of SW_SHOWNORMAL/SW_SHOWNOACTIVATE so that
++    // the window is not restored to its original position if it is maximized.
++    // This could be used unconditionally for ui::SHOW_STATE_INACTIVE, but
++    // cross-platform behavior when showing a minimized window is inconsistent,
++    // some platforms restore the position, some do not. See crbug.com/1296710
+     switch (show_state) {
+       case ui::SHOW_STATE_INACTIVE:
+-        native_show_state = SW_SHOWNOACTIVATE;
++        native_show_state = is_maximized ? SW_SHOWNA : SW_SHOWNOACTIVATE;
+         break;
+       case ui::SHOW_STATE_MAXIMIZED:
+         native_show_state = SW_SHOWMAXIMIZED;
+@@ -677,9 +684,9 @@ void HWNDMessageHandler::Show(ui::WindowShowState show_state,
+       case ui::SHOW_STATE_NORMAL:
+         if ((GetWindowLong(hwnd(), GWL_EXSTYLE) & WS_EX_TRANSPARENT) ||
+             (GetWindowLong(hwnd(), GWL_EXSTYLE) & WS_EX_NOACTIVATE)) {
+-          native_show_state = SW_SHOWNOACTIVATE;
++          native_show_state = is_maximized ? SW_SHOWNA : SW_SHOWNOACTIVATE;
+         } else {
+-          native_show_state = SW_SHOWNORMAL;
++          native_show_state = is_maximized ? SW_SHOW : SW_SHOWNORMAL;
+         }
+         break;
+       case ui::SHOW_STATE_FULLSCREEN:
diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts
index c181cb4f29b44..d7585b3c07a54 100644
--- a/spec-main/api-browser-window-spec.ts
+++ b/spec-main/api-browser-window-spec.ts
@@ -735,6 +735,22 @@ describe('BrowserWindow module', () => {
         w.showInactive();
         expect(w.isFocused()).to.equal(false);
       });
+
+      // TODO(dsanders11): Enable for Linux once CI plays nice with these kinds of tests
+      ifit(process.platform !== 'linux')('should not restore maximized windows', async () => {
+        const maximize = emittedOnce(w, 'maximize');
+        const shown = emittedOnce(w, 'show');
+        w.maximize();
+        // TODO(dsanders11): The maximize event isn't firing on macOS for a window initially hidden
+        if (process.platform !== 'darwin') {
+          await maximize;
+        } else {
+          await delay(1000);
+        }
+        w.showInactive();
+        await shown;
+        expect(w.isMaximized()).to.equal(true);
+      });
     });
 
     describe('BrowserWindow.focus()', () => {


Test patch: