From 11ee384908725836a84a72ed61e03f7fb1926a5b Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sat, 17 Oct 2020 15:19:06 -0500 Subject: [PATCH] fix windows build --- src/win/window.rs | 42 ++++++++++++++++++++++++++++++------------ src/window_info.rs | 36 ++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/win/window.rs b/src/win/window.rs index 6e077a6..aaa758b 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -24,7 +24,7 @@ use raw_window_handle::{ use crate::{ Event, KeyboardEvent, MouseButton, MouseEvent, Parent::WithParent, ScrollDelta, WindowEvent, - WindowHandler, WindowInfo, WindowOpenOptions, + WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult, WindowScalePolicy, Size, Point, }; unsafe fn message_box(title: &str, msg: &str) { @@ -76,13 +76,21 @@ unsafe extern "system" fn wnd_proc( match msg { WM_MOUSEMOVE => { - let x = (lparam & 0xFFFF) as i32; - let y = ((lparam >> 16) & 0xFFFF) as i32; - window_state.borrow_mut().handler.on_event( + let x = (lparam & 0xFFFF) as f64; + let y = ((lparam >> 16) & 0xFFFF) as f64; + let physical_pos = Point::new(x, y); + + let mut window_state = window_state.borrow_mut(); + + // FIXME: For some reason, the data in window_info is corrupted. + // let logical_pos = window_state.window_info.physical_to_logical(physical_pos); + let logical_pos = physical_pos; + + window_state.handler.on_event( &mut window, Event::Mouse(MouseEvent::CursorMoved { - x: x as i32, - y: y as i32, + logical_pos: logical_pos.into(), + physical_pos: physical_pos.into(), }), ); return 0; @@ -134,7 +142,7 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) { struct WindowState { window_class: ATOM, - scaling: Option, // DPI scale, 96.0 is "default". + window_info: WindowInfo, handler: H, } @@ -166,7 +174,7 @@ impl WindowHandle { } impl Window { - pub fn open(options: WindowOpenOptions) -> WindowHandle { + pub fn open(options: WindowOpenOptions) -> Result<(WindowHandle, WindowInfo), ()> { unsafe { let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; @@ -180,13 +188,23 @@ impl Window { | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPSIBLINGS; + + let scaling = match options.scale { + // TODO: Find system scale factor + WindowScalePolicy::TrySystemScaleFactor => 1.0, + WindowScalePolicy::TrySystemScaleFactorTimes(user_scale) => 1.0 * user_scale, + WindowScalePolicy::UseScaleFactor(user_scale) => user_scale, + WindowScalePolicy::NoScaling => 1.0, + }; + + let window_info = options.window_info_from_scale(scaling); let mut rect = RECT { left: 0, top: 0, // todo: check if usize fits into i32 - right: options.width as i32, - bottom: options.height as i32, + right: window_info.physical_size().width as i32, + bottom: window_info.physical_size().height as i32, }; // todo: add check flags https://github.com/wrl/rutabaga/blob/f30ff67e157375cafdbafe5fb549f1790443a3a8/src/platform/win/window.c#L351 @@ -226,7 +244,7 @@ impl Window { let window_state = Rc::new(RefCell::new(WindowState { window_class, - scaling: None, + window_info, handler, })); @@ -235,7 +253,7 @@ impl Window { SetWindowLongPtrA(hwnd, GWLP_USERDATA, Rc::into_raw(win) as *const _ as _); SetTimer(hwnd, 4242, 13, None); - WindowHandle { hwnd } + Ok((WindowHandle { hwnd }, window_info)) } } } diff --git a/src/window_info.rs b/src/window_info.rs index 81da6bf..b125e77 100644 --- a/src/window_info.rs +++ b/src/window_info.rs @@ -1,6 +1,7 @@ use crate::{Size, Point}; /// The info about the window +#[repr(C)] #[derive(Debug, Copy, Clone)] pub struct WindowInfo { logical_size: Size, @@ -11,27 +12,42 @@ pub struct WindowInfo { impl WindowInfo { pub fn from_logical_size(logical_size: Size, scale: f64) -> Self { - let scale_recip = 1.0 / scale; + let (scale_recip, physical_size) = if scale == 1.0 { + (1.0, logical_size) + } else { + ( + 1.0 / scale, + Size { + width: (logical_size.width as f64 * scale).round() as u32, + height: (logical_size.height as f64 * scale).round() as u32, + } + ) + }; Self { logical_size, - physical_size: Size { - width: (logical_size.width as f64 * scale).round() as u32, - height: (logical_size.height as f64 * scale).round() as u32, - }, + physical_size, scale, scale_recip, } } pub fn from_physical_size(physical_size: Size, scale: f64) -> Self { - let scale_recip = 1.0 / scale; + let (scale_recip, logical_size) = if scale == 1.0 { + (1.0, physical_size) + } else { + let scale_recip = 1.0 / scale; + ( + scale_recip, + Size { + width: (physical_size.width as f64 * scale_recip).round() as u32, + height: (physical_size.height as f64 * scale_recip).round() as u32, + } + ) + }; Self { - logical_size: Size { - width: (physical_size.width as f64 * scale_recip).round() as u32, - height: (physical_size.height as f64 * scale_recip).round() as u32, - }, + logical_size, physical_size, scale, scale_recip,