1use std::{ops::Range, rc::Rc, time::Duration};
2
3use crate::{
4 actions::{Cancel, SelectDown, SelectUp},
5 h_flex,
6 menu::{ContextMenuExt, PopupMenu},
7 scroll::{ScrollableMask, Scrollbar, ScrollbarState},
8 v_flex, ActiveTheme, Icon, IconName, Sizable, Size, StyleSized as _, StyledExt,
9 VirtualListScrollHandle,
10};
11use gpui::{
12 actions, canvas, div, prelude::FluentBuilder, px, uniform_list, App, AppContext, Axis, Bounds,
13 Context, Div, DragMoveEvent, Edges, Entity, EventEmitter, FocusHandle, Focusable,
14 InteractiveElement, IntoElement, KeyBinding, ListSizingBehavior, MouseButton, MouseDownEvent,
15 ParentElement, Pixels, Point, Render, RenderOnce, ScrollStrategy, SharedString,
16 StatefulInteractiveElement as _, Styled, Task, UniformListScrollHandle, Window,
17};
18
19mod column;
20mod delegate;
21mod loading;
22
23pub use column::*;
24pub use delegate::*;
25
26actions!(table, [SelectPrevColumn, SelectNextColumn]);
27
28const CONTEXT: &'static str = "Table";
29pub(crate) fn init(cx: &mut App) {
30 cx.bind_keys([
31 KeyBinding::new("escape", Cancel, Some(CONTEXT)),
32 KeyBinding::new("up", SelectUp, Some(CONTEXT)),
33 KeyBinding::new("down", SelectDown, Some(CONTEXT)),
34 KeyBinding::new("left", SelectPrevColumn, Some(CONTEXT)),
35 KeyBinding::new("right", SelectNextColumn, Some(CONTEXT)),
36 ]);
37}
38
39#[derive(Copy, Clone, Debug, PartialEq, Eq)]
40enum SelectionState {
41 Column,
42 Row,
43}
44
45#[derive(Clone)]
47pub enum TableEvent {
48 SelectRow(usize),
50 DoubleClickedRow(usize),
52 SelectColumn(usize),
54 ColumnWidthsChanged(Vec<Pixels>),
58 MoveColumn(usize, usize),
63}
64
65#[derive(Debug, Default)]
67pub struct TableVisibleRange {
68 rows: Range<usize>,
70 cols: Range<usize>,
72}
73
74impl TableVisibleRange {
75 pub fn rows(&self) -> &Range<usize> {
77 &self.rows
78 }
79
80 pub fn cols(&self) -> &Range<usize> {
82 &self.cols
83 }
84}
85
86struct TableOptions {
87 scrollbar_visible: Edges<bool>,
88 stripe: bool,
90 bordered: bool,
92 size: Size,
94}
95
96impl Default for TableOptions {
97 fn default() -> Self {
98 Self {
99 scrollbar_visible: Edges::all(true),
100 stripe: false,
101 bordered: true,
102 size: Size::default(),
103 }
104 }
105}
106
107pub struct TableState<D: TableDelegate> {
109 focus_handle: FocusHandle,
110 delegate: D,
111 options: TableOptions,
112 bounds: Bounds<Pixels>,
114 fixed_head_cols_bounds: Bounds<Pixels>,
116
117 col_groups: Vec<ColGroup>,
118
119 pub loop_selection: bool,
123 pub col_selectable: bool,
125 pub row_selectable: bool,
127 pub sortable: bool,
129 pub col_resizable: bool,
131 pub col_movable: bool,
133 pub col_fixed: bool,
135
136 pub vertical_scroll_handle: UniformListScrollHandle,
137 pub vertical_scroll_state: ScrollbarState,
138 pub horizontal_scroll_handle: VirtualListScrollHandle,
139 pub horizontal_scroll_state: ScrollbarState,
140
141 selected_row: Option<usize>,
142 selection_state: SelectionState,
143 right_clicked_row: Option<usize>,
144 selected_col: Option<usize>,
145
146 resizing_col: Option<usize>,
148
149 visible_range: TableVisibleRange,
151
152 _measure: Vec<Duration>,
153 _load_more_task: Task<()>,
154}
155
156impl<D> TableState<D>
157where
158 D: TableDelegate,
159{
160 pub fn new(delegate: D, _: &mut Window, cx: &mut Context<Self>) -> Self {
162 let mut this = Self {
163 focus_handle: cx.focus_handle(),
164 options: TableOptions::default(),
165 delegate,
166 col_groups: Vec::new(),
167 horizontal_scroll_handle: VirtualListScrollHandle::new(),
168 vertical_scroll_handle: UniformListScrollHandle::new(),
169 vertical_scroll_state: ScrollbarState::default(),
170 horizontal_scroll_state: ScrollbarState::default(),
171 selection_state: SelectionState::Row,
172 selected_row: None,
173 right_clicked_row: None,
174 selected_col: None,
175 resizing_col: None,
176 bounds: Bounds::default(),
177 fixed_head_cols_bounds: Bounds::default(),
178 visible_range: TableVisibleRange::default(),
179 loop_selection: true,
180 col_selectable: true,
181 row_selectable: true,
182 sortable: true,
183 col_movable: true,
184 col_resizable: true,
185 col_fixed: true,
186 _load_more_task: Task::ready(()),
187 _measure: Vec::new(),
188 };
189
190 this.prepare_col_groups(cx);
191 this
192 }
193
194 pub fn delegate(&self) -> &D {
196 &self.delegate
197 }
198
199 pub fn delegate_mut(&mut self) -> &mut D {
201 &mut self.delegate
202 }
203
204 pub fn loop_selection(mut self, loop_selection: bool) -> Self {
206 self.loop_selection = loop_selection;
207 self
208 }
209
210 pub fn col_movable(mut self, col_movable: bool) -> Self {
212 self.col_movable = col_movable;
213 self
214 }
215
216 pub fn col_resizable(mut self, col_resizable: bool) -> Self {
218 self.col_resizable = col_resizable;
219 self
220 }
221
222 pub fn sortable(mut self, sortable: bool) -> Self {
224 self.sortable = sortable;
225 self
226 }
227
228 pub fn row_selectable(mut self, row_selectable: bool) -> Self {
230 self.row_selectable = row_selectable;
231 self
232 }
233
234 pub fn col_selectable(mut self, col_selectable: bool) -> Self {
236 self.col_selectable = col_selectable;
237 self
238 }
239
240 pub fn refresh(&mut self, cx: &mut Context<Self>) {
242 self.prepare_col_groups(cx);
243 }
244
245 pub fn scroll_to_row(&mut self, row_ix: usize, cx: &mut Context<Self>) {
247 self.vertical_scroll_handle
248 .scroll_to_item(row_ix, ScrollStrategy::Top);
249 cx.notify();
250 }
251
252 pub fn scroll_to_col(&mut self, col_ix: usize, cx: &mut Context<Self>) {
254 let col_ix = col_ix.saturating_sub(self.fixed_left_cols_count());
255
256 self.horizontal_scroll_handle
257 .scroll_to_item(col_ix, ScrollStrategy::Top);
258 cx.notify();
259 }
260
261 pub fn selected_row(&self) -> Option<usize> {
263 self.selected_row
264 }
265
266 pub fn set_selected_row(&mut self, row_ix: usize, cx: &mut Context<Self>) {
268 let is_down = match self.selected_row {
269 Some(selected_row) => row_ix > selected_row,
270 None => true,
271 };
272
273 self.selection_state = SelectionState::Row;
274 self.right_clicked_row = None;
275 self.selected_row = Some(row_ix);
276 if let Some(row_ix) = self.selected_row {
277 self.vertical_scroll_handle.scroll_to_item(
278 row_ix,
279 if is_down {
280 ScrollStrategy::Bottom
281 } else {
282 ScrollStrategy::Top
283 },
284 );
285 }
286 cx.emit(TableEvent::SelectRow(row_ix));
287 cx.notify();
288 }
289
290 pub fn selected_col(&self) -> Option<usize> {
292 self.selected_col
293 }
294
295 pub fn set_selected_col(&mut self, col_ix: usize, cx: &mut Context<Self>) {
297 self.selection_state = SelectionState::Column;
298 self.selected_col = Some(col_ix);
299 if let Some(col_ix) = self.selected_col {
300 self.scroll_to_col(col_ix, cx);
301 }
302 cx.emit(TableEvent::SelectColumn(col_ix));
303 cx.notify();
304 }
305
306 pub fn clear_selection(&mut self, cx: &mut Context<Self>) {
308 self.selection_state = SelectionState::Row;
309 self.selected_row = None;
310 self.selected_col = None;
311 cx.notify();
312 }
313
314 pub fn visible_range(&self) -> &TableVisibleRange {
318 &self.visible_range
319 }
320
321 fn prepare_col_groups(&mut self, cx: &mut Context<Self>) {
322 self.col_groups = (0..self.delegate.columns_count(cx))
323 .map(|col_ix| {
324 let column = self.delegate().column(col_ix, cx);
325 ColGroup {
326 width: column.width,
327 bounds: Bounds::default(),
328 column: column.clone(),
329 }
330 })
331 .collect();
332 cx.notify();
333 }
334
335 fn fixed_left_cols_count(&self) -> usize {
336 if !self.col_fixed {
337 return 0;
338 }
339
340 self.col_groups
341 .iter()
342 .filter(|col| col.column.fixed == Some(ColumnFixed::Left))
343 .count()
344 }
345
346 fn on_row_click(
347 &mut self,
348 ev: &MouseDownEvent,
349 row_ix: usize,
350 _: &mut Window,
351 cx: &mut Context<Self>,
352 ) {
353 if ev.button == MouseButton::Right {
354 self.right_clicked_row = Some(row_ix);
355 } else {
356 self.set_selected_row(row_ix, cx);
357
358 if ev.click_count == 2 {
359 cx.emit(TableEvent::DoubleClickedRow(row_ix));
360 }
361 }
362 }
363
364 fn on_col_head_click(&mut self, col_ix: usize, _: &mut Window, cx: &mut Context<Self>) {
365 if !self.col_selectable {
366 return;
367 }
368
369 let Some(col_group) = self.col_groups.get(col_ix) else {
370 return;
371 };
372
373 if !col_group.column.selectable {
374 return;
375 }
376
377 self.set_selected_col(col_ix, cx)
378 }
379
380 fn has_selection(&self) -> bool {
381 self.selected_row.is_some() || self.selected_col.is_some()
382 }
383
384 fn action_cancel(&mut self, _: &Cancel, _: &mut Window, cx: &mut Context<Self>) {
385 if self.has_selection() {
386 self.clear_selection(cx);
387 return;
388 }
389 cx.propagate();
390 }
391
392 fn action_select_prev(&mut self, _: &SelectUp, _: &mut Window, cx: &mut Context<Self>) {
393 let rows_count = self.delegate.rows_count(cx);
394 if rows_count < 1 {
395 return;
396 }
397
398 let mut selected_row = self.selected_row.unwrap_or(0);
399 if selected_row > 0 {
400 selected_row = selected_row.saturating_sub(1);
401 } else {
402 if self.loop_selection {
403 selected_row = rows_count.saturating_sub(1);
404 }
405 }
406
407 self.set_selected_row(selected_row, cx);
408 }
409
410 fn action_select_next(&mut self, _: &SelectDown, _: &mut Window, cx: &mut Context<Self>) {
411 let rows_count = self.delegate.rows_count(cx);
412 if rows_count < 1 {
413 return;
414 }
415
416 let selected_row = match self.selected_row {
417 Some(selected_row) if selected_row < rows_count.saturating_sub(1) => selected_row + 1,
418 Some(selected_row) => {
419 if self.loop_selection {
420 0
421 } else {
422 selected_row
423 }
424 }
425 _ => 0,
426 };
427
428 self.set_selected_row(selected_row, cx);
429 }
430
431 fn action_select_prev_col(
432 &mut self,
433 _: &SelectPrevColumn,
434 _: &mut Window,
435 cx: &mut Context<Self>,
436 ) {
437 let mut selected_col = self.selected_col.unwrap_or(0);
438 let columns_count = self.delegate.columns_count(cx);
439 if selected_col > 0 {
440 selected_col = selected_col.saturating_sub(1);
441 } else {
442 if self.loop_selection {
443 selected_col = columns_count.saturating_sub(1);
444 }
445 }
446 self.set_selected_col(selected_col, cx);
447 }
448
449 fn action_select_next_col(
450 &mut self,
451 _: &SelectNextColumn,
452 _: &mut Window,
453 cx: &mut Context<Self>,
454 ) {
455 let mut selected_col = self.selected_col.unwrap_or(0);
456 if selected_col < self.delegate.columns_count(cx).saturating_sub(1) {
457 selected_col += 1;
458 } else {
459 if self.loop_selection {
460 selected_col = 0;
461 }
462 }
463
464 self.set_selected_col(selected_col, cx);
465 }
466
467 fn scroll_table_by_col_resizing(
469 &mut self,
470 mouse_position: Point<Pixels>,
471 col_group: &ColGroup,
472 ) {
473 if mouse_position.x > self.bounds.right() {
475 return;
476 }
477
478 let mut offset = self.horizontal_scroll_handle.offset();
479 let col_bounds = col_group.bounds;
480
481 if mouse_position.x < self.bounds.left()
482 && col_bounds.right() < self.bounds.left() + px(20.)
483 {
484 offset.x += px(1.);
485 } else if mouse_position.x > self.bounds.right()
486 && col_bounds.right() > self.bounds.right() - px(20.)
487 {
488 offset.x -= px(1.);
489 }
490
491 self.horizontal_scroll_handle.set_offset(offset);
492 }
493
494 fn resize_cols(&mut self, ix: usize, size: Pixels, _: &mut Window, cx: &mut Context<Self>) {
497 if !self.col_resizable {
498 return;
499 }
500
501 const MIN_WIDTH: Pixels = px(10.0);
502 const MAX_WIDTH: Pixels = px(1200.0);
503 let Some(col_group) = self.col_groups.get_mut(ix) else {
504 return;
505 };
506
507 if !col_group.is_resizable() {
508 return;
509 }
510 let size = size.floor();
511
512 let old_width = col_group.width;
513 let new_width = size;
514 if new_width < MIN_WIDTH {
515 return;
516 }
517 let changed_width = new_width - old_width;
518 if changed_width > px(-1.0) && changed_width < px(1.0) {
520 return;
521 }
522 col_group.width = new_width.min(MAX_WIDTH);
523
524 cx.notify();
525 }
526
527 fn perform_sort(&mut self, col_ix: usize, window: &mut Window, cx: &mut Context<Self>) {
528 if !self.sortable {
529 return;
530 }
531
532 let sort = self.col_groups.get(col_ix).and_then(|g| g.column.sort);
533 if sort.is_none() {
534 return;
535 }
536
537 let sort = sort.unwrap();
538 let sort = match sort {
539 ColumnSort::Ascending => ColumnSort::Default,
540 ColumnSort::Descending => ColumnSort::Ascending,
541 ColumnSort::Default => ColumnSort::Descending,
542 };
543
544 for (ix, col_group) in self.col_groups.iter_mut().enumerate() {
545 if ix == col_ix {
546 col_group.column.sort = Some(sort);
547 } else {
548 if col_group.column.sort.is_some() {
549 col_group.column.sort = Some(ColumnSort::Default);
550 }
551 }
552 }
553
554 self.delegate_mut().perform_sort(col_ix, sort, window, cx);
555
556 cx.notify();
557 }
558
559 fn move_column(
560 &mut self,
561 col_ix: usize,
562 to_ix: usize,
563 window: &mut Window,
564 cx: &mut Context<Self>,
565 ) {
566 if col_ix == to_ix {
567 return;
568 }
569
570 self.delegate.move_column(col_ix, to_ix, window, cx);
571 let col_group = self.col_groups.remove(col_ix);
572 self.col_groups.insert(to_ix, col_group);
573
574 cx.emit(TableEvent::MoveColumn(col_ix, to_ix));
575 cx.notify();
576 }
577
578 fn load_more_if_need(
580 &mut self,
581 rows_count: usize,
582 visible_end: usize,
583 window: &mut Window,
584 cx: &mut Context<Self>,
585 ) {
586 let threshold = self.delegate.load_more_threshold();
587 if visible_end >= rows_count.saturating_sub(threshold) {
589 if !self.delegate.is_eof(cx) {
590 return;
591 }
592
593 self._load_more_task = cx.spawn_in(window, async move |view, window| {
594 _ = view.update_in(window, |view, window, cx| {
595 view.delegate.load_more(window, cx);
596 });
597 });
598 }
599 }
600
601 fn update_visible_range_if_need(
602 &mut self,
603 visible_range: Range<usize>,
604 axis: Axis,
605 window: &mut Window,
606 cx: &mut Context<Self>,
607 ) {
608 if visible_range.len() <= 1 {
611 return;
612 }
613
614 if axis == Axis::Vertical {
615 if self.visible_range.rows == visible_range {
616 return;
617 }
618 self.delegate_mut()
619 .visible_rows_changed(visible_range.clone(), window, cx);
620 self.visible_range.rows = visible_range;
621 } else {
622 if self.visible_range.cols == visible_range {
623 return;
624 }
625 self.delegate_mut()
626 .visible_columns_changed(visible_range.clone(), window, cx);
627 self.visible_range.cols = visible_range;
628 }
629 }
630
631 fn render_cell(&self, col_ix: usize, _window: &mut Window, _cx: &mut Context<Self>) -> Div {
632 let Some(col_group) = self.col_groups.get(col_ix) else {
633 return div();
634 };
635
636 let col_width = col_group.width;
637 let col_padding = col_group.column.paddings;
638
639 div()
640 .w(col_width)
641 .h_full()
642 .flex_shrink_0()
643 .overflow_hidden()
644 .whitespace_nowrap()
645 .table_cell_size(self.options.size)
646 .map(|this| match col_padding {
647 Some(padding) => this
648 .pl(padding.left)
649 .pr(padding.right)
650 .pt(padding.top)
651 .pb(padding.bottom),
652 None => this,
653 })
654 }
655
656 fn render_col_wrap(&self, col_ix: usize, _: &mut Window, cx: &mut Context<Self>) -> Div {
658 let el = h_flex().h_full();
659 let selectable = self.col_selectable
660 && self
661 .col_groups
662 .get(col_ix)
663 .map(|col_group| col_group.column.selectable)
664 .unwrap_or(false);
665
666 if selectable
667 && self.selected_col == Some(col_ix)
668 && self.selection_state == SelectionState::Column
669 {
670 el.bg(cx.theme().table_active)
671 } else {
672 el
673 }
674 }
675
676 fn render_resize_handle(
677 &self,
678 ix: usize,
679 _: &mut Window,
680 cx: &mut Context<Self>,
681 ) -> impl IntoElement {
682 const HANDLE_SIZE: Pixels = px(2.);
683
684 let resizable = self.col_resizable
685 && self
686 .col_groups
687 .get(ix)
688 .map(|col| col.is_resizable())
689 .unwrap_or(false);
690 if !resizable {
691 return div().into_any_element();
692 }
693
694 let group_id = SharedString::from(format!("resizable-handle:{}", ix));
695
696 h_flex()
697 .id(("resizable-handle", ix))
698 .group(group_id.clone())
699 .occlude()
700 .cursor_col_resize()
701 .h_full()
702 .w(HANDLE_SIZE)
703 .ml(-(HANDLE_SIZE))
704 .justify_end()
705 .items_center()
706 .child(
707 div()
708 .h_full()
709 .justify_center()
710 .bg(cx.theme().table_row_border)
711 .group_hover(group_id, |this| this.bg(cx.theme().border).h_full())
712 .w(px(1.)),
713 )
714 .on_drag_move(
715 cx.listener(move |view, e: &DragMoveEvent<ResizeColumn>, window, cx| {
716 match e.drag(cx) {
717 ResizeColumn((entity_id, ix)) => {
718 if cx.entity_id() != *entity_id {
719 return;
720 }
721
722 let ix = *ix;
729 view.resizing_col = Some(ix);
730
731 let col_group = view
732 .col_groups
733 .get(ix)
734 .expect("BUG: invalid col index")
735 .clone();
736
737 view.resize_cols(
738 ix,
739 e.event.position.x - HANDLE_SIZE - col_group.bounds.left(),
740 window,
741 cx,
742 );
743
744 view.scroll_table_by_col_resizing(e.event.position, &col_group);
746 }
747 };
748 }),
749 )
750 .on_drag(ResizeColumn((cx.entity_id(), ix)), |drag, _, _, cx| {
751 cx.stop_propagation();
752 cx.new(|_| drag.clone())
753 })
754 .on_mouse_up_out(
755 MouseButton::Left,
756 cx.listener(|view, _, _, cx| {
757 if view.resizing_col.is_none() {
758 return;
759 }
760
761 view.resizing_col = None;
762
763 let new_widths = view.col_groups.iter().map(|g| g.width).collect();
764 cx.emit(TableEvent::ColumnWidthsChanged(new_widths));
765 cx.notify();
766 }),
767 )
768 .into_any_element()
769 }
770
771 fn render_sort_icon(
772 &self,
773 col_ix: usize,
774 col_group: &ColGroup,
775 _: &mut Window,
776 cx: &mut Context<Self>,
777 ) -> Option<impl IntoElement> {
778 if !self.sortable {
779 return None;
780 }
781
782 let Some(sort) = col_group.column.sort else {
783 return None;
784 };
785
786 let (icon, is_on) = match sort {
787 ColumnSort::Ascending => (IconName::SortAscending, true),
788 ColumnSort::Descending => (IconName::SortDescending, true),
789 ColumnSort::Default => (IconName::ChevronsUpDown, false),
790 };
791
792 Some(
793 div()
794 .id(("icon-sort", col_ix))
795 .p(px(2.))
796 .rounded(cx.theme().radius / 2.)
797 .map(|this| match is_on {
798 true => this,
799 false => this.opacity(0.5),
800 })
801 .hover(|this| this.bg(cx.theme().secondary).opacity(7.))
802 .active(|this| this.bg(cx.theme().secondary_active).opacity(1.))
803 .on_click(
804 cx.listener(move |table, _, window, cx| table.perform_sort(col_ix, window, cx)),
805 )
806 .child(
807 Icon::new(icon)
808 .size_3()
809 .text_color(cx.theme().secondary_foreground),
810 ),
811 )
812 }
813
814 fn render_th(
819 &self,
820 col_ix: usize,
821 window: &mut Window,
822 cx: &mut Context<Self>,
823 ) -> impl IntoElement {
824 let entity_id = cx.entity_id();
825 let col_group = self.col_groups.get(col_ix).expect("BUG: invalid col index");
826
827 let movable = self.col_movable && col_group.column.movable;
828 let paddings = col_group.column.paddings;
829 let name = col_group.column.name.clone();
830
831 h_flex()
832 .h_full()
833 .child(
834 self.render_cell(col_ix, window, cx)
835 .id(("col-header", col_ix))
836 .on_mouse_down(
837 MouseButton::Left,
838 cx.listener(move |this, _, window, cx| {
839 this.on_col_head_click(col_ix, window, cx);
840 }),
841 )
842 .child(
843 h_flex()
844 .size_full()
845 .justify_between()
846 .items_center()
847 .child(self.delegate.render_th(col_ix, window, cx))
848 .when_some(paddings, |this, paddings| {
849 let offset_pr =
851 self.options.size.table_cell_padding().right - paddings.right;
852 this.pr(offset_pr.max(px(0.)))
853 })
854 .children(self.render_sort_icon(col_ix, &col_group, window, cx)),
855 )
856 .when(movable, |this| {
857 this.on_drag(
858 DragColumn {
859 entity_id,
860 col_ix,
861 name,
862 width: col_group.width,
863 },
864 |drag, _, _, cx| {
865 cx.stop_propagation();
866 cx.new(|_| drag.clone())
867 },
868 )
869 .drag_over::<DragColumn>(|this, _, _, cx| {
870 this.rounded_l_none()
871 .border_l_2()
872 .border_r_0()
873 .border_color(cx.theme().drag_border)
874 })
875 .on_drop(cx.listener(
876 move |table, drag: &DragColumn, window, cx| {
877 if drag.entity_id != cx.entity_id() {
879 return;
880 }
881
882 table.move_column(drag.col_ix, col_ix, window, cx);
883 },
884 ))
885 }),
886 )
887 .child(self.render_resize_handle(col_ix, window, cx))
889 .child({
891 let view = cx.entity().clone();
892 canvas(
893 move |bounds, _, cx| {
894 view.update(cx, |r, _| r.col_groups[col_ix].bounds = bounds)
895 },
896 |_, _, _, _| {},
897 )
898 .absolute()
899 .size_full()
900 })
901 }
902
903 fn render_table_head(
904 &mut self,
905 left_columns_count: usize,
906 window: &mut Window,
907 cx: &mut Context<Self>,
908 ) -> impl IntoElement {
909 let view = cx.entity().clone();
910 let horizontal_scroll_handle = self.horizontal_scroll_handle.clone();
911
912 if left_columns_count == 0 {
914 self.fixed_head_cols_bounds = Bounds::default();
915 }
916
917 h_flex()
918 .w_full()
919 .h(self.options.size.table_row_height())
920 .flex_shrink_0()
921 .border_b_1()
922 .border_color(cx.theme().border)
923 .text_color(cx.theme().table_head_foreground)
924 .when(left_columns_count > 0, |this| {
925 let view = view.clone();
926 this.child(
928 h_flex()
929 .relative()
930 .h_full()
931 .bg(cx.theme().table_head)
932 .children(
933 self.col_groups
934 .iter()
935 .filter(|col| col.column.fixed == Some(ColumnFixed::Left))
936 .enumerate()
937 .map(|(col_ix, _)| self.render_th(col_ix, window, cx)),
938 )
939 .child(
940 div()
942 .absolute()
943 .top_0()
944 .right_0()
945 .bottom_0()
946 .w_0()
947 .flex_shrink_0()
948 .border_r_1()
949 .border_color(cx.theme().border),
950 )
951 .child(
952 canvas(
953 move |bounds, _, cx| {
954 view.update(cx, |r, _| r.fixed_head_cols_bounds = bounds)
955 },
956 |_, _, _, _| {},
957 )
958 .absolute()
959 .size_full(),
960 ),
961 )
962 })
963 .child(
964 h_flex()
966 .id("table-head")
967 .size_full()
968 .overflow_scroll()
969 .relative()
970 .track_scroll(&horizontal_scroll_handle)
971 .bg(cx.theme().table_head)
972 .child(
973 h_flex()
974 .relative()
975 .children(
976 self.col_groups
977 .iter()
978 .skip(left_columns_count)
979 .enumerate()
980 .map(|(col_ix, _)| {
981 self.render_th(left_columns_count + col_ix, window, cx)
982 }),
983 )
984 .child(self.delegate.render_last_empty_col(window, cx)),
985 ),
986 )
987 }
988
989 #[allow(clippy::too_many_arguments)]
990 fn render_table_row(
991 &mut self,
992 row_ix: usize,
993 rows_count: usize,
994 left_columns_count: usize,
995 col_sizes: Rc<Vec<gpui::Size<Pixels>>>,
996 columns_count: usize,
997 is_filled: bool,
998 window: &mut Window,
999 cx: &mut Context<Self>,
1000 ) -> impl IntoElement {
1001 let horizontal_scroll_handle = self.horizontal_scroll_handle.clone();
1002 let is_stripe_row = self.options.stripe && row_ix % 2 != 0;
1003 let is_selected = self.selected_row == Some(row_ix);
1004 let view = cx.entity().clone();
1005 let row_height = self.options.size.table_row_height();
1006
1007 if row_ix < rows_count {
1008 let is_last_row = row_ix + 1 == rows_count;
1009 let need_render_border = is_selected || !is_last_row || !is_filled;
1010
1011 let mut tr = self.delegate.render_tr(row_ix, window, cx);
1012 let style = tr.style().clone();
1013
1014 tr.h_flex()
1015 .w_full()
1016 .h(row_height)
1017 .when(need_render_border, |this| {
1018 this.border_b_1().border_color(cx.theme().table_row_border)
1019 })
1020 .when(is_stripe_row, |this| this.bg(cx.theme().table_even))
1021 .refine_style(&style)
1022 .hover(|this| {
1023 if is_selected || self.right_clicked_row == Some(row_ix) {
1024 this
1025 } else {
1026 this.bg(cx.theme().table_hover)
1027 }
1028 })
1029 .when(left_columns_count > 0, |this| {
1030 this.child(
1032 h_flex()
1033 .relative()
1034 .h_full()
1035 .children({
1036 let mut items = Vec::with_capacity(left_columns_count);
1037
1038 (0..left_columns_count).for_each(|col_ix| {
1039 items.push(self.render_col_wrap(col_ix, window, cx).child(
1040 self.render_cell(col_ix, window, cx).child(
1041 self.measure_render_td(row_ix, col_ix, window, cx),
1042 ),
1043 ));
1044 });
1045
1046 items
1047 })
1048 .child(
1049 div()
1051 .absolute()
1052 .top_0()
1053 .right_0()
1054 .bottom_0()
1055 .w_0()
1056 .flex_shrink_0()
1057 .border_r_1()
1058 .border_color(cx.theme().border),
1059 ),
1060 )
1061 })
1062 .child(
1063 h_flex()
1064 .flex_1()
1065 .h_full()
1066 .overflow_hidden()
1067 .relative()
1068 .child(
1069 crate::virtual_list::virtual_list(
1070 view,
1071 row_ix,
1072 Axis::Horizontal,
1073 col_sizes,
1074 {
1075 move |table, visible_range: Range<usize>, window, cx| {
1076 table.update_visible_range_if_need(
1077 visible_range.clone(),
1078 Axis::Horizontal,
1079 window,
1080 cx,
1081 );
1082
1083 let mut items = Vec::with_capacity(
1084 visible_range.end - visible_range.start,
1085 );
1086
1087 visible_range.for_each(|col_ix| {
1088 let col_ix = col_ix + left_columns_count;
1089 let el =
1090 table.render_col_wrap(col_ix, window, cx).child(
1091 table.render_cell(col_ix, window, cx).child(
1092 table.measure_render_td(
1093 row_ix, col_ix, window, cx,
1094 ),
1095 ),
1096 );
1097
1098 items.push(el);
1099 });
1100
1101 items
1102 }
1103 },
1104 )
1105 .with_scroll_handle(&self.horizontal_scroll_handle),
1106 )
1107 .child(self.delegate.render_last_empty_col(window, cx)),
1108 )
1109 .when_some(self.selected_row, |this, _| {
1111 this.when(
1112 is_selected && self.selection_state == SelectionState::Row,
1113 |this| {
1114 this.border_color(gpui::transparent_white()).child(
1115 div()
1116 .top(if row_ix == 0 { px(0.) } else { px(-1.) })
1117 .left(px(0.))
1118 .right(px(0.))
1119 .bottom(px(-1.))
1120 .absolute()
1121 .bg(cx.theme().table_active)
1122 .border_1()
1123 .border_color(cx.theme().table_active_border),
1124 )
1125 },
1126 )
1127 })
1128 .when(self.right_clicked_row == Some(row_ix), |this| {
1130 this.border_color(gpui::transparent_white()).child(
1131 div()
1132 .top(if row_ix == 0 { px(0.) } else { px(-1.) })
1133 .left(px(0.))
1134 .right(px(0.))
1135 .bottom(px(-1.))
1136 .absolute()
1137 .border_1()
1138 .border_color(cx.theme().selection),
1139 )
1140 })
1141 .on_mouse_down(
1142 MouseButton::Left,
1143 cx.listener(move |this, ev, window, cx| {
1144 this.on_row_click(ev, row_ix, window, cx);
1145 }),
1146 )
1147 .on_mouse_down(
1148 MouseButton::Right,
1149 cx.listener(move |this, ev, window, cx| {
1150 this.on_row_click(ev, row_ix, window, cx);
1151 }),
1152 )
1153 } else {
1154 self.delegate
1156 .render_tr(row_ix, window, cx)
1157 .h_flex()
1158 .w_full()
1159 .h(row_height)
1160 .border_b_1()
1161 .border_color(cx.theme().table_row_border)
1162 .when(is_stripe_row, |this| this.bg(cx.theme().table_even))
1163 .children((0..columns_count).map(|col_ix| {
1164 h_flex()
1165 .left(horizontal_scroll_handle.offset().x)
1166 .child(self.render_cell(col_ix, window, cx))
1167 }))
1168 .child(self.delegate.render_last_empty_col(window, cx))
1169 }
1170 }
1171
1172 fn calculate_extra_rows_needed(&self, total_height: Pixels, actual_height: Pixels, row_height: Pixels) -> usize {
1174 let mut extra_rows_needed = 0;
1175
1176 let remaining_height = total_height - actual_height;
1177 if remaining_height > px(0.) {
1178 extra_rows_needed = (remaining_height / row_height).floor() as usize;
1179 }
1180
1181 extra_rows_needed
1182 }
1183
1184 #[inline]
1185 fn measure_render_td(
1186 &mut self,
1187 row_ix: usize,
1188 col_ix: usize,
1189 window: &mut Window,
1190 cx: &mut Context<Self>,
1191 ) -> impl IntoElement {
1192 if !crate::measure_enable() {
1193 return self
1194 .delegate
1195 .render_td(row_ix, col_ix, window, cx)
1196 .into_any_element();
1197 }
1198
1199 let start = std::time::Instant::now();
1200 let el = self.delegate.render_td(row_ix, col_ix, window, cx);
1201 self._measure.push(start.elapsed());
1202 el.into_any_element()
1203 }
1204
1205 fn measure(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
1206 if !crate::measure_enable() {
1207 return;
1208 }
1209
1210 if self._measure.len() > 0 {
1212 let total = self
1213 ._measure
1214 .iter()
1215 .fold(Duration::default(), |acc, d| acc + *d);
1216 let avg = total / self._measure.len() as u32;
1217 eprintln!(
1218 "last render {} cells total: {:?}, avg: {:?}",
1219 self._measure.len(),
1220 total,
1221 avg,
1222 );
1223 }
1224 self._measure.clear();
1225 }
1226
1227 fn render_vertical_scrollbar(
1228 &mut self,
1229
1230 _: &mut Window,
1231 _: &mut Context<Self>,
1232 ) -> Option<impl IntoElement> {
1233 Some(
1234 div()
1235 .occlude()
1236 .absolute()
1237 .top(self.options.size.table_row_height())
1238 .right_0()
1239 .bottom_0()
1240 .w(Scrollbar::width())
1241 .child(
1242 Scrollbar::uniform_scroll(
1243 &self.vertical_scroll_state,
1244 &self.vertical_scroll_handle,
1245 )
1246 .max_fps(60),
1247 ),
1248 )
1249 }
1250
1251 fn render_horizontal_scrollbar(
1252 &mut self,
1253 _: &mut Window,
1254 _: &mut Context<Self>,
1255 ) -> impl IntoElement {
1256 div()
1257 .occlude()
1258 .absolute()
1259 .left(self.fixed_head_cols_bounds.size.width)
1260 .right_0()
1261 .bottom_0()
1262 .h(Scrollbar::width())
1263 .child(Scrollbar::horizontal(
1264 &self.horizontal_scroll_state,
1265 &self.horizontal_scroll_handle,
1266 ))
1267 }
1268}
1269
1270impl<D> Focusable for TableState<D>
1271where
1272 D: TableDelegate,
1273{
1274 fn focus_handle(&self, _cx: &gpui::App) -> FocusHandle {
1275 self.focus_handle.clone()
1276 }
1277}
1278impl<D> EventEmitter<TableEvent> for TableState<D> where D: TableDelegate {}
1279
1280impl<D> Render for TableState<D>
1281where
1282 D: TableDelegate,
1283{
1284 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1285 self.measure(window, cx);
1286
1287 let columns_count = self.delegate.columns_count(cx);
1288 let left_columns_count = self
1289 .col_groups
1290 .iter()
1291 .filter(|col| self.col_fixed && col.column.fixed == Some(ColumnFixed::Left))
1292 .count();
1293 let rows_count = self.delegate.rows_count(cx);
1294 let loading = self.delegate.loading(cx);
1295
1296 let row_height = self.options.size.table_row_height();
1297 let total_height = self
1298 .vertical_scroll_handle
1299 .0
1300 .borrow()
1301 .base_handle
1302 .bounds()
1303 .size
1304 .height;
1305 let actual_height = row_height * rows_count as f32;
1306 let extra_rows_count = self.calculate_extra_rows_needed(total_height, actual_height, row_height);
1307 let render_rows_count = if self.options.stripe {
1308 rows_count + extra_rows_count
1309 } else {
1310 rows_count
1311 };
1312 let right_clicked_row = self.right_clicked_row;
1313 let is_filled = total_height > Pixels::ZERO && total_height <= actual_height;
1314
1315 let loading_view = if loading {
1316 Some(
1317 self.delegate
1318 .render_loading(self.options.size, window, cx)
1319 .into_any_element(),
1320 )
1321 } else {
1322 None
1323 };
1324
1325 let empty_view = if rows_count == 0 {
1326 Some(
1327 div()
1328 .size_full()
1329 .child(self.delegate.render_empty(window, cx))
1330 .into_any_element(),
1331 )
1332 } else {
1333 None
1334 };
1335
1336 let inner_table = v_flex()
1337 .id("table-inner")
1338 .size_full()
1339 .overflow_hidden()
1340 .child(self.render_table_head(left_columns_count, window, cx))
1341 .context_menu({
1342 let view = cx.entity().clone();
1343 move |this, window: &mut Window, cx: &mut Context<PopupMenu>| {
1344 if let Some(row_ix) = view.read(cx).right_clicked_row {
1345 view.update(cx, |menu, cx| {
1346 menu.delegate().context_menu(row_ix, this, window, cx)
1347 })
1348 } else {
1349 this
1350 }
1351 }
1352 })
1353 .map(|this| {
1354 if rows_count == 0 {
1355 this.children(empty_view)
1356 } else {
1357 this.child(
1358 h_flex().id("table-body").flex_grow().size_full().child(
1359 uniform_list(
1360 "table-uniform-list",
1361 render_rows_count,
1362 cx.processor(
1363 move |table, visible_range: Range<usize>, window, cx| {
1364 let col_sizes: Rc<Vec<gpui::Size<Pixels>>> = Rc::new(
1367 table
1368 .col_groups
1369 .iter()
1370 .skip(left_columns_count)
1371 .map(|col| col.bounds.size)
1372 .collect(),
1373 );
1374
1375 table.load_more_if_need(
1376 rows_count,
1377 visible_range.end,
1378 window,
1379 cx,
1380 );
1381 table.update_visible_range_if_need(
1382 visible_range.clone(),
1383 Axis::Vertical,
1384 window,
1385 cx,
1386 );
1387
1388 if visible_range.end > rows_count {
1389 table.scroll_to_row(
1390 std::cmp::min(
1391 visible_range.start,
1392 rows_count.saturating_sub(1),
1393 ),
1394 cx,
1395 );
1396 }
1397
1398 let mut items = Vec::with_capacity(
1399 visible_range.end.saturating_sub(visible_range.start),
1400 );
1401
1402 visible_range.for_each(|row_ix| {
1404 items.push(table.render_table_row(
1406 row_ix,
1407 rows_count,
1408 left_columns_count,
1409 col_sizes.clone(),
1410 columns_count,
1411 is_filled,
1412 window,
1413 cx,
1414 ));
1415 });
1416
1417 items
1418 },
1419 ),
1420 )
1421 .flex_grow()
1422 .size_full()
1423 .with_sizing_behavior(ListSizingBehavior::Auto)
1424 .track_scroll(self.vertical_scroll_handle.clone())
1425 .into_any_element(),
1426 ),
1427 )
1428 }
1429 });
1430
1431 div()
1432 .size_full()
1433 .children(loading_view)
1434 .when(!loading, |this| {
1435 this.child(inner_table)
1436 .child(ScrollableMask::new(
1437 Axis::Horizontal,
1438 &self.horizontal_scroll_handle,
1439 ))
1440 .when(right_clicked_row.is_some(), |this| {
1441 this.on_mouse_down_out(cx.listener(|this, _, _, cx| {
1442 this.right_clicked_row = None;
1443 cx.notify();
1444 }))
1445 })
1446 })
1447 .child(canvas(
1448 {
1449 let state = cx.entity();
1450 move |bounds, _, cx| state.update(cx, |state, _| state.bounds = bounds)
1451 },
1452 |_, _, _, _| {},
1453 ))
1454 .when(!window.is_inspector_picking(cx), |this| {
1455 this.child(
1456 div()
1457 .absolute()
1458 .top_0()
1459 .size_full()
1460 .when(self.options.scrollbar_visible.bottom, |this| {
1461 this.child(self.render_horizontal_scrollbar(window, cx))
1462 })
1463 .when(
1464 self.options.scrollbar_visible.right && rows_count > 0,
1465 |this| this.children(self.render_vertical_scrollbar(window, cx)),
1466 ),
1467 )
1468 })
1469 }
1470}
1471
1472#[derive(IntoElement)]
1474pub struct Table<D: TableDelegate> {
1475 state: Entity<TableState<D>>,
1476 options: TableOptions,
1477}
1478
1479impl<D> Table<D>
1480where
1481 D: TableDelegate,
1482{
1483 pub fn new(state: &Entity<TableState<D>>) -> Self {
1485 Self {
1486 state: state.clone(),
1487 options: TableOptions::default(),
1488 }
1489 }
1490
1491 pub fn stripe(mut self, stripe: bool) -> Self {
1493 self.options.stripe = stripe;
1494 self
1495 }
1496
1497 pub fn bordered(mut self, bordered: bool) -> Self {
1499 self.options.bordered = bordered;
1500 self
1501 }
1502
1503 pub fn scrollbar_visible(mut self, vertical: bool, horizontal: bool) -> Self {
1505 self.options.scrollbar_visible = Edges {
1506 right: vertical,
1507 bottom: horizontal,
1508 ..Default::default()
1509 };
1510 self
1511 }
1512}
1513
1514impl<D> Sizable for Table<D>
1515where
1516 D: TableDelegate,
1517{
1518 fn with_size(mut self, size: impl Into<Size>) -> Self {
1519 self.options.size = size.into();
1520 self
1521 }
1522}
1523
1524impl<D> RenderOnce for Table<D>
1525where
1526 D: TableDelegate,
1527{
1528 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
1529 let bordered = self.options.bordered;
1530 let focus_handle = self.state.focus_handle(cx);
1531 self.state.update(cx, |state, _| {
1532 state.options = self.options;
1533 });
1534
1535 div()
1536 .id("table")
1537 .size_full()
1538 .key_context(CONTEXT)
1539 .track_focus(&focus_handle)
1540 .on_action(window.listener_for(&self.state, TableState::action_cancel))
1541 .on_action(window.listener_for(&self.state, TableState::action_select_next))
1542 .on_action(window.listener_for(&self.state, TableState::action_select_prev))
1543 .on_action(window.listener_for(&self.state, TableState::action_select_next_col))
1544 .on_action(window.listener_for(&self.state, TableState::action_select_prev_col))
1545 .bg(cx.theme().table)
1546 .when(bordered, |this| {
1547 this.rounded(cx.theme().radius)
1548 .border_1()
1549 .border_color(cx.theme().border)
1550 })
1551 .child(self.state)
1552 }
1553}