Skip to content

fix: handle failing to enter fullscreen on macOS#43112

Merged
codebytere merged 2 commits intoelectron:mainfrom
cptpcrd:fullscreen-fail
Aug 1, 2024
Merged

fix: handle failing to enter fullscreen on macOS#43112
codebytere merged 2 commits intoelectron:mainfrom
cptpcrd:fullscreen-fail

Conversation

@cptpcrd
Copy link
Copy Markdown
Contributor

@cptpcrd cptpcrd commented Jul 30, 2024

Description of Change

On macOS, entering or exiting fullscreen mode can fail even after windowWillEnterFullScreen or windowWillExitFullScreen has run (see the documentation for windowDidFailToEnterFullScreen and windowDidFailToExitFullScreen). If this happens, it is necessary to properly restore the window's original state, but Electron currently does not do that. The most prominent bug caused by this is that if the window fails to enter fullscreen, fullscreen_transition_state_ is "stuck" at FullScreenTransitionState::kEntering, and app.quit() does not work properly because the window refuses to close.

I don't know if it's possible to trigger these failures with automation, but windowDidFailToEnterFullScreen can be triggered manually on a Macbook without much effort:

  1. Ensure the system has at least 2 virtual desktops (swipe up with 4 fingers and click the plus icon on the upper right).
  2. Run the below snippet.
  3. Before the first 3-second timer fires, press 3 or 4 fingers on the touchpad and move them slightly to one side (as if to swipe to another desktop). Keep the fingers held down, so that the UI remains in the "halfway" state between the two desktops without being fully in either one or the other.
  4. Entering fullscreen is apparently not allowed when swiping between desktops like this, so windowWillEnterFullScreen runs followed by windowDidFailToEnterFullScreen. Without this change, the app will refuse to quit when the app.quit() call eventually runs. With this change, it quits normally.
const { app, BrowserWindow } = require('electron')

app.whenReady().then(() => {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    // Uncomment to test custom title bars, which require some special handling
    // titleBarStyle: "hidden",
    // trafficLightPosition: { x: 30, y: 30 },
  })

  mainWindow.loadFile("/dev/null");

  setTimeout(() => {
    mainWindow.setFullScreen(true)
    setTimeout(() => {
      mainWindow.setFullScreen(false)
      setTimeout(() => {
        app.quit()
      }, 3000)
    }, 3000)
  }, 3000)
})

Exiting fullscreen seems to behave oddly - when doing the same "swipe halfway into the other desktop" test with a window that is exiting fullscreen, windowDidFailToExitFullScreen does fire, but windowDidExitFullScreen also fires, and the window properly exits fullscreen once the user fully switches to a single desktop. To be safe, I tried to write this change such that it will work if either just windowDidFailToExitFullScreen runs or if both run.

Checklist

Release Notes

Notes: Fix behavior when entering/exiting fullscreen fails on macOS.

On macOS, failing to enter/exit fullscreen can fail. If this happens,
properly restore the original window state.
@electron-cation electron-cation bot added the new-pr 🌱 PR opened recently label Jul 30, 2024
@codebytere codebytere self-requested a review July 30, 2024 19:36
Copy link
Copy Markdown
Member

@codebytere codebytere left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! Generally this looks good - just one change/removal i think we can make.

Seem to be unnecessary since the window exits fullscreen anyway.
@cptpcrd cptpcrd changed the title fix: handle failing to enter/exit fullscreen on macOS fix: handle failing to enter fullscreen on macOS Jul 30, 2024
@ckerr ckerr added platform/macOS semver/patch backwards-compatible bug fixes labels Jul 30, 2024
@jkleinsc jkleinsc added target/29-x-y PR should also be added to the "29-x-y" branch. target/30-x-y PR should also be added to the "30-x-y" branch. target/31-x-y PR should also be added to the "31-x-y" branch. target/32-x-y PR should also be added to the "32-x-y" branch. labels Jul 31, 2024
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened recently label Jul 31, 2024
@codebytere codebytere merged commit 2337d86 into electron:main Aug 1, 2024
@release-clerk
Copy link
Copy Markdown

release-clerk bot commented Aug 1, 2024

Release Notes Persisted

Fix behavior when entering/exiting fullscreen fails on macOS.

@trop
Copy link
Copy Markdown
Contributor

trop bot commented Aug 1, 2024

I have automatically backported this PR to "30-x-y", please check out #43151

@trop trop bot added the in-flight/30-x-y label Aug 1, 2024
@trop
Copy link
Copy Markdown
Contributor

trop bot commented Aug 1, 2024

I have automatically backported this PR to "32-x-y", please check out #43152

@trop trop bot removed the target/30-x-y PR should also be added to the "30-x-y" branch. label Aug 1, 2024
@trop
Copy link
Copy Markdown
Contributor

trop bot commented Aug 1, 2024

I have automatically backported this PR to "29-x-y", please check out #43153

@trop
Copy link
Copy Markdown
Contributor

trop bot commented Aug 1, 2024

I have automatically backported this PR to "31-x-y", please check out #43154

@trop trop bot removed target/32-x-y PR should also be added to the "32-x-y" branch. target/29-x-y PR should also be added to the "29-x-y" branch. target/31-x-y PR should also be added to the "31-x-y" branch. labels Aug 1, 2024
codebytere pushed a commit that referenced this pull request Aug 1, 2024
* fix: handle failing to enter/exit fullscreen on macOS

On macOS, failing to enter/exit fullscreen can fail. If this happens,
properly restore the original window state.

* refactor: remove fail to exit fullscreen handlers

Seem to be unnecessary since the window exits fullscreen anyway.
@trop trop bot added merged/31-x-y PR was merged to the "31-x-y" branch. merged/32-x-y PR was merged to the "32-x-y" branch. merged/30-x-y PR was merged to the "30-x-y" branch. merged/29-x-y PR was merged to the "29-x-y" branch. and removed in-flight/31-x-y in-flight/32-x-y in-flight/30-x-y in-flight/29-x-y labels Aug 1, 2024
@bpasero
Copy link
Copy Markdown
Contributor

bpasero commented Aug 17, 2024

This is 👍 , I am sure some VS Code users (including myself) have had this issue in the past and I was never sure what caused it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/29-x-y PR was merged to the "29-x-y" branch. merged/30-x-y PR was merged to the "30-x-y" branch. merged/31-x-y PR was merged to the "31-x-y" branch. merged/32-x-y PR was merged to the "32-x-y" branch. platform/macOS semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants