Skip to content

Commit

Permalink
macOS: skip releasing unavailable monitors
Browse files Browse the repository at this point in the history
Prevent assertion error when trying to close a fullscreen window that
was on a display that got disconnected.
  • Loading branch information
killercup authored Aug 5, 2024
1 parent 546962c commit 54ff9c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ changelog entry.
### Fixed

- On Web, pen events are now routed through to `WindowEvent::Cursor*`.
- On macOS, fix panic when releasing not available monitor.
31 changes: 17 additions & 14 deletions src/platform_impl/apple/appkit/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,13 +1428,7 @@ impl WindowDelegate {
toggle_fullscreen(self.window());
},
(Some(Fullscreen::Exclusive(ref video_mode)), None) => {
unsafe {
ffi::CGRestorePermanentDisplayConfiguration();
assert_eq!(
ffi::CGDisplayRelease(video_mode.monitor().native_identifier()),
ffi::kCGErrorSuccess
);
};
restore_and_release_display(&video_mode.monitor());
toggle_fullscreen(self.window());
},
(Some(Fullscreen::Borderless(_)), Some(Fullscreen::Exclusive(_))) => {
Expand Down Expand Up @@ -1465,13 +1459,7 @@ impl WindowDelegate {
);
app.setPresentationOptions(presentation_options);

unsafe {
ffi::CGRestorePermanentDisplayConfiguration();
assert_eq!(
ffi::CGDisplayRelease(video_mode.monitor().native_identifier()),
ffi::kCGErrorSuccess
);
};
restore_and_release_display(&video_mode.monitor());

// Restore the normal window level following the Borderless fullscreen
// `CGShieldingWindowLevel() + 1` hack.
Expand Down Expand Up @@ -1667,6 +1655,21 @@ impl WindowDelegate {
}
}

fn restore_and_release_display(monitor: &MonitorHandle) {
let available_monitors = monitor::available_monitors();
if available_monitors.contains(monitor) {
unsafe {
ffi::CGRestorePermanentDisplayConfiguration();
assert_eq!(ffi::CGDisplayRelease(monitor.native_identifier()), ffi::kCGErrorSuccess);
};
} else {
warn!(
monitor = monitor.name(),
"Tried to restore exclusive fullscreen on a monitor that is no longer available"
);
}
}

impl WindowExtMacOS for WindowDelegate {
#[inline]
fn simple_fullscreen(&self) -> bool {
Expand Down

0 comments on commit 54ff9c3

Please sign in to comment.