From 49dbf2c14658cb5f2aafdbb75d826d8ba8fedc31 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jun 2020 15:45:57 +0200 Subject: Request a redraw only on relevant events --- winit/src/application.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 73ac72b2..a5d00407 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -150,6 +150,10 @@ pub fn run( event_loop.run(move |event, _, control_flow| match event { event::Event::MainEventsCleared => { + if state.is_queue_empty() { + return; + } + let command = runtime.enter(|| { state.update( clipboard.as_ref().map(|c| c as _), -- cgit From 4c0286e8acdf0792a9680f6f8212a534a51e3da0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 12 Jun 2020 22:12:15 +0200 Subject: Add `background_color` to `Application` and `Sandbox` --- winit/src/application.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index a5d00407..ceca5645 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -1,6 +1,6 @@ //! Create interactive, native cross-platform applications. use crate::{ - conversion, mouse, Clipboard, Command, Debug, Executor, Mode, Proxy, + conversion, mouse, Clipboard, Color, Command, Debug, Executor, Mode, Proxy, Runtime, Settings, Size, Subscription, }; use iced_graphics::window; @@ -73,6 +73,17 @@ pub trait Application: Program { fn mode(&self) -> Mode { Mode::Windowed } + + /// Returns the background [`Color`] of the [`Application`]. + /// + /// By default, it returns [`Color::WHITE`]. + /// + /// [`Color`]: struct.Color.html + /// [`Application`]: trait.Application.html + /// [`Color::WHITE`]: struct.Color.html#const.WHITE + fn background_color(&self) -> Color { + Color::WHITE + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -112,6 +123,7 @@ pub fn run( let mut title = application.title(); let mut mode = application.mode(); + let mut background_color = application.background_color(); let window = settings .window @@ -193,6 +205,9 @@ pub fn run( mode = new_mode; } + + // Update background color + background_color = program.background_color(); } window.request_redraw(); @@ -219,6 +234,7 @@ pub fn run( &mut renderer, &mut swap_chain, &viewport, + background_color, state.primitive(), &debug.overlay(), ); -- cgit From c9696ca687446d78de374a828183de0a5e4bace3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 Jun 2020 19:17:05 +0200 Subject: Add `scale_factor` to `Application` and `Sandbox` --- winit/src/application.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index ceca5645..b512aace 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -84,6 +84,21 @@ pub trait Application: Program { fn background_color(&self) -> Color { Color::WHITE } + + /// Returns the scale factor of the [`Application`]. + /// + /// It can be used to dynamically control the size of the UI at runtime + /// (i.e. zooming). + /// + /// For instance, a scale factor of `2.0` will make widgets twice as big, + /// while a scale factor of `0.5` will shrink them to half their size. + /// + /// By default, it returns `1.0`. + /// + /// [`Application`]: trait.Application.html + fn scale_factor(&self) -> f64 { + 1.0 + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -124,6 +139,7 @@ pub fn run( let mut title = application.title(); let mut mode = application.mode(); let mut background_color = application.background_color(); + let mut scale_factor = application.scale_factor(); let window = settings .window @@ -138,7 +154,7 @@ pub fn run( let physical_size = window.inner_size(); let mut viewport = Viewport::with_physical_size( Size::new(physical_size.width, physical_size.height), - window.scale_factor(), + window.scale_factor() * scale_factor, ); let mut resized = false; @@ -208,6 +224,20 @@ pub fn run( // Update background color background_color = program.background_color(); + + // Update scale factor + let new_scale_factor = program.scale_factor(); + + if scale_factor != new_scale_factor { + let size = window.inner_size(); + + viewport = Viewport::with_physical_size( + Size::new(size.width, size.height), + window.scale_factor() * new_scale_factor, + ); + + scale_factor = new_scale_factor; + } } window.request_redraw(); @@ -259,6 +289,7 @@ pub fn run( handle_window_event( &window_event, &window, + scale_factor, control_flow, &mut modifiers, &mut viewport, @@ -286,6 +317,7 @@ pub fn run( pub fn handle_window_event( event: &winit::event::WindowEvent<'_>, window: &winit::window::Window, + scale_factor: f64, control_flow: &mut winit::event_loop::ControlFlow, modifiers: &mut winit::event::ModifiersState, viewport: &mut Viewport, @@ -298,8 +330,10 @@ pub fn handle_window_event( WindowEvent::Resized(new_size) => { let size = Size::new(new_size.width, new_size.height); - *viewport = - Viewport::with_physical_size(size, window.scale_factor()); + *viewport = Viewport::with_physical_size( + size, + window.scale_factor() * scale_factor, + ); *resized = true; } WindowEvent::CloseRequested => { -- cgit From bbdf558bd7eb3abbf69c922b34075360cd5f12c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Jun 2020 06:12:06 +0200 Subject: Relayout when `Application::scale_factor` changes --- winit/src/application.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index b512aace..cb1bbf1e 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -236,6 +236,19 @@ pub fn run( window.scale_factor() * new_scale_factor, ); + // We relayout the UI with the new logical size. + // The queue is empty, therefore this will never produce + // a `Command`. + // + // TODO: Properly queue `WindowResized` and `CursorMoved` + // events. + let _ = state.update( + clipboard.as_ref().map(|c| c as _), + viewport.logical_size(), + &mut renderer, + &mut debug, + ); + scale_factor = new_scale_factor; } } -- cgit From f30a666dc81fdc85d225dc83f1a33e32d5dccbd2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Jun 2020 06:44:34 +0200 Subject: Decouple `cursor_position` from `Cache` Instead, we ask explicitly for it in the different `update` and `draw` methods. This way, the runtime can derive the logical position of the cursor from the source of truth. --- winit/src/application.rs | 22 ++++++++++++++++++---- winit/src/conversion.rs | 12 +++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index cb1bbf1e..5b93c8af 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -148,6 +148,7 @@ pub fn run( .expect("Open window"); let clipboard = Clipboard::new(&window); + let mut cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0); let mut mouse_interaction = mouse::Interaction::default(); let mut modifiers = winit::event::ModifiersState::default(); @@ -171,6 +172,7 @@ pub fn run( let mut state = program::State::new( application, viewport.logical_size(), + conversion::cursor_position(cursor_position, viewport.scale_factor()), &mut renderer, &mut debug, ); @@ -184,8 +186,12 @@ pub fn run( let command = runtime.enter(|| { state.update( - clipboard.as_ref().map(|c| c as _), viewport.logical_size(), + conversion::cursor_position( + cursor_position, + viewport.scale_factor(), + ), + clipboard.as_ref().map(|c| c as _), &mut renderer, &mut debug, ) @@ -240,11 +246,14 @@ pub fn run( // The queue is empty, therefore this will never produce // a `Command`. // - // TODO: Properly queue `WindowResized` and `CursorMoved` - // events. + // TODO: Properly queue `WindowResized` let _ = state.update( - clipboard.as_ref().map(|c| c as _), viewport.logical_size(), + conversion::cursor_position( + cursor_position, + viewport.scale_factor(), + ), + clipboard.as_ref().map(|c| c as _), &mut renderer, &mut debug, ); @@ -304,6 +313,7 @@ pub fn run( &window, scale_factor, control_flow, + &mut cursor_position, &mut modifiers, &mut viewport, &mut resized, @@ -332,6 +342,7 @@ pub fn handle_window_event( window: &winit::window::Window, scale_factor: f64, control_flow: &mut winit::event_loop::ControlFlow, + cursor_position: &mut winit::dpi::PhysicalPosition, modifiers: &mut winit::event::ModifiersState, viewport: &mut Viewport, resized: &mut bool, @@ -352,6 +363,9 @@ pub fn handle_window_event( WindowEvent::CloseRequested => { *control_flow = ControlFlow::Exit; } + WindowEvent::CursorMoved { position, .. } => { + *cursor_position = *position; + } WindowEvent::ModifiersChanged(new_modifiers) => { *modifiers = *new_modifiers; } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b887db6e..80727bd8 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -4,7 +4,7 @@ //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, window, Event, Mode, + mouse, window, Event, Mode, Point, }; /// Converts a winit window event into an iced event. @@ -174,6 +174,16 @@ pub fn modifiers_state( } } +/// Converts a physical cursor position to a logical `Point`. +pub fn cursor_position( + position: winit::dpi::PhysicalPosition, + scale_factor: f64, +) -> Point { + let logical_position = position.to_logical(scale_factor); + + Point::new(logical_position.x, logical_position.y) +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 65a4dca0d965ca963428231173ca5fb9c672ab52 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 25 Jun 2020 00:32:41 +0200 Subject: Add `min_size` and `max_size` to `window::Settings` --- winit/src/settings.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'winit/src') diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 37cb832f..6799f23f 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -33,6 +33,12 @@ pub struct Window { /// The size of the window. pub size: (u32, u32), + /// The minimum size of the window. + pub min_size: Option<(u32, u32)>, + + /// The maximum size of the window. + pub max_size: Option<(u32, u32)>, + /// Whether the window should be resizable or not. pub resizable: bool, @@ -62,6 +68,16 @@ impl Window { .with_decorations(self.decorations) .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); + if let Some((width, height)) = self.min_size { + window_builder = window_builder + .with_min_inner_size(winit::dpi::LogicalSize { width, height }); + } + + if let Some((width, height)) = self.max_size { + window_builder = window_builder + .with_max_inner_size(winit::dpi::LogicalSize { width, height }); + } + #[cfg(target_os = "windows")] { use winit::platform::windows::WindowBuilderExtWindows; @@ -79,6 +95,8 @@ impl Default for Window { fn default() -> Window { Window { size: (1024, 768), + min_size: None, + max_size: None, resizable: true, decorations: true, platform_specific: Default::default(), -- cgit From 78cb805fac6e6f6d5679b24e222271a2c2ed221e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 07:36:13 +0200 Subject: Add `ModifiersChanged` to `keyboard::Event` --- winit/src/conversion.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit/src') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 80727bd8..cfa76e88 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -92,6 +92,9 @@ pub fn window_event( } } })), + WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( + keyboard::Event::ModifiersChanged(modifiers_state(*new_modifiers)), + )), WindowEvent::HoveredFile(path) => { Some(Event::Window(window::Event::FileHovered(path.clone()))) } -- cgit From 9a037a23e9b32d9dbe7086a54d777b5f0550a660 Mon Sep 17 00:00:00 2001 From: Francesco Pasa Date: Sat, 11 Apr 2020 15:16:10 +0200 Subject: Add support for setting window icon This adds a new property from Settings::window::iconand a Icon struct which can be converted to winit::window::Icon. It also adds code to display this icon in Application::run. Due to the fact that the Icon struct is non copyable, I also had to remove the Copy trait from all Settings, both in `iced` and `iced_winit`. --- winit/src/lib.rs | 1 + winit/src/settings.rs | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'winit/src') diff --git a/winit/src/lib.rs b/winit/src/lib.rs index bdab3ed7..4e5dc547 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -38,5 +38,6 @@ pub use clipboard::Clipboard; pub use mode::Mode; pub use proxy::Proxy; pub use settings::Settings; +pub use settings::Icon; pub use iced_graphics::Viewport; diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 6799f23f..8cfadf02 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -14,7 +14,7 @@ use winit::monitor::MonitorHandle; use winit::window::WindowBuilder; /// The settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Settings { /// The [`Window`] settings /// @@ -28,7 +28,7 @@ pub struct Settings { } /// The window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Window { /// The size of the window. pub size: (u32, u32), @@ -45,6 +45,9 @@ pub struct Window { /// Whether the window should have a border, a title bar, etc. pub decorations: bool, + /// The window icon, which is also usually used in the taskbar + pub icon: Option, + /// Platform specific settings. pub platform_specific: platform::PlatformSpecific, } @@ -66,6 +69,7 @@ impl Window { .with_inner_size(winit::dpi::LogicalSize { width, height }) .with_resizable(self.resizable) .with_decorations(self.decorations) + .with_window_icon(self.icon.map(Icon::into)) .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); if let Some((width, height)) = self.min_size { @@ -99,7 +103,38 @@ impl Default for Window { max_size: None, resizable: true, decorations: true, + icon: None, platform_specific: Default::default(), } } } + +/// An Icon +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Icon { + rgba: Vec, + width: u32, + height: u32, +} + +impl Icon { + /// + pub fn new(rgba: &[u8], width: u32, height: u32) -> Self { + Self { + rgba: rgba.to_vec(), + width, + height, + } + } +} + +impl Into for Icon { + fn into(self) -> winit::window::Icon { + winit::window::Icon::from_rgba( + self.rgba.to_vec(), + self.width, + self.height, + ) + .unwrap() + } +} -- cgit From a0cc7e4e43f16de4c19607f913b7f587c61aa5ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 06:09:39 +0200 Subject: Move `Icon` to `iced` crate and introduce `Error` --- winit/src/lib.rs | 1 - winit/src/settings.rs | 38 ++++---------------------------------- 2 files changed, 4 insertions(+), 35 deletions(-) (limited to 'winit/src') diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 4e5dc547..bdab3ed7 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -38,6 +38,5 @@ pub use clipboard::Clipboard; pub use mode::Mode; pub use proxy::Proxy; pub use settings::Settings; -pub use settings::Icon; pub use iced_graphics::Viewport; diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 8cfadf02..4155bf7d 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -14,7 +14,7 @@ use winit::monitor::MonitorHandle; use winit::window::WindowBuilder; /// The settings of an application. -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, Default)] pub struct Settings { /// The [`Window`] settings /// @@ -28,7 +28,7 @@ pub struct Settings { } /// The window settings of an application. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone)] pub struct Window { /// The size of the window. pub size: (u32, u32), @@ -46,7 +46,7 @@ pub struct Window { pub decorations: bool, /// The window icon, which is also usually used in the taskbar - pub icon: Option, + pub icon: Option, /// Platform specific settings. pub platform_specific: platform::PlatformSpecific, @@ -69,7 +69,7 @@ impl Window { .with_inner_size(winit::dpi::LogicalSize { width, height }) .with_resizable(self.resizable) .with_decorations(self.decorations) - .with_window_icon(self.icon.map(Icon::into)) + .with_window_icon(self.icon) .with_fullscreen(conversion::fullscreen(primary_monitor, mode)); if let Some((width, height)) = self.min_size { @@ -108,33 +108,3 @@ impl Default for Window { } } } - -/// An Icon -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Icon { - rgba: Vec, - width: u32, - height: u32, -} - -impl Icon { - /// - pub fn new(rgba: &[u8], width: u32, height: u32) -> Self { - Self { - rgba: rgba.to_vec(), - width, - height, - } - } -} - -impl Into for Icon { - fn into(self) -> winit::window::Icon { - winit::window::Icon::from_rgba( - self.rgba.to_vec(), - self.width, - self.height, - ) - .unwrap() - } -} -- cgit