From 6958a4cc364daadebfeb93235e8c3b5df81d22d4 Mon Sep 17 00:00:00 2001 From: Adam Cosner Date: Wed, 1 Jan 2025 18:04:12 -0800 Subject: [PATCH] Added custom widget version of table Currently does not have mouse interaction, in the demo the normal one is right and custom is left --- examples/table-view/src/main.rs | 16 ++++++++-------- src/widget/table/model/category.rs | 4 ++-- src/widget/table/widget/state.rs | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/examples/table-view/src/main.rs b/examples/table-view/src/main.rs index a80714a9..64bbbe75 100644 --- a/examples/table-view/src/main.rs +++ b/examples/table-view/src/main.rs @@ -32,11 +32,11 @@ impl std::fmt::Display for Category { } impl table::ItemCategory for Category { - fn width(&self) -> iced::Length { + fn width(&self) -> u16 { match self { - Self::Name => iced::Length::Fill, - Self::Date => iced::Length::Fixed(200.0), - Self::Size => iced::Length::Fixed(150.0), + Self::Name => 300, + Self::Date => 200, + Self::Size => 150, } } } @@ -66,11 +66,11 @@ impl table::ItemInterface for Item { } } - fn get_text(&self, category: Category) -> std::borrow::Cow<'static, str> { + fn get_text(&self, category: Category) -> Option { match category { - Category::Name => self.name.clone().into(), - Category::Date => self.date.format("%Y/%m/%d").to_string().into(), - Category::Size => format!("{} items", self.size).into(), + Category::Name => Some(self.name.clone()), + Category::Date => Some(self.date.format("%Y/%m/%d").to_string()), + Category::Size => Some(format!("{} items", self.size)), } } diff --git a/src/widget/table/model/category.rs b/src/widget/table/model/category.rs index 5c79d404..cc06c76a 100644 --- a/src/widget/table/model/category.rs +++ b/src/widget/table/model/category.rs @@ -8,12 +8,12 @@ pub trait ItemCategory: Default + std::fmt::Display + Clone + Copy + PartialEq + Eq + std::hash::Hash { /// Function that gets the width of the data - fn width(&self) -> iced::Length; + fn width(&self) -> u16; } pub trait ItemInterface: Default { fn get_icon(&self, category: Category) -> Option; - fn get_text(&self, category: Category) -> Cow<'static, str>; + fn get_text(&self, category: Category) -> Option; fn compare(&self, other: &Self, category: Category) -> std::cmp::Ordering; } diff --git a/src/widget/table/widget/state.rs b/src/widget/table/widget/state.rs index 4d529db4..82fb88db 100644 --- a/src/widget/table/widget/state.rs +++ b/src/widget/table/widget/state.rs @@ -1,8 +1,21 @@ +use std::collections::HashMap; + +use iced::Size; use slotmap::SecondaryMap; -use crate::widget::table::Entity; +use crate::widget::table::{Entity, ItemCategory}; -pub struct State { +pub struct State +where + Category: ItemCategory + 'static, +{ pub(super) num_items: usize, pub(super) selected: SecondaryMap, + pub(super) paragraphs: SecondaryMap>, + pub(super) text_hashes: SecondaryMap, + pub(super) item_layout: Vec, + + pub(super) sort_hash: HashMap, + pub(super) header_paragraphs: HashMap, + pub(super) category_layout: Vec, }