diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index 151b5c93..3a663384 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -104,10 +104,7 @@ where state.title(&self.program, window) } - fn subscription( - &self, - state: &Self::State, - ) -> runtime::futures::Subscription { + fn subscription(&self, state: &Self::State) -> Subscription { state.subscription(&self.program) } @@ -438,9 +435,19 @@ where } fn subscription(&self, program: &P) -> Subscription> { - let subscription = - program.subscription(&self.state).map(Event::Program); - debug::subscriptions_tracked(subscription.units()); + let subscription = match &self.mode { + Mode::Open { tester } if !tester.is_idle() => { + tester.subscription(program).map(Event::Tester) + } + _ => { + let subscription = + program.subscription(&self.state).map(Event::Program); + + debug::subscriptions_tracked(subscription.units()); + + subscription + } + }; let hotkeys = futures::keyboard::on_key_press(|key, _modifiers| match key { @@ -473,7 +480,11 @@ where } fn scale_factor(&self, program: &P, window: window::Id) -> f64 { - program.scale_factor(self.state(), window) + if let Mode::Open { .. } = &self.mode { + 1.0 + } else { + program.scale_factor(self.state(), window) + } } fn state(&self) -> &P::State { diff --git a/devtools/src/tester.rs b/devtools/src/tester.rs index 73a7793a..e5aa441b 100644 --- a/devtools/src/tester.rs +++ b/devtools/src/tester.rs @@ -9,6 +9,7 @@ use crate::core::alignment::Horizontal::Right; use crate::core::border; use crate::core::window; use crate::core::{Element, Event, Size, Theme}; +use crate::futures::Subscription; use crate::futures::futures::channel::mpsc; use crate::icon; use crate::program; @@ -62,8 +63,12 @@ impl Tester

{ } } + pub fn is_idle(&self) -> bool { + matches!(self.state, State::Idle) + } + pub fn is_busy(&self) -> bool { - matches!(self.state, State::Idle | State::Playing { .. }) + matches!(self.state, State::Recording { .. } | State::Playing { .. }) } pub fn update(&mut self, program: &P, message: Message) -> Task> { @@ -160,6 +165,15 @@ impl Tester

{ } } + pub fn subscription(&self, program: &P) -> Subscription> { + match &self.state { + State::Idle | State::Playing { .. } => Subscription::none(), + State::Recording { state } => { + program.subscription(state).map(Tick::Program) + } + } + } + pub fn view<'a, T: 'static>( &'a self, program: &P, diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index de23558e..5e16a2ac 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["tokio", "time-travel", "tester"] +iced.features = ["tokio", "debug", "time-travel"] serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml index c47e3c93..b2053fa6 100644 --- a/examples/websocket/Cargo.toml +++ b/examples/websocket/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["debug", "tokio", "sipper"] +iced.features = ["tester", "tokio", "sipper"] warp = "0.3" diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index c52de37e..81083a22 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -1,9 +1,11 @@ mod echo; +use iced::futures::stream; use iced::widget::{ self, button, center, column, row, scrollable, text, text_input, }; use iced::{Center, Element, Fill, Subscription, Task, color}; + use std::sync::LazyLock; pub fn main() -> iced::Result { @@ -34,10 +36,7 @@ impl WebSocket { new_message: String::new(), state: State::Disconnected, }, - Task::batch([ - Task::perform(echo::server::run(), |_| Message::Server), - widget::focus_next(), - ]), + widget::focus_next(), ) } @@ -87,7 +86,11 @@ impl WebSocket { } fn subscription(&self) -> Subscription { - Subscription::run(echo::connect).map(Message::Echo) + Subscription::batch([ + Subscription::run(|| stream::once(echo::server::run())) + .map(|_| Message::Server), + Subscription::run(echo::connect).map(Message::Echo), + ]) } fn view(&self) -> Element {