diff options
author | 2022-12-30 13:10:53 -0800 | |
---|---|---|
committer | 2022-12-30 13:11:09 -0800 | |
commit | 8d5fba8a1f09e9b5f6796618cfe9da4154a0dcbd (patch) | |
tree | 4cb20ae86303c81b84b61a6e08f9467d3ae4319b /lazy | |
parent | a6d0d5773f0561a841a84b538523cbd97e91eccd (diff) | |
download | iced-8d5fba8a1f09e9b5f6796618cfe9da4154a0dcbd.tar.gz iced-8d5fba8a1f09e9b5f6796618cfe9da4154a0dcbd.tar.bz2 iced-8d5fba8a1f09e9b5f6796618cfe9da4154a0dcbd.zip |
Add `operate` method to `Component` trait
Diffstat (limited to 'lazy')
-rw-r--r-- | lazy/src/component.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lazy/src/component.rs b/lazy/src/component.rs index ad15d69d..a39cd020 100644 --- a/lazy/src/component.rs +++ b/lazy/src/component.rs @@ -46,6 +46,14 @@ pub trait Component<Message, Renderer> { /// Produces the widgets of the [`Component`], which may trigger an [`Event`](Component::Event) /// on user interaction. fn view(&self, state: &Self::State) -> Element<'_, Self::Event, Renderer>; + + /// Update the [`Component`] state based on the provided [`Operation`](widget::Operation) + fn operate( + &self, + #[allow(unused)] state: &mut Self::State, + #[allow(unused)] operation: &mut dyn widget::Operation<Message>, + ) { + } } /// Turns an implementor of [`Component`] into an [`Element`] that can be @@ -106,6 +114,26 @@ where ); } + fn rebuild_element_with_operation( + &self, + state: &mut S, + operation: &mut dyn widget::Operation<Message>, + ) { + let heads = self.state.borrow_mut().take().unwrap().into_heads(); + + heads.component.operate(state, operation); + + *self.state.borrow_mut() = Some( + StateBuilder { + component: heads.component, + message: PhantomData, + state: PhantomData, + element_builder: |component| Some(component.view(state)), + } + .build(), + ); + } + fn with_element<T>( &self, f: impl FnOnce(&Element<'_, Event, Renderer>) -> T, @@ -237,6 +265,11 @@ where renderer: &Renderer, operation: &mut dyn widget::Operation<Message>, ) { + self.rebuild_element_with_operation( + tree.state.downcast_mut(), + operation, + ); + struct MapOperation<'a, B> { operation: &'a mut dyn widget::Operation<B>, } |