Repository: electron/electron (language: cpp)

Issue:
[Bug]: BrowserWindow.maximize Does Not Show Window on macOS
### 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

19.0.0-nightly.20220209

### What operating system are you using?

macOS

### Operating System Version

macOS 11.6.2

### What arch are you using?

x64

### Last Known Working Electron version

_No response_

### Expected Behavior

Maximizing a window which isn't being shown should show the window. According to the docs for `BrowserWindow.maximize()`:

> Maximizes the window. This will also show (but not focus) the window if it isn't being displayed already.

### Actual Behavior

On macOS, the window is not shown, but `window.isMaximized()` returns true, and a subsequent `window.show()` will show a maximized window as expected.

### Testcase Gist URL

https://gist.github.com/09e5ef031f69be2cce1e23c5174d6e5b

### Additional Information

Tested on Windows 10 and Ubuntu and it works as expected, so this looks to be just macOS.


Discussion / hints:

Fix patch:
diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm
index 3220741ec356c..7149a06905b75 100644
--- a/shell/browser/native_window_mac.mm
+++ b/shell/browser/native_window_mac.mm
@@ -600,13 +600,23 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
 }
 
 void NativeWindowMac::Maximize() {
-  if (IsMaximized())
+  const bool is_visible = [window_ isVisible];
+
+  if (IsMaximized()) {
+    if (!is_visible)
+      ShowInactive();
     return;
+  }
 
   // Take note of the current window size
   if (IsNormal())
     original_frame_ = [window_ frame];
   [window_ zoom:nil];
+
+  if (!is_visible) {
+    ShowInactive();
+    NotifyWindowMaximize();
+  }
 }
 
 void NativeWindowMac::Unmaximize() {
diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts
index d4d4a70799a16..8c3b54217fedd 100644
--- a/spec-main/api-browser-window-spec.ts
+++ b/spec-main/api-browser-window-spec.ts
@@ -3579,22 +3579,24 @@ describe('BrowserWindow module', () => {
   // TODO(dsanders11): Enable once maximize event works on Linux again on CI
   ifdescribe(process.platform !== 'linux')('BrowserWindow.maximize()', () => {
     afterEach(closeAllWindows);
-    // TODO(dsanders11): Disabled on macOS, see https://github.com/electron/electron/issues/32947
-    ifit(process.platform !== 'darwin')('should show the window if it is not currently shown', async () => {
+    it('should show the window if it is not currently shown', async () => {
       const w = new BrowserWindow({ show: false });
       const hidden = emittedOnce(w, 'hide');
-      const shown = emittedOnce(w, 'show');
+      let shown = emittedOnce(w, 'show');
       const maximize = emittedOnce(w, 'maximize');
       expect(w.isVisible()).to.be.false('visible');
       w.maximize();
       await maximize;
+      await shown;
+      expect(w.isMaximized()).to.be.true('maximized');
       expect(w.isVisible()).to.be.true('visible');
       // Even if the window is already maximized
       w.hide();
       await hidden;
       expect(w.isVisible()).to.be.false('visible');
+      shown = emittedOnce(w, 'show');
       w.maximize();
-      await shown; // Ensure a 'show' event happens when it becomes visible
+      await shown;
       expect(w.isVisible()).to.be.true('visible');
     });
   });


Test patch: