diff options
author | 2019-10-11 21:16:36 +0200 | |
---|---|---|
committer | 2019-10-11 22:15:12 +0200 | |
commit | 650d020fde6e684bf3c865de823ace08194b5220 (patch) | |
tree | 0293ad3c36b8d2d1552b6a0113cd1b9519020f64 /winit | |
parent | 1f60ca3ab4287b5f9bf605fcb7b3cdcf20e17074 (diff) | |
download | iced-650d020fde6e684bf3c865de823ace08194b5220.tar.gz iced-650d020fde6e684bf3c865de823ace08194b5220.tar.bz2 iced-650d020fde6e684bf3c865de823ace08194b5220.zip |
Handle window resizes in `iced_winit`
Diffstat (limited to 'winit')
-rw-r--r-- | winit/src/application.rs | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs index ac3a4619..54c31126 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -29,14 +29,17 @@ pub trait Application { .build(&event_loop) .expect("Open window"); - let size = window.inner_size().to_physical(window.hidpi_factor());; - let (width, height) = (size.width as u16, size.height as u16); + let mut size: Size = window + .inner_size() + .to_physical(window.hidpi_factor()) + .into(); + let mut new_size: Option<Size> = None; 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( - document(&mut self, width, height), + document(&mut self, size), Cache::default(), &mut renderer, ); @@ -56,7 +59,7 @@ pub trait Application { // This will allow us to rebuild it only when a message is // handled. let mut user_interface = UserInterface::build( - document(&mut self, width, height), + document(&mut self, size), cache.take().unwrap(), &mut renderer, ); @@ -79,7 +82,7 @@ pub trait Application { } let user_interface = UserInterface::build( - document(&mut self, width, height), + document(&mut self, size), temp_cache, &mut renderer, ); @@ -92,6 +95,11 @@ pub trait Application { window.request_redraw(); } 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); if new_mouse_cursor != mouse_cursor { @@ -127,6 +135,12 @@ pub trait Application { WindowEvent::CloseRequested => { *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>( - application: &mut Application, +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +struct Size { width: 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> where Application: self::Application, Application::Message: 'static, { Column::new() - .width(Length::Units(width)) - .height(Length::Units(height)) + .width(Length::Units(size.width)) + .height(Length::Units(size.height)) .push(application.view()) .into() } |