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/application.rs') 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/application.rs') 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/application.rs') 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/application.rs') 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 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'winit/src/application.rs') 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; } -- cgit