Skip to content

Commit 773d324

Browse files
authored
fix(macos): handle possible nil when trying to get current NSScreen (#1121)
1 parent 67f0058 commit 773d324

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tao: patch
3+
---
4+
5+
Fixed an issue that caused a panic on macOS when tao received `nil` from the OS when trying to get the current NSScreen.

src/platform_impl/macos/window.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,18 @@ impl UnownedWindow {
11221122
// does not take a screen parameter, but uses the current screen)
11231123
if let Some(ref fullscreen) = fullscreen {
11241124
let new_screen = match fullscreen {
1125-
Fullscreen::Borderless(borderless) => {
1126-
let RootMonitorHandle { inner: monitor } = borderless
1127-
.clone()
1128-
.unwrap_or_else(|| self.current_monitor_inner());
1125+
Fullscreen::Borderless(Some(borderless)) => {
1126+
let RootMonitorHandle { inner: monitor } = borderless.clone();
11291127
monitor
11301128
}
1129+
Fullscreen::Borderless(None) => {
1130+
if let Some(current) = self.current_monitor_inner() {
1131+
let RootMonitorHandle { inner: monitor } = current;
1132+
monitor
1133+
} else {
1134+
return;
1135+
}
1136+
}
11311137
Fullscreen::Exclusive(RootVideoMode {
11321138
video_mode: VideoMode { ref monitor, .. },
11331139
}) => monitor.clone(),
@@ -1399,22 +1405,22 @@ impl UnownedWindow {
13991405

14001406
#[inline]
14011407
// Allow directly accessing the current monitor internally without unwrapping.
1402-
pub(crate) fn current_monitor_inner(&self) -> RootMonitorHandle {
1408+
pub(crate) fn current_monitor_inner(&self) -> Option<RootMonitorHandle> {
14031409
unsafe {
1404-
let screen: Retained<NSScreen> = msg_send![&self.ns_window, screen];
1410+
let screen: Retained<NSScreen> = self.ns_window.screen()?;
14051411
let desc = NSScreen::deviceDescription(&screen);
14061412
let key = NSString::from_str("NSScreenNumber");
14071413
let value = NSDictionary::objectForKey(&desc, &key).unwrap();
14081414
let display_id: NSUInteger = msg_send![&value, unsignedIntegerValue];
1409-
RootMonitorHandle {
1415+
Some(RootMonitorHandle {
14101416
inner: MonitorHandle::new(display_id.try_into().unwrap()),
1411-
}
1417+
})
14121418
}
14131419
}
14141420

14151421
#[inline]
14161422
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
1417-
Some(self.current_monitor_inner())
1423+
self.current_monitor_inner()
14181424
}
14191425
#[inline]
14201426
pub fn monitor_from_point(&self, x: f64, y: f64) -> Option<RootMonitorHandle> {

src/platform_impl/macos/window_delegate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ extern "C" fn window_will_enter_fullscreen(this: &Object, _: Sel, _: id) {
511511
// Otherwise, we must've reached fullscreen by the user clicking
512512
// on the green fullscreen button. Update state!
513513
None => {
514-
let current_monitor = Some(window.current_monitor_inner());
514+
let current_monitor = window.current_monitor_inner();
515515
shared_state.fullscreen = Some(Fullscreen::Borderless(current_monitor))
516516
}
517517
}

0 commit comments

Comments
 (0)