1use std::{ops::Range, rc::Rc, time::Duration};
2
3use editor::{EditorSettings, ShowScrollbar, scroll::ScrollbarAutoHide};
4use gpui::{
5 AbsoluteLength, AppContext, Axis, Context, DefiniteLength, DragMoveEvent, Entity, FocusHandle,
6 Length, ListHorizontalSizingBehavior, ListSizingBehavior, MouseButton, Point, Stateful, Task,
7 UniformListScrollHandle, WeakEntity, transparent_black, uniform_list,
8};
9
10use itertools::intersperse_with;
11use settings::Settings as _;
12use ui::{
13 ActiveTheme as _, AnyElement, App, Button, ButtonCommon as _, ButtonStyle, Color, Component,
14 ComponentScope, Div, ElementId, FixedWidth as _, FluentBuilder as _, Indicator,
15 InteractiveElement, IntoElement, ParentElement, Pixels, RegisterComponent, RenderOnce,
16 Scrollbar, ScrollbarState, StatefulInteractiveElement, Styled, StyledExt as _,
17 StyledTypography, Window, div, example_group_with_title, h_flex, px, single_example, v_flex,
18};
19
20#[derive(Debug)]
21struct DraggedColumn(usize);
22
23struct UniformListData<const COLS: usize> {
24 render_item_fn: Box<dyn Fn(Range<usize>, &mut Window, &mut App) -> Vec<[AnyElement; COLS]>>,
25 element_id: ElementId,
26 row_count: usize,
27}
28
29enum TableContents<const COLS: usize> {
30 Vec(Vec<[AnyElement; COLS]>),
31 UniformList(UniformListData<COLS>),
32}
33
34impl<const COLS: usize> TableContents<COLS> {
35 fn rows_mut(&mut self) -> Option<&mut Vec<[AnyElement; COLS]>> {
36 match self {
37 TableContents::Vec(rows) => Some(rows),
38 TableContents::UniformList(_) => None,
39 }
40 }
41
42 fn len(&self) -> usize {
43 match self {
44 TableContents::Vec(rows) => rows.len(),
45 TableContents::UniformList(data) => data.row_count,
46 }
47 }
48
49 fn is_empty(&self) -> bool {
50 self.len() == 0
51 }
52}
53
54pub struct TableInteractionState {
55 pub focus_handle: FocusHandle,
56 pub scroll_handle: UniformListScrollHandle,
57 pub horizontal_scrollbar: ScrollbarProperties,
58 pub vertical_scrollbar: ScrollbarProperties,
59}
60
61impl TableInteractionState {
62 pub fn new(window: &mut Window, cx: &mut App) -> Entity<Self> {
63 cx.new(|cx| {
64 let focus_handle = cx.focus_handle();
65
66 cx.on_focus_out(&focus_handle, window, |this: &mut Self, _, window, cx| {
67 this.hide_scrollbars(window, cx);
68 })
69 .detach();
70
71 let scroll_handle = UniformListScrollHandle::new();
72 let vertical_scrollbar = ScrollbarProperties {
73 axis: Axis::Vertical,
74 state: ScrollbarState::new(scroll_handle.clone()).parent_entity(&cx.entity()),
75 show_scrollbar: false,
76 show_track: false,
77 auto_hide: false,
78 hide_task: None,
79 };
80
81 let horizontal_scrollbar = ScrollbarProperties {
82 axis: Axis::Horizontal,
83 state: ScrollbarState::new(scroll_handle.clone()).parent_entity(&cx.entity()),
84 show_scrollbar: false,
85 show_track: false,
86 auto_hide: false,
87 hide_task: None,
88 };
89
90 let mut this = Self {
91 focus_handle,
92 scroll_handle,
93 horizontal_scrollbar,
94 vertical_scrollbar,
95 };
96
97 this.update_scrollbar_visibility(cx);
98 this
99 })
100 }
101
102 pub fn get_scrollbar_offset(&self, axis: Axis) -> Point<Pixels> {
103 match axis {
104 Axis::Vertical => self.vertical_scrollbar.state.scroll_handle().offset(),
105 Axis::Horizontal => self.horizontal_scrollbar.state.scroll_handle().offset(),
106 }
107 }
108
109 pub fn set_scrollbar_offset(&self, axis: Axis, offset: Point<Pixels>) {
110 match axis {
111 Axis::Vertical => self
112 .vertical_scrollbar
113 .state
114 .scroll_handle()
115 .set_offset(offset),
116 Axis::Horizontal => self
117 .horizontal_scrollbar
118 .state
119 .scroll_handle()
120 .set_offset(offset),
121 }
122 }
123
124 fn update_scrollbar_visibility(&mut self, cx: &mut Context<Self>) {
125 let show_setting = EditorSettings::get_global(cx).scrollbar.show;
126
127 let scroll_handle = self.scroll_handle.0.borrow();
128
129 let autohide = |show: ShowScrollbar, cx: &mut Context<Self>| match show {
130 ShowScrollbar::Auto => true,
131 ShowScrollbar::System => cx
132 .try_global::<ScrollbarAutoHide>()
133 .map_or_else(|| cx.should_auto_hide_scrollbars(), |autohide| autohide.0),
134 ShowScrollbar::Always => false,
135 ShowScrollbar::Never => false,
136 };
137
138 let longest_item_width = scroll_handle.last_item_size.and_then(|size| {
139 (size.contents.width > size.item.width).then_some(size.contents.width)
140 });
141
142 // is there an item long enough that we should show a horizontal scrollbar?
143 let item_wider_than_container = if let Some(longest_item_width) = longest_item_width {
144 longest_item_width > px(scroll_handle.base_handle.bounds().size.width.0)
145 } else {
146 true
147 };
148
149 let show_scrollbar = match show_setting {
150 ShowScrollbar::Auto | ShowScrollbar::System | ShowScrollbar::Always => true,
151 ShowScrollbar::Never => false,
152 };
153 let show_vertical = show_scrollbar;
154
155 let show_horizontal = item_wider_than_container && show_scrollbar;
156
157 let show_horizontal_track =
158 show_horizontal && matches!(show_setting, ShowScrollbar::Always);
159
160 // TODO: we probably should hide the scroll track when the list doesn't need to scroll
161 let show_vertical_track = show_vertical && matches!(show_setting, ShowScrollbar::Always);
162
163 self.vertical_scrollbar = ScrollbarProperties {
164 axis: self.vertical_scrollbar.axis,
165 state: self.vertical_scrollbar.state.clone(),
166 show_scrollbar: show_vertical,
167 show_track: show_vertical_track,
168 auto_hide: autohide(show_setting, cx),
169 hide_task: None,
170 };
171
172 self.horizontal_scrollbar = ScrollbarProperties {
173 axis: self.horizontal_scrollbar.axis,
174 state: self.horizontal_scrollbar.state.clone(),
175 show_scrollbar: show_horizontal,
176 show_track: show_horizontal_track,
177 auto_hide: autohide(show_setting, cx),
178 hide_task: None,
179 };
180
181 cx.notify();
182 }
183
184 fn hide_scrollbars(&mut self, window: &mut Window, cx: &mut Context<Self>) {
185 self.horizontal_scrollbar.hide(window, cx);
186 self.vertical_scrollbar.hide(window, cx);
187 }
188
189 pub fn listener<E: ?Sized>(
190 this: &Entity<Self>,
191 f: impl Fn(&mut Self, &E, &mut Window, &mut Context<Self>) + 'static,
192 ) -> impl Fn(&E, &mut Window, &mut App) + 'static {
193 let view = this.downgrade();
194 move |e: &E, window: &mut Window, cx: &mut App| {
195 view.update(cx, |view, cx| f(view, e, window, cx)).ok();
196 }
197 }
198
199 fn render_resize_handles<const COLS: usize>(
200 &self,
201 column_widths: &[Length; COLS],
202 resizable_columns: &[ResizeBehavior; COLS],
203 initial_sizes: [DefiniteLength; COLS],
204 columns: Option<Entity<ColumnWidths<COLS>>>,
205 window: &mut Window,
206 cx: &mut App,
207 ) -> AnyElement {
208 let spacers = column_widths
209 .iter()
210 .map(|width| base_cell_style(Some(*width)).into_any_element());
211
212 let mut column_ix = 0;
213 let resizable_columns_slice = *resizable_columns;
214 let mut resizable_columns = resizable_columns.into_iter();
215 let dividers = intersperse_with(spacers, || {
216 window.with_id(column_ix, |window| {
217 let mut resize_divider = div()
218 // This is required because this is evaluated at a different time than the use_state call above
219 .id(column_ix)
220 .relative()
221 .top_0()
222 .w_0p5()
223 .h_full()
224 .bg(cx.theme().colors().border.opacity(0.5));
225
226 let mut resize_handle = div()
227 .id("column-resize-handle")
228 .absolute()
229 .left_neg_0p5()
230 .w(px(5.0))
231 .h_full();
232
233 if resizable_columns
234 .next()
235 .is_some_and(ResizeBehavior::is_resizable)
236 {
237 let hovered = window.use_state(cx, |_window, _cx| false);
238 resize_divider = resize_divider.when(*hovered.read(cx), |div| {
239 div.bg(cx.theme().colors().border_focused)
240 });
241 resize_handle = resize_handle
242 .on_hover(move |&was_hovered, _, cx| hovered.write(cx, was_hovered))
243 .cursor_col_resize()
244 .when_some(columns.clone(), |this, columns| {
245 this.on_click(move |event, window, cx| {
246 if event.down.click_count >= 2 {
247 columns.update(cx, |columns, _| {
248 columns.on_double_click(
249 column_ix,
250 &initial_sizes,
251 &resizable_columns_slice,
252 window,
253 );
254 })
255 }
256
257 cx.stop_propagation();
258 })
259 })
260 .on_drag(DraggedColumn(column_ix), |_, _offset, _window, cx| {
261 cx.new(|_cx| gpui::Empty)
262 })
263 }
264
265 column_ix += 1;
266 resize_divider.child(resize_handle).into_any_element()
267 })
268 });
269
270 div()
271 .id("resize-handles")
272 .h_flex()
273 .absolute()
274 .w_full()
275 .inset_0()
276 .children(dividers)
277 .into_any_element()
278 }
279
280 fn render_vertical_scrollbar_track(
281 this: &Entity<Self>,
282 parent: Div,
283 scroll_track_size: Pixels,
284 cx: &mut App,
285 ) -> Div {
286 if !this.read(cx).vertical_scrollbar.show_track {
287 return parent;
288 }
289 let child = v_flex()
290 .h_full()
291 .flex_none()
292 .w(scroll_track_size)
293 .bg(cx.theme().colors().background)
294 .child(
295 div()
296 .size_full()
297 .flex_1()
298 .border_l_1()
299 .border_color(cx.theme().colors().border),
300 );
301 parent.child(child)
302 }
303
304 fn render_vertical_scrollbar(this: &Entity<Self>, parent: Div, cx: &mut App) -> Div {
305 if !this.read(cx).vertical_scrollbar.show_scrollbar {
306 return parent;
307 }
308 let child = div()
309 .id(("table-vertical-scrollbar", this.entity_id()))
310 .occlude()
311 .flex_none()
312 .h_full()
313 .cursor_default()
314 .absolute()
315 .right_0()
316 .top_0()
317 .bottom_0()
318 .w(px(12.))
319 .on_mouse_move(Self::listener(this, |_, _, _, cx| {
320 cx.notify();
321 cx.stop_propagation()
322 }))
323 .on_hover(|_, _, cx| {
324 cx.stop_propagation();
325 })
326 .on_mouse_up(
327 MouseButton::Left,
328 Self::listener(this, |this, _, window, cx| {
329 if !this.vertical_scrollbar.state.is_dragging()
330 && !this.focus_handle.contains_focused(window, cx)
331 {
332 this.vertical_scrollbar.hide(window, cx);
333 cx.notify();
334 }
335
336 cx.stop_propagation();
337 }),
338 )
339 .on_any_mouse_down(|_, _, cx| {
340 cx.stop_propagation();
341 })
342 .on_scroll_wheel(Self::listener(&this, |_, _, _, cx| {
343 cx.notify();
344 }))
345 .children(Scrollbar::vertical(
346 this.read(cx).vertical_scrollbar.state.clone(),
347 ));
348 parent.child(child)
349 }
350
351 /// Renders the horizontal scrollbar.
352 ///
353 /// The right offset is used to determine how far to the right the
354 /// scrollbar should extend to, useful for ensuring it doesn't collide
355 /// with the vertical scrollbar when visible.
356 fn render_horizontal_scrollbar(
357 this: &Entity<Self>,
358 parent: Div,
359 right_offset: Pixels,
360 cx: &mut App,
361 ) -> Div {
362 if !this.read(cx).horizontal_scrollbar.show_scrollbar {
363 return parent;
364 }
365 let child = div()
366 .id(("table-horizontal-scrollbar", this.entity_id()))
367 .occlude()
368 .flex_none()
369 .w_full()
370 .cursor_default()
371 .absolute()
372 .bottom_neg_px()
373 .left_0()
374 .right_0()
375 .pr(right_offset)
376 .on_mouse_move(Self::listener(this, |_, _, _, cx| {
377 cx.notify();
378 cx.stop_propagation()
379 }))
380 .on_hover(|_, _, cx| {
381 cx.stop_propagation();
382 })
383 .on_any_mouse_down(|_, _, cx| {
384 cx.stop_propagation();
385 })
386 .on_mouse_up(
387 MouseButton::Left,
388 Self::listener(this, |this, _, window, cx| {
389 if !this.horizontal_scrollbar.state.is_dragging()
390 && !this.focus_handle.contains_focused(window, cx)
391 {
392 this.horizontal_scrollbar.hide(window, cx);
393 cx.notify();
394 }
395
396 cx.stop_propagation();
397 }),
398 )
399 .on_scroll_wheel(Self::listener(this, |_, _, _, cx| {
400 cx.notify();
401 }))
402 .children(Scrollbar::horizontal(
403 // percentage as f32..end_offset as f32,
404 this.read(cx).horizontal_scrollbar.state.clone(),
405 ));
406 parent.child(child)
407 }
408
409 fn render_horizontal_scrollbar_track(
410 this: &Entity<Self>,
411 parent: Div,
412 scroll_track_size: Pixels,
413 cx: &mut App,
414 ) -> Div {
415 if !this.read(cx).horizontal_scrollbar.show_track {
416 return parent;
417 }
418 let child = h_flex()
419 .w_full()
420 .h(scroll_track_size)
421 .flex_none()
422 .relative()
423 .child(
424 div()
425 .w_full()
426 .flex_1()
427 // for some reason the horizontal scrollbar is 1px
428 // taller than the vertical scrollbar??
429 .h(scroll_track_size - px(1.))
430 .bg(cx.theme().colors().background)
431 .border_t_1()
432 .border_color(cx.theme().colors().border),
433 )
434 .when(this.read(cx).vertical_scrollbar.show_track, |parent| {
435 parent
436 .child(
437 div()
438 .flex_none()
439 // -1px prevents a missing pixel between the two container borders
440 .w(scroll_track_size - px(1.))
441 .h_full(),
442 )
443 .child(
444 // HACK: Fill the missing 1px 🥲
445 div()
446 .absolute()
447 .right(scroll_track_size - px(1.))
448 .bottom(scroll_track_size - px(1.))
449 .size_px()
450 .bg(cx.theme().colors().border),
451 )
452 });
453
454 parent.child(child)
455 }
456}
457
458#[derive(Debug, Copy, Clone, PartialEq)]
459pub enum ResizeBehavior {
460 None,
461 Resizable,
462 MinSize(f32),
463}
464
465impl ResizeBehavior {
466 pub fn is_resizable(&self) -> bool {
467 *self != ResizeBehavior::None
468 }
469
470 pub fn min_size(&self) -> Option<f32> {
471 match self {
472 ResizeBehavior::None => None,
473 ResizeBehavior::Resizable => Some(0.05),
474 ResizeBehavior::MinSize(min_size) => Some(*min_size),
475 }
476 }
477}
478
479pub struct ColumnWidths<const COLS: usize> {
480 widths: [DefiniteLength; COLS],
481 cached_bounds_width: Pixels,
482 initialized: bool,
483}
484
485impl<const COLS: usize> ColumnWidths<COLS> {
486 pub fn new(_: &mut App) -> Self {
487 Self {
488 widths: [DefiniteLength::default(); COLS],
489 cached_bounds_width: Default::default(),
490 initialized: false,
491 }
492 }
493
494 fn get_fraction(length: &DefiniteLength, bounds_width: Pixels, rem_size: Pixels) -> f32 {
495 match length {
496 DefiniteLength::Absolute(AbsoluteLength::Pixels(pixels)) => *pixels / bounds_width,
497 DefiniteLength::Absolute(AbsoluteLength::Rems(rems_width)) => {
498 rems_width.to_pixels(rem_size) / bounds_width
499 }
500 DefiniteLength::Fraction(fraction) => *fraction,
501 }
502 }
503
504 fn on_double_click(
505 &mut self,
506 double_click_position: usize,
507 initial_sizes: &[DefiniteLength; COLS],
508 resize_behavior: &[ResizeBehavior; COLS],
509 window: &mut Window,
510 ) {
511 let bounds_width = self.cached_bounds_width;
512 let rem_size = window.rem_size();
513
514 let diff =
515 Self::get_fraction(
516 &initial_sizes[double_click_position],
517 bounds_width,
518 rem_size,
519 ) - Self::get_fraction(&self.widths[double_click_position], bounds_width, rem_size);
520
521 let mut curr_column = double_click_position + 1;
522 let mut diff_left = diff;
523
524 while diff_left != 0.0 && curr_column < COLS {
525 let Some(min_size) = resize_behavior[curr_column].min_size() else {
526 curr_column += 1;
527 continue;
528 };
529
530 let mut curr_width =
531 Self::get_fraction(&self.widths[curr_column], bounds_width, rem_size) - diff_left;
532
533 diff_left = 0.0;
534 if min_size > curr_width {
535 diff_left += min_size - curr_width;
536 curr_width = min_size;
537 }
538 self.widths[curr_column] = DefiniteLength::Fraction(curr_width);
539 curr_column += 1;
540 }
541
542 self.widths[double_click_position] = DefiniteLength::Fraction(
543 Self::get_fraction(&self.widths[double_click_position], bounds_width, rem_size)
544 + (diff - diff_left),
545 );
546 }
547
548 fn on_drag_move(
549 &mut self,
550 drag_event: &DragMoveEvent<DraggedColumn>,
551 resize_behavior: &[ResizeBehavior; COLS],
552 window: &mut Window,
553 cx: &mut Context<Self>,
554 ) {
555 // - [ ] Fix bugs in resize
556 let drag_position = drag_event.event.position;
557 let bounds = drag_event.bounds;
558
559 let mut col_position = 0.0;
560 let rem_size = window.rem_size();
561 let bounds_width = bounds.right() - bounds.left();
562 let col_idx = drag_event.drag(cx).0;
563
564 for length in self.widths[0..=col_idx].iter() {
565 col_position += Self::get_fraction(length, bounds_width, rem_size);
566 }
567
568 let mut total_length_ratio = col_position;
569 for length in self.widths[col_idx + 1..].iter() {
570 total_length_ratio += Self::get_fraction(length, bounds_width, rem_size);
571 }
572
573 let drag_fraction = (drag_position.x - bounds.left()) / bounds_width;
574 let drag_fraction = drag_fraction * total_length_ratio;
575 let diff = drag_fraction - col_position;
576
577 let is_dragging_right = diff > 0.0;
578
579 let mut diff_left = diff;
580 let mut curr_column = col_idx + 1;
581
582 if is_dragging_right {
583 while diff_left > 0.0 && curr_column < COLS {
584 let Some(min_size) = resize_behavior[curr_column - 1].min_size() else {
585 curr_column += 1;
586 continue;
587 };
588
589 let mut curr_width =
590 Self::get_fraction(&self.widths[curr_column], bounds_width, rem_size)
591 - diff_left;
592
593 diff_left = 0.0;
594 if min_size > curr_width {
595 diff_left += min_size - curr_width;
596 curr_width = min_size;
597 }
598 self.widths[curr_column] = DefiniteLength::Fraction(curr_width);
599 curr_column += 1;
600 }
601
602 self.widths[col_idx] = DefiniteLength::Fraction(
603 Self::get_fraction(&self.widths[col_idx], bounds_width, rem_size)
604 + (diff - diff_left),
605 );
606 } else {
607 curr_column = col_idx;
608 // Resize behavior should be improved in the future by also seeking to the right column when there's not enough space
609 while diff_left < 0.0 {
610 let Some(min_size) = resize_behavior[curr_column].min_size() else {
611 if curr_column == 0 {
612 break;
613 }
614 curr_column -= 1;
615 continue;
616 };
617
618 let mut curr_width =
619 Self::get_fraction(&self.widths[curr_column], bounds_width, rem_size)
620 + diff_left;
621
622 diff_left = 0.0;
623 if curr_width < min_size {
624 diff_left = curr_width - min_size;
625 curr_width = min_size
626 }
627
628 self.widths[curr_column] = DefiniteLength::Fraction(curr_width);
629 if curr_column == 0 {
630 break;
631 }
632 curr_column -= 1;
633 }
634
635 self.widths[col_idx + 1] = DefiniteLength::Fraction(
636 Self::get_fraction(&self.widths[col_idx + 1], bounds_width, rem_size)
637 - (diff - diff_left),
638 );
639 }
640 }
641}
642
643pub struct TableWidths<const COLS: usize> {
644 initial: [DefiniteLength; COLS],
645 current: Option<Entity<ColumnWidths<COLS>>>,
646 resizable: [ResizeBehavior; COLS],
647}
648
649impl<const COLS: usize> TableWidths<COLS> {
650 pub fn new(widths: [impl Into<DefiniteLength>; COLS]) -> Self {
651 let widths = widths.map(Into::into);
652
653 TableWidths {
654 initial: widths,
655 current: None,
656 resizable: [ResizeBehavior::None; COLS],
657 }
658 }
659
660 fn lengths(&self, cx: &App) -> [Length; COLS] {
661 self.current
662 .as_ref()
663 .map(|entity| entity.read(cx).widths.map(Length::Definite))
664 .unwrap_or(self.initial.map(Length::Definite))
665 }
666}
667
668/// A table component
669#[derive(RegisterComponent, IntoElement)]
670pub struct Table<const COLS: usize = 3> {
671 striped: bool,
672 width: Option<Length>,
673 headers: Option<[AnyElement; COLS]>,
674 rows: TableContents<COLS>,
675 interaction_state: Option<WeakEntity<TableInteractionState>>,
676 col_widths: Option<TableWidths<COLS>>,
677 map_row: Option<Rc<dyn Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement>>,
678 empty_table_callback: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>>,
679}
680
681impl<const COLS: usize> Table<COLS> {
682 /// number of headers provided.
683 pub fn new() -> Self {
684 Self {
685 striped: false,
686 width: None,
687 headers: None,
688 rows: TableContents::Vec(Vec::new()),
689 interaction_state: None,
690 map_row: None,
691 empty_table_callback: None,
692 col_widths: None,
693 }
694 }
695
696 /// Enables uniform list rendering.
697 /// The provided function will be passed directly to the `uniform_list` element.
698 /// Therefore, if this method is called, any calls to [`Table::row`] before or after
699 /// this method is called will be ignored.
700 pub fn uniform_list(
701 mut self,
702 id: impl Into<ElementId>,
703 row_count: usize,
704 render_item_fn: impl Fn(Range<usize>, &mut Window, &mut App) -> Vec<[AnyElement; COLS]>
705 + 'static,
706 ) -> Self {
707 self.rows = TableContents::UniformList(UniformListData {
708 element_id: id.into(),
709 row_count: row_count,
710 render_item_fn: Box::new(render_item_fn),
711 });
712 self
713 }
714
715 /// Enables row striping.
716 pub fn striped(mut self) -> Self {
717 self.striped = true;
718 self
719 }
720
721 /// Sets the width of the table.
722 /// Will enable horizontal scrolling if [`Self::interactable`] is also called.
723 pub fn width(mut self, width: impl Into<Length>) -> Self {
724 self.width = Some(width.into());
725 self
726 }
727
728 /// Enables interaction (primarily scrolling) with the table.
729 ///
730 /// Vertical scrolling will be enabled by default if the table is taller than its container.
731 ///
732 /// Horizontal scrolling will only be enabled if [`Self::width`] is also called, otherwise
733 /// the list will always shrink the table columns to fit their contents I.e. If [`Self::uniform_list`]
734 /// is used without a width and with [`Self::interactable`], the [`ListHorizontalSizingBehavior`] will
735 /// be set to [`ListHorizontalSizingBehavior::FitList`].
736 pub fn interactable(mut self, interaction_state: &Entity<TableInteractionState>) -> Self {
737 self.interaction_state = Some(interaction_state.downgrade());
738 self
739 }
740
741 pub fn header(mut self, headers: [impl IntoElement; COLS]) -> Self {
742 self.headers = Some(headers.map(IntoElement::into_any_element));
743 self
744 }
745
746 pub fn row(mut self, items: [impl IntoElement; COLS]) -> Self {
747 if let Some(rows) = self.rows.rows_mut() {
748 rows.push(items.map(IntoElement::into_any_element));
749 }
750 self
751 }
752
753 pub fn column_widths(mut self, widths: [impl Into<DefiniteLength>; COLS]) -> Self {
754 if self.col_widths.is_none() {
755 self.col_widths = Some(TableWidths::new(widths));
756 }
757 self
758 }
759
760 pub fn resizable_columns(
761 mut self,
762 resizable: [ResizeBehavior; COLS],
763 column_widths: &Entity<ColumnWidths<COLS>>,
764 cx: &mut App,
765 ) -> Self {
766 if let Some(table_widths) = self.col_widths.as_mut() {
767 table_widths.resizable = resizable;
768 let column_widths = table_widths
769 .current
770 .get_or_insert_with(|| column_widths.clone());
771
772 column_widths.update(cx, |widths, _| {
773 if !widths.initialized {
774 widths.initialized = true;
775 widths.widths = table_widths.initial;
776 }
777 })
778 }
779 self
780 }
781
782 pub fn map_row(
783 mut self,
784 callback: impl Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement + 'static,
785 ) -> Self {
786 self.map_row = Some(Rc::new(callback));
787 self
788 }
789
790 /// Provide a callback that is invoked when the table is rendered without any rows
791 pub fn empty_table_callback(
792 mut self,
793 callback: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
794 ) -> Self {
795 self.empty_table_callback = Some(Rc::new(callback));
796 self
797 }
798}
799
800fn base_cell_style(width: Option<Length>) -> Div {
801 div()
802 .px_1p5()
803 .when_some(width, |this, width| this.w(width))
804 .when(width.is_none(), |this| this.flex_1())
805 .justify_start()
806 .whitespace_nowrap()
807 .text_ellipsis()
808 .overflow_hidden()
809}
810
811fn base_cell_style_text(width: Option<Length>, cx: &App) -> Div {
812 base_cell_style(width).text_ui(cx)
813}
814
815pub fn render_row<const COLS: usize>(
816 row_index: usize,
817 items: [impl IntoElement; COLS],
818 table_context: TableRenderContext<COLS>,
819 window: &mut Window,
820 cx: &mut App,
821) -> AnyElement {
822 let is_striped = table_context.striped;
823 let is_last = row_index == table_context.total_row_count - 1;
824 let bg = if row_index % 2 == 1 && is_striped {
825 Some(cx.theme().colors().text.opacity(0.05))
826 } else {
827 None
828 };
829 let column_widths = table_context
830 .column_widths
831 .map_or([None; COLS], |widths| widths.map(Some));
832
833 let mut row = h_flex()
834 .h_full()
835 .id(("table_row", row_index))
836 .w_full()
837 .justify_between()
838 .when_some(bg, |row, bg| row.bg(bg))
839 .when(!is_striped, |row| {
840 row.border_b_1()
841 .border_color(transparent_black())
842 .when(!is_last, |row| row.border_color(cx.theme().colors().border))
843 });
844
845 row = row.children(
846 items
847 .map(IntoElement::into_any_element)
848 .into_iter()
849 .zip(column_widths)
850 .map(|(cell, width)| base_cell_style_text(width, cx).px_1p5().py_1().child(cell)),
851 );
852
853 let row = if let Some(map_row) = table_context.map_row {
854 map_row((row_index, row), window, cx)
855 } else {
856 row.into_any_element()
857 };
858
859 div().h_full().w_full().child(row).into_any_element()
860}
861
862pub fn render_header<const COLS: usize>(
863 headers: [impl IntoElement; COLS],
864 table_context: TableRenderContext<COLS>,
865 cx: &mut App,
866) -> impl IntoElement {
867 let column_widths = table_context
868 .column_widths
869 .map_or([None; COLS], |widths| widths.map(Some));
870 div()
871 .flex()
872 .flex_row()
873 .items_center()
874 .justify_between()
875 .w_full()
876 .p_2()
877 .border_b_1()
878 .border_color(cx.theme().colors().border)
879 .children(
880 headers
881 .into_iter()
882 .zip(column_widths)
883 .map(|(h, width)| base_cell_style_text(width, cx).child(h)),
884 )
885}
886
887#[derive(Clone)]
888pub struct TableRenderContext<const COLS: usize> {
889 pub striped: bool,
890 pub total_row_count: usize,
891 pub column_widths: Option<[Length; COLS]>,
892 pub map_row: Option<Rc<dyn Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement>>,
893}
894
895impl<const COLS: usize> TableRenderContext<COLS> {
896 fn new(table: &Table<COLS>, cx: &App) -> Self {
897 Self {
898 striped: table.striped,
899 total_row_count: table.rows.len(),
900 column_widths: table.col_widths.as_ref().map(|widths| widths.lengths(cx)),
901 map_row: table.map_row.clone(),
902 }
903 }
904}
905
906impl<const COLS: usize> RenderOnce for Table<COLS> {
907 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
908 let table_context = TableRenderContext::new(&self, cx);
909 let interaction_state = self.interaction_state.and_then(|state| state.upgrade());
910 let current_widths = self
911 .col_widths
912 .as_ref()
913 .and_then(|widths| Some((widths.current.as_ref()?, widths.resizable)))
914 .map(|(curr, resize_behavior)| (curr.downgrade(), resize_behavior));
915
916 let scroll_track_size = px(16.);
917 let h_scroll_offset = if interaction_state
918 .as_ref()
919 .is_some_and(|state| state.read(cx).vertical_scrollbar.show_scrollbar)
920 {
921 // magic number
922 px(3.)
923 } else {
924 px(0.)
925 };
926
927 let width = self.width;
928 let no_rows_rendered = self.rows.is_empty();
929
930 let table = div()
931 .when_some(width, |this, width| this.w(width))
932 .h_full()
933 .v_flex()
934 .when_some(self.headers.take(), |this, headers| {
935 this.child(render_header(headers, table_context.clone(), cx))
936 })
937 .when_some(current_widths, {
938 |this, (widths, resize_behavior)| {
939 this.on_drag_move::<DraggedColumn>({
940 let widths = widths.clone();
941 move |e, window, cx| {
942 widths
943 .update(cx, |widths, cx| {
944 widths.on_drag_move(e, &resize_behavior, window, cx);
945 })
946 .ok();
947 }
948 })
949 .on_children_prepainted(move |bounds, _, cx| {
950 widths
951 .update(cx, |widths, _| {
952 // This works because all children x axis bounds are the same
953 widths.cached_bounds_width = bounds[0].right() - bounds[0].left();
954 })
955 .ok();
956 })
957 }
958 })
959 .on_drop::<DraggedColumn>(|_, _, _| {
960 // Finish the resize operation
961 })
962 .child(
963 div()
964 .flex_grow()
965 .w_full()
966 .relative()
967 .overflow_hidden()
968 .map(|parent| match self.rows {
969 TableContents::Vec(items) => {
970 parent.children(items.into_iter().enumerate().map(|(index, row)| {
971 render_row(index, row, table_context.clone(), window, cx)
972 }))
973 }
974 TableContents::UniformList(uniform_list_data) => parent.child(
975 uniform_list(
976 uniform_list_data.element_id,
977 uniform_list_data.row_count,
978 {
979 let render_item_fn = uniform_list_data.render_item_fn;
980 move |range: Range<usize>, window, cx| {
981 let elements = render_item_fn(range.clone(), window, cx);
982 elements
983 .into_iter()
984 .zip(range)
985 .map(|(row, row_index)| {
986 render_row(
987 row_index,
988 row,
989 table_context.clone(),
990 window,
991 cx,
992 )
993 })
994 .collect()
995 }
996 },
997 )
998 .size_full()
999 .flex_grow()
1000 .with_sizing_behavior(ListSizingBehavior::Auto)
1001 .with_horizontal_sizing_behavior(if width.is_some() {
1002 ListHorizontalSizingBehavior::Unconstrained
1003 } else {
1004 ListHorizontalSizingBehavior::FitList
1005 })
1006 .when_some(
1007 interaction_state.as_ref(),
1008 |this, state| {
1009 this.track_scroll(
1010 state.read_with(cx, |s, _| s.scroll_handle.clone()),
1011 )
1012 },
1013 ),
1014 ),
1015 })
1016 .when_some(
1017 self.col_widths.as_ref().zip(interaction_state.as_ref()),
1018 |parent, (table_widths, state)| {
1019 parent.child(state.update(cx, |state, cx| {
1020 let resizable_columns = table_widths.resizable;
1021 let column_widths = table_widths.lengths(cx);
1022 let columns = table_widths.current.clone();
1023 let initial_sizes = table_widths.initial;
1024 state.render_resize_handles(
1025 &column_widths,
1026 &resizable_columns,
1027 initial_sizes,
1028 columns,
1029 window,
1030 cx,
1031 )
1032 }))
1033 },
1034 )
1035 .when_some(interaction_state.as_ref(), |this, interaction_state| {
1036 this.map(|this| {
1037 TableInteractionState::render_vertical_scrollbar_track(
1038 interaction_state,
1039 this,
1040 scroll_track_size,
1041 cx,
1042 )
1043 })
1044 .map(|this| {
1045 TableInteractionState::render_vertical_scrollbar(
1046 interaction_state,
1047 this,
1048 cx,
1049 )
1050 })
1051 }),
1052 )
1053 .when_some(
1054 no_rows_rendered
1055 .then_some(self.empty_table_callback)
1056 .flatten(),
1057 |this, callback| {
1058 this.child(
1059 h_flex()
1060 .size_full()
1061 .p_3()
1062 .items_start()
1063 .justify_center()
1064 .child(callback(window, cx)),
1065 )
1066 },
1067 )
1068 .when_some(
1069 width.and(interaction_state.as_ref()),
1070 |this, interaction_state| {
1071 this.map(|this| {
1072 TableInteractionState::render_horizontal_scrollbar_track(
1073 interaction_state,
1074 this,
1075 scroll_track_size,
1076 cx,
1077 )
1078 })
1079 .map(|this| {
1080 TableInteractionState::render_horizontal_scrollbar(
1081 interaction_state,
1082 this,
1083 h_scroll_offset,
1084 cx,
1085 )
1086 })
1087 },
1088 );
1089
1090 if let Some(interaction_state) = interaction_state.as_ref() {
1091 table
1092 .track_focus(&interaction_state.read(cx).focus_handle)
1093 .id(("table", interaction_state.entity_id()))
1094 .on_hover({
1095 let interaction_state = interaction_state.downgrade();
1096 move |hovered, window, cx| {
1097 interaction_state
1098 .update(cx, |interaction_state, cx| {
1099 if *hovered {
1100 interaction_state.horizontal_scrollbar.show(cx);
1101 interaction_state.vertical_scrollbar.show(cx);
1102 cx.notify();
1103 } else if !interaction_state
1104 .focus_handle
1105 .contains_focused(window, cx)
1106 {
1107 interaction_state.hide_scrollbars(window, cx);
1108 }
1109 })
1110 .ok();
1111 }
1112 })
1113 .into_any_element()
1114 } else {
1115 table.into_any_element()
1116 }
1117 }
1118}
1119
1120// computed state related to how to render scrollbars
1121// one per axis
1122// on render we just read this off the keymap editor
1123// we update it when
1124// - settings change
1125// - on focus in, on focus out, on hover, etc.
1126#[derive(Debug)]
1127pub struct ScrollbarProperties {
1128 axis: Axis,
1129 show_scrollbar: bool,
1130 show_track: bool,
1131 auto_hide: bool,
1132 hide_task: Option<Task<()>>,
1133 state: ScrollbarState,
1134}
1135
1136impl ScrollbarProperties {
1137 // Shows the scrollbar and cancels any pending hide task
1138 fn show(&mut self, cx: &mut Context<TableInteractionState>) {
1139 if !self.auto_hide {
1140 return;
1141 }
1142 self.show_scrollbar = true;
1143 self.hide_task.take();
1144 cx.notify();
1145 }
1146
1147 fn hide(&mut self, window: &mut Window, cx: &mut Context<TableInteractionState>) {
1148 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
1149
1150 if !self.auto_hide {
1151 return;
1152 }
1153
1154 let axis = self.axis;
1155 self.hide_task = Some(cx.spawn_in(window, async move |keymap_editor, cx| {
1156 cx.background_executor()
1157 .timer(SCROLLBAR_SHOW_INTERVAL)
1158 .await;
1159
1160 if let Some(keymap_editor) = keymap_editor.upgrade() {
1161 keymap_editor
1162 .update(cx, |keymap_editor, cx| {
1163 match axis {
1164 Axis::Vertical => {
1165 keymap_editor.vertical_scrollbar.show_scrollbar = false
1166 }
1167 Axis::Horizontal => {
1168 keymap_editor.horizontal_scrollbar.show_scrollbar = false
1169 }
1170 }
1171 cx.notify();
1172 })
1173 .ok();
1174 }
1175 }));
1176 }
1177}
1178
1179impl Component for Table<3> {
1180 fn scope() -> ComponentScope {
1181 ComponentScope::Layout
1182 }
1183
1184 fn description() -> Option<&'static str> {
1185 Some("A table component for displaying data in rows and columns with optional styling.")
1186 }
1187
1188 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
1189 Some(
1190 v_flex()
1191 .gap_6()
1192 .children(vec![
1193 example_group_with_title(
1194 "Basic Tables",
1195 vec![
1196 single_example(
1197 "Simple Table",
1198 Table::new()
1199 .width(px(400.))
1200 .header(["Name", "Age", "City"])
1201 .row(["Alice", "28", "New York"])
1202 .row(["Bob", "32", "San Francisco"])
1203 .row(["Charlie", "25", "London"])
1204 .into_any_element(),
1205 ),
1206 single_example(
1207 "Two Column Table",
1208 Table::new()
1209 .header(["Category", "Value"])
1210 .width(px(300.))
1211 .row(["Revenue", "$100,000"])
1212 .row(["Expenses", "$75,000"])
1213 .row(["Profit", "$25,000"])
1214 .into_any_element(),
1215 ),
1216 ],
1217 ),
1218 example_group_with_title(
1219 "Styled Tables",
1220 vec![
1221 single_example(
1222 "Default",
1223 Table::new()
1224 .width(px(400.))
1225 .header(["Product", "Price", "Stock"])
1226 .row(["Laptop", "$999", "In Stock"])
1227 .row(["Phone", "$599", "Low Stock"])
1228 .row(["Tablet", "$399", "Out of Stock"])
1229 .into_any_element(),
1230 ),
1231 single_example(
1232 "Striped",
1233 Table::new()
1234 .width(px(400.))
1235 .striped()
1236 .header(["Product", "Price", "Stock"])
1237 .row(["Laptop", "$999", "In Stock"])
1238 .row(["Phone", "$599", "Low Stock"])
1239 .row(["Tablet", "$399", "Out of Stock"])
1240 .row(["Headphones", "$199", "In Stock"])
1241 .into_any_element(),
1242 ),
1243 ],
1244 ),
1245 example_group_with_title(
1246 "Mixed Content Table",
1247 vec![single_example(
1248 "Table with Elements",
1249 Table::new()
1250 .width(px(840.))
1251 .header(["Status", "Name", "Priority", "Deadline", "Action"])
1252 .row([
1253 Indicator::dot().color(Color::Success).into_any_element(),
1254 "Project A".into_any_element(),
1255 "High".into_any_element(),
1256 "2023-12-31".into_any_element(),
1257 Button::new("view_a", "View")
1258 .style(ButtonStyle::Filled)
1259 .full_width()
1260 .into_any_element(),
1261 ])
1262 .row([
1263 Indicator::dot().color(Color::Warning).into_any_element(),
1264 "Project B".into_any_element(),
1265 "Medium".into_any_element(),
1266 "2024-03-15".into_any_element(),
1267 Button::new("view_b", "View")
1268 .style(ButtonStyle::Filled)
1269 .full_width()
1270 .into_any_element(),
1271 ])
1272 .row([
1273 Indicator::dot().color(Color::Error).into_any_element(),
1274 "Project C".into_any_element(),
1275 "Low".into_any_element(),
1276 "2024-06-30".into_any_element(),
1277 Button::new("view_c", "View")
1278 .style(ButtonStyle::Filled)
1279 .full_width()
1280 .into_any_element(),
1281 ])
1282 .into_any_element(),
1283 )],
1284 ),
1285 ])
1286 .into_any_element(),
1287 )
1288 }
1289}