Add resize_events subscription to window module

This commit is contained in:
Héctor Ramón Jiménez 2024-07-13 12:53:06 +02:00
parent 1eabd38219
commit a108b2eebe
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 16 additions and 15 deletions

View file

@ -23,20 +23,10 @@ pub enum Event {
Closed, Closed,
/// A window was moved. /// A window was moved.
Moved { Moved(Point),
/// The new logical x location of the window
x: i32,
/// The new logical y location of the window
y: i32,
},
/// A window was resized. /// A window was resized.
Resized { Resized(Size),
/// The new logical width of the window
width: u32,
/// The new logical height of the window
height: u32,
},
/// A window redraw was requested. /// A window redraw was requested.
/// ///

View file

@ -194,6 +194,17 @@ pub fn close_events() -> Subscription<Id> {
}) })
} }
/// Subscribes to all [`Event::Resized`] occurrences in the running application.
pub fn resize_events() -> Subscription<(Id, Size)> {
event::listen_with(|event, _status, id| {
if let crate::core::Event::Window(Event::Resized(size)) = event {
Some((id, size))
} else {
None
}
})
}
/// Subscribes to all [`Event::CloseRequested`] occurences in the running application. /// Subscribes to all [`Event::CloseRequested`] occurences in the running application.
pub fn close_requests() -> Subscription<Id> { pub fn close_requests() -> Subscription<Id> {
event::listen_with(|event, _status, id| { event::listen_with(|event, _status, id| {

View file

@ -132,10 +132,10 @@ pub fn window_event(
WindowEvent::Resized(new_size) => { WindowEvent::Resized(new_size) => {
let logical_size = new_size.to_logical(scale_factor); let logical_size = new_size.to_logical(scale_factor);
Some(Event::Window(window::Event::Resized { Some(Event::Window(window::Event::Resized(Size {
width: logical_size.width, width: logical_size.width,
height: logical_size.height, height: logical_size.height,
})) })))
} }
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
Some(Event::Window(window::Event::CloseRequested)) Some(Event::Window(window::Event::CloseRequested))
@ -277,7 +277,7 @@ pub fn window_event(
let winit::dpi::LogicalPosition { x, y } = let winit::dpi::LogicalPosition { x, y } =
position.to_logical(scale_factor); position.to_logical(scale_factor);
Some(Event::Window(window::Event::Moved { x, y })) Some(Event::Window(window::Event::Moved(Point::new(x, y))))
} }
_ => None, _ => None,
} }