Skip to content

Commit

Permalink
Added custom widget version of table
Browse files Browse the repository at this point in the history
Currently does not have mouse interaction, in the demo the normal one is right and custom is left
  • Loading branch information
Adam-Cosner committed Jan 2, 2025
1 parent 5c96696 commit 825792e
Show file tree
Hide file tree
Showing 4 changed files with 759 additions and 85 deletions.
53 changes: 32 additions & 21 deletions examples/table-view/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cosmic::widget::nav_bar;
use cosmic::widget::table;
use cosmic::{executor, iced, Element};

#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Category {
#[default]
Name,
Expand All @@ -29,11 +29,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,
}
}
}
Expand Down Expand Up @@ -63,11 +63,11 @@ impl table::ItemInterface<Category> for Item {
}
}

fn get_text(&self, category: Category) -> std::borrow::Cow<'static, str> {
fn get_text(&self, category: Category) -> Option<String> {
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)),
}
}

Expand Down Expand Up @@ -192,19 +192,30 @@ impl cosmic::Application for App {

/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
cosmic::widget::responsive(|size| {
if size.width < 600.0 {
// cosmic::widget::responsive(|size| {
// if size.width < 600.0 {
// table::SingleSelectTableView::new(&self.table_model)
// .on_item_select(Message::ItemSelect)
// .on_category_select(Message::CategorySelect)
// .element_compact()
// } else {
// table::SingleSelectTableView::new(&self.table_model)
// .on_item_select(Message::ItemSelect)
// .on_category_select(Message::CategorySelect)
// .element_standard()
// }
// })
// .into()
cosmic::widget::row()
.push(Element::new(
table::SingleSelectTableView::new(&self.table_model)
.on_item_select(Message::ItemSelect)
.on_category_select(Message::CategorySelect)
.element_compact()
} else {
.on_item_left(Message::ItemSelect),
))
.push(
table::SingleSelectTableView::new(&self.table_model)
.on_item_select(Message::ItemSelect)
.on_category_select(Message::CategorySelect)
.element_standard()
}
})
.into()
.on_item_left(Message::ItemSelect)
.element_standard(),
)
.into()
}
}
4 changes: 2 additions & 2 deletions src/widget/table/model/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::widget::Icon;
/// Ideally, this is implemented on an enum.
pub trait ItemCategory: Default + std::fmt::Display + Clone + Copy + PartialEq + Eq {
/// Function that gets the width of the data
fn width(&self) -> iced::Length;
fn width(&self) -> u16;
}

pub trait ItemInterface<Category: ItemCategory>: Default {
fn get_icon(&self, category: Category) -> Option<Icon>;
fn get_text(&self, category: Category) -> Cow<'static, str>;
fn get_text(&self, category: Category) -> Option<String>;

fn compare(&self, other: &Self, category: Category) -> std::cmp::Ordering;
}
Loading

0 comments on commit 825792e

Please sign in to comment.