Merge #1105: fix ui: wrong usage of widget state

a9cfe45c0a010e39cb2fdd0b59926be5c13f2a4e fix ui: wrong usage of widget state (edouardparis)

Pull request description:

  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.

ACKs for top commit:
  jp1ac4:
    Tested ACK a9cfe45c0a.

Tree-SHA512: abe108775441680139d72b7067dac0679f20f176762fc4d9471fc31061de594840099c7b4f4fc7a3bf75d54026af8cb7256bee9d3c15ef84eb2da1f20b88cbda
This commit is contained in:
edouardparis 2024-06-06 14:49:33 +02:00
commit 8985bb34a7
No known key found for this signature in database
GPG Key ID: E65F7A089C20DC8F

View File

@ -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<bool>;
type Event = Event<T>;
fn update(&mut self, _state: &mut Self::State, event: Event<T>) -> Option<Message> {
fn update(&mut self, state: &mut Self::State, event: Event<T>) -> Option<Message> {
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<Self::Event, Theme, Renderer> {
if self.state {
column![
fn view(&self, state: &Self::State) -> Element<Self::Event, Theme, Renderer> {
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()
}
}
}
}
}