From 001bca8f74bfad9bb485f76f07eacc748bb4d233 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Sat, 9 Jan 2016 20:49:14 +0100 Subject: [PATCH] Implemented set_position for Mac --- examples/noise.rs | 2 -- src/native/macosx/MacMiniFB.m | 20 ++++++++++++++++++++ src/os/macos/mod.rs | 4 +++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/examples/noise.rs b/examples/noise.rs index 38479f1..edde38a 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -20,8 +20,6 @@ fn main() { } }; - window.set_position(10, 10); - while window.is_open() && !window.is_key_down(Key::Escape) { for i in buffer.iter_mut() { noise = seed; diff --git a/src/native/macosx/MacMiniFB.m b/src/native/macosx/MacMiniFB.m index 445137d..cc4ddc0 100644 --- a/src/native/macosx/MacMiniFB.m +++ b/src/native/macosx/MacMiniFB.m @@ -93,6 +93,26 @@ int mfb_update(void* window, void* buffer) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +static float transformY(float y) +{ + float b = CGDisplayBounds(CGMainDisplayID()).size.height; + float t = b - y; + return t; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void mfb_set_position(void* window, int x, int y) +{ + OSXWindow* win = (OSXWindow*)window; + const NSRect contentRect = [[win contentView] frame]; + const NSRect dummyRect = NSMakeRect(x, transformY(y + contentRect.size.height), 0, 0); + const NSRect frameRect = [win frameRectForContentRect:dummyRect]; + [win setFrameOrigin:frameRect.origin]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + int mfb_should_close(void* window) { OSXWindow* win = (OSXWindow*)window; diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index b72165c..c82b862 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -146,6 +146,7 @@ extern { fn mfb_open(name: *const c_char, width: u32, height: u32, scale: i32) -> *mut c_void; fn mfb_close(window: *mut c_void); fn mfb_update(window: *mut c_void, buffer: *const c_uchar); + fn mfb_set_position(window: *mut c_void, x: i32, y: i32); fn mfb_set_key_callback(window: *mut c_void, target: *mut c_void, cb: unsafe extern fn(*mut c_void, i32, i32)); fn mfb_should_close(window: *mut c_void) -> i32; fn mfb_get_screen_size() -> u32; @@ -202,7 +203,8 @@ impl Window { } #[inline] - pub fn set_position(&mut self, _: isize, _: isize) { + pub fn set_position(&mut self, x: isize, y: isize) { + unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) } } #[inline]