From 5de1d50c87730232ed513d2d1d289059cd83172c Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Wed, 2 Jul 2025 08:38:46 +0200 Subject: [PATCH] Update to iced 0.14 - Switched debug toggle to the new `iced_debug` crate, that is using a process-global API - Switched to the new `iced_future` crate - Update keyboard API usage - Fix clippies, etc. --- Cargo.toml | 15 +++-- src/application.rs | 131 +++++++++++++-------------------------- src/application/state.rs | 18 ++++-- src/clipboard.rs | 14 ++--- src/conversion.rs | 21 ++++--- src/proxy.rs | 2 +- src/settings.rs | 11 +--- 7 files changed, 84 insertions(+), 128 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9b7a8b3..78d6c55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,6 @@ categories = ["gui"] [features] default = ["wgpu"] -# Enables a debug view in native platforms (press F12) -debug = ["iced_runtime/debug"] # Enable the wgu renderer wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"] image = ["iced_graphics/image", "iced_widget/image", "iced_renderer/image"] @@ -27,15 +25,20 @@ web-colors = ["iced_graphics/web-colors", "iced_renderer/web-colors"] canvas = ["iced_widget/canvas"] system = ["dep:sysinfo"] trace = [] +toggle_debug = [] [dependencies] baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "579130ecb4f9f315ae52190af42f0ea46aeaa4a2" } cfg-if = "1" window_clipboard = "0.4.1" -iced_runtime = "0.13" -iced_renderer = "0.13" -iced_graphics = "0.13" -iced_widget = "0.13" +iced_runtime = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_renderer = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_graphics = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_widget = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_debug = { git = "https://github.com/iced-rs/iced", branch = "master" } +iced_futures = { git = "https://github.com/iced-rs/iced", branch = "master", features = [ + "smol", +] } keyboard-types = { version = "0.6", default-features = false } log = "0.4" raw-window-handle = "0.5" diff --git a/src/application.rs b/src/application.rs index 238e579..8b5c923 100644 --- a/src/application.rs +++ b/src/application.rs @@ -4,6 +4,8 @@ mod profiler; mod state; use baseview::EventStatus; + +use iced_debug::Span; use iced_runtime::Action; use iced_runtime::Task; use iced_widget::core::Color; @@ -12,7 +14,6 @@ use iced_widget::Theme; use raw_window_handle::HasRawDisplayHandle; pub use state::State; -use crate::core::mouse; use crate::core::renderer; use crate::core::widget::operation; use crate::core::Size; @@ -21,7 +22,6 @@ use crate::futures::{Executor, Runtime, Subscription}; use crate::graphics::compositor::{self, Compositor}; use crate::runtime::clipboard; use crate::runtime::user_interface::{self, UserInterface}; -use crate::runtime::Debug; use crate::window::{IcedWindow, RuntimeEvent, WindowQueue, WindowSubs}; use crate::{Clipboard, Error, Proxy, Renderer, Settings}; @@ -196,8 +196,7 @@ where #[cfg(feature = "trace")] let _guard = Profiler::init(); - let mut debug = Debug::new(); - debug.startup_started(); + let boot_trace = iced_debug::boot(); #[cfg(feature = "trace")] let _ = info_span!("Application", "RUN").entered(); @@ -245,8 +244,7 @@ where let window06 = crate::conversion::convert_window(window); let graphics_settings = settings.graphics_settings; - let mut compositor = - crate::futures::futures::executor::block_on(C::new(graphics_settings, window06.clone()))?; + let mut compositor = runtime.block_on(C::new(graphics_settings, window06.clone()))?; let surface = compositor.create_surface( window06, viewport.physical_width(), @@ -271,7 +269,6 @@ where application, compositor, renderer, - debug, runtime, event_receiver, clipboard, @@ -281,6 +278,7 @@ where event_status.clone(), state, window_queue, + boot_trace, ); #[cfg(feature = "trace")] @@ -303,11 +301,11 @@ where }) } +#[allow(clippy::too_many_arguments)] async fn run_instance( mut application: A, mut compositor: C, mut renderer: Renderer, - mut debug: Debug, mut runtime: Runtime, iced_runtime::Action>, mut event_receiver: mpsc::UnboundedReceiver>, mut clipboard: Clipboard, @@ -318,6 +316,7 @@ async fn run_instance( event_status: Rc>, mut state: State, mut window_queue: WindowQueue, + boot_trace: Span, ) where // What an absolute monstrosity of generics. C: Compositor + 'static, @@ -332,16 +331,16 @@ async fn run_instance( let mut events = Vec::new(); let mut messages = Vec::new(); + let window_id = crate::window::Id::unique(); + let mut user_interface = ManuallyDrop::new(build_user_interface( &application, cache, &mut renderer, state.logical_size(), - &mut debug, + window_id, )); - let mut mouse_interaction = mouse::Interaction::default(); - // Triggered whenever a baseview event gets sent let mut redraw_requested = true; // May be triggered when processing baseview events, will cause the UI to be updated in the next @@ -349,9 +348,9 @@ async fn run_instance( let mut needs_update = true; let mut did_process_event = false; - debug.startup_finished(); + boot_trace.finish(); - let window_id = crate::window::Id::unique(); + let mut render_span = None; loop { // Empty the queue if possible @@ -383,8 +382,7 @@ async fn run_instance( did_process_event = false; if !events.is_empty() { - debug.event_processing_started(); - + let interact_time = iced_debug::interact(window_id); let (interface_state, statuses) = user_interface.update( &events, state.cursor(), @@ -395,8 +393,6 @@ async fn run_instance( needs_update |= matches!(interface_state, user_interface::State::Outdated,); - debug.event_processing_finished(); - for (event, status) in events.drain(..).zip(statuses.into_iter()) { runtime.broadcast(crate::futures::subscription::Event::Interaction { window: window_id, @@ -404,6 +400,7 @@ async fn run_instance( status, }); } + interact_time.finish(); } // The user interface update may have pushed a new message onto the stack @@ -418,7 +415,6 @@ async fn run_instance( update( &mut application, &mut runtime, - &mut debug, &mut messages, &mut window_subs, //&mut window_queue, @@ -434,7 +430,7 @@ async fn run_instance( cache, &mut renderer, state.logical_size(), - &mut debug, + window_id, )); if should_exit { @@ -442,8 +438,8 @@ async fn run_instance( } } - debug.draw_started(); - let new_mouse_interaction = user_interface.draw( + render_span = Some(iced_debug::draw(window_id)); + user_interface.draw( &mut renderer, state.theme(), &iced_runtime::core::renderer::Style { @@ -451,19 +447,6 @@ async fn run_instance( }, state.cursor(), ); - debug.draw_finished(); - - if new_mouse_interaction != mouse_interaction { - // TODO: Set mouse cursor for MacOS once baseview supports it. - #[cfg(not(target_os = "macos"))] - if let Err(_) = window_queue.set_mouse_cursor( - crate::conversion::convert_mouse_interaction(new_mouse_interaction), - ) { - debug.log_message(&"could not send set_mouse_cursor command".to_string()); - } - - mouse_interaction = new_mouse_interaction; - } redraw_requested = true; } @@ -471,11 +454,10 @@ async fn run_instance( run_action::( message, &mut compositor, - &mut renderer, + &renderer, &mut messages, &mut clipboard, &mut user_interface, - &mut debug, &mut window_queue, ); } @@ -495,21 +477,20 @@ async fn run_instance( continue; } - debug.render_started(); let current_viewport_version = state.viewport_version(); if viewport_version != current_viewport_version { let logical_size = state.logical_size(); - debug.layout_started(); + let layout_span = iced_debug::layout(window_id); user_interface = ManuallyDrop::new( ManuallyDrop::into_inner(user_interface) .relayout(logical_size, &mut renderer), ); - debug.layout_finished(); + layout_span.finish(); - debug.draw_started(); - let new_mouse_interaction = user_interface.draw( + let draw_span = iced_debug::draw(window_id); + user_interface.draw( &mut renderer, state.theme(), &renderer::Style { @@ -517,22 +498,7 @@ async fn run_instance( }, state.cursor(), ); - - if new_mouse_interaction != mouse_interaction { - // TODO: Set mouse cursor for MacOS once baseview supports it. - #[cfg(not(target_os = "macos"))] - if let Err(_) = window_queue.set_mouse_cursor( - crate::conversion::convert_mouse_interaction(new_mouse_interaction), - ) { - debug.log_message( - &"could not send set_mouse_cursor command".to_string(), - ); - } - - mouse_interaction = new_mouse_interaction; - } - - debug.draw_finished(); + draw_span.finish(); compositor.configure_surface( &mut surface, @@ -548,13 +514,15 @@ async fn run_instance( &mut surface, state.viewport(), state.background_color(), - &debug.overlay(), + || {}, ) { Ok(()) => { - debug.render_finished(); - // TODO: Handle animations! // Maybe we can use `ControlFlow::WaitUntil` for this. + if let Some(span) = render_span { + span.finish(); + render_span = None; + } } Err(error) => match error { // This is an unrecoverable error. @@ -562,15 +530,13 @@ async fn run_instance( panic!("{error:?}"); } _ => { - debug.render_finished(); - redraw_requested = true; } }, } } RuntimeEvent::Baseview((event, do_send_status)) => { - state.update(&event, &mut debug); + state.update(&event); let ignore_non_modifier_keys = application .ignore_non_modifier_keys() @@ -604,7 +570,6 @@ async fn run_instance( update( &mut application, &mut runtime, - &mut debug, &mut messages, &mut window_subs, ); @@ -613,11 +578,11 @@ async fn run_instance( state.synchronize(&application); user_interface = ManuallyDrop::new(build_user_interface( - &mut application, + &application, cache, &mut renderer, state.logical_size(), - &mut debug, + window_id, )); } @@ -637,7 +602,7 @@ pub fn build_user_interface<'a, A: Application>( cache: user_interface::Cache, renderer: &mut Renderer, size: Size, - debug: &mut Debug, + window_id: crate::window::Id, ) -> UserInterface<'a, A::Message, A::Theme, Renderer> where A::Theme: DefaultStyle, @@ -645,22 +610,22 @@ where #[cfg(feature = "trace")] let view_span = info_span!("Application", "VIEW").entered(); - debug.view_started(); + let view_span = iced_debug::view(window_id); let view = application.view(); + view_span.finish(); #[cfg(feature = "trace")] let _ = view_span.exit(); - debug.view_finished(); #[cfg(feature = "trace")] let layout_span = info_span!("Application", "LAYOUT").entered(); - debug.layout_started(); + let layout_span = iced_debug::layout(window_id); let user_interface = UserInterface::build(view, size, cache, renderer); + layout_span.finish(); #[cfg(feature = "trace")] let _ = layout_span.exit(); - debug.layout_finished(); user_interface } @@ -670,7 +635,6 @@ where pub fn update( application: &mut A, runtime: &mut Runtime, iced_runtime::Action>, - debug: &mut Debug, messages: &mut Vec, window_subs: &mut WindowSubs, //window_queue: &mut WindowQueue, @@ -681,14 +645,10 @@ pub fn update( #[cfg(feature = "trace")] let update_span = info_span!("Application", "UPDATE").entered(); - debug.log_message(&message); - debug.update_started(); - let task = runtime.enter(|| application.update(message)); #[cfg(feature = "trace")] let _ = update_span.exit(); - debug.update_finished(); if let Some(stream) = crate::runtime::task::into_stream(task) { runtime.run(stream); @@ -709,7 +669,6 @@ pub fn run_action( messages: &mut Vec, clipboard: &mut Clipboard, interface: &mut UserInterface<'_, A::Message, A::Theme, Renderer>, - debug: &mut Debug, window_queue: &mut WindowQueue, ) where C: Compositor + 'static, @@ -732,19 +691,13 @@ pub fn run_action( }, Action::Window(action) => match action { IWindowAction::Close(_) => { - if let Err(_) = window_queue.close_window() { - debug.log_message(&"could not send close_window command".to_string()); - } + let _ = window_queue.close_window(); } IWindowAction::Resize(_, size) => { - if let Err(_) = window_queue.resize_window(size) { - debug.log_message(&"could not send resize_window command".to_string()); - } + let _ = window_queue.resize_window(size); } IWindowAction::GainFocus(_) => { - if let Err(_) = window_queue.focus() { - debug.log_message(&"could not send get_window command".to_string()); - } + let _ = window_queue.focus(); } _ => {} }, @@ -784,9 +737,9 @@ pub fn run_action( let _ = channel.send(Ok(())); } Action::Exit => { - if let Err(_) = window_queue.close_window() { - debug.log_message(&"could not send exit command".to_string()); - } + // ignore errors when closing + let _ = window_queue.close_window(); } + Action::Reload => todo!(), } } diff --git a/src/application/state.rs b/src/application/state.rs index 41dd2e9..4e9c3b4 100644 --- a/src/application/state.rs +++ b/src/application/state.rs @@ -4,7 +4,6 @@ use crate::application::{Appearance, Application, DefaultStyle}; use crate::core::mouse; use crate::core::{Color, Size}; use crate::graphics::Viewport; -use crate::runtime::Debug; use std::marker::PhantomData; @@ -25,6 +24,9 @@ where system_scale_factor: f64, scale_policy: WindowScalePolicy, modifiers: iced_runtime::core::keyboard::Modifiers, + + #[cfg(feature = "toggle_debug")] + debug_enabled: bool, } impl State @@ -50,6 +52,8 @@ where system_scale_factor: 1.0, scale_policy, modifiers: Default::default(), + #[cfg(feature = "toggle_debug")] + debug_enabled: false, } } @@ -99,7 +103,7 @@ where /// Processes the provided window event and updates the [`State`] /// accordingly. - pub fn update(&mut self, event: &baseview::Event, _debug: &mut Debug) { + pub fn update(&mut self, event: &baseview::Event) { match event { baseview::Event::Window(baseview::WindowEvent::Resized(window_info)) => { // Cache system window info in case users changes their scale policy in the future. @@ -133,11 +137,17 @@ where } #[allow(unused_variables)] baseview::Event::Keyboard(event) => { - #[cfg(feature = "debug")] + #[cfg(feature = "toggle_debug")] { use keyboard_types::{Key, KeyState}; if event.key == Key::F12 && event.state == KeyState::Down { - _debug.toggle(); + if self.debug_enabled { + iced_debug::enable(); + self.debug_enabled = true; + } else { + iced_debug::disable(); + self.debug_enabled = true; + } } } } diff --git a/src/clipboard.rs b/src/clipboard.rs index 072d7f5..54ba181 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -73,18 +73,16 @@ impl Clipboard { pub fn write(&mut self, kind: ClipboardKind, contents: String) { match &mut self.state { State::Connected(clipboard) => match kind { - ClipboardKind::Primary => match clipboard.borrow_mut().write_primary(contents) { - Some(Err(e)) => { + ClipboardKind::Primary => { + if let Some(Err(e)) = clipboard.borrow_mut().write_primary(contents) { log::warn!("Failed to write to clipboard: {}", e); } - _ => {} - }, - ClipboardKind::Standard => match clipboard.borrow_mut().write(contents) { - Ok(()) => {} - Err(e) => { + } + ClipboardKind::Standard => { + if let Err(e) = clipboard.borrow_mut().write(contents) { log::warn!("Failed to write to clipboard: {}", e); } - }, + } }, State::Unavailable => {} } diff --git a/src/conversion.rs b/src/conversion.rs index abb2c62..4fcf1a6 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -83,21 +83,20 @@ pub fn baseview_to_iced_events( let key = baseview_to_iced_key(event.key); let location = baseview_key_location_to_iced(event.location); + let physical_key = if let Some(code) = baseview_to_iced_keycode(event.code) { + iced_runtime::core::keyboard::key::Physical::Code(code) + } else { + iced_runtime::core::keyboard::key::Physical::Unidentified( + iced_runtime::core::keyboard::key::NativeCode::Unidentified, + ) + }; + if is_down { let text = if let iced_runtime::core::keyboard::Key::Character(s) = &key { Some(s.clone()) } else { None }; - - let physical_key = if let Some(code) = baseview_to_iced_keycode(event.code) { - iced_runtime::core::keyboard::key::Physical::Code(code) - } else { - iced_runtime::core::keyboard::key::Physical::Unidentified( - iced_runtime::core::keyboard::key::NativeCode::Unidentified, - ) - }; - iced_events.push(IcedEvent::Keyboard(IcedKeyEvent::KeyPressed { key: key.clone(), modified_key: key, @@ -108,9 +107,11 @@ pub fn baseview_to_iced_events( })); } else { iced_events.push(IcedEvent::Keyboard(IcedKeyEvent::KeyReleased { - key, + key: key.clone(), location, modifiers: *iced_modifiers, + modified_key: key, + physical_key, })); } } diff --git a/src/proxy.rs b/src/proxy.rs index 30ba0ec..675a6b6 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -35,7 +35,7 @@ impl Sink> for Proxy { } fn start_send(mut self: Pin<&mut Self>, message: Action) -> Result<(), Self::Error> { - let _ = self.sender.start_send(message)?; + self.sender.start_send(message)?; Ok(()) } diff --git a/src/settings.rs b/src/settings.rs index 90ac847..68ae56e 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -41,7 +41,7 @@ impl Default for Settings { } /// Any settings specific to `iced_baseview`. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Default)] pub struct IcedBaseviewSettings { /// Ignore key inputs, except for modifier keys such as SHIFT and ALT pub ignore_non_modifier_keys: bool, @@ -53,12 +53,3 @@ pub struct IcedBaseviewSettings { /// without using an asynchronous timer stream to send redraw messages to the application. pub always_redraw: bool, } - -impl Default for IcedBaseviewSettings { - fn default() -> Self { - Self { - ignore_non_modifier_keys: false, - always_redraw: false, - } - } -}