Enable tester feature in websocket example

This commit is contained in:
Héctor Ramón Jiménez 2025-06-03 09:54:14 +02:00
parent 1821dc7ff0
commit 9cd1a93e5a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 44 additions and 16 deletions

View file

@ -104,10 +104,7 @@ where
state.title(&self.program, window)
}
fn subscription(
&self,
state: &Self::State,
) -> runtime::futures::Subscription<Self::Message> {
fn subscription(&self, state: &Self::State) -> Subscription<Self::Message> {
state.subscription(&self.program)
}
@ -438,9 +435,19 @@ where
}
fn subscription(&self, program: &P) -> Subscription<Event<P>> {
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 {

View file

@ -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<P: Program + 'static> Tester<P> {
}
}
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<Tick<P>> {
@ -160,6 +165,15 @@ impl<P: Program + 'static> Tester<P> {
}
}
pub fn subscription(&self, program: &P) -> Subscription<Tick<P>> {
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,

View file

@ -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"

View file

@ -7,7 +7,7 @@ publish = false
[dependencies]
iced.workspace = true
iced.features = ["debug", "tokio", "sipper"]
iced.features = ["tester", "tokio", "sipper"]
warp = "0.3"

View file

@ -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<Message> {
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<Message> {