From 3eede5c248e022ef30b604dcc254d13c632bf5d0 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 7 Feb 2023 12:59:54 -0700 Subject: [PATCH] macos: make initial position consistent with window:set_position refs: https://github.com/wez/wezterm/issues/2976 --- window/src/os/macos/window.rs | 42 +++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 2fcd73daf..b6f26a58b 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -397,6 +397,21 @@ pub struct Window { unsafe impl Send for Window {} unsafe impl Sync for Window {} +fn set_window_position(window: *mut Object, coords: ScreenPoint) { + unsafe { + let cartesian = screen_point_to_cartesian(coords); + let frame = NSWindow::frame(window); + let content_frame = NSWindow::contentRectForFrameRect_(window, frame); + let delta_x = content_frame.origin.x - frame.origin.x; + let delta_y = content_frame.origin.y - frame.origin.y; + let point = NSPoint::new( + cartesian.x as f64 - delta_x, + cartesian.y as f64 - delta_y - content_frame.size.height, + ); + NSWindow::setFrameOrigin_(window, point); + } +} + impl Window { pub async fn new_window( _class_name: &str, @@ -421,10 +436,8 @@ impl Window { x, y, } = conn.resolve_geometry(geometry); - let pos = match (x, y) { - (Some(x), Some(y)) => Some(screen_point_to_cartesian(ScreenPoint::new( - x as isize, y as isize, - ))), + let initial_pos = match (x, y) { + (Some(x), Some(y)) => Some(ScreenPoint::new(x as isize, y as isize)), _ => None, }; @@ -513,7 +526,13 @@ impl Window { } LAST_POSITION.with(|last_pos| { - let pos = pos.or_else(|| last_pos.borrow_mut().take()); + if let Some(pos) = initial_pos { + // Put it where they asked it to be, without influencing + // future positioning info + set_window_position(*window, pos); + return; + } + let pos = last_pos.borrow_mut().take(); let next_pos = match pos { Some(pos) if point_in_rect(pos, active_screen_frame) => { // Only continue the cascade if the prior point is @@ -1109,18 +1128,7 @@ impl WindowInner { } fn set_window_position(&self, coords: ScreenPoint) { - unsafe { - let cartesian = screen_point_to_cartesian(coords); - let frame = NSWindow::frame(*self.window); - let content_frame = NSWindow::contentRectForFrameRect_(*self.window, frame); - let delta_x = content_frame.origin.x - frame.origin.x; - let delta_y = content_frame.origin.y - frame.origin.y; - let point = NSPoint::new( - cartesian.x as f64 - delta_x, - cartesian.y as f64 - delta_y - content_frame.size.height, - ); - NSWindow::setFrameOrigin_(*self.window, point); - } + set_window_position(*self.window, coords); } fn set_text_cursor_position(&mut self, cursor: Rect) {