diff --git a/crates/eggshell-ui-extras/src/command/list.rs b/crates/eggshell-ui-extras/src/command/list.rs index c7d2c24f..716b0792 100644 --- a/crates/eggshell-ui-extras/src/command/list.rs +++ b/crates/eggshell-ui-extras/src/command/list.rs @@ -443,11 +443,10 @@ pub(crate) fn show<'f, A: 'static>( } let text = style.role(&style.menu.text).clone(); let row_height = style.menu.row_height(&text); - let height = list_height(&style, &command, &listing, row_height); let mut list = s .virtual_list( Part::Results, - tw().h_px(height) + tw().max_h_px(command.list_height) .p(style.menu.padding / 4.0) .scroll_py(1.0) .scrollbar_none(), @@ -519,25 +518,6 @@ pub(crate) fn show<'f, A: 'static>( }); } -/// How tall the rows are, up to the style's limit: a scroll container is as -/// tall as it is told, so the list is told what its rows come to. -fn list_height( - style: &Style, - command: &super::CommandStyle, - listing: &Listing, - row_height: f32, -) -> f32 { - let heading = style - .role(&Some(style.menu.shortcut_text.clone())) - .line_height; - let headings: f32 = (0..listing.rows.len()) - .filter(|&at| listing.heading(at).is_some()) - .map(|at| heading + 12.0 + if at == 0 { 0.0 } else { 5.0 }) - .sum(); - let rows = listing.rows.len() as f32 * row_height; - (rows + headings + 2.0 * style.menu.padding).min(command.list_height) -} - fn search_field( s: &mut Scope<'_, '_, A>, style: &Style, diff --git a/src/builder.rs b/src/builder.rs index cfb7f1d0..4b867b4b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -666,6 +666,40 @@ impl<'b, 'f, A> Builder<'b, 'f, A> { } } + /// Sizes the scroll container `node` to its content, `rows` tall inside + /// the content's padding, unless its classes gave it a height, and no + /// taller than its max height: the container's parents measure it by this + /// height, not by the max. + pub(crate) fn fit_height(&mut self, node: NodeId, rows: f32) { + use taffy::{BoxSizing, CompactLength, Dimension}; + let mut style = self.st.tree.style(node).clone(); + if style.size.height != Dimension::auto() { + return; + } + let length = |side: CompactLength| match side { + side if side.tag() == CompactLength::LENGTH_TAG => side.value(), + _ => 0.0, + }; + let padding = self.st.tree.scroll(node).map_or(0.0, |scroll| { + let padding = self.st.tree.style(scroll.content).padding; + length(padding.top.into_raw()) + length(padding.bottom.into_raw()) + }); + let border = length(style.border.top.into_raw()) + length(style.border.bottom.into_raw()); + let insets = match style.box_sizing { + BoxSizing::BorderBox => padding + border, + BoxSizing::ContentBox => 0.0, + }; + let wanted = rows + insets; + let max = style.max_size.height.into_raw(); + let capped = if max.tag() == CompactLength::LENGTH_TAG { + wanted.min(max.value()) + } else { + wanted + }; + style.size.height = Dimension::length(capped); + self.st.tree.set_style(node, style); + } + /// How many roots have been declared so far this frame. pub(crate) fn roots_declared(&self) -> usize { self.frame.roots.len() diff --git a/src/vlist.rs b/src/vlist.rs index d8c6c695..616d2480 100644 --- a/src/vlist.rs +++ b/src/vlist.rs @@ -414,6 +414,7 @@ impl<'a, 'f, A: 'static, R: Key + Copy> ListChain<'a, 'f, A, R> { }); let scroll = handle.node(); let mut b = handle.into_builder(); + b.fit_height(scroll, plan.total()); let behavior = listbox.map(|keys| Listbox { list, kept: kept.clone(), diff --git a/tests/it/vlist.rs b/tests/it/vlist.rs index 90dda458..5ab3c802 100644 --- a/tests/it/vlist.rs +++ b/tests/it/vlist.rs @@ -76,6 +76,8 @@ struct App { start_at_end: bool, /// The row whose 200-point collapsible is open. opened: Option, + /// A cap on the height in place of the fixed viewport height. + max_height: Option, } impl App { @@ -93,6 +95,7 @@ impl App { menu_in: None, start_at_end: false, opened: None, + max_height: None, } } @@ -107,7 +110,11 @@ impl App { const VIEWPORT: f32 = 300.0; fn view<'f>(b: &mut Builder<'_, 'f, App>, app: &'f App) { - let list = b.virtual_list("list", tw().w_px(app.width).h_px(VIEWPORT), &app.rows); + let height = match app.max_height { + Some(max) => tw().max_h_px(max), + None => tw().h_px(VIEWPORT), + }; + let list = b.virtual_list("list", tw().w_px(app.width).merge(height), &app.rows); let list = match app.list { VList::Estimated { estimate, overscan } => list.estimate_px(estimate).overscan_px(overscan), VList::Fixed { height, overscan } => list.fixed_px(height).overscan_px(overscan), @@ -253,6 +260,18 @@ fn only_the_rows_in_the_viewport_and_the_overscan_are_built() { assert_eq!(step_counting(&mut d), rows.len()); } +#[test] +fn a_list_with_only_a_max_height_is_as_tall_as_its_rows_up_to_the_max() { + let height = |rows: u32| { + let mut app = App::new(rows, VList::fixed(20.0), Rows::Tall(20.0)); + app.max_height = Some(VIEWPORT); + viewport(&harness_of(app)).rect.height() + }; + assert_eq!(height(3), 60.0, "three rows of 20"); + assert_eq!(height(10), 200.0, "ten rows of 20"); + assert_eq!(height(1000), VIEWPORT, "more rows than fit stop at the max"); +} + #[test] fn fixed_rows_build_the_same_rows_as_measured_ones() { let at = |list: VList, offset: f32| {