From 80688689aa4b15bc23824df899974a9094a77b07 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Jul 2022 02:46:51 +0200 Subject: Draft widget operations --- native/src/widget/action.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 native/src/widget/action.rs (limited to 'native/src/widget/action.rs') diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs new file mode 100644 index 00000000..23ea4269 --- /dev/null +++ b/native/src/widget/action.rs @@ -0,0 +1,78 @@ +use crate::widget::state; +use crate::widget::{Id, Operation}; + +use iced_futures::MaybeSend; + +pub struct Action(Box>); + +impl Action { + pub fn new(operation: impl Operation + 'static) -> Self { + Self(Box::new(operation)) + } + + pub fn map( + self, + f: impl Fn(T) -> A + 'static + MaybeSend + Sync, + ) -> Action + where + T: 'static, + A: 'static, + { + Action(Box::new(Map { + operation: self.0, + f: Box::new(f), + })) + } + + pub fn into_operation(self) -> Box> { + self.0 + } +} + +struct Map { + operation: Box>, + f: Box B>, +} + +impl Operation for Map +where + A: 'static, + B: 'static, +{ + fn container( + &mut self, + id: Option<&Id>, + operate_on_children: &dyn Fn(&mut dyn Operation), + ) { + struct MapRef<'a, A, B> { + operation: &'a mut dyn Operation, + f: &'a dyn Fn(A) -> B, + } + + impl<'a, A, B> Operation for MapRef<'a, A, B> { + fn container( + &mut self, + id: Option<&Id>, + operate_on_children: &dyn Fn(&mut dyn Operation), + ) { + let Self { operation, f } = self; + + operation.container(id, &|operation| { + operate_on_children(&mut MapRef { operation, f }); + }); + } + } + + let Self { operation, f } = self; + + MapRef { + operation: operation.as_mut(), + f, + } + .container(id, operate_on_children); + } + + fn focusable(&mut self, state: &mut dyn state::Focusable, id: Option<&Id>) { + self.operation.focusable(state, id); + } +} -- cgit From 52f84e51e90db1c324310565f2aff8b7e6987cba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Jul 2022 03:53:47 +0200 Subject: Implement `Widget::operate` for `TextInput` --- native/src/widget/action.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native/src/widget/action.rs') diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs index 23ea4269..69723358 100644 --- a/native/src/widget/action.rs +++ b/native/src/widget/action.rs @@ -42,7 +42,7 @@ where fn container( &mut self, id: Option<&Id>, - operate_on_children: &dyn Fn(&mut dyn Operation), + operate_on_children: &mut dyn FnMut(&mut dyn Operation), ) { struct MapRef<'a, A, B> { operation: &'a mut dyn Operation, @@ -53,11 +53,11 @@ where fn container( &mut self, id: Option<&Id>, - operate_on_children: &dyn Fn(&mut dyn Operation), + operate_on_children: &mut dyn FnMut(&mut dyn Operation), ) { let Self { operation, f } = self; - operation.container(id, &|operation| { + operation.container(id, &mut |operation| { operate_on_children(&mut MapRef { operation, f }); }); } -- cgit From 13dd1ca0a83cc95eea52e2106da9dc1ee1f37958 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Aug 2022 03:55:41 +0200 Subject: Implement `scrollable::snap_to` operation --- native/src/widget/action.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'native/src/widget/action.rs') diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs index 69723358..21032dbb 100644 --- a/native/src/widget/action.rs +++ b/native/src/widget/action.rs @@ -1,5 +1,5 @@ -use crate::widget::state; -use crate::widget::{Id, Operation}; +use crate::widget::operation::{self, Operation}; +use crate::widget::Id; use iced_futures::MaybeSend; @@ -72,7 +72,11 @@ where .container(id, operate_on_children); } - fn focusable(&mut self, state: &mut dyn state::Focusable, id: Option<&Id>) { + fn focusable( + &mut self, + state: &mut dyn operation::Focusable, + id: Option<&Id>, + ) { self.operation.focusable(state, id); } } -- cgit From 66f7d43dc98df96c8b19cfd2aef6dcdd4187316c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Aug 2022 05:15:41 +0200 Subject: Write missing documentation in `iced_native` --- native/src/widget/action.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'native/src/widget/action.rs') diff --git a/native/src/widget/action.rs b/native/src/widget/action.rs index 21032dbb..766e902b 100644 --- a/native/src/widget/action.rs +++ b/native/src/widget/action.rs @@ -3,13 +3,17 @@ use crate::widget::Id; use iced_futures::MaybeSend; +/// An operation to be performed on the widget tree. +#[allow(missing_debug_implementations)] pub struct Action(Box>); impl Action { + /// Creates a new [`Action`] with the given [`Operation`]. pub fn new(operation: impl Operation + 'static) -> Self { Self(Box::new(operation)) } + /// Maps the output of an [`Action`] using the given function. pub fn map( self, f: impl Fn(T) -> A + 'static + MaybeSend + Sync, @@ -24,11 +28,13 @@ impl Action { })) } + /// Consumes the [`Action`] and returns the internal [`Operation`]. pub fn into_operation(self) -> Box> { self.0 } } +#[allow(missing_debug_implementations)] struct Map { operation: Box>, f: Box B>, -- cgit