diff options
author | 2023-01-02 19:46:58 +0100 | |
---|---|---|
committer | 2023-01-02 19:46:58 +0100 | |
commit | fd2ddfa1aa9d23c585236f005c9b892167b18726 (patch) | |
tree | 90696b2467dc0771ea81c1ec5a97df52f8caacc3 /lazy/src | |
parent | 7594e483ef0453306560a4ff7bffe2c1505686e6 (diff) | |
parent | cec28e2ef52011859481d1fcaae74bfe9b71d26c (diff) | |
download | iced-fd2ddfa1aa9d23c585236f005c9b892167b18726.tar.gz iced-fd2ddfa1aa9d23c585236f005c9b892167b18726.tar.bz2 iced-fd2ddfa1aa9d23c585236f005c9b892167b18726.zip |
Merge pull request #1628 from nicksenger/feat/component-operations
Add `operate` method to `Component` trait
Diffstat (limited to '')
-rw-r--r-- | lazy/src/component.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lazy/src/component.rs b/lazy/src/component.rs index ad15d69d..d8f21f8a 100644 --- a/lazy/src/component.rs +++ b/lazy/src/component.rs @@ -46,6 +46,16 @@ 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) + /// + /// By default, it does nothing. + fn operate( + &self, + _state: &mut Self::State, + _operation: &mut dyn widget::Operation<Message>, + ) { + } } /// Turns an implementor of [`Component`] into an [`Element`] that can be @@ -106,6 +116,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 +267,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>, } |