Repository: electron/electron (language: cpp)

Issue:
[Bug]: Menu Bar Visibility
### 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

20.0.0

### What operating system are you using?

Other Linux

### Operating System Version

Fedora 36

### What arch are you using?

x64

### Last Known Working Electron version

19.0.0

### Expected Behavior

If I specify:

```js
win.setMenuBarVisibility(false);
```
then menu bar will be hidden which is great. But if you spam F11 key, menu bar will become visible again.

### Actual Behavior

If I specify:

```js
win.setMenuBarVisibility(false);
```
Menu bar shouldn't be visible even while spamming F11.

### Testcase Gist URL

_No response_

### Additional Information

_No response_


Discussion / hints:
https://github.com/electron/electron/issues/19426
https://github.com/electron/electron/issues/20237

In the Future please provide a Fiddle to reproduce the error https://github.com/electron/fiddle

The vanilla Fiddle is enough, it just needs a single line added:

https://gist.github.com/b89bfaf8532bcfb5c7d7ee2c5ee010e2

[Screencast from 25.09.2022 18:41:06.webm](https://user-images.githubusercontent.com/679144/192154972-31467b17-e021-41cc-b5a7-cfbe892cd95b.webm)

I just tested it on both Windows and mac and I can confirm that the bug only occurs on Linux.
> The latest code also has this problem
This issue has been automatically marked as stale. **If this issue is still affecting you, please leave any comment** (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the [latest version of Electron](https://www.electronjs.org/releases/stable) or in the [beta](https://www.electronjs.org/releases/beta)—please include it with your comment!
Still repros in 22.0.0 and 23.0.0-alpha.3
This issue has been automatically marked as stale. **If this issue is still affecting you, please leave any comment** (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the [latest version of Electron](https://www.electronjs.org/releases/stable) or in the [beta](https://www.electronjs.org/releases/beta)—please include it with your comment!
I wanna pick this issue, allow me to contribute it. I found the location of the code that might be wrong(https://github.com/electron/electron/blob/9640ac441d37ecbb15f2f0df9fb6b24d29b288ff/shell/browser/native_window_views.cc#LL736C1-L739C48), can you please give me some  advice.   @codebytere @BlackHole1 

Here's my opinion:
When exiting fullscreen, in addition to judging the `IsMenuBarAutoHide()` of the window, you should also judge the user-set visibility property of menu bar. As I known, when entering fullscreen, `IsMenuBarVisible()` will change and may not be consistent with the state set by `mainWindow.setMenuBarVisibility`.




Fix patch:
diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc
index 0298aba40c651..a1581d98d0020 100644
--- a/shell/browser/native_window_views.cc
+++ b/shell/browser/native_window_views.cc
@@ -731,10 +731,14 @@ void NativeWindowViews::SetFullScreen(bool fullscreen) {
                                             gfx::Rect());
 
   // Auto-hide menubar when in fullscreen.
-  if (fullscreen)
+  if (fullscreen) {
+    menu_bar_visible_before_fullscreen_ = IsMenuBarVisible();
     SetMenuBarVisibility(false);
-  else
-    SetMenuBarVisibility(!IsMenuBarAutoHide());
+  } else {
+    SetMenuBarVisibility(!IsMenuBarAutoHide() &&
+                         menu_bar_visible_before_fullscreen_);
+    menu_bar_visible_before_fullscreen_ = false;
+  }
 #endif
 }
 
diff --git a/shell/browser/native_window_views.h b/shell/browser/native_window_views.h
index 179f4575b2c5e..942dfc0460d6a 100644
--- a/shell/browser/native_window_views.h
+++ b/shell/browser/native_window_views.h
@@ -321,6 +321,9 @@ class NativeWindowViews : public NativeWindow,
   // Handles unhandled keyboard messages coming back from the renderer process.
   views::UnhandledKeyboardEventHandler keyboard_event_handler_;
 
+  // Whether the menubar is visible before the window enters fullscreen
+  bool menu_bar_visible_before_fullscreen_ = false;
+
   // Whether the window should be enabled based on user calls to SetEnabled()
   bool is_enabled_ = true;
   // How many modal children this window has;
diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts
index 450e503f5c58c..789d36aa8fece 100644
--- a/spec/api-browser-window-spec.ts
+++ b/spec/api-browser-window-spec.ts
@@ -5338,6 +5338,48 @@ describe('BrowserWindow module', () => {
       });
     });
 
+    ifdescribe(process.platform !== 'darwin')('when fullscreen state is changed', () => {
+      it('correctly remembers state prior to fullscreen change', async () => {
+        const w = new BrowserWindow({ show: false });
+        expect(w.isMenuBarVisible()).to.be.true('isMenuBarVisible');
+        w.setMenuBarVisibility(false);
+        expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
+
+        const enterFS = once(w, 'enter-full-screen');
+        w.setFullScreen(true);
+        await enterFS;
+        expect(w.fullScreen).to.be.true('not fullscreen');
+
+        const exitFS = once(w, 'leave-full-screen');
+        w.setFullScreen(false);
+        await exitFS;
+        expect(w.fullScreen).to.be.false('not fullscreen');
+
+        expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
+      });
+
+      it('correctly remembers state prior to fullscreen change with autoHide', async () => {
+        const w = new BrowserWindow({ show: false });
+        expect(w.autoHideMenuBar).to.be.false('autoHideMenuBar');
+        w.autoHideMenuBar = true;
+        expect(w.autoHideMenuBar).to.be.true('autoHideMenuBar');
+        w.setMenuBarVisibility(false);
+        expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
+
+        const enterFS = once(w, 'enter-full-screen');
+        w.setFullScreen(true);
+        await enterFS;
+        expect(w.fullScreen).to.be.true('not fullscreen');
+
+        const exitFS = once(w, 'leave-full-screen');
+        w.setFullScreen(false);
+        await exitFS;
+        expect(w.fullScreen).to.be.false('not fullscreen');
+
+        expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
+      });
+    });
+
     ifdescribe(process.platform === 'darwin')('fullscreenable state', () => {
       it('with functions', () => {
         it('can be set with fullscreenable constructor option', () => {


Test patch: