diff options
author | Felix Kaaman <trundmatu@gmail.com> | 2015-04-24 19:33:21 +0200 |
---|---|---|
committer | Felix Kaaman <trundmatu@gmail.com> | 2015-04-24 19:40:43 +0200 |
commit | 32e14a9a0a47c6eb5084b5ff8af9e652b0d07346 (patch) | |
tree | 3807914f33bdac2c56a080f1e1748df9cf562c8f /src/api | |
parent | 7eeb96909c4008398e88a9d32e958b75ab7113de (diff) | |
download | glutin-32e14a9a0a47c6eb5084b5ff8af9e652b0d07346.tar.gz glutin-32e14a9a0a47c6eb5084b5ff8af9e652b0d07346.zip |
Fix window position getters and setters on cocoa
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/cocoa/mod.rs | 25 |
1 files changed, 21 insertions, 4 deletions
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); } } |