From 253883fa4de08ef5925ee99f0c61040362db5382 Mon Sep 17 00:00:00 2001 From: Ben Kunkle Date: Fri, 13 Jun 2025 16:21:20 +0200 Subject: [PATCH] fix horizontal scrolling by copying VSCode and using ListHorizontalSizingBehavior::FitList --- crates/settings_ui/src/ui_components/table.rs | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/crates/settings_ui/src/ui_components/table.rs b/crates/settings_ui/src/ui_components/table.rs index bb3e75785d111c94b6afa7dedaeb00b69d582ac2..2d024d4e4c0e1f9d4bbc8c8764e7d78405be8755 100644 --- a/crates/settings_ui/src/ui_components/table.rs +++ b/crates/settings_ui/src/ui_components/table.rs @@ -349,7 +349,7 @@ impl TableInteractionState { #[derive(RegisterComponent, IntoElement)] pub struct Table { striped: bool, - width: Length, + width: Option, headers: Option<[AnyElement; COLS]>, rows: TableContents, interaction_state: Option>, @@ -363,7 +363,7 @@ impl Table { pub fn new() -> Self { Table { striped: false, - width: Length::Auto, + width: None, headers: None, rows: TableContents::Vec(Vec::new()), interaction_state: None, @@ -399,11 +399,20 @@ impl Table { } /// Sets the width of the table. + /// Will enable horizontal scrolling if [`Self::interactable`] is also called. pub fn width(mut self, width: impl Into) -> Self { - self.width = width.into(); + self.width = Some(width.into()); self } + /// Enables interaction (primarily scrolling) with the table. + /// + /// Vertical scrolling will be enabled by default if the table is taller than its container. + /// + /// Horizontal scrolling will only be enabled if [`Self::width`] is also called, otherwise + /// the list will always shrink the table columns to fit their contents I.e. If [`Self::uniform_list`] + /// is used without a width and with [`Self::interactable`], the [`ListHorizontalSizingBehavior`] will + /// be set to [`ListHorizontalSizingBehavior::FitList`]. pub fn interactable(mut self, interaction_state: &Entity) -> Self { self.interaction_state = Some(interaction_state.downgrade()); self @@ -557,7 +566,6 @@ impl TableRenderContext { impl RenderOnce for Table { fn render(mut self, _window: &mut Window, cx: &mut App) -> impl IntoElement { - // match self.ro let table_context = TableRenderContext::new(&self); let interaction_state = self.interaction_state.and_then(|state| state.upgrade()); @@ -572,8 +580,10 @@ impl RenderOnce for Table { px(0.) }; + let width = self.width; + let table = div() - .w(self.width) + .when_some(width, |this, width| this.w(width)) .h_full() .v_flex() .when_some(self.headers.take(), |this, headers| { @@ -617,9 +627,11 @@ impl RenderOnce for Table { .size_full() .flex_grow() .with_sizing_behavior(ListSizingBehavior::Auto) - .with_horizontal_sizing_behavior( - ListHorizontalSizingBehavior::Unconstrained, - ) + .with_horizontal_sizing_behavior(if width.is_some() { + ListHorizontalSizingBehavior::Unconstrained + } else { + ListHorizontalSizingBehavior::FitList + }) .when_some( interaction_state.as_ref(), |this, state| { @@ -648,24 +660,27 @@ impl RenderOnce for Table { }) }), ) - .when_some(interaction_state.as_ref(), |this, interaction_state| { - this.map(|this| { - TableInteractionState::render_horizantal_scrollbar_track( - interaction_state, - this, - scroll_track_size, - cx, - ) - }) - .map(|this| { - TableInteractionState::render_horizontal_scrollbar( - interaction_state, - this, - h_scroll_offset, - cx, - ) - }) - }); + .when_some( + width.and(interaction_state.as_ref()), + |this, interaction_state| { + this.map(|this| { + TableInteractionState::render_horizantal_scrollbar_track( + interaction_state, + this, + scroll_track_size, + cx, + ) + }) + .map(|this| { + TableInteractionState::render_horizontal_scrollbar( + interaction_state, + this, + h_scroll_offset, + cx, + ) + }) + }, + ); if let Some(interaction_state) = interaction_state.as_ref() { table