1use super::source::{SortComparer, SortDirection};
6use crate::*;
7use std::cmp::Ordering;
8use std::fmt;
9use std::rc::Rc;
10use waft_foundation::{App, ChangeNotifier, ChangeNotifierData, Handle, HandleId, Ref};
11use waft_painting::{AlignmentGeometry, EdgeInsetsGeometry, TextOverflow};
12use waft_widgets::*;
13
14pub(crate) const DEFAULT_WIDTH: f64 = 120.0;
16
17const DEFAULT_MIN_WIDTH: f64 = 20.0;
19
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
22pub enum TableViewFrozenEdge {
23 #[default]
24 None,
25 Leading,
26 Trailing,
27}
28
29#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
31pub enum TableViewSortCycle {
32 #[default]
33 AscendingDescending,
34 AscendingDescendingNone,
35 DescendingAscending,
36 DescendingAscendingNone,
37}
38
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
41pub(crate) enum ColumnChange {
42 #[default]
43 None,
44 Width,
46 Header,
48 HeaderToolTip,
50 FrozenEdge,
52 CanResize,
54 Visibility,
56 CanSort,
58 CustomSortComparer,
60 SortDirection,
62 Other,
64}
65
66#[derive(Clone, Copy, Debug, PartialEq)]
68pub(crate) struct TableViewCellStyle {
69 pub(crate) padding: [f64; 4],
70 pub(crate) font_size: f64,
71}
72
73pub type TableViewCellTemplate<T> = Rc<dyn Fn(&mut App, BuildContext, &T) -> WidgetRef>;
75
76enum ColumnCell<T> {
78 Text(Rc<dyn Fn(&T) -> String>),
80 Template(TableViewCellTemplate<T>),
82}
83
84pub struct TableViewColumn<T> {
86 notifier: ChangeNotifierData,
87 cell: ColumnCell<T>,
88 header: String,
89 header_tool_tip: Option<String>,
90 width: GridLength,
91 min_width: f64,
92 min_width_set: bool,
94 max_width: f64,
95 can_resize: bool,
96 actual_width: f64,
97 frozen_edge: TableViewFrozenEdge,
98 visible: bool,
99 can_sort: bool,
100 sort_cycle: TableViewSortCycle,
101 sort_key: Option<SortComparer<T>>,
103 custom_sort_comparer: Option<SortComparer<T>>,
104 sort_direction: SortDirection,
105 owner: Option<HandleId>,
107 change: ColumnChange,
108}
109
110impl<T: 'static> TableViewColumn<T> {
111 fn new(app: &mut App, header: String, cell: ColumnCell<T>) -> Ref<Self> {
112 app.create(Self {
113 notifier: ChangeNotifierData::new(),
114 cell,
115 header,
116 header_tool_tip: None,
117 width: GridLength::pixel(DEFAULT_WIDTH),
118 min_width: DEFAULT_MIN_WIDTH,
119 min_width_set: false,
120 max_width: f64::INFINITY,
121 can_resize: true,
122 actual_width: DEFAULT_WIDTH,
123 frozen_edge: TableViewFrozenEdge::None,
124 visible: true,
125 can_sort: true,
126 sort_cycle: TableViewSortCycle::AscendingDescending,
127 sort_key: None,
128 custom_sort_comparer: None,
129 sort_direction: SortDirection::None,
130 owner: None,
131 change: ColumnChange::None,
132 })
133 }
134
135 pub fn text(
137 app: &mut App,
138 header: impl Into<String>,
139 binding: impl Fn(&T) -> String + 'static,
140 ) -> Ref<Self> {
141 Self::new(app, header.into(), ColumnCell::Text(Rc::new(binding)))
142 }
143
144 pub fn template(
146 app: &mut App,
147 header: impl Into<String>,
148 cell_template: impl Fn(&mut App, BuildContext, &T) -> WidgetRef + 'static,
149 ) -> Ref<Self> {
150 Self::new(
151 app,
152 header.into(),
153 ColumnCell::Template(Rc::new(cell_template)),
154 )
155 }
156
157 pub fn dispose(self: Handle<Self>, app: &mut App) {
159 ChangeNotifierData::dispose(app, self);
160 }
161
162 pub fn header(self: Handle<Self>, app: &App) -> &str {
164 &app.get(self).header
165 }
166
167 pub fn set_header(self: Handle<Self>, app: &mut App, value: impl Into<String>) {
169 let value = value.into();
170 if app.get(self).header != value {
171 app.get_mut(self).header = value;
172 self.changed(app, ColumnChange::Header);
173 }
174 }
175
176 pub fn header_tool_tip(self: Handle<Self>, app: &App) -> Option<&str> {
178 app.get(self).header_tool_tip.as_deref()
179 }
180
181 pub fn set_header_tool_tip(self: Handle<Self>, app: &mut App, value: Option<String>) {
183 if app.get(self).header_tool_tip != value {
184 app.get_mut(self).header_tool_tip = value;
185 self.changed(app, ColumnChange::HeaderToolTip);
186 }
187 }
188
189 pub fn width(self: Handle<Self>, app: &App) -> GridLength {
191 app.get(self).width
192 }
193
194 pub fn set_width(self: Handle<Self>, app: &mut App, value: GridLength) {
196 if app.get(self).width != value {
197 app.get_mut(self).width = value;
198 self.width_changed(app);
199 }
200 }
201
202 pub fn min_width(self: Handle<Self>, app: &App) -> f64 {
204 app.get(self).min_width
205 }
206
207 pub fn set_min_width(self: Handle<Self>, app: &mut App, value: f64) {
209 let column = app.get_mut(self);
210 column.min_width_set = true;
211 if column.min_width != value {
212 column.min_width = value;
213 self.width_changed(app);
214 }
215 }
216
217 pub fn max_width(self: Handle<Self>, app: &App) -> f64 {
219 app.get(self).max_width
220 }
221
222 pub fn set_max_width(self: Handle<Self>, app: &mut App, value: f64) {
224 if app.get(self).max_width != value {
225 app.get_mut(self).max_width = value;
226 self.width_changed(app);
227 }
228 }
229
230 pub fn can_resize(self: Handle<Self>, app: &App) -> bool {
232 app.get(self).can_resize
233 }
234
235 pub fn set_can_resize(self: Handle<Self>, app: &mut App, value: bool) {
237 if app.get(self).can_resize != value {
238 app.get_mut(self).can_resize = value;
239 self.changed(app, ColumnChange::CanResize);
240 }
241 }
242
243 pub fn actual_width(self: Handle<Self>, app: &App) -> f64 {
245 app.get(self).actual_width
246 }
247
248 pub fn frozen_edge(self: Handle<Self>, app: &App) -> TableViewFrozenEdge {
250 app.get(self).frozen_edge
251 }
252
253 pub fn set_frozen_edge(self: Handle<Self>, app: &mut App, value: TableViewFrozenEdge) {
255 if app.get(self).frozen_edge != value {
256 app.get_mut(self).frozen_edge = value;
257 self.changed(app, ColumnChange::FrozenEdge);
258 }
259 }
260
261 pub fn visible(self: Handle<Self>, app: &App) -> bool {
263 app.get(self).visible
264 }
265
266 pub fn set_visible(self: Handle<Self>, app: &mut App, value: bool) {
268 if app.get(self).visible != value {
269 app.get_mut(self).visible = value;
270 self.changed(app, ColumnChange::Visibility);
271 }
272 }
273
274 pub fn can_sort(self: Handle<Self>, app: &App) -> bool {
276 app.get(self).can_sort
277 }
278
279 pub fn set_can_sort(self: Handle<Self>, app: &mut App, value: bool) {
281 if app.get(self).can_sort != value {
282 app.get_mut(self).can_sort = value;
283 self.changed(app, ColumnChange::CanSort);
284 }
285 }
286
287 pub fn sort_cycle(self: Handle<Self>, app: &App) -> TableViewSortCycle {
289 app.get(self).sort_cycle
290 }
291
292 pub fn set_sort_cycle(self: Handle<Self>, app: &mut App, value: TableViewSortCycle) {
294 app.get_mut(self).sort_cycle = value;
295 }
296
297 pub fn set_sort_key<K: Ord>(
299 self: Handle<Self>,
300 app: &mut App,
301 key: impl Fn(&T) -> K + 'static,
302 ) {
303 app.get_mut(self).sort_key = Some(Rc::new(move |a, b| key(a).cmp(&key(b))));
304 self.changed(app, ColumnChange::Other);
305 }
306
307 pub fn set_custom_sort_comparer(
309 self: Handle<Self>,
310 app: &mut App,
311 comparer: impl Fn(&T, &T) -> Ordering + 'static,
312 ) {
313 app.get_mut(self).custom_sort_comparer = Some(Rc::new(comparer));
314 self.changed(app, ColumnChange::CustomSortComparer);
315 }
316
317 pub fn sort_direction(self: Handle<Self>, app: &App) -> SortDirection {
319 app.get(self).sort_direction
320 }
321}
322
323impl<T: 'static> TableViewColumn<T> {
326 pub(crate) fn set_owner(self: Handle<Self>, app: &mut App, owner: Option<HandleId>) -> bool {
328 let column = app.get_mut(self);
329 if let (Some(existing), Some(new)) = (column.owner, owner)
330 && existing != new
331 {
332 debug_assert!(
333 false,
334 "TableViewColumn re-owned without removing it from the previous TableView first"
335 );
336 return false;
337 }
338 column.owner = owner;
339 true
340 }
341
342 pub(crate) fn owner(self: Handle<Self>, app: &App) -> Option<HandleId> {
343 app.get(self).owner
344 }
345
346 pub(crate) fn change(self: Handle<Self>, app: &App) -> ColumnChange {
347 app.get(self).change
348 }
349
350 pub(crate) fn min_width_set(self: Handle<Self>, app: &App) -> bool {
351 app.get(self).min_width_set
352 }
353
354 pub(crate) fn set_actual_width(self: Handle<Self>, app: &mut App, width: f64) {
356 app.get_mut(self).actual_width = width;
357 }
358
359 pub(crate) fn set_sort_state(self: Handle<Self>, app: &mut App, direction: SortDirection) {
361 if app.get(self).sort_direction != direction {
362 app.get_mut(self).sort_direction = direction;
363 self.changed(app, ColumnChange::SortDirection);
364 }
365 }
366
367 pub(crate) fn sort_comparer(self: Handle<Self>, app: &App) -> Option<SortComparer<T>> {
369 let column = app.get(self);
370 if let Some(comparer) = &column.custom_sort_comparer {
371 return Some(comparer.clone());
372 }
373 if let Some(key) = &column.sort_key {
374 return Some(key.clone());
375 }
376 match &column.cell {
377 ColumnCell::Text(binding) => {
378 let binding = binding.clone();
379 Some(Rc::new(move |a, b| binding(a).cmp(&binding(b))))
380 }
381 ColumnCell::Template(_) => None,
382 }
383 }
384
385 pub(crate) fn is_sortable(self: Handle<Self>, app: &App) -> bool {
387 self.can_sort(app) && self.sort_comparer(app).is_some()
388 }
389
390 pub(crate) fn generate_element(
392 self: Handle<Self>,
393 app: &mut App,
394 context: BuildContext,
395 item: &T,
396 style: TableViewCellStyle,
397 ) -> WidgetRef {
398 match &app.get(self).cell {
399 ColumnCell::Text(binding) => {
400 let text = binding(item);
401 let r = ThemeResources::of(app, context);
402 let [left, top, right, bottom] = style.padding;
403 Align::new()
404 .alignment(AlignmentGeometry::CENTER_LEFT)
405 .child(
406 Padding::new(EdgeInsetsGeometry::from_ltrb(left, top, right, bottom))
407 .child(
408 Text::new(text)
409 .style(
410 waft_painting::TextStyle::new()
411 .font_family(r.content_control_theme_font_family)
412 .font_size(style.font_size)
413 .font_weight(waft_embedder::FontWeight::NORMAL),
414 )
415 .max_lines(1)
416 .soft_wrap(false)
417 .overflow(TextOverflow::Ellipsis),
418 ),
419 )
420 .into_widget()
421 }
422 ColumnCell::Template(template) => {
423 let template = template.clone();
424 template(app, context, item)
425 }
426 }
427 }
428
429 fn width_changed(self: Handle<Self>, app: &mut App) {
431 let column = app.get_mut(self);
432 let pixels = if column.width.unit == GridUnitType::Pixel {
433 column.width.value
434 } else {
435 DEFAULT_WIDTH
436 };
437 let lo = column.min_width;
438 let hi = lo.max(column.max_width);
439 column.actual_width = pixels.clamp(lo, hi);
440 self.changed(app, ColumnChange::Width);
441 }
442
443 fn changed(self: Handle<Self>, app: &mut App, change: ColumnChange) {
444 app.get_mut(self).change = change;
445 self.notify_listeners(app);
446 }
447}
448
449impl<T: 'static> ChangeNotifier for TableViewColumn<T> {
450 fn change_notifier_data(&self) -> &ChangeNotifierData {
451 &self.notifier
452 }
453
454 fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
455 &mut self.notifier
456 }
457}
458
459impl<T> fmt::Debug for TableViewColumn<T> {
460 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
461 f.debug_struct("TableViewColumn")
462 .field("header", &self.header)
463 .field("width", &self.width)
464 .field("actual_width", &self.actual_width)
465 .field("sort_direction", &self.sort_direction)
466 .finish_non_exhaustive()
467 }
468}