add resize callback
This commit is contained in:
1 parent
56dc24a1ee
commit
b9b94aa516
4 files changed
+36
-5
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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<LRESULT> {
|
||||
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<LRESULT> {
|
||||
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();
|
||||
|
||||
@@ -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) };
|
||||
|
||||
Reference in new issue
Block a user