1use crate::*;
3
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum SortDirection {
7 #[default]
8 None,
9 Ascending,
10 Descending,
11}
12
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15pub struct TableSortState {
16 pub column: Option<usize>,
17 pub direction: SortDirection,
18}
19
20impl TableSortState {
21 pub fn toggle(&mut self, col: usize) {
23 if self.column != Some(col) || self.direction == SortDirection::None {
24 self.column = Some(col);
25 self.direction = SortDirection::Ascending;
26 } else if self.direction == SortDirection::Ascending {
27 self.direction = SortDirection::Descending;
28 } else {
29 self.column = None;
30 self.direction = SortDirection::None;
31 }
32 }
33}
34
35#[derive(Clone, Copy, Debug)]
37pub struct TableColumn<'a> {
38 pub label: &'a str,
39 pub weight: f32,
41 pub sortable: bool,
42 pub align_end: bool,
43}
44
45impl Frame<'_> {
46 pub fn data_table(
49 &mut self,
50 id: u64,
51 columns: &[TableColumn<'_>],
52 row_count: usize,
53 sort: &mut TableSortState,
54 mut cell_text: impl FnMut(usize, usize) -> String,
55 ) -> bool {
56 if columns.is_empty() {
57 return false;
58 }
59 let t = self.theme();
60 let mut changed = false;
61 let weight_sum: f32 = columns.iter().map(|c| c.weight.max(0.01)).sum();
62 let mut header_rect = Rect::default();
63 self.with_gap(0.0, |f| header_rect = f.reserve(56.0));
64
65 let mut x = header_rect.x;
66 for (i, col) in columns.iter().enumerate() {
67 let w = if i + 1 == columns.len() {
68 (header_rect.x + header_rect.width - x).max(0.0)
69 } else {
70 header_rect.width * (col.weight.max(0.01) / weight_sum)
71 };
72 let cell = Rect { x, width: w, ..header_rect };
73 x += w;
74 if col.sortable {
75 let r = self.target(id.wrapping_add(i as u64), cell, col.label, true, Role::Button);
76 self.state_layer(r, 4.0, t.on_surface_variant);
77 self.focus_ring(r);
78 if r.clicked {
79 sort.toggle(i);
80 changed = true;
81 }
82 }
83 let align = if col.align_end { 2 } else { 0 };
84 self.styled_text(
85 cell,
86 col.label,
87 t.text,
88 TextStyle { size: 14.0, weight: 500, align, padding: 16.0, letter_spacing: 0.1, ..TextStyle::default() },
89 );
90 if sort.column == Some(i) && sort.direction != SortDirection::None {
91 let label_w = self.text_width(col.label, TextStyle::LABEL);
92 let ax = if col.align_end {
93 (cell.x + cell.width - 22.0 - label_w).max(cell.x + 8.0)
94 } else {
95 (cell.x + 22.0 + label_w).min(cell.x + cell.width - 8.0)
96 };
97 let ay = cell.y + cell.height * 0.5;
98 let dir = if sort.direction == SortDirection::Descending { 1.0 } else { -1.0 };
99 self.paint_line([ax, ay - 5.0], [ax, ay + 5.0], 1.5, t.on_surface_variant);
100 self.paint_line([ax - 3.0, ay + dir * 2.0], [ax, ay + dir * 5.0], 1.5, t.on_surface_variant);
101 self.paint_line([ax, ay + dir * 5.0], [ax + 3.0, ay + dir * 2.0], 1.5, t.on_surface_variant);
102 }
103 }
104 self.paint_rect(
105 Rect { y: header_rect.y + header_rect.height - 1.0, height: 1.0, ..header_rect },
106 0.0,
107 t.outline_variant,
108 );
109 self.with_gap(0.0, |f| {
110 for row in 0..row_count {
111 let b = f.reserve(52.0);
112 let mut rx = b.x;
113 for (c, col) in columns.iter().enumerate() {
114 let w = if c + 1 == columns.len() {
115 (b.x + b.width - rx).max(0.0)
116 } else {
117 b.width * (col.weight.max(0.01) / weight_sum)
118 };
119 let cell = Rect { x: rx, width: w, ..b };
120 rx += w;
121 let text = cell_text(row, c);
122 f.styled_text(
123 cell,
124 &text,
125 t.text,
126 TextStyle {
127 size: 14.0,
128 align: if col.align_end { 2 } else { 0 },
129 padding: 16.0,
130 letter_spacing: 0.25,
131 ..TextStyle::default()
132 },
133 );
134 }
135 if row + 1 < row_count {
136 f.paint_rect(Rect { y: b.y + b.height - 1.0, height: 1.0, ..b }, 0.0, t.outline_variant);
137 }
138 }
139 });
140 changed
141 }
142}
143
144#[cfg(test)]
145#[path = "data_table.spec.rs"]
146mod tests;