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"
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]]
name = "equivalent"
version = "1.0.1"
@ -719,6 +739,7 @@ dependencies = [
"anyhow",
"directories",
"either",
"enum-map",
"iced",
"iced_aw",
"itertools",

View file

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

View file

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