I am trying to layout and size components depending on the window size. I have found window::fetch_size()
, but don’t understand how I can use it or find an example
I use it just like:
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
if !self.is_ctrl_pressed {
Command::none()
} else {
let _id = window::Id::MAIN;
window::fetch_size(_id, move |size| {
let (_, _y) = match delta {
ScrollDelta::Lines { x, y } => (x, y),
ScrollDelta::Pixels { x, y } => (x, y),
};
let new_size = Size::new(
size.width + _y * WINDOW_RESIZE_STEP,
size.height + _y * WINDOW_RESIZE_STEP,
);
Message::Resize(new_size)
})
}
}
And Message
is defined as:
pub enum Message {
CommonEvent(Event),
Resize(Size),
}
Then make another function to handle the Resize
Message:
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::CommonEvent(event) => self.handle_event(event),
// Handle here
Message::Resize(size) => window::resize(window::Id::MAIN, size),
// _ => Command::none(),
}
}
and update
function I do it just like the examples events
:
Thanks @Nebell . That was just the pointer I needed to find where to look. Actually i don’t need window::fetch_size
, all I need to do is to listen to the resize event, which I had not found. The events example pointed to is a real discovery.
pub(crate) fn subscription(&self) -> Subscription<Message> {
event::listen_with(|event, _status| {
match event {
Event::Window(id, window::Event::Resized {width, height}) => {
Some(Message::WindowResized(width, height))
}
_ => None
}
})
}