From a980024bbf7fec595efd2daa56b0572574ba2ce7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 12 Jan 2023 03:11:08 +0100 Subject: Implement widget redraw support in `iced_glutin` --- glutin/src/application.rs | 72 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 11 deletions(-) (limited to 'glutin') diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 1464bb2d..a3cef829 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -12,10 +12,11 @@ use iced_winit::futures; use iced_winit::futures::channel::mpsc; use iced_winit::renderer; use iced_winit::user_interface; -use iced_winit::{Clipboard, Command, Debug, Proxy, Settings}; +use iced_winit::{Clipboard, Command, Debug, Event, Proxy, Settings}; use glutin::window::Window; use std::mem::ManuallyDrop; +use std::time::Instant; #[cfg(feature = "tracing")] use tracing::{info_span, instrument::Instrument}; @@ -131,7 +132,8 @@ where })? }; - 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::( @@ -141,7 +143,8 @@ where runtime, proxy, debug, - receiver, + event_receiver, + control_sender, context, init_command, settings.exit_on_close_request, @@ -179,14 +182,20 @@ 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; + } + } } }); @@ -200,7 +209,10 @@ async fn run_instance( mut runtime: Runtime, A::Message>, mut proxy: glutin::event_loop::EventLoopProxy, mut debug: Debug, - mut receiver: mpsc::UnboundedReceiver>, + mut event_receiver: mpsc::UnboundedReceiver< + glutin::event::Event<'_, A::Message>, + >, + mut control_sender: mpsc::UnboundedSender, mut context: glutin::ContextWrapper, init_command: Command, exit_on_close_request: bool, @@ -211,6 +223,7 @@ async fn run_instance( ::Theme: StyleSheet, { use glutin::event; + use glutin::event_loop::ControlFlow; use iced_winit::futures::stream::StreamExt; let mut clipboard = Clipboard::connect(context.window()); @@ -247,13 +260,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; } @@ -315,6 +336,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, @@ -335,6 +374,17 @@ async fn run_instance( } context.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 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 --- glutin/src/application.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'glutin') diff --git a/glutin/src/application.rs b/glutin/src/application.rs index a3cef829..3bb9e61a 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -270,6 +270,7 @@ async fn run_instance( redraw_pending = matches!( start_cause, event::StartCause::Init + | event::StartCause::Poll | event::StartCause::ResumeTimeReached { .. } ); } @@ -379,8 +380,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, }); -- 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 --- glutin/Cargo.toml | 7 ++++--- glutin/src/application.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'glutin') diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 304170cd..a458ee9e 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -15,8 +15,9 @@ trace = ["iced_winit/trace"] debug = ["iced_winit/debug"] system = ["iced_winit/system"] -[dependencies.log] -version = "0.4" +[dependencies] +log = "0.4" +instant = "0.1" [dependencies.glutin] version = "0.29" @@ -39,4 +40,4 @@ features = ["opengl"] [dependencies.tracing] version = "0.1.6" -optional = true \ No newline at end of file +optional = true diff --git a/glutin/src/application.rs b/glutin/src/application.rs index 3bb9e61a..a2096ee4 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -15,8 +15,8 @@ use iced_winit::user_interface; use iced_winit::{Clipboard, Command, Debug, Event, Proxy, Settings}; use glutin::window::Window; +use instant::Instant; use std::mem::ManuallyDrop; -use std::time::Instant; #[cfg(feature = "tracing")] use tracing::{info_span, instrument::Instrument}; -- 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` --- glutin/Cargo.toml | 1 - glutin/src/application.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'glutin') diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index a458ee9e..709b118b 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -17,7 +17,6 @@ system = ["iced_winit/system"] [dependencies] log = "0.4" -instant = "0.1" [dependencies.glutin] version = "0.29" diff --git a/glutin/src/application.rs b/glutin/src/application.rs index a2096ee4..da0af5c0 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -11,11 +11,11 @@ use iced_winit::conversion; use iced_winit::futures; use iced_winit::futures::channel::mpsc; use iced_winit::renderer; +use iced_winit::time::Instant; use iced_winit::user_interface; use iced_winit::{Clipboard, Command, Debug, Event, Proxy, Settings}; use glutin::window::Window; -use instant::Instant; use std::mem::ManuallyDrop; #[cfg(feature = "tracing")] -- 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 --- glutin/src/application.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'glutin') diff --git a/glutin/src/application.rs b/glutin/src/application.rs index da0af5c0..b7bf21c3 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -340,9 +340,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: --- glutin/Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'glutin') diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 709b118b..27ed29e8 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_glutin" -version = "0.5.0" +version = "0.6.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A glutin runtime for Iced" @@ -24,16 +24,16 @@ git = "https://github.com/iced-rs/glutin" rev = "da8d291486b4c9bec12487a46c119c4b1d386abf" [dependencies.iced_native] -version = "0.7" +version = "0.8" path = "../native" [dependencies.iced_winit] -version = "0.6" +version = "0.7" path = "../winit" features = ["application"] [dependencies.iced_graphics] -version = "0.5" +version = "0.6" path = "../graphics" features = ["opengl"] -- cgit From 35c0fa3b00666fae69da4cccf8f7467e7c04ef59 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Jan 2023 01:14:02 +0100 Subject: Bump versions in `README`s --- glutin/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'glutin') diff --git a/glutin/README.md b/glutin/README.md index 263cc0af..1d873874 100644 --- a/glutin/README.md +++ b/glutin/README.md @@ -20,7 +20,7 @@ It exposes a renderer-agnostic `Application` trait that can be implemented and t Add `iced_glutin` as a dependency in your `Cargo.toml`: ```toml -iced_glutin = "0.2" +iced_glutin = "0.6" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If -- cgit