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.
This commit is contained in:
edouardparis 2024-06-06 13:35:55 +02:00
parent c4c32d2634
commit a9cfe45c0a

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()
}
}
}
}
}