diff --git a/common/Cargo.toml b/common/Cargo.toml index 492c1c0..b8d4c1a 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "chargrid_common" description = "Common utilities for making text UIs with chargrid" -version = "0.7.1" +version = "0.7.2" authors = ["Stephen Sherratt "] license = "MIT" readme = "README.md" diff --git a/common/src/control_flow/boxed.rs b/common/src/control_flow/boxed.rs index 195b8bf..f29d88d 100644 --- a/common/src/control_flow/boxed.rs +++ b/common/src/control_flow/boxed.rs @@ -449,6 +449,11 @@ impl CF, S> { pub fn on_each_tick(self, f: F) -> Self { self.0.on_each_tick(f).boxed() } + + /// Call a given function on the state on each tick of the component. + pub fn on_each_tick_with_state(self, f: F) -> Self { + self.0.on_each_tick_with_state(f).boxed() + } } impl CF { diff --git a/common/src/control_flow/unboxed.rs b/common/src/control_flow/unboxed.rs index 581bfa5..f101d4c 100644 --- a/common/src/control_flow/unboxed.rs +++ b/common/src/control_flow/unboxed.rs @@ -404,6 +404,16 @@ impl>> CF { f, }) } + + pub fn on_each_tick_with_state( + self, + f: F, + ) -> CF> { + cf(OnEachTickWithState { + component: self.0, + f, + }) + } } impl> CF { @@ -1933,3 +1943,22 @@ impl Component for OnEachTick { self.component.size(state, ctx) } } + +pub struct OnEachTickWithState { + component: C, + f: F, +} +impl Component for OnEachTickWithState { + type Output = C::Output; + type State = C::State; + fn render(&self, state: &Self::State, ctx: Ctx, fb: &mut FrameBuffer) { + self.component.render(state, ctx, fb); + } + fn update(&mut self, state: &mut Self::State, ctx: Ctx, event: Event) -> Self::Output { + (self.f)(state); + self.component.update(state, ctx, event) + } + fn size(&self, state: &Self::State, ctx: Ctx) -> Size { + self.component.size(state, ctx) + } +}