qframe/widgets/table/
model.rs1use crate::text;
4use crate::widget::Align;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum ColumnWidth {
9 Fixed(u16),
11 Fit,
13 Fill(u16),
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum SortDirection {
20 Ascending,
22 Descending,
24}
25
26impl SortDirection {
27 #[must_use]
29 pub fn reversed(self) -> Self {
30 match self {
31 Self::Ascending => Self::Descending,
32 Self::Descending => Self::Ascending,
33 }
34 }
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct Column {
40 pub(super) title: String,
41 pub(super) width: ColumnWidth,
42 pub(super) min: Option<u16>,
43 pub(super) align: Align,
44 pub(super) sortable: bool,
45}
46
47impl Column {
48 #[must_use]
50 pub fn new(title: impl Into<String>) -> Self {
51 Self { title: title.into(), width: ColumnWidth::Fill(1), min: None, align: Align::Start, sortable: false }
52 }
53
54 #[must_use]
56 pub fn width(mut self, width: ColumnWidth) -> Self {
57 self.width = width;
58 self
59 }
60
61 #[must_use]
65 pub fn min(mut self, cells: u16) -> Self {
66 self.min = Some(cells);
67 self
68 }
69
70 #[must_use]
72 pub fn align(mut self, align: Align) -> Self {
73 self.align = align;
74 self
75 }
76
77 #[must_use]
79 pub fn sortable(mut self, sortable: bool) -> Self {
80 self.sortable = sortable;
81 self
82 }
83
84 pub(super) fn title_width(&self) -> u16 {
85 text::width(&self.title).saturating_add(if self.sortable { 2 } else { 0 })
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Default)]
91pub struct TableCell {
92 pub(super) text: String,
93 pub(super) icon: Option<String>,
94 pub(super) icon_color: Option<String>,
95 pub(super) color: Option<String>,
96}
97
98impl TableCell {
99 #[must_use]
101 pub fn new(text: impl Into<String>) -> Self {
102 Self { text: text.into(), ..Self::default() }
103 }
104
105 #[must_use]
107 pub fn icon(mut self, key: impl Into<String>, color: Option<&str>) -> Self {
108 self.icon = Some(key.into());
109 self.icon_color = color.map(str::to_owned);
110 self
111 }
112
113 #[must_use]
115 pub fn color(mut self, token: impl Into<String>) -> Self {
116 self.color = Some(token.into());
117 self
118 }
119
120 pub(super) fn width(&self) -> u16 {
121 text::width(&self.text).saturating_add(self.icon.as_ref().map_or(0, |_| 2))
122 }
123}
124
125impl From<&str> for TableCell {
126 fn from(text: &str) -> Self {
127 Self::new(text)
128 }
129}
130
131impl From<String> for TableCell {
132 fn from(text: String) -> Self {
133 Self::new(text)
134 }
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Default)]
139pub struct TableRow {
140 pub(super) cells: Vec<TableCell>,
141 pub(super) faint: bool,
142}
143
144impl TableRow {
145 #[must_use]
147 pub fn new(cells: impl IntoIterator<Item = impl Into<TableCell>>) -> Self {
148 Self { cells: cells.into_iter().map(Into::into).collect(), faint: false }
149 }
150
151 #[must_use]
153 pub fn faint(mut self, faint: bool) -> Self {
154 self.faint = faint;
155 self
156 }
157}