Skip to content

Commit

Permalink
On macOS, fix panic in current_monitor_inner
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopengli89 committed Nov 5, 2022
1 parent 97d4c7b commit 8669c2e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- **Breaking:** Removed `WindowBuilderExtWindows::with_theme` and `WindowBuilderExtWayland::with_wayland_csd_theme` in favour of `WindowBuilder::with_theme`.
- **Breaking:** Removed `WindowExtWindows::theme` in favour of `Window::theme`.
- Enabled `doc_auto_cfg` when generating docs on docs.rs for feature labels.
- On macOS, fix panic when getting current monitor without any monitor attached.

# 0.27.5

Expand Down
16 changes: 11 additions & 5 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,13 @@ impl WinitWindow {
if let Some(ref fullscreen) = fullscreen {
let new_screen = match fullscreen {
Fullscreen::Borderless(Some(monitor)) => monitor.clone(),
Fullscreen::Borderless(None) => self.current_monitor_inner(),
Fullscreen::Borderless(None) => {
if let Some(monitor) = self.current_monitor_inner() {
monitor
} else {
return;
}
}
Fullscreen::Exclusive(video_mode) => video_mode.monitor(),
}
.ns_screen()
Expand Down Expand Up @@ -1067,14 +1073,14 @@ impl WinitWindow {

#[inline]
// Allow directly accessing the current monitor internally without unwrapping.
pub(crate) fn current_monitor_inner(&self) -> MonitorHandle {
let display_id = self.screen().expect("expected screen").display_id();
MonitorHandle::new(display_id)
pub(crate) fn current_monitor_inner(&self) -> Option<MonitorHandle> {
let display_id = self.screen()?.display_id();
Some(MonitorHandle::new(display_id))
}

#[inline]
pub fn current_monitor(&self) -> Option<MonitorHandle> {
Some(self.current_monitor_inner())
self.current_monitor_inner()
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/macos/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ declare_class!(
// Otherwise, we must've reached fullscreen by the user clicking
// on the green fullscreen button. Update state!
None => {
let current_monitor = Some(self.window.current_monitor_inner());
let current_monitor = self.window.current_monitor_inner();
shared_state.fullscreen = Some(Fullscreen::Borderless(current_monitor))
}
}
Expand Down

0 comments on commit 8669c2e

Please sign in to comment.