From e82b10da7785b23dcb245d52becc8a3364d2eb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 1 May 2025 04:19:44 +0200 Subject: [PATCH] Maintain application metadata in `beacon` connection --- beacon/src/client.rs | 37 +++++++++++++++++++++++++++++++++---- beacon/src/lib.rs | 6 ++++++ debug/src/lib.rs | 44 +++++++++++++++++++++++++++++++------------- src/application.rs | 10 +++++++++- src/daemon.rs | 10 +++++++++- winit/src/lib.rs | 2 -- 6 files changed, 88 insertions(+), 21 deletions(-) diff --git a/beacon/src/client.rs b/beacon/src/client.rs index 3850ca63..d381b567 100644 --- a/beacon/src/client.rs +++ b/beacon/src/client.rs @@ -30,6 +30,8 @@ pub enum Message { at: SystemTime, name: String, version: Version, + theme: Option, + can_time_travel: bool, }, EventLogged { at: SystemTime, @@ -77,15 +79,22 @@ impl Client { } } +#[derive(Debug, Clone, Default)] +pub struct Metadata { + pub name: &'static str, + pub theme: Option, + pub can_time_travel: bool, +} + #[must_use] -pub fn connect(name: String) -> Client { +pub fn connect(metadata: Metadata) -> Client { let (sender, receiver) = mpsc::channel(10_000); let is_connected = Arc::new(AtomicBool::new(false)); let handle = { let is_connected = is_connected.clone(); - std::thread::spawn(move || run(name, is_connected, receiver)) + std::thread::spawn(move || run(metadata, is_connected, receiver)) }; Client { @@ -108,7 +117,7 @@ pub enum Command { #[tokio::main] async fn run( - name: String, + mut metadata: Metadata, is_connected: Arc, mut receiver: mpsc::Receiver, ) { @@ -133,8 +142,10 @@ async fn run( &mut writer, Message::Connected { at: SystemTime::now(), - name: name.clone(), + name: metadata.name.to_owned(), version: version.clone(), + can_time_travel: metadata.can_time_travel, + theme: metadata.theme, }, ) .await; @@ -148,6 +159,16 @@ async fn run( loop { match receive(&mut reader, &mut buffer).await { Ok(command) => { + match command { + Command::RewindTo { .. } + | Command::GoLive + if !metadata.can_time_travel => + { + continue; + } + _ => {} + } + let sender = command_sender.lock().await; let _ = sender.send(command).await; } @@ -161,6 +182,14 @@ async fn run( while let Some(action) = receiver.recv().await { match action { Action::Send(message) => { + if let Message::EventLogged { + event: Event::ThemeChanged(palette), + .. + } = message + { + metadata.theme = Some(palette); + } + match send(&mut writer, message).await { Ok(()) => {} Err(error) => { diff --git a/beacon/src/lib.rs b/beacon/src/lib.rs index 50af8b04..103ec567 100644 --- a/beacon/src/lib.rs +++ b/beacon/src/lib.rs @@ -53,6 +53,8 @@ pub enum Event { at: SystemTime, name: String, version: Version, + theme: Option, + can_time_travel: bool, }, Disconnected { at: SystemTime, @@ -161,6 +163,8 @@ pub fn run() -> impl Stream { at, name, version, + theme, + can_time_travel, } => { let _ = output .send(Event::Connected { @@ -170,6 +174,8 @@ pub fn run() -> impl Stream { at, name, version, + theme, + can_time_travel, }) .await; } diff --git a/debug/src/lib.rs b/debug/src/lib.rs index 6090cfbf..8c714d56 100644 --- a/debug/src/lib.rs +++ b/debug/src/lib.rs @@ -7,6 +7,13 @@ use crate::futures::Subscription; pub use internal::Span; +#[derive(Debug, Clone, Copy)] +pub struct Metadata { + pub name: &'static str, + pub theme: Option, + pub can_time_travel: bool, +} + #[derive(Debug, Clone, Copy)] pub enum Primitive { Quad, @@ -30,8 +37,8 @@ pub fn disable() { internal::disable(); } -pub fn init(name: &str) { - internal::init(name); +pub fn init(metadata: Metadata) { + internal::init(metadata); } pub fn quit() -> bool { @@ -113,7 +120,7 @@ mod internal { use crate::core::window; use crate::futures::Subscription; use crate::futures::futures::Stream; - use crate::{Command, Primitive}; + use crate::{Command, Metadata, Primitive}; use iced_beacon as beacon; @@ -123,10 +130,15 @@ mod internal { use std::sync::atomic::{self, AtomicBool, AtomicUsize}; use std::sync::{LazyLock, RwLock}; - pub fn init(name: &str) { - let name = name.split("::").next().unwrap_or(name); + pub fn init(metadata: Metadata) { + let name = metadata.name.split("::").next().unwrap_or(metadata.name); - name.clone_into(&mut NAME.write().expect("Write application name")); + *METADATA.write().expect("Write application metadata") = + client::Metadata { + name, + theme: metadata.theme, + can_time_travel: metadata.can_time_travel, + }; } pub fn quit() -> bool { @@ -144,12 +156,12 @@ mod internal { return; }; - if LAST_PALETTE.read().expect("Read last palette").as_ref() + if METADATA.read().expect("Read last palette").theme.as_ref() != Some(&palette) { log(client::Event::ThemeChanged(palette)); - *LAST_PALETTE.write().expect("Write last palette") = Some(palette); + METADATA.write().expect("Write last palette").theme = Some(palette); } } @@ -292,12 +304,18 @@ mod internal { } static BEACON: LazyLock = LazyLock::new(|| { - client::connect(NAME.read().expect("Read application name").to_owned()) + let metadata = METADATA.read().expect("Read application metadata"); + + client::connect(metadata.clone()) + }); + + static METADATA: RwLock = RwLock::new(client::Metadata { + name: "", + theme: None, + can_time_travel: false, }); - static NAME: RwLock = RwLock::new(String::new()); static LAST_UPDATE: AtomicUsize = AtomicUsize::new(0); - static LAST_PALETTE: RwLock> = RwLock::new(None); static ENABLED: AtomicBool = AtomicBool::new(true); } @@ -306,12 +324,12 @@ mod internal { use crate::core::theme; use crate::core::window; use crate::futures::Subscription; - use crate::{Command, Primitive}; + use crate::{Command, Metadata, Primitive}; pub fn enable() {} pub fn disable() {} - pub fn init(_name: &str) {} + pub fn init(_metadata: Metadata) {} pub fn quit() -> bool { false diff --git a/src/application.rs b/src/application.rs index a1846333..e068d87a 100644 --- a/src/application.rs +++ b/src/application.rs @@ -174,7 +174,15 @@ impl Application

{ Self: 'static, { #[cfg(all(feature = "debug", not(target_arch = "wasm32")))] - let program = iced_devtools::attach(self.raw); + let program = { + iced_debug::init(iced_debug::Metadata { + name: P::name(), + theme: None, + can_time_travel: cfg!(feature = "time-travel"), + }); + + iced_devtools::attach(self.raw) + }; #[cfg(any(not(feature = "debug"), target_arch = "wasm32"))] let program = self.raw; diff --git a/src/daemon.rs b/src/daemon.rs index 80271e73..9336e941 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -118,7 +118,15 @@ impl Daemon

{ Self: 'static, { #[cfg(all(feature = "debug", not(target_arch = "wasm32")))] - let program = iced_devtools::attach(self.raw); + let program = { + iced_debug::init(iced_debug::Metadata { + name: P::name(), + theme: None, + can_time_travel: cfg!(feature = "time-travel"), + }); + + iced_devtools::attach(self.raw) + }; #[cfg(any(not(feature = "debug"), target_arch = "wasm32"))] let program = self.raw; diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 814d4f58..52a987d4 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -76,8 +76,6 @@ where { use winit::event_loop::EventLoop; - debug::init(P::name()); - let boot_span = debug::boot(); let graphics_settings = settings.clone().into();