Allow to collapse sections
This commit is contained in:
parent
984e3a5be5
commit
fa56c09513
1 changed files with 59 additions and 13 deletions
|
|
@ -2,8 +2,8 @@ use std::{cmp::Ordering, collections::BTreeMap, iter};
|
|||
|
||||
use iced::{
|
||||
widget::{
|
||||
button, column, component, container, horizontal_rule, row, scrollable, text,
|
||||
vertical_rule, Column, Row,
|
||||
button, column, component, container, horizontal_rule, horizontal_space, row, scrollable,
|
||||
text, toggler, vertical_rule, Column, Row,
|
||||
},
|
||||
Alignment, Length, Renderer,
|
||||
};
|
||||
|
|
@ -20,12 +20,31 @@ pub(crate) struct Compare<F> {
|
|||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct CompareState {}
|
||||
pub(crate) struct CompareState {
|
||||
recurring_collapse: bool,
|
||||
variable_collapse: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum CompareMsg {
|
||||
LoadLeft(CompareLoad),
|
||||
LoadRight(CompareLoad),
|
||||
SetCollapse(Section, bool),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum Section {
|
||||
Recurring,
|
||||
Variable,
|
||||
}
|
||||
|
||||
impl Section {
|
||||
fn name(&self) -> &'static str {
|
||||
match self {
|
||||
Section::Recurring => "Recurring",
|
||||
Section::Variable => "Variable",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<M, F> iced::widget::Component<M, Renderer> for Compare<F>
|
||||
|
|
@ -35,14 +54,20 @@ where
|
|||
type State = CompareState;
|
||||
type Event = CompareMsg;
|
||||
|
||||
fn update(&mut self, _state: &mut Self::State, event: Self::Event) -> Option<M> {
|
||||
fn update(&mut self, state: &mut Self::State, event: Self::Event) -> Option<M> {
|
||||
match event {
|
||||
CompareMsg::LoadLeft(d) => Some((self.on_load)(CompareSide::Left, d)),
|
||||
CompareMsg::LoadRight(d) => Some((self.on_load)(CompareSide::Right, d)),
|
||||
CompareMsg::LoadLeft(d) => return Some((self.on_load)(CompareSide::Left, d)),
|
||||
CompareMsg::LoadRight(d) => return Some((self.on_load)(CompareSide::Right, d)),
|
||||
CompareMsg::SetCollapse(s, v) => match s {
|
||||
Section::Recurring => state.recurring_collapse = v,
|
||||
Section::Variable => state.variable_collapse = v,
|
||||
},
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn view(&self, _state: &Self::State) -> iced_aw::Element<'_, Self::Event, Renderer> {
|
||||
fn view(&self, state: &Self::State) -> iced_aw::Element<'_, Self::Event, Renderer> {
|
||||
let heading_txt = |side: &Option<(CompareLoad, _)>| {
|
||||
match side {
|
||||
Some((c, _)) => text(c.to_string()),
|
||||
|
|
@ -112,9 +137,17 @@ where
|
|||
),
|
||||
];
|
||||
|
||||
let mk_section = |name| {
|
||||
let mk_section = |section: Section, status: bool| {
|
||||
[
|
||||
Some(text(name).size(TEXT_EMPH2).into()),
|
||||
Some(
|
||||
row![
|
||||
text(section.name()).size(TEXT_EMPH2),
|
||||
horizontal_space(Length::Fill),
|
||||
toggler(None, !status, move |b| CompareMsg::SetCollapse(section, !b)),
|
||||
]
|
||||
.align_items(Alignment::Center)
|
||||
.into(),
|
||||
),
|
||||
Some(text("").size(TEXT_EMPH2).into()),
|
||||
Some(text("").size(TEXT_EMPH2).into()),
|
||||
Some(text("").size(TEXT_EMPH2).into()),
|
||||
|
|
@ -125,12 +158,17 @@ where
|
|||
let right = self.right.as_ref().map(|(_, r)| r);
|
||||
|
||||
fn item_compare<'a, I, M>(
|
||||
collapse: bool,
|
||||
left: Option<I>,
|
||||
right: Option<I>,
|
||||
) -> Vec<[Option<iced::Element<'a, M>>; 4]>
|
||||
where
|
||||
I: IntoIterator<Item = (&'a str, f64)>,
|
||||
{
|
||||
if collapse {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let to_btree = |i: I| i.into_iter().collect::<BTreeMap<_, _>>();
|
||||
|
||||
let float_text = |f: f64| Some(text(format!("{f:.2}")).into());
|
||||
|
|
@ -250,10 +288,18 @@ where
|
|||
properties,
|
||||
itertools::chain![
|
||||
iter::once(headings),
|
||||
iter::once(mk_section("Recurring")),
|
||||
item_compare(left.map(|r| r.recurring()), right.map(|r| r.recurring())),
|
||||
iter::once(mk_section("Variable")),
|
||||
item_compare(left.map(|r| r.variable()), right.map(|r| r.variable())),
|
||||
iter::once(mk_section(Section::Recurring, state.recurring_collapse)),
|
||||
item_compare(
|
||||
state.recurring_collapse,
|
||||
left.map(|r| r.recurring()),
|
||||
right.map(|r| r.recurring())
|
||||
),
|
||||
iter::once(mk_section(Section::Variable, state.variable_collapse)),
|
||||
item_compare(
|
||||
state.variable_collapse,
|
||||
left.map(|r| r.variable()),
|
||||
right.map(|r| r.variable())
|
||||
),
|
||||
],
|
||||
))
|
||||
.into()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue