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 != 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.saturating_sub(1)].min_size()
611 else {
612 if curr_column == 0 {
613 break;
614 }
615 curr_column -= 1;
616 continue;
617 };
618
619 let mut curr_width =
620 Self::get_fraction(&self.widths[curr_column], bounds_width, rem_size)
621 + diff_left;
622
623 diff_left = 0.0;
624 if curr_width < min_size {
625 diff_left = curr_width - min_size;
626 curr_width = min_size
627 }
628
629 self.widths[curr_column] = DefiniteLength::Fraction(curr_width);
630 if curr_column == 0 {
631 break;
632 }
633 curr_column -= 1;
634 }
635
636 self.widths[col_idx + 1] = DefiniteLength::Fraction(
637 Self::get_fraction(&self.widths[col_idx + 1], bounds_width, rem_size)
638 - (diff - diff_left),
639 );
640 }
641 }
642}
643
644pub struct TableWidths<const COLS: usize> {
645 initial: [DefiniteLength; COLS],
646 current: Option<Entity<ColumnWidths<COLS>>>,
647 resizable: [ResizeBehavior; COLS],
648}
649
650impl<const COLS: usize> TableWidths<COLS> {
651 pub fn new(widths: [impl Into<DefiniteLength>; COLS]) -> Self {
652 let widths = widths.map(Into::into);
653
654 TableWidths {
655 initial: widths,
656 current: None,
657 resizable: [ResizeBehavior::None; COLS],
658 }
659 }
660
661 fn lengths(&self, cx: &App) -> [Length; COLS] {
662 self.current
663 .as_ref()
664 .map(|entity| entity.read(cx).widths.map(Length::Definite))
665 .unwrap_or(self.initial.map(Length::Definite))
666 }
667}
668
669/// A table component
670#[derive(RegisterComponent, IntoElement)]
671pub struct Table<const COLS: usize = 3> {
672 striped: bool,
673 width: Option<Length>,
674 headers: Option<[AnyElement; COLS]>,
675 rows: TableContents<COLS>,
676 interaction_state: Option<WeakEntity<TableInteractionState>>,
677 col_widths: Option<TableWidths<COLS>>,
678 map_row: Option<Rc<dyn Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement>>,
679 empty_table_callback: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyElement>>,
680}
681
682impl<const COLS: usize> Table<COLS> {
683 /// number of headers provided.
684 pub fn new() -> Self {
685 Self {
686 striped: false,
687 width: None,
688 headers: None,
689 rows: TableContents::Vec(Vec::new()),
690 interaction_state: None,
691 map_row: None,
692 empty_table_callback: None,
693 col_widths: None,
694 }
695 }
696
697 /// Enables uniform list rendering.
698 /// The provided function will be passed directly to the `uniform_list` element.
699 /// Therefore, if this method is called, any calls to [`Table::row`] before or after
700 /// this method is called will be ignored.
701 pub fn uniform_list(
702 mut self,
703 id: impl Into<ElementId>,
704 row_count: usize,
705 render_item_fn: impl Fn(Range<usize>, &mut Window, &mut App) -> Vec<[AnyElement; COLS]>
706 + 'static,
707 ) -> Self {
708 self.rows = TableContents::UniformList(UniformListData {
709 element_id: id.into(),
710 row_count: row_count,
711 render_item_fn: Box::new(render_item_fn),
712 });
713 self
714 }
715
716 /// Enables row striping.
717 pub fn striped(mut self) -> Self {
718 self.striped = true;
719 self
720 }
721
722 /// Sets the width of the table.
723 /// Will enable horizontal scrolling if [`Self::interactable`] is also called.
724 pub fn width(mut self, width: impl Into<Length>) -> Self {
725 self.width = Some(width.into());
726 self
727 }
728
729 /// Enables interaction (primarily scrolling) with the table.
730 ///
731 /// Vertical scrolling will be enabled by default if the table is taller than its container.
732 ///
733 /// Horizontal scrolling will only be enabled if [`Self::width`] is also called, otherwise
734 /// the list will always shrink the table columns to fit their contents I.e. If [`Self::uniform_list`]
735 /// is used without a width and with [`Self::interactable`], the [`ListHorizontalSizingBehavior`] will
736 /// be set to [`ListHorizontalSizingBehavior::FitList`].
737 pub fn interactable(mut self, interaction_state: &Entity<TableInteractionState>) -> Self {
738 self.interaction_state = Some(interaction_state.downgrade());
739 self
740 }
741
742 pub fn header(mut self, headers: [impl IntoElement; COLS]) -> Self {
743 self.headers = Some(headers.map(IntoElement::into_any_element));
744 self
745 }
746
747 pub fn row(mut self, items: [impl IntoElement; COLS]) -> Self {
748 if let Some(rows) = self.rows.rows_mut() {
749 rows.push(items.map(IntoElement::into_any_element));
750 }
751 self
752 }
753
754 pub fn column_widths(mut self, widths: [impl Into<DefiniteLength>; COLS]) -> Self {
755 if self.col_widths.is_none() {
756 self.col_widths = Some(TableWidths::new(widths));
757 }
758 self
759 }
760
761 pub fn resizable_columns(
762 mut self,
763 resizable: [ResizeBehavior; COLS],
764 column_widths: &Entity<ColumnWidths<COLS>>,
765 cx: &mut App,
766 ) -> Self {
767 if let Some(table_widths) = self.col_widths.as_mut() {
768 table_widths.resizable = resizable;
769 let column_widths = table_widths
770 .current
771 .get_or_insert_with(|| column_widths.clone());
772
773 column_widths.update(cx, |widths, _| {
774 if !widths.initialized {
775 widths.initialized = true;
776 widths.widths = table_widths.initial;
777 }
778 })
779 }
780 self
781 }
782
783 pub fn map_row(
784 mut self,
785 callback: impl Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement + 'static,
786 ) -> Self {
787 self.map_row = Some(Rc::new(callback));
788 self
789 }
790
791 /// Provide a callback that is invoked when the table is rendered without any rows
792 pub fn empty_table_callback(
793 mut self,
794 callback: impl Fn(&mut Window, &mut App) -> AnyElement + 'static,
795 ) -> Self {
796 self.empty_table_callback = Some(Rc::new(callback));
797 self
798 }
799}
800
801fn base_cell_style(width: Option<Length>) -> Div {
802 div()
803 .px_1p5()
804 .when_some(width, |this, width| this.w(width))
805 .when(width.is_none(), |this| this.flex_1())
806 .justify_start()
807 .whitespace_nowrap()
808 .text_ellipsis()
809 .overflow_hidden()
810}
811
812fn base_cell_style_text(width: Option<Length>, cx: &App) -> Div {
813 base_cell_style(width).text_ui(cx)
814}
815
816pub fn render_row<const COLS: usize>(
817 row_index: usize,
818 items: [impl IntoElement; COLS],
819 table_context: TableRenderContext<COLS>,
820 window: &mut Window,
821 cx: &mut App,
822) -> AnyElement {
823 let is_striped = table_context.striped;
824 let is_last = row_index == table_context.total_row_count - 1;
825 let bg = if row_index % 2 == 1 && is_striped {
826 Some(cx.theme().colors().text.opacity(0.05))
827 } else {
828 None
829 };
830 let column_widths = table_context
831 .column_widths
832 .map_or([None; COLS], |widths| widths.map(Some));
833
834 let mut row = h_flex()
835 .h_full()
836 .id(("table_row", row_index))
837 .w_full()
838 .justify_between()
839 .when_some(bg, |row, bg| row.bg(bg))
840 .when(!is_striped, |row| {
841 row.border_b_1()
842 .border_color(transparent_black())
843 .when(!is_last, |row| row.border_color(cx.theme().colors().border))
844 });
845
846 row = row.children(
847 items
848 .map(IntoElement::into_any_element)
849 .into_iter()
850 .zip(column_widths)
851 .map(|(cell, width)| base_cell_style_text(width, cx).px_1p5().py_1().child(cell)),
852 );
853
854 let row = if let Some(map_row) = table_context.map_row {
855 map_row((row_index, row), window, cx)
856 } else {
857 row.into_any_element()
858 };
859
860 div().h_full().w_full().child(row).into_any_element()
861}
862
863pub fn render_header<const COLS: usize>(
864 headers: [impl IntoElement; COLS],
865 table_context: TableRenderContext<COLS>,
866 cx: &mut App,
867) -> impl IntoElement {
868 let column_widths = table_context
869 .column_widths
870 .map_or([None; COLS], |widths| widths.map(Some));
871 div()
872 .flex()
873 .flex_row()
874 .items_center()
875 .justify_between()
876 .w_full()
877 .p_2()
878 .border_b_1()
879 .border_color(cx.theme().colors().border)
880 .children(
881 headers
882 .into_iter()
883 .zip(column_widths)
884 .map(|(h, width)| base_cell_style_text(width, cx).child(h)),
885 )
886}
887
888#[derive(Clone)]
889pub struct TableRenderContext<const COLS: usize> {
890 pub striped: bool,
891 pub total_row_count: usize,
892 pub column_widths: Option<[Length; COLS]>,
893 pub map_row: Option<Rc<dyn Fn((usize, Stateful<Div>), &mut Window, &mut App) -> AnyElement>>,
894}
895
896impl<const COLS: usize> TableRenderContext<COLS> {
897 fn new(table: &Table<COLS>, cx: &App) -> Self {
898 Self {
899 striped: table.striped,
900 total_row_count: table.rows.len(),
901 column_widths: table.col_widths.as_ref().map(|widths| widths.lengths(cx)),
902 map_row: table.map_row.clone(),
903 }
904 }
905}
906
907impl<const COLS: usize> RenderOnce for Table<COLS> {
908 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
909 let table_context = TableRenderContext::new(&self, cx);
910 let interaction_state = self.interaction_state.and_then(|state| state.upgrade());
911 let current_widths = self
912 .col_widths
913 .as_ref()
914 .and_then(|widths| Some((widths.current.as_ref()?, widths.resizable)))
915 .map(|(curr, resize_behavior)| (curr.downgrade(), resize_behavior));
916
917 let scroll_track_size = px(16.);
918 let h_scroll_offset = if interaction_state
919 .as_ref()
920 .is_some_and(|state| state.read(cx).vertical_scrollbar.show_scrollbar)
921 {
922 // magic number
923 px(3.)
924 } else {
925 px(0.)
926 };
927
928 let width = self.width;
929 let no_rows_rendered = self.rows.is_empty();
930
931 let table = div()
932 .when_some(width, |this, width| this.w(width))
933 .h_full()
934 .v_flex()
935 .when_some(self.headers.take(), |this, headers| {
936 this.child(render_header(headers, table_context.clone(), cx))
937 })
938 .when_some(current_widths, {
939 |this, (widths, resize_behavior)| {
940 this.on_drag_move::<DraggedColumn>({
941 let widths = widths.clone();
942 move |e, window, cx| {
943 widths
944 .update(cx, |widths, cx| {
945 widths.on_drag_move(e, &resize_behavior, window, cx);
946 })
947 .ok();
948 }
949 })
950 .on_children_prepainted(move |bounds, _, cx| {
951 widths
952 .update(cx, |widths, _| {
953 // This works because all children x axis bounds are the same
954 widths.cached_bounds_width = bounds[0].right() - bounds[0].left();
955 })
956 .ok();
957 })
958 }
959 })
960 .on_drop::<DraggedColumn>(|_, _, _| {
961 // Finish the resize operation
962 })
963 .child(
964 div()
965 .flex_grow()
966 .w_full()
967 .relative()
968 .overflow_hidden()
969 .map(|parent| match self.rows {
970 TableContents::Vec(items) => {
971 parent.children(items.into_iter().enumerate().map(|(index, row)| {
972 render_row(index, row, table_context.clone(), window, cx)
973 }))
974 }
975 TableContents::UniformList(uniform_list_data) => parent.child(
976 uniform_list(
977 uniform_list_data.element_id,
978 uniform_list_data.row_count,
979 {
980 let render_item_fn = uniform_list_data.render_item_fn;
981 move |range: Range<usize>, window, cx| {
982 let elements = render_item_fn(range.clone(), window, cx);
983 elements
984 .into_iter()
985 .zip(range)
986 .map(|(row, row_index)| {
987 render_row(
988 row_index,
989 row,
990 table_context.clone(),
991 window,
992 cx,
993 )
994 })
995 .collect()
996 }
997 },
998 )
999 .size_full()
1000 .flex_grow()
1001 .with_sizing_behavior(ListSizingBehavior::Auto)
1002 .with_horizontal_sizing_behavior(if width.is_some() {
1003 ListHorizontalSizingBehavior::Unconstrained
1004 } else {
1005 ListHorizontalSizingBehavior::FitList
1006 })
1007 .when_some(
1008 interaction_state.as_ref(),
1009 |this, state| {
1010 this.track_scroll(
1011 state.read_with(cx, |s, _| s.scroll_handle.clone()),
1012 )
1013 },
1014 ),
1015 ),
1016 })
1017 .when_some(
1018 self.col_widths.as_ref().zip(interaction_state.as_ref()),
1019 |parent, (table_widths, state)| {
1020 parent.child(state.update(cx, |state, cx| {
1021 let resizable_columns = table_widths.resizable;
1022 let column_widths = table_widths.lengths(cx);
1023 let columns = table_widths.current.clone();
1024 let initial_sizes = table_widths.initial;
1025 state.render_resize_handles(
1026 &column_widths,
1027 &resizable_columns,
1028 initial_sizes,
1029 columns,
1030 window,
1031 cx,
1032 )
1033 }))
1034 },
1035 )
1036 .when_some(interaction_state.as_ref(), |this, interaction_state| {
1037 this.map(|this| {
1038 TableInteractionState::render_vertical_scrollbar_track(
1039 interaction_state,
1040 this,
1041 scroll_track_size,
1042 cx,
1043 )
1044 })
1045 .map(|this| {
1046 TableInteractionState::render_vertical_scrollbar(
1047 interaction_state,
1048 this,
1049 cx,
1050 )
1051 })
1052 }),
1053 )
1054 .when_some(
1055 no_rows_rendered
1056 .then_some(self.empty_table_callback)
1057 .flatten(),
1058 |this, callback| {
1059 this.child(
1060 h_flex()
1061 .size_full()
1062 .p_3()
1063 .items_start()
1064 .justify_center()
1065 .child(callback(window, cx)),
1066 )
1067 },
1068 )
1069 .when_some(
1070 width.and(interaction_state.as_ref()),
1071 |this, interaction_state| {
1072 this.map(|this| {
1073 TableInteractionState::render_horizontal_scrollbar_track(
1074 interaction_state,
1075 this,
1076 scroll_track_size,
1077 cx,
1078 )
1079 })
1080 .map(|this| {
1081 TableInteractionState::render_horizontal_scrollbar(
1082 interaction_state,
1083 this,
1084 h_scroll_offset,
1085 cx,
1086 )
1087 })
1088 },
1089 );
1090
1091 if let Some(interaction_state) = interaction_state.as_ref() {
1092 table
1093 .track_focus(&interaction_state.read(cx).focus_handle)
1094 .id(("table", interaction_state.entity_id()))
1095 .on_hover({
1096 let interaction_state = interaction_state.downgrade();
1097 move |hovered, window, cx| {
1098 interaction_state
1099 .update(cx, |interaction_state, cx| {
1100 if *hovered {
1101 interaction_state.horizontal_scrollbar.show(cx);
1102 interaction_state.vertical_scrollbar.show(cx);
1103 cx.notify();
1104 } else if !interaction_state
1105 .focus_handle
1106 .contains_focused(window, cx)
1107 {
1108 interaction_state.hide_scrollbars(window, cx);
1109 }
1110 })
1111 .ok();
1112 }
1113 })
1114 .into_any_element()
1115 } else {
1116 table.into_any_element()
1117 }
1118 }
1119}
1120
1121// computed state related to how to render scrollbars
1122// one per axis
1123// on render we just read this off the keymap editor
1124// we update it when
1125// - settings change
1126// - on focus in, on focus out, on hover, etc.
1127#[derive(Debug)]
1128pub struct ScrollbarProperties {
1129 axis: Axis,
1130 show_scrollbar: bool,
1131 show_track: bool,
1132 auto_hide: bool,
1133 hide_task: Option<Task<()>>,
1134 state: ScrollbarState,
1135}
1136
1137impl ScrollbarProperties {
1138 // Shows the scrollbar and cancels any pending hide task
1139 fn show(&mut self, cx: &mut Context<TableInteractionState>) {
1140 if !self.auto_hide {
1141 return;
1142 }
1143 self.show_scrollbar = true;
1144 self.hide_task.take();
1145 cx.notify();
1146 }
1147
1148 fn hide(&mut self, window: &mut Window, cx: &mut Context<TableInteractionState>) {
1149 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
1150
1151 if !self.auto_hide {
1152 return;
1153 }
1154
1155 let axis = self.axis;
1156 self.hide_task = Some(cx.spawn_in(window, async move |keymap_editor, cx| {
1157 cx.background_executor()
1158 .timer(SCROLLBAR_SHOW_INTERVAL)
1159 .await;
1160
1161 if let Some(keymap_editor) = keymap_editor.upgrade() {
1162 keymap_editor
1163 .update(cx, |keymap_editor, cx| {
1164 match axis {
1165 Axis::Vertical => {
1166 keymap_editor.vertical_scrollbar.show_scrollbar = false
1167 }
1168 Axis::Horizontal => {
1169 keymap_editor.horizontal_scrollbar.show_scrollbar = false
1170 }
1171 }
1172 cx.notify();
1173 })
1174 .ok();
1175 }
1176 }));
1177 }
1178}
1179
1180impl Component for Table<3> {
1181 fn scope() -> ComponentScope {
1182 ComponentScope::Layout
1183 }
1184
1185 fn description() -> Option<&'static str> {
1186 Some("A table component for displaying data in rows and columns with optional styling.")
1187 }
1188
1189 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
1190 Some(
1191 v_flex()
1192 .gap_6()
1193 .children(vec![
1194 example_group_with_title(
1195 "Basic Tables",
1196 vec![
1197 single_example(
1198 "Simple Table",
1199 Table::new()
1200 .width(px(400.))
1201 .header(["Name", "Age", "City"])
1202 .row(["Alice", "28", "New York"])
1203 .row(["Bob", "32", "San Francisco"])
1204 .row(["Charlie", "25", "London"])
1205 .into_any_element(),
1206 ),
1207 single_example(
1208 "Two Column Table",
1209 Table::new()
1210 .header(["Category", "Value"])
1211 .width(px(300.))
1212 .row(["Revenue", "$100,000"])
1213 .row(["Expenses", "$75,000"])
1214 .row(["Profit", "$25,000"])
1215 .into_any_element(),
1216 ),
1217 ],
1218 ),
1219 example_group_with_title(
1220 "Styled Tables",
1221 vec![
1222 single_example(
1223 "Default",
1224 Table::new()
1225 .width(px(400.))
1226 .header(["Product", "Price", "Stock"])
1227 .row(["Laptop", "$999", "In Stock"])
1228 .row(["Phone", "$599", "Low Stock"])
1229 .row(["Tablet", "$399", "Out of Stock"])
1230 .into_any_element(),
1231 ),
1232 single_example(
1233 "Striped",
1234 Table::new()
1235 .width(px(400.))
1236 .striped()
1237 .header(["Product", "Price", "Stock"])
1238 .row(["Laptop", "$999", "In Stock"])
1239 .row(["Phone", "$599", "Low Stock"])
1240 .row(["Tablet", "$399", "Out of Stock"])
1241 .row(["Headphones", "$199", "In Stock"])
1242 .into_any_element(),
1243 ),
1244 ],
1245 ),
1246 example_group_with_title(
1247 "Mixed Content Table",
1248 vec![single_example(
1249 "Table with Elements",
1250 Table::new()
1251 .width(px(840.))
1252 .header(["Status", "Name", "Priority", "Deadline", "Action"])
1253 .row([
1254 Indicator::dot().color(Color::Success).into_any_element(),
1255 "Project A".into_any_element(),
1256 "High".into_any_element(),
1257 "2023-12-31".into_any_element(),
1258 Button::new("view_a", "View")
1259 .style(ButtonStyle::Filled)
1260 .full_width()
1261 .into_any_element(),
1262 ])
1263 .row([
1264 Indicator::dot().color(Color::Warning).into_any_element(),
1265 "Project B".into_any_element(),
1266 "Medium".into_any_element(),
1267 "2024-03-15".into_any_element(),
1268 Button::new("view_b", "View")
1269 .style(ButtonStyle::Filled)
1270 .full_width()
1271 .into_any_element(),
1272 ])
1273 .row([
1274 Indicator::dot().color(Color::Error).into_any_element(),
1275 "Project C".into_any_element(),
1276 "Low".into_any_element(),
1277 "2024-06-30".into_any_element(),
1278 Button::new("view_c", "View")
1279 .style(ButtonStyle::Filled)
1280 .full_width()
1281 .into_any_element(),
1282 ])
1283 .into_any_element(),
1284 )],
1285 ),
1286 ])
1287 .into_any_element(),
1288 )
1289 }
1290}