From a9cfe45c0a010e39cb2fdd0b59926be5c13f2a4e Mon Sep 17 00:00:00 2001 From: edouardparis Date: Thu, 6 Jun 2024 13:35:55 +0200 Subject: [PATCH] fix ui: wrong usage of widget state In a previous PR #1096, we made usage of the widget state instead of the component state to set the collapsed value when the widget is created. But the widget is reset at each refresh of the interface, instead the component state does not change. This commit makes the distinction between the two by having the widget state as an initial state and the component one as a capture of the user clicks. --- gui/ui/src/component/collapse.rs | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/gui/ui/src/component/collapse.rs b/gui/ui/src/component/collapse.rs index 23b2577d..e59ee7d4 100644 --- a/gui/ui/src/component/collapse.rs +++ b/gui/ui/src/component/collapse.rs @@ -10,7 +10,7 @@ pub struct Collapse<'a, M, H, F, C> { after: F, content: C, phantom: PhantomData<&'a M>, - state: bool, + init_state: bool, } impl<'a, Message, T, H, F, C, Theme, Renderer> Collapse<'a, Message, H, F, C> @@ -29,12 +29,12 @@ where after, content, phantom: PhantomData, - state: false, + init_state: false, } } pub fn collapsed(mut self, state: bool) -> Self { - self.state = state; + self.init_state = state; self } } @@ -55,28 +55,38 @@ where Renderer: 'a + advanced::Renderer, Theme: 'a + iced::widget::button::StyleSheet, { - type State = bool; + type State = Option; type Event = Event; - fn update(&mut self, _state: &mut Self::State, event: Event) -> Option { + fn update(&mut self, state: &mut Self::State, event: Event) -> Option { match event { Event::Internal(e) => Some(e.into()), Event::Collapse(s) => { - self.state = s; + *state = Some(s); None } } } - fn view(&self, _state: &Self::State) -> Element { - if self.state { - column![ + fn view(&self, state: &Self::State) -> Element { + match state { + Some(true) => column![ (self.after)().on_press(Event::Collapse(false)), (self.content)().map(Event::Internal) ] - .into() - } else { - column![(self.before)().on_press(Event::Collapse(true))].into() + .into(), + Some(false) => column![(self.before)().on_press(Event::Collapse(true))].into(), + None => { + if self.init_state { + column![ + (self.after)().on_press(Event::Collapse(false)), + (self.content)().map(Event::Internal) + ] + .into() + } else { + column![(self.before)().on_press(Event::Collapse(true))].into() + } + } } } }