new window resize event

master
TheOddGarlic 2022-05-22 12:52:26 +03:00
parent c41c2b9c69
commit 0649c53695
8 changed files with 21 additions and 31 deletions

View File

@ -165,27 +165,10 @@ impl Renderer {
} }
pub fn size(&self) -> (u32, u32) { pub fn size(&self) -> (u32, u32) {
unsafe { unsafe {(
let mut root = mem::zeroed(); cairo_xlib_surface_get_width(self.surface).try_into().unwrap(),
let mut x = mem::zeroed(); cairo_xlib_surface_get_height(self.surface).try_into().unwrap(),
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)
}
} }
fn text_extents(&self, text: &str) -> TextExtents { fn text_extents(&self, text: &str) -> TextExtents {

View File

@ -39,7 +39,7 @@ impl Renderer {
.unwrap() .unwrap()
}; };
Self { let mut renderer = Self {
handle, handle,
factory, factory,
dw_factory, dw_factory,
@ -48,12 +48,12 @@ impl Renderer {
stroke_style, stroke_style,
system_fonts, system_fonts,
text_format, text_format,
} };
renderer.setup_target();
renderer
} }
pub fn begin_draw(&mut self) { pub fn begin_draw(&mut self) {
self.setup_target();
unsafe { self.target.as_ref().unwrap().BeginDraw() } unsafe { self.target.as_ref().unwrap().BeginDraw() }
} }

View File

@ -104,7 +104,7 @@ impl Application {
self.windows.remove(&window_id); self.windows.remove(&window_id);
if self.windows.is_empty() { if self.windows.is_empty() {
emit_app_event!(self, ApplicationEvent::AllWindowsClosed); emit_app_event!(self, ApplicationEvent::AllWindowsDestroyed);
} }
} }
Event::WindowEvent { Event::WindowEvent {

View File

@ -9,5 +9,5 @@
#[non_exhaustive] #[non_exhaustive]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Event { pub enum Event {
AllWindowsClosed, AllWindowsDestroyed,
} }

View File

@ -10,4 +10,5 @@
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Event { pub enum Event {
Closed, Closed,
Resized,
} }

View File

@ -23,7 +23,7 @@ fn launch() -> _ {
.add(Label::new("this is a label! jjjjyyy") .add(Label::new("this is a label! jjjjyyy")
.bg_color(rgb!(0xFF0000FF))) .bg_color(rgb!(0xFF0000FF)))
.padding_left(10)) .padding_left(10))
.on_event(WindowEvent::Closed, |_, window| { .on_event(WindowEvent::Resized, |_, window| {
window.set_title("CLOSING!") println!("window resized: {:?}", window.size())
})) }))
} }

View File

@ -25,7 +25,7 @@ pub struct QuitPlugin;
impl Plugin for QuitPlugin { impl Plugin for QuitPlugin {
fn apply(&self, app: Application) -> Application { fn apply(&self, app: Application) -> Application {
app.on_event(Event::AllWindowsClosed, |ctx| { app.on_event(Event::AllWindowsDestroyed, |ctx| {
if Platform::target() != Platform::MacOS { if Platform::target() != Platform::MacOS {
ctx.quit() ctx.quit()
} }

View File

@ -99,6 +99,11 @@ impl Window {
self.window.set_title(title.as_ref()) 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) { pub(crate) fn emit_event(&mut self, event: Event) {
if let Some(handlers) = self.events.get(&event) { if let Some(handlers) = self.events.get(&event) {
let ctx = self.ctx.clone(); let ctx = self.ctx.clone();
@ -112,7 +117,8 @@ impl Window {
} }
pub(crate) fn resized(&mut self, size: PhysicalSize<u32>) { pub(crate) fn resized(&mut self, size: PhysicalSize<u32>) {
self.renderer.resized(size.width, size.height) self.renderer.resized(size.width, size.height);
self.emit_event(Event::Resized)
} }
} }