Maintain application metadata in beacon connection

This commit is contained in:
Héctor Ramón Jiménez 2025-05-01 04:19:44 +02:00
parent dc69fdee46
commit e82b10da77
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
6 changed files with 88 additions and 21 deletions

View file

@ -30,6 +30,8 @@ pub enum Message {
at: SystemTime,
name: String,
version: Version,
theme: Option<theme::Palette>,
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<theme::Palette>,
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<AtomicBool>,
mut receiver: mpsc::Receiver<Action>,
) {
@ -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) => {

View file

@ -53,6 +53,8 @@ pub enum Event {
at: SystemTime,
name: String,
version: Version,
theme: Option<theme::Palette>,
can_time_travel: bool,
},
Disconnected {
at: SystemTime,
@ -161,6 +163,8 @@ pub fn run() -> impl Stream<Item = Event> {
at,
name,
version,
theme,
can_time_travel,
} => {
let _ = output
.send(Event::Connected {
@ -170,6 +174,8 @@ pub fn run() -> impl Stream<Item = Event> {
at,
name,
version,
theme,
can_time_travel,
})
.await;
}

View file

@ -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<theme::Palette>,
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<Client> = 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<client::Metadata> = RwLock::new(client::Metadata {
name: "",
theme: None,
can_time_travel: false,
});
static NAME: RwLock<String> = RwLock::new(String::new());
static LAST_UPDATE: AtomicUsize = AtomicUsize::new(0);
static LAST_PALETTE: RwLock<Option<theme::Palette>> = 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

View file

@ -174,7 +174,15 @@ impl<P: Program> Application<P> {
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;

View file

@ -118,7 +118,15 @@ impl<P: Program> Daemon<P> {
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;

View file

@ -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();