From f15bb186f47ec89fb24a415f8f580255b79f3bf0 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 25 Jan 2023 18:06:46 -0700 Subject: [PATCH] macos: maybe fix window positioning for multi-monitor systems refs: https://github.com/wez/wezterm/issues/2958 --- window/src/os/macos/window.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index be928dedd..6bc416467 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -497,13 +497,35 @@ impl Window { thread_local! { static LAST_POSITION: RefCell> = RefCell::new(None); } + + let frame = NSWindow::frame(*window); + let active_screen = NSScreen::mainScreen(nil); + let active_screen_frame = NSScreen::frame(active_screen); + + fn point_in_rect(pt: NSPoint, rect: NSRect) -> bool { + let rect: euclid::Rect = euclid::rect( + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + ); + rect.contains(euclid::point2(pt.x, pt.y)) + } + LAST_POSITION.with(|last_pos| { let pos = pos.or_else(|| last_pos.borrow_mut().take()); - let next_pos = if let Some(pos) = pos { - window.cascadeTopLeftFromPoint_(pos) - } else { - window.center(); - window.cascadeTopLeftFromPoint_(NSPoint::new(0., 0.)) + let next_pos = match pos { + Some(pos) if point_in_rect(pos, active_screen_frame) => { + // Only continue the cascade if the prior point is + // still within the currently active screen + window.cascadeTopLeftFromPoint_(pos) + } + _ => { + // Otherwise, position as if it is the first time + // we're displaying on this screen + window.center(); + window.cascadeTopLeftFromPoint_(frame.origin) + } }; last_pos.borrow_mut().replace(next_pos); });