diff --git a/src/compare.rs b/src/compare.rs index fa83bd5..0635514 100644 --- a/src/compare.rs +++ b/src/compare.rs @@ -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 { } #[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 iced::widget::Component for Compare @@ -35,14 +54,20 @@ where type State = CompareState; type Event = CompareMsg; - fn update(&mut self, _state: &mut Self::State, event: Self::Event) -> Option { + fn update(&mut self, state: &mut Self::State, event: Self::Event) -> Option { 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, right: Option, ) -> Vec<[Option>; 4]> where I: IntoIterator, { + if collapse { + return Vec::new(); + } + let to_btree = |i: I| i.into_iter().collect::>(); 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()