Allow to easily add more compare sections

This commit is contained in:
traxys 2023-12-08 21:13:50 +01:00
parent fa56c09513
commit 49d2745257
3 changed files with 34 additions and 12 deletions

21
Cargo.lock generated
View file

@ -482,6 +482,26 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "enum-map"
version = "2.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6866f3bfdf8207509a033af1a75a7b08abda06bbaaeae6669323fd5a097df2e9"
dependencies = [
"enum-map-derive",
]
[[package]]
name = "enum-map-derive"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -719,6 +739,7 @@ dependencies = [
"anyhow", "anyhow",
"directories", "directories",
"either", "either",
"enum-map",
"iced", "iced",
"iced_aw", "iced_aw",
"itertools", "itertools",

View file

@ -8,6 +8,7 @@ edition = "2021"
anyhow = "1.0.75" anyhow = "1.0.75"
directories = "5.0.1" directories = "5.0.1"
either = "1.9.0" either = "1.9.0"
enum-map = "2.7.3"
iced = { version = "0.10.0", features = ["lazy"] } iced = { version = "0.10.0", features = ["lazy"] }
iced_aw = { version = "0.7.0", default-features = false, features = [ iced_aw = { version = "0.7.0", default-features = false, features = [
"modal", "modal",

View file

@ -1,5 +1,6 @@
use std::{cmp::Ordering, collections::BTreeMap, iter}; use std::{cmp::Ordering, collections::BTreeMap, iter};
use enum_map::{Enum, EnumMap};
use iced::{ use iced::{
widget::{ widget::{
button, column, component, container, horizontal_rule, horizontal_space, row, scrollable, button, column, component, container, horizontal_rule, horizontal_space, row, scrollable,
@ -21,8 +22,7 @@ pub(crate) struct Compare<F> {
#[derive(Default)] #[derive(Default)]
pub(crate) struct CompareState { pub(crate) struct CompareState {
recurring_collapse: bool, collapsed: EnumMap<Section, bool>,
variable_collapse: bool,
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -32,10 +32,11 @@ pub(crate) enum CompareMsg {
SetCollapse(Section, bool), SetCollapse(Section, bool),
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy, Enum)]
pub(crate) enum Section { pub(crate) enum Section {
Recurring, Recurring,
Variable, Variable,
Total,
} }
impl Section { impl Section {
@ -43,6 +44,7 @@ impl Section {
match self { match self {
Section::Recurring => "Recurring", Section::Recurring => "Recurring",
Section::Variable => "Variable", Section::Variable => "Variable",
Section::Total => "Total",
} }
} }
} }
@ -58,10 +60,7 @@ where
match event { match event {
CompareMsg::LoadLeft(d) => return Some((self.on_load)(CompareSide::Left, d)), CompareMsg::LoadLeft(d) => return Some((self.on_load)(CompareSide::Left, d)),
CompareMsg::LoadRight(d) => return Some((self.on_load)(CompareSide::Right, d)), CompareMsg::LoadRight(d) => return Some((self.on_load)(CompareSide::Right, d)),
CompareMsg::SetCollapse(s, v) => match s { CompareMsg::SetCollapse(s, v) => state.collapsed[s] = v,
Section::Recurring => state.recurring_collapse = v,
Section::Variable => state.variable_collapse = v,
},
} }
None None
@ -137,7 +136,8 @@ where
), ),
]; ];
let mk_section = |section: Section, status: bool| { let mk_section = |section: Section| {
let status = state.collapsed[section];
[ [
Some( Some(
row![ row![
@ -288,15 +288,15 @@ where
properties, properties,
itertools::chain![ itertools::chain![
iter::once(headings), iter::once(headings),
iter::once(mk_section(Section::Recurring, state.recurring_collapse)), iter::once(mk_section(Section::Recurring)),
item_compare( item_compare(
state.recurring_collapse, state.collapsed[Section::Recurring],
left.map(|r| r.recurring()), left.map(|r| r.recurring()),
right.map(|r| r.recurring()) right.map(|r| r.recurring())
), ),
iter::once(mk_section(Section::Variable, state.variable_collapse)), iter::once(mk_section(Section::Variable)),
item_compare( item_compare(
state.variable_collapse, state.collapsed[Section::Variable],
left.map(|r| r.variable()), left.map(|r| r.variable()),
right.map(|r| r.variable()) right.map(|r| r.variable())
), ),