From 32e14a9a0a47c6eb5084b5ff8af9e652b0d07346 Mon Sep 17 00:00:00 2001 From: Felix Kaaman Date: Fri, 24 Apr 2015 19:33:21 +0200 Subject: Fix window position getters and setters on cocoa --- src/api/cocoa/mod.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 96bf842..41ef3eb 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -27,6 +27,8 @@ use core_foundation::base::TCFType; use core_foundation::string::CFString; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; +use core_graphics::display::{CGMainDisplayID, CGDisplayPixelsHigh}; + use std::ffi::CStr; use std::collections::VecDeque; use std::str::FromStr; @@ -521,15 +523,30 @@ impl Window { pub fn get_position(&self) -> Option<(i32, i32)> { unsafe { let content_rect = NSWindow::contentRectForFrameRect_(*self.window, NSWindow::frame(*self.window)); - // NOTE: coordinate system might be inconsistent with other backends - Some((content_rect.origin.x as i32, content_rect.origin.y as i32)) + + // TODO: consider extrapolating the calculations for the y axis to + // a private method + Some((content_rect.origin.x as i32, (CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (content_rect.origin.y + content_rect.size.height)) as i32)) } } pub fn set_position(&self, x: i32, y: i32) { unsafe { - // NOTE: coordinate system might be inconsistent with other backends - NSWindow::setFrameOrigin_(*self.window, NSPoint::new(x as f64, y as f64)); + let frame = NSWindow::frame(*self.view); + + // NOTE: `setFrameOrigin` might not give desirable results when + // setting window, as it treats bottom left as origin. + // `setFrameTopLeftPoint` treats top left as origin (duh), but + // does not equal the value returned by `get_window_position` + // (there is a difference by 22 for me on yosemite) + + // TODO: consider extrapolating the calculations for the y axis to + // a private method + let dummy = NSRect::new(NSPoint::new(x as f64, CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (frame.size.height + y as f64)), NSSize::new(0f64, 0f64)); + let conv = NSWindow::frameRectForContentRect_(*self.window, dummy); + + // NSWindow::setFrameTopLeftPoint_(*self.window, conv.origin); + NSWindow::setFrameOrigin_(*self.window, conv.origin); } } -- cgit v1.2.3