From f540aae1b9fe1085ddf4fca103cce28451e29d11 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Sun, 1 May 2016 09:54:34 +0200 Subject: [PATCH] Started with implementing get_size() Currently Mac is implemented with stub versions for Linux and Windows --- src/lib.rs | 15 +++++++++++ src/native/macosx/OSXWindowFrameView.m | 31 +++++++++++++++++++++-- src/os/macos/mod.rs | 4 +++ src/os/unix/mod.rs | 5 ++++ src/os/windows/mod.rs | 35 +++++++++++++++----------- 5 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 39170b0..c99e789 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,6 +251,21 @@ impl Window { self.0.set_position(x, y) } + /// + /// Returns the current size of the window + /// + /// # Examples + /// + /// ```ignore + /// let size = window.get_size(); + /// println!("width {} height {}", size.0, size.1); + /// ``` + /// + #[inline] + pub fn get_size(&self) -> (usize, usize) { + self.0.get_size() + } + /// /// Get the current position of the mouse relative to the current window /// The coordinate system is as 0, 0 as the upper left corner diff --git a/src/native/macosx/OSXWindowFrameView.m b/src/native/macosx/OSXWindowFrameView.m index 7af8079..031c02f 100644 --- a/src/native/macosx/OSXWindowFrameView.m +++ b/src/native/macosx/OSXWindowFrameView.m @@ -27,9 +27,9 @@ CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort]; CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); - CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, draw_buffer, width * height * 4, NULL); + CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, draw_buffer, width * height * 4, NULL); - CGImageRef img = CGImageCreate(width, height, 8, 32, width * 4, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, + CGImageRef img = CGImageCreate(width, height, 8, 32, width * 4, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, provider, NULL, false, kCGRenderingIntentDefault); CGColorSpaceRelease(space); @@ -95,5 +95,32 @@ return YES; } +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)viewDidMoveToWindow +{ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowResized:) name:NSWindowDidResizeNotification + object:[self window]]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)windowResized:(NSNotification *)notification; +{ + NSSize size = [[self window] frame].size; + OSXWindow* window = (OSXWindow*)[self window]; + window->shared_data->width = (int)size.width; + window->shared_data->height = (int)size.height; +} + @end diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 8b9ca35..ca9fa35 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -280,6 +280,10 @@ impl Window { unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) } } + pub fn get_size(&self) -> (usize, usize) { + (self.shared_data.width as usize, self.shared_data.height as usize) + } + pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> { let sx = self.shared_data.scroll_x; let sy = self.shared_data.scroll_y; diff --git a/src/os/unix/mod.rs b/src/os/unix/mod.rs index 6bf24cc..f39155b 100644 --- a/src/os/unix/mod.rs +++ b/src/os/unix/mod.rs @@ -233,6 +233,11 @@ impl Window { unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) } } + #[inline] + pub fn get_size(&self) -> (usize, usize) { + (0, 0) + } + pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { let s = self.shared_data.scale as f32; let w = self.shared_data.width as f32; diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 3030e9e..c921250 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -331,7 +331,7 @@ pub struct Window { #[link(name = "user32")] #[allow(non_snake_case)] extern "system" { - fn TranslateAcceleratorW(hWnd: HWND, accel: *const ACCEL, pmsg: *const MSG) -> INT; + fn TranslateAcceleratorW(hWnd: HWND, accel: *const ACCEL, pmsg: *const MSG) -> INT; } impl Window { @@ -465,6 +465,11 @@ impl Window { } } + #[inline] + pub fn get_size(&self) -> (usize, usize) { + (0, 0) + } + pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { let s = self.scale_factor as f32; let w = self.width as f32; @@ -743,11 +748,11 @@ impl Window { let menu_height = user32::GetSystemMetrics(winapi::winuser::SM_CYMENU); user32::GetWindowRect(handle, &mut rect); - user32::MoveWindow(handle, - rect.left, - rect.top, - rect.right - rect.left, - (rect.bottom - rect.top) + menu_height, + user32::MoveWindow(handle, + rect.left, + rect.top, + rect.right - rect.left, + (rect.bottom - rect.top) + menu_height, 1); } @@ -787,7 +792,7 @@ impl Window { } fn get_virt_key(menu_item: &Menu, key: raw::c_int) -> u32 { - let mut virt = Self::is_key_virtual_range(key); + let mut virt = Self::is_key_virtual_range(key); if (menu_item.modifier & MENU_KEY_ALT) == MENU_KEY_ALT { virt |= 0x10; @@ -806,10 +811,10 @@ impl Window { fn add_accel(accel_table: &mut Vec, menu_item: &Menu) { let vk_accel = Self::map_key_to_vk_accel(menu_item.key); - let virt = Self::get_virt_key(menu_item, vk_accel.0); - let accel = winuser::ACCEL { - fVirt: virt as BYTE, - cmd: menu_item.id as WORD, + let virt = Self::get_virt_key(menu_item, vk_accel.0); + let accel = winuser::ACCEL { + fVirt: virt as BYTE, + cmd: menu_item.id as WORD, key: vk_accel.0 as WORD }; accel_table.push(accel); @@ -845,8 +850,8 @@ impl Window { user32::DestroyAcceleratorTable(self.accel_table); } - self.accel_table = user32::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(), - temp_accel_table.len() as i32); + self.accel_table = user32::CreateAcceleratorTableW(temp_accel_table.as_mut_ptr(), + temp_accel_table.len() as i32); } @@ -886,7 +891,7 @@ impl Window { for m in menu.iter() { if let Some(ref sub_menu) = m.sub_menu { Self::clone_menu(accel_dest, sub_menu); - } + } if m.key != Key::Unknown { Self::add_accel(accel_dest, m); @@ -947,7 +952,7 @@ impl Window { } } } - + // TODO: Proper return here Ok(()) }