From 8af69be47e88896b3c5f70174db609eee0c67971 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Mar 2023 06:23:40 +0100 Subject: Converge `Command` types from `iced_futures` and `iced_native` --- core/src/widget/operation.rs | 116 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/widget/operation.rs b/core/src/widget/operation.rs index 53688a21..ad188c36 100644 --- a/core/src/widget/operation.rs +++ b/core/src/widget/operation.rs @@ -11,6 +11,7 @@ use crate::widget::Id; use std::any::Any; use std::fmt; +use std::rc::Rc; /// A piece of logic that can traverse the widget tree of an application in /// order to query or update some widget state. @@ -68,9 +69,122 @@ where } } +/// Maps the output of an [`Operation`] using the given function. +pub fn map( + operation: Box>, + f: impl Fn(A) -> B + 'static, +) -> impl Operation +where + A: 'static, + B: 'static, +{ + #[allow(missing_debug_implementations)] + struct Map { + operation: Box>, + f: Rc B>, + } + + impl Operation for Map + where + A: 'static, + B: 'static, + { + fn container( + &mut self, + id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation), + ) { + struct MapRef<'a, A> { + operation: &'a mut dyn Operation, + } + + impl<'a, A, B> Operation for MapRef<'a, A> { + fn container( + &mut self, + id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation), + ) { + let Self { operation, .. } = self; + + operation.container(id, &mut |operation| { + operate_on_children(&mut MapRef { operation }); + }); + } + + fn scrollable( + &mut self, + state: &mut dyn Scrollable, + id: Option<&Id>, + ) { + self.operation.scrollable(state, id); + } + + fn focusable( + &mut self, + state: &mut dyn Focusable, + id: Option<&Id>, + ) { + self.operation.focusable(state, id); + } + + fn text_input( + &mut self, + state: &mut dyn TextInput, + id: Option<&Id>, + ) { + self.operation.text_input(state, id); + } + + fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { + self.operation.custom(state, id); + } + } + + let Self { operation, .. } = self; + + MapRef { + operation: operation.as_mut(), + } + .container(id, operate_on_children); + } + + fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { + self.operation.focusable(state, id); + } + + fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) { + self.operation.scrollable(state, id); + } + + fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) { + self.operation.text_input(state, id); + } + + fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) { + self.operation.custom(state, id); + } + + fn finish(&self) -> Outcome { + match self.operation.finish() { + Outcome::None => Outcome::None, + Outcome::Some(output) => Outcome::Some((self.f)(output)), + Outcome::Chain(next) => Outcome::Chain(Box::new(Map { + operation: next, + f: self.f.clone(), + })), + } + } + } + + Map { + operation, + f: Rc::new(f), + } +} + /// Produces an [`Operation`] that applies the given [`Operation`] to the /// children of a container with the given [`Id`]. -pub fn scoped( +pub fn scope( target: Id, operation: impl Operation + 'static, ) -> impl Operation { -- cgit