From 0649c536952930d55dde9a21eec8bf929d53138b Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sun, 22 May 2022 12:52:26 +0300 Subject: [PATCH] new window resize event --- abletk-cairo/src/lib.rs | 25 ++++--------------------- abletk-direct2d/src/lib.rs | 8 ++++---- src/application.rs | 2 +- src/event/application.rs | 2 +- src/event/window.rs | 1 + src/main.rs | 4 ++-- src/plugin.rs | 2 +- src/window.rs | 8 +++++++- 8 files changed, 21 insertions(+), 31 deletions(-) diff --git a/abletk-cairo/src/lib.rs b/abletk-cairo/src/lib.rs index 0637f46..a959bb7 100644 --- a/abletk-cairo/src/lib.rs +++ b/abletk-cairo/src/lib.rs @@ -165,27 +165,10 @@ impl Renderer { } pub fn size(&self) -> (u32, u32) { - unsafe { - let mut root = mem::zeroed(); - let mut x = mem::zeroed(); - let mut y = mem::zeroed(); - let mut width = mem::zeroed(); - let mut height = mem::zeroed(); - let mut border_width = mem::zeroed(); - let mut depth = mem::zeroed(); - assert_ne!(XGetGeometry( - self.handle.display as _, - self.handle.window.try_into().unwrap(), - &mut root, - &mut x, - &mut y, - &mut width, - &mut height, - &mut border_width, - &mut depth, - ), 0); - (width, height) - } + unsafe {( + cairo_xlib_surface_get_width(self.surface).try_into().unwrap(), + cairo_xlib_surface_get_height(self.surface).try_into().unwrap(), + )} } fn text_extents(&self, text: &str) -> TextExtents { diff --git a/abletk-direct2d/src/lib.rs b/abletk-direct2d/src/lib.rs index 9876a7a..c46e88b 100755 --- a/abletk-direct2d/src/lib.rs +++ b/abletk-direct2d/src/lib.rs @@ -39,7 +39,7 @@ impl Renderer { .unwrap() }; - Self { + let mut renderer = Self { handle, factory, dw_factory, @@ -48,12 +48,12 @@ impl Renderer { stroke_style, system_fonts, text_format, - } + }; + renderer.setup_target(); + renderer } pub fn begin_draw(&mut self) { - self.setup_target(); - unsafe { self.target.as_ref().unwrap().BeginDraw() } } diff --git a/src/application.rs b/src/application.rs index 0e8a61c..28248ec 100755 --- a/src/application.rs +++ b/src/application.rs @@ -104,7 +104,7 @@ impl Application { self.windows.remove(&window_id); if self.windows.is_empty() { - emit_app_event!(self, ApplicationEvent::AllWindowsClosed); + emit_app_event!(self, ApplicationEvent::AllWindowsDestroyed); } } Event::WindowEvent { diff --git a/src/event/application.rs b/src/event/application.rs index e2d9fe4..6dcbfb1 100755 --- a/src/event/application.rs +++ b/src/event/application.rs @@ -9,5 +9,5 @@ #[non_exhaustive] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Event { - AllWindowsClosed, + AllWindowsDestroyed, } diff --git a/src/event/window.rs b/src/event/window.rs index 99be11f..505703e 100755 --- a/src/event/window.rs +++ b/src/event/window.rs @@ -10,4 +10,5 @@ #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Event { Closed, + Resized, } diff --git a/src/main.rs b/src/main.rs index a4a660a..82cc67e 100755 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ fn launch() -> _ { .add(Label::new("this is a label! jjjjyyy") .bg_color(rgb!(0xFF0000FF))) .padding_left(10)) - .on_event(WindowEvent::Closed, |_, window| { - window.set_title("CLOSING!") + .on_event(WindowEvent::Resized, |_, window| { + println!("window resized: {:?}", window.size()) })) } diff --git a/src/plugin.rs b/src/plugin.rs index 9447e7f..40fd892 100755 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -25,7 +25,7 @@ pub struct QuitPlugin; impl Plugin for QuitPlugin { fn apply(&self, app: Application) -> Application { - app.on_event(Event::AllWindowsClosed, |ctx| { + app.on_event(Event::AllWindowsDestroyed, |ctx| { if Platform::target() != Platform::MacOS { ctx.quit() } diff --git a/src/window.rs b/src/window.rs index b948935..f467874 100755 --- a/src/window.rs +++ b/src/window.rs @@ -99,6 +99,11 @@ impl Window { self.window.set_title(title.as_ref()) } + pub fn size(&self) -> (u32, u32) { + let size = self.window.inner_size(); + (size.width, size.height) + } + pub(crate) fn emit_event(&mut self, event: Event) { if let Some(handlers) = self.events.get(&event) { let ctx = self.ctx.clone(); @@ -112,7 +117,8 @@ impl Window { } pub(crate) fn resized(&mut self, size: PhysicalSize) { - self.renderer.resized(size.width, size.height) + self.renderer.resized(size.width, size.height); + self.emit_event(Event::Resized) } }