fix(macos): handle possible nil when trying to get current NSScreen (#1121)

This commit is contained in:
Fabian-Lars
2025-06-19 14:10:10 +02:00
committed by GitHub
parent 67f00588b4
commit 773d324be5
3 changed files with 21 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
tao: patch
---
Fixed an issue that caused a panic on macOS when tao received `nil` from the OS when trying to get the current NSScreen.

View File

@@ -1122,12 +1122,18 @@ impl UnownedWindow {
// does not take a screen parameter, but uses the current screen)
if let Some(ref fullscreen) = fullscreen {
let new_screen = match fullscreen {
Fullscreen::Borderless(borderless) => {
let RootMonitorHandle { inner: monitor } = borderless
.clone()
.unwrap_or_else(|| self.current_monitor_inner());
Fullscreen::Borderless(Some(borderless)) => {
let RootMonitorHandle { inner: monitor } = borderless.clone();
monitor
}
Fullscreen::Borderless(None) => {
if let Some(current) = self.current_monitor_inner() {
let RootMonitorHandle { inner: monitor } = current;
monitor
} else {
return;
}
}
Fullscreen::Exclusive(RootVideoMode {
video_mode: VideoMode { ref monitor, .. },
}) => monitor.clone(),
@@ -1399,22 +1405,22 @@ impl UnownedWindow {
#[inline]
// Allow directly accessing the current monitor internally without unwrapping.
pub(crate) fn current_monitor_inner(&self) -> RootMonitorHandle {
pub(crate) fn current_monitor_inner(&self) -> Option<RootMonitorHandle> {
unsafe {
let screen: Retained<NSScreen> = msg_send![&self.ns_window, screen];
let screen: Retained<NSScreen> = self.ns_window.screen()?;
let desc = NSScreen::deviceDescription(&screen);
let key = NSString::from_str("NSScreenNumber");
let value = NSDictionary::objectForKey(&desc, &key).unwrap();
let display_id: NSUInteger = msg_send![&value, unsignedIntegerValue];
RootMonitorHandle {
Some(RootMonitorHandle {
inner: MonitorHandle::new(display_id.try_into().unwrap()),
}
})
}
}
#[inline]
pub fn current_monitor(&self) -> Option<RootMonitorHandle> {
Some(self.current_monitor_inner())
self.current_monitor_inner()
}
#[inline]
pub fn monitor_from_point(&self, x: f64, y: f64) -> Option<RootMonitorHandle> {

View File

@@ -511,7 +511,7 @@ extern "C" fn window_will_enter_fullscreen(this: &Object, _: Sel, _: id) {
// Otherwise, we must've reached fullscreen by the user clicking
// on the green fullscreen button. Update state!
None => {
let current_monitor = Some(window.current_monitor_inner());
let current_monitor = window.current_monitor_inner();
shared_state.fullscreen = Some(Fullscreen::Borderless(current_monitor))
}
}