Handle window resizes in iced_winit

This commit is contained in:
Héctor Ramón Jiménez 2019-10-11 21:16:36 +02:00
parent 1f60ca3ab4
commit 650d020fde

View file

@ -29,14 +29,17 @@ pub trait Application {
.build(&event_loop) .build(&event_loop)
.expect("Open window"); .expect("Open window");
let size = window.inner_size().to_physical(window.hidpi_factor());; let mut size: Size = window
let (width, height) = (size.width as u16, size.height as u16); .inner_size()
.to_physical(window.hidpi_factor())
.into();
let mut new_size: Option<Size> = None;
let mut renderer = Self::Renderer::new(&window); let mut renderer = Self::Renderer::new(&window);
let mut target = renderer.target(width, height); let mut target = renderer.target(size.width, size.height);
let user_interface = UserInterface::build( let user_interface = UserInterface::build(
document(&mut self, width, height), document(&mut self, size),
Cache::default(), Cache::default(),
&mut renderer, &mut renderer,
); );
@ -56,7 +59,7 @@ pub trait Application {
// This will allow us to rebuild it only when a message is // This will allow us to rebuild it only when a message is
// handled. // handled.
let mut user_interface = UserInterface::build( let mut user_interface = UserInterface::build(
document(&mut self, width, height), document(&mut self, size),
cache.take().unwrap(), cache.take().unwrap(),
&mut renderer, &mut renderer,
); );
@ -79,7 +82,7 @@ pub trait Application {
} }
let user_interface = UserInterface::build( let user_interface = UserInterface::build(
document(&mut self, width, height), document(&mut self, size),
temp_cache, temp_cache,
&mut renderer, &mut renderer,
); );
@ -92,6 +95,11 @@ pub trait Application {
window.request_redraw(); window.request_redraw();
} }
event::Event::RedrawRequested(_) => { event::Event::RedrawRequested(_) => {
if let Some(new_size) = new_size.take() {
target = renderer.target(new_size.width, new_size.height);
size = new_size;
}
let new_mouse_cursor = renderer.draw(&mut target, &primitive); let new_mouse_cursor = renderer.draw(&mut target, &primitive);
if new_mouse_cursor != mouse_cursor { if new_mouse_cursor != mouse_cursor {
@ -127,6 +135,12 @@ pub trait Application {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit; *control_flow = ControlFlow::Exit;
} }
WindowEvent::Resized(size) => {
new_size =
Some(size.to_physical(window.hidpi_factor()).into());
log::debug!("Resized: {:?}", new_size);
}
_ => {} _ => {}
}, },
_ => { _ => {
@ -136,18 +150,32 @@ pub trait Application {
} }
} }
fn document<Application>( #[derive(Debug, Clone, Copy, Eq, PartialEq)]
application: &mut Application, struct Size {
width: u16, width: u16,
height: u16, height: u16,
}
impl From<winit::dpi::PhysicalSize> for Size {
fn from(physical_size: winit::dpi::PhysicalSize) -> Self {
Self {
width: physical_size.width.round() as u16,
height: physical_size.height.round() as u16,
}
}
}
fn document<Application>(
application: &mut Application,
size: Size,
) -> Element<Application::Message, Application::Renderer> ) -> Element<Application::Message, Application::Renderer>
where where
Application: self::Application, Application: self::Application,
Application::Message: 'static, Application::Message: 'static,
{ {
Column::new() Column::new()
.width(Length::Units(width)) .width(Length::Units(size.width))
.height(Length::Units(height)) .height(Length::Units(size.height))
.push(application.view()) .push(application.view())
.into() .into()
} }