gpui_component/table/
mod.rs

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