Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions crates/eggshell-ui-extras/src/command/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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<A>(
style: &Style,
command: &super::CommandStyle,
listing: &Listing<A>,
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<A: 'static>(
s: &mut Scope<'_, '_, A>,
style: &Style,
Expand Down
34 changes: 34 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
21 changes: 20 additions & 1 deletion tests/it/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ struct App {
start_at_end: bool,
/// The row whose 200-point collapsible is open.
opened: Option<Row>,
/// A cap on the height in place of the fixed viewport height.
max_height: Option<f32>,
}

impl App {
Expand All @@ -93,6 +95,7 @@ impl App {
menu_in: None,
start_at_end: false,
opened: None,
max_height: None,
}
}

Expand All @@ -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),
Expand Down Expand Up @@ -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| {
Expand Down
Loading