From 7354f68b3ca345767de3f09dccddf168493977bf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 02:59:08 +0100 Subject: Draft `Shell:request_redraw` API ... and implement `TextInput` cursor blink :tada: --- winit/src/application.rs | 72 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 11 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 74c73815..0f5309d2 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -11,7 +11,7 @@ use crate::mouse; use crate::renderer; use crate::widget::operation; use crate::{ - Command, Debug, Error, Executor, Proxy, Runtime, Settings, Size, + Command, Debug, Error, Event, Executor, Proxy, Runtime, Settings, Size, Subscription, }; @@ -25,6 +25,7 @@ use iced_native::user_interface::{self, UserInterface}; pub use iced_native::application::{Appearance, StyleSheet}; use std::mem::ManuallyDrop; +use std::time::Instant; #[cfg(feature = "trace")] pub use profiler::Profiler; @@ -186,7 +187,8 @@ where let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; - let (mut sender, receiver) = mpsc::unbounded(); + let (mut event_sender, event_receiver) = mpsc::unbounded(); + let (control_sender, mut control_receiver) = mpsc::unbounded(); let mut instance = Box::pin({ let run_instance = run_instance::( @@ -196,7 +198,8 @@ where runtime, proxy, debug, - receiver, + event_receiver, + control_sender, init_command, window, settings.exit_on_close_request, @@ -234,13 +237,19 @@ where }; if let Some(event) = event { - sender.start_send(event).expect("Send event"); + event_sender.start_send(event).expect("Send event"); let poll = instance.as_mut().poll(&mut context); - *control_flow = match poll { - task::Poll::Pending => ControlFlow::Wait, - task::Poll::Ready(_) => ControlFlow::Exit, + match poll { + task::Poll::Pending => { + if let Ok(Some(flow)) = control_receiver.try_next() { + *control_flow = flow; + } + } + task::Poll::Ready(_) => { + *control_flow = ControlFlow::Exit; + } }; } }) @@ -253,7 +262,10 @@ async fn run_instance( mut runtime: Runtime, A::Message>, mut proxy: winit::event_loop::EventLoopProxy, mut debug: Debug, - mut receiver: mpsc::UnboundedReceiver>, + mut event_receiver: mpsc::UnboundedReceiver< + winit::event::Event<'_, A::Message>, + >, + mut control_sender: mpsc::UnboundedSender, init_command: Command, window: winit::window::Window, exit_on_close_request: bool, @@ -265,6 +277,7 @@ async fn run_instance( { use iced_futures::futures::stream::StreamExt; use winit::event; + use winit::event_loop::ControlFlow; let mut clipboard = Clipboard::connect(&window); let mut cache = user_interface::Cache::default(); @@ -309,13 +322,21 @@ async fn run_instance( let mut mouse_interaction = mouse::Interaction::default(); let mut events = Vec::new(); let mut messages = Vec::new(); + let mut redraw_pending = false; debug.startup_finished(); - while let Some(event) = receiver.next().await { + while let Some(event) = event_receiver.next().await { match event { + event::Event::NewEvents(start_cause) => { + redraw_pending = matches!( + start_cause, + event::StartCause::Init + | event::StartCause::ResumeTimeReached { .. } + ); + } event::Event::MainEventsCleared => { - if events.is_empty() && messages.is_empty() { + if !redraw_pending && events.is_empty() && messages.is_empty() { continue; } @@ -338,7 +359,7 @@ async fn run_instance( if !messages.is_empty() || matches!( interface_state, - user_interface::State::Outdated, + user_interface::State::Outdated ) { let mut cache = @@ -376,6 +397,24 @@ async fn run_instance( } } + // TODO: Avoid redrawing all the time by forcing widgets to + // request redraws on state changes + // + // Then, we can use the `interface_state` here to decide whether + // if a redraw is needed right away, or simply wait until a + // specific time. + let redraw_event = Event::Window( + crate::window::Event::RedrawRequested(Instant::now()), + ); + + let (interface_state, _) = user_interface.update( + &[redraw_event.clone()], + state.cursor_position(), + &mut renderer, + &mut clipboard, + &mut messages, + ); + debug.draw_started(); let new_mouse_interaction = user_interface.draw( &mut renderer, @@ -396,6 +435,17 @@ async fn run_instance( } window.request_redraw(); + runtime + .broadcast((redraw_event, crate::event::Status::Ignored)); + + let _ = control_sender.start_send(match interface_state { + user_interface::State::Updated { + redraw_requested_at: Some(at), + } => ControlFlow::WaitUntil(at), + _ => ControlFlow::Wait, + }); + + redraw_pending = false; } event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( event::MacOS::ReceivedUrl(url), -- cgit From 0b86c4a299d384cafca31206eac8c94f1123518d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 04:35:41 +0100 Subject: Implement `window::frames` subscription ... and use it in the `solar_system` example :tada: --- winit/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit/src') diff --git a/winit/src/window.rs b/winit/src/window.rs index 89db3262..0b9e4c46 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -2,7 +2,7 @@ use crate::command::{self, Command}; use iced_native::window; -pub use window::{Event, Mode, UserAttention}; +pub use window::{frames, Event, Mode, UserAttention}; /// Closes the current window and exits the application. pub fn close() -> Command { -- cgit From e2ddef74387bcd81859b56e47316c47d7b739a01 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 05:18:25 +0100 Subject: Replace `Option` with `RedrawRequest` enum --- winit/src/application.rs | 12 ++++++++++-- winit/src/window.rs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 0f5309d2..b6485cb7 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -332,6 +332,7 @@ async fn run_instance( redraw_pending = matches!( start_cause, event::StartCause::Init + | event::StartCause::Poll | event::StartCause::ResumeTimeReached { .. } ); } @@ -440,8 +441,15 @@ async fn run_instance( let _ = control_sender.start_send(match interface_state { user_interface::State::Updated { - redraw_requested_at: Some(at), - } => ControlFlow::WaitUntil(at), + redraw_request: Some(redraw_request), + } => match redraw_request { + crate::window::RedrawRequest::NextFrame => { + ControlFlow::Poll + } + crate::window::RedrawRequest::At(at) => { + ControlFlow::WaitUntil(at) + } + }, _ => ControlFlow::Wait, }); diff --git a/winit/src/window.rs b/winit/src/window.rs index 0b9e4c46..2306bdf1 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -2,7 +2,7 @@ use crate::command::{self, Command}; use iced_native::window; -pub use window::{frames, Event, Mode, UserAttention}; +pub use window::{frames, Event, Mode, RedrawRequest, UserAttention}; /// Closes the current window and exits the application. pub fn close() -> Command { -- cgit From fc54d6ba31246157422d092914ba7c1e483129c4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 05:26:39 +0100 Subject: Use `instant` to fix Wasm target --- winit/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index b6485cb7..4d7382cc 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -24,8 +24,8 @@ use iced_native::user_interface::{self, UserInterface}; pub use iced_native::application::{Appearance, StyleSheet}; +use instant::Instant; use std::mem::ManuallyDrop; -use std::time::Instant; #[cfg(feature = "trace")] pub use profiler::Profiler; -- cgit From c6d0046102bb6951bf0f1f6102f748199c5889e2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 06:24:44 +0100 Subject: Use `instant` instead of `wasm-timer` in `iced_core` --- winit/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 4d7382cc..88179391 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -20,11 +20,11 @@ use iced_futures::futures::channel::mpsc; use iced_graphics::compositor; use iced_graphics::window; use iced_native::program::Program; +use iced_native::time::Instant; use iced_native::user_interface::{self, UserInterface}; pub use iced_native::application::{Appearance, StyleSheet}; -use instant::Instant; use std::mem::ManuallyDrop; #[cfg(feature = "trace")] -- cgit From 507820a8438cec25074f92b72e118e0931fa7f9f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 13 Jan 2023 18:19:05 +0100 Subject: Fix grammar of `TODO` comment in `application` modules --- winit/src/application.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'winit/src') diff --git a/winit/src/application.rs b/winit/src/application.rs index 88179391..77ca4b31 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -401,9 +401,8 @@ async fn run_instance( // TODO: Avoid redrawing all the time by forcing widgets to // request redraws on state changes // - // Then, we can use the `interface_state` here to decide whether - // if a redraw is needed right away, or simply wait until a - // specific time. + // Then, we can use the `interface_state` here to decide if a redraw + // is needed right away, or simply wait until a specific time. let redraw_event = Event::Window( crate::window::Event::RedrawRequested(Instant::now()), ); -- cgit From 9fe46de13f86967543de8f00b5a4f9e45d8f5bcf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Jan 2023 00:49:58 +0100 Subject: Bump versions :tada: --- winit/src/conversion.rs | 12 ++++++------ winit/src/lib.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'winit/src') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 1418e346..e83e55ec 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -1,7 +1,7 @@ //! Convert [`winit`] types into [`iced_native`] types, and viceversa. //! //! [`winit`]: https://github.com/rust-windowing/winit -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native use crate::keyboard; use crate::mouse; use crate::touch; @@ -218,7 +218,7 @@ pub fn mode(mode: Option) -> window::Mode { /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native pub fn mouse_interaction( interaction: mouse::Interaction, ) -> winit::window::CursorIcon { @@ -242,7 +242,7 @@ pub fn mouse_interaction( /// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { match mouse_button { winit::event::MouseButton::Left => mouse::Button::Left, @@ -258,7 +258,7 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// modifiers state. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { @@ -285,7 +285,7 @@ pub fn cursor_position( /// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native pub fn touch_event( touch: winit::event::Touch, scale_factor: f64, @@ -316,7 +316,7 @@ pub fn touch_event( /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native pub fn key_code( virtual_keycode: winit::event::VirtualKeyCode, ) -> keyboard::KeyCode { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 06674109..c3172319 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -11,7 +11,7 @@ //! Additionally, a [`conversion`] module is available for users that decide to //! implement a custom event loop. //! -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.6/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native //! [`winit`]: https://github.com/rust-windowing/winit //! [`conversion`]: crate::conversion #![doc( -- cgit