Introduce scoped in widget::operation
This commit is contained in:
parent
11ec80dee2
commit
335df2028d
1 changed files with 43 additions and 0 deletions
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue