table.rs

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