window position restoration working

This commit is contained in:
Kay Simmons 2023-01-26 16:35:00 -08:00
parent a369fb8033
commit 1593b1e13d
3 changed files with 66 additions and 33 deletions

View file

@ -42,31 +42,16 @@ impl RectFExt for RectF {
}
fn to_ns_rect(&self) -> NSRect {
dbg!(&self);
NSRect::new(
NSPoint::new(
dbg!(self.origin_x() as f64),
dbg!(-(self.origin_y() - self.height()) as f64),
self.origin_x() as f64,
-(self.origin_y() + self.height()) as f64,
),
NSSize::new(self.width() as f64, self.height() as f64),
)
}
}
pub trait NSPointExt {
/// Converts self to a Vector2F with y axis pointing down.
/// Also takes care of converting from window scaled coordinates to screen coordinates
fn to_window_vector2f(&self, native_window: id) -> Vector2F;
}
impl NSPointExt for NSPoint {
fn to_window_vector2f(&self, native_window: id) -> Vector2F {
unsafe {
let point: NSPoint = msg_send![native_window, convertPointFromScreen: self];
vec2f(point.x as f32, -point.y as f32)
}
}
}
pub trait NSRectExt {
/// Converts self to a RectF with y axis pointing down.
/// The resulting RectF will have an origin at the top left of the rectangle.
@ -77,11 +62,13 @@ pub trait NSRectExt {
/// The resulting RectF will have an origin at the top left of the rectangle.
/// Unlike to_screen_ns_rect, coordinates are not converted and are assumed to already be in screen scale
fn to_rectf(&self) -> RectF;
fn intersects(&self, other: Self) -> bool;
}
impl NSRectExt for NSRect {
fn to_window_rectf(&self, native_window: id) -> RectF {
unsafe {
dbg!(self.origin.x);
self.origin.x;
let rect: NSRect = native_window.convertRectFromScreen_(*self);
rect.to_rectf()
}
@ -90,10 +77,21 @@ impl NSRectExt for NSRect {
fn to_rectf(&self) -> RectF {
RectF::new(
vec2f(
dbg!(self.origin.x as f32),
dbg!(-(self.origin.y - self.size.height) as f32),
self.origin.x as f32,
-(self.origin.y + self.size.height) as f32,
),
vec2f(self.size.width as f32, self.size.height as f32),
)
}
fn intersects(&self, other: Self) -> bool {
self.size.width > 0.
&& self.size.height > 0.
&& other.size.width > 0.
&& other.size.height > 0.
&& self.origin.x <= other.origin.x + other.size.width
&& self.origin.x + self.size.width >= other.origin.x
&& self.origin.y <= other.origin.y + other.size.height
&& self.origin.y + self.size.height >= other.origin.y
}
}

View file

@ -371,14 +371,8 @@ impl WindowState {
return WindowBounds::Fullscreen;
}
let screen_frame = self
.native_window
.screen()
.visibleFrame()
.to_window_rectf(self.native_window);
let window_frame = self.frame();
if screen_frame == window_frame {
if window_frame == self.native_window.screen().visibleFrame().to_rectf() {
WindowBounds::Maximized
} else {
WindowBounds::Fixed(window_frame)
@ -388,7 +382,10 @@ impl WindowState {
// Returns the window bounds in window coordinates
fn frame(&self) -> RectF {
unsafe { NSWindow::frame(self.native_window).to_window_rectf(self.native_window) }
unsafe {
let ns_frame = NSWindow::frame(self.native_window);
ns_frame.to_rectf()
}
}
fn content_size(&self) -> Vector2F {
@ -474,7 +471,13 @@ impl Window {
native_window.setFrame_display_(screen.visibleFrame(), YES);
}
WindowBounds::Fixed(rect) => {
native_window.setFrame_display_(rect.to_screen_ns_rect(native_window), YES);
let screen_frame = screen.visibleFrame();
let ns_rect = rect.to_ns_rect();
if ns_rect.intersects(screen_frame) {
native_window.setFrame_display_(ns_rect, YES);
} else {
native_window.setFrame_display_(screen_frame, YES);
}
}
}