Fix window position getters and setters on cocoa

This commit is contained in:
Felix Kaaman 2015-04-24 19:33:21 +02:00
parent 7eeb96909c
commit 32e14a9a0a

View file

@ -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);
}
}