diff --git a/window/examples/basic.rs b/window/examples/basic.rs index 517bf9fce..6bf04bd19 100644 --- a/window/examples/basic.rs +++ b/window/examples/basic.rs @@ -28,7 +28,11 @@ impl WindowCallbacks for MyWindow { } fn paint(&mut self, context: &mut dyn PaintContext) { - context.clear(Color::rgb(0x40, 0, 0)); + context.clear(Color::rgb(0x40, 0x20, 0x60)); + } + + fn resize(&mut self, dims: Dimensions) { + eprintln!("resize {:?}", dims); } } diff --git a/window/src/lib.rs b/window/src/lib.rs index 0a43d15a3..9063d3ceb 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -83,7 +83,7 @@ pub trait PaintContext { pub trait WindowCallbacks { /// Called when the window close button is clicked. /// Return true to allow the close to continue, false to - /// prevent it from continuing. + /// prevent it from closing. fn can_close(&mut self) -> bool { true } @@ -91,8 +91,13 @@ pub trait WindowCallbacks { /// Called when the window is being destroyed by the gui system fn destroy(&mut self) {} + /// Called when the window is resized, or when the dpi has changed + #[allow(unused_variables)] + fn resize(&mut self, dimensions: Dimensions) {} + + /// Called when the window contents need painting #[allow(unused_variables)] fn paint(&mut self, context: &mut dyn PaintContext) { - // context.clear(Color::rgb(0, 0, 0)); + context.clear(Color::rgb(0x20, 0x40, 0x60)); } } diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 751fad469..03ffd41e4 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -168,8 +168,10 @@ impl Window { } pub fn show(&self) { - let inner = self.inner.lock().unwrap(); - unsafe { ShowWindow(inner.hwnd, SW_NORMAL) }; + // ShowWindow can call to the window proc and may attempt + // to lock inner, so take care here! + let hwnd = self.inner.lock().unwrap().hwnd; + unsafe { ShowWindow(hwnd, SW_NORMAL) }; } } @@ -280,6 +282,20 @@ impl PaintContext for GdiGraphicsContext { } } +unsafe fn wm_size(hwnd: HWND, _msg: UINT, _wparam: WPARAM, lparam: LPARAM) -> Option { + if let Some(inner) = arc_from_hwnd(hwnd) { + let mut inner = inner.lock().unwrap(); + let pixel_width = LOWORD(lparam as DWORD) as usize; + let pixel_height = HIWORD(lparam as DWORD) as usize; + inner.callbacks.resize(Dimensions { + pixel_width, + pixel_height, + dpi: GetDpiForWindow(hwnd) as usize, + }); + } + None +} + unsafe fn wm_paint(hwnd: HWND, _msg: UINT, _wparam: WPARAM, _lparam: LPARAM) -> Option { if let Some(inner) = arc_from_hwnd(hwnd) { let mut inner = inner.lock().unwrap(); @@ -341,6 +357,7 @@ unsafe fn do_wnd_proc(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> WM_NCCREATE => wm_nccreate(hwnd, msg, wparam, lparam), WM_NCDESTROY => wm_ncdestroy(hwnd, msg, wparam, lparam), WM_PAINT => wm_paint(hwnd, msg, wparam, lparam), + WM_SIZE => wm_size(hwnd, msg, wparam, lparam), WM_CLOSE => { if let Some(inner) = arc_from_hwnd(hwnd) { let mut inner = inner.lock().unwrap(); diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 2bc558427..6996cbb67 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -209,6 +209,11 @@ impl WindowInner { let cfg: &xcb::ConfigureNotifyEvent = unsafe { xcb::cast_event(event) }; self.width = cfg.width(); self.height = cfg.height(); + self.callbacks.resize(Dimensions { + pixel_width: self.width, + pixel_height: self.height, + dpi: 96, + }) } xcb::KEY_PRESS => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(event) };