diff options
| author | Joe Carstairs <me@joeac.net> | 2026-02-08 20:26:30 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-02-08 20:26:30 +0000 |
| commit | 97b413aa5962bded37dedb380034fa7b97bdc06c (patch) | |
| tree | 2cb96478c3808e027528a175e673f77dcc328f9d /schist_desktop_gui/src/gui/components/table/view_table.rs | |
| parent | 170c1a58ebd8b6ff1dceb5f46146f76b601c91ad (diff) | |
task-085: display transaction data in tables
Diffstat (limited to 'schist_desktop_gui/src/gui/components/table/view_table.rs')
| -rw-r--r-- | schist_desktop_gui/src/gui/components/table/view_table.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/schist_desktop_gui/src/gui/components/table/view_table.rs b/schist_desktop_gui/src/gui/components/table/view_table.rs new file mode 100644 index 0000000..1d5f10b --- /dev/null +++ b/schist_desktop_gui/src/gui/components/table/view_table.rs @@ -0,0 +1,48 @@ +use std::{fmt::Debug, hash::Hash}; + +use iced::widget::{column, container, row}; + +use crate::{gui::components::Text, style::SPACING_MD, traits::Viewable}; + +use super::{Message, Table, Value}; + +impl<'a, Ix> Viewable<'a, Message> for Table<Ix> +where + Ix: Clone + Debug + Eq + Hash, +{ + fn view(&'a self) -> iced::Element<'a, Message> { + let headers = row(self.cols.iter().map(|col| { + container(Text::default(&col.name)) + .width(iced::Length::FillPortion(1)) + .into() + })) + .padding(SPACING_MD); + let rows = column(self.rows.iter().map(|row_data| { + row(self.cols.iter().map(|col| { + container( + row_data + .get(&col.index) + .map(|datum| match datum { + Value::String(text) => Text::default(text).weak(), + Value::Currency(amount) => format_currency(*amount), + }) + .unwrap_or_else(|| Text::default("")), + ) + .width(iced::Length::FillPortion(1)) + .into() + })) + .spacing(SPACING_MD) + .into() + })); + column![headers, rows].into() + } +} + +fn format_currency(amount: i32) -> Text { + let text = if amount >= 0 { + format!(" {} · {} ", amount / 100, amount % 100) + } else { + format!("({} · {})", -amount / 100, -amount % 100) + }; + Text::default(&text).align_right().width(iced::Length::Fill) +} |
