diff options
author | 2022-11-09 23:53:26 +0100 | |
---|---|---|
committer | 2022-11-09 23:53:26 +0100 | |
commit | 335df2028d2c15b81b3d7a4c6e49984152ea1963 (patch) | |
tree | 647209ccc4331e28e833c5c4a6bff1f71ef891bb /native/src/widget/operation.rs | |
parent | 11ec80dee21b78968e7d0339ced893da4f76a1ca (diff) | |
download | iced-335df2028d2c15b81b3d7a4c6e49984152ea1963.tar.gz iced-335df2028d2c15b81b3d7a4c6e49984152ea1963.tar.bz2 iced-335df2028d2c15b81b3d7a4c6e49984152ea1963.zip |
Introduce `scoped` in `widget::operation`
Diffstat (limited to 'native/src/widget/operation.rs')
-rw-r--r-- | native/src/widget/operation.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/native/src/widget/operation.rs b/native/src/widget/operation.rs index ef636aa2..dd6b3c54 100644 --- a/native/src/widget/operation.rs +++ b/native/src/widget/operation.rs @@ -58,3 +58,46 @@ where } } } + +/// Produces an [`Operation`] that applies the given [`Operation`] to the +/// children of a container with the given [`Id`]. +pub fn scoped<T: 'static>( + target: Id, + operation: impl Operation<T> + 'static, +) -> impl Operation<T> { + struct ScopedOperation<Message> { + target: Id, + operation: Box<dyn Operation<Message>>, + } + + impl<Message: 'static> Operation<Message> for ScopedOperation<Message> { + fn container( + &mut self, + id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation<Message>), + ) { + if id == Some(&self.target) { + operate_on_children(self.operation.as_mut()); + } else { + operate_on_children(self); + } + } + + fn finish(&self) -> Outcome<Message> { + match self.operation.finish() { + Outcome::Chain(next) => { + Outcome::Chain(Box::new(ScopedOperation { + target: self.target.clone(), + operation: next, + })) + } + outcome => outcome, + } + } + } + + ScopedOperation { + target: target.into(), + operation: Box::new(operation), + } +} |