From d0d96eb81be0d15cc554db03357bc1f403d2d28e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 23:40:55 -0800 Subject: [PATCH] ui: a combo box narrows below its widest option, as a text field does A combo box measured its least width as its widest option, its current text and its placeholder plus the chevron, at every proposal. A text field at its least shows a short part of its text and scrolls the rest. In a grid whose fill columns did not fit beside each other at their leasts, a combo box of long options made the grid stack its cells one under another while a text field in the same place would have narrowed. At its least the combo box now counts only the chevron's room; its natural width is unchanged. --- crates/ui-widgets/src/pickers/combo_box.rs | 8 +++--- .../ui-widgets/tests/it/layout/grid_least.rs | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/ui-widgets/src/pickers/combo_box.rs b/crates/ui-widgets/src/pickers/combo_box.rs index 10c0f5278c..66b65a05cd 100644 --- a/crates/ui-widgets/src/pickers/combo_box.rs +++ b/crates/ui-widgets/src/pickers/combo_box.rs @@ -21,7 +21,7 @@ use crate::primitives::{TextKey, TextOptions, TextRun}; use crate::runtime::env::Label; use crate::runtime::overlay::CloseReason; use crate::text::{Adornment, place, reserve}; -use craft_behavior::{HitLevel, HitPriority, NavMove, PartKey, PlainText, Preview, Selection, Sense}; +use craft_behavior::{HitLevel, HitPriority, NavMove, PartKey, PlainText, Preview, Proposal, Selection, Sense}; use craft_i18n::matcher::Matcher; use egui::{Rect, Response, Vec2}; @@ -191,14 +191,16 @@ impl Component for ComboBox<'_> { } /// The widest of the current text, the placeholder and the options, and the room of the chevron: the field keeps - /// its size whichever option is picked. + /// its size whichever option is picked. At its least, as a text field, it shows a short part of its text and + /// scrolls the rest, so only the chevron's room counts. fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { let current = cx.value().map_or("", String::as_str); let placeholder = self.placeholder.as_ref().map_or("", Label::as_str); let texts = self.options.iter().filter_map(|option| option.value().map(String::as_str)).chain([current, placeholder]); let widest = texts.map(|text| cx.field_text(&TextRun::new(text))).fold(Vec2::ZERO, Vec2::max); + let text = if cx.proposal == Proposal::Min { 0.0 } else { widest.x }; let name = cx.words::().combo_options.clone(); - Vec2::new(widest.x + reserve(cx.style, &[], &Self::trailing(name, cx.env.get())), widest.y) + Vec2::new(text + reserve(cx.style, &[], &Self::trailing(name, cx.env.get())), widest.y) } fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/tests/it/layout/grid_least.rs b/crates/ui-widgets/tests/it/layout/grid_least.rs index 08f113e109..b05c292f28 100644 --- a/crates/ui-widgets/tests/it/layout/grid_least.rs +++ b/crates/ui-widgets/tests/it/layout/grid_least.rs @@ -4,10 +4,12 @@ use craft_behavior::{ColumnWidth, Mixed}; use craft_ui::choice::Checkbox; -use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; use craft_ui::display::Text; use craft_ui::layout_components::{Grid, Stack}; +use craft_ui::pickers::ComboBox; use craft_ui::testing::{Frames, shrink_least_runs, trial}; +use craft_ui::text::TextField; use craft_ui::value_drag::PointPicker; use egui::{Rect, Ui, Vec2}; use std::cell::Cell; @@ -112,3 +114,24 @@ fn labelled_check_boxes_stand_two_to_a_line() { assert!(underline.top() >= bold.bottom() - SNAP, "the second pair is on the next line"); assert!((italic.left() - strike.left()).abs() <= SNAP, "the second column starts at one edge"); } + +/// A combo box narrows below its widest option, as a text field narrows below its text: in a grid of two fill columns +/// too narrow for that option, the combo box and the field beside it share the line and stay inside the room. +#[trial] +fn a_combo_box_narrows_below_its_widest_option_in_a_grid() { + const ROOM: f32 = 160.0; + const PAIR: &[ColumnWidth] = &[ColumnWidth::fill(1.0), ColumnWidth::fill(1.0)]; + let options = [Entry::new(LONG.to_owned(), LONG)]; + let seen = Cell::new((Rect::NOTHING, Rect::NOTHING)); + let _ = in_room(ROOM, |ui| { + Grid::new(PAIR).show(ui, |ui, _| { + let combo = ComboBox::new("Size", Bind::given("1x".to_owned()), &options).show(ui).edit.response.rect; + let field = TextField::new("Suffix", Bind::given(String::new())).show(ui).response.rect; + seen.set((combo, field)); + }); + }); + let (combo, field) = seen.get(); + assert!((combo.center().y - field.center().y).abs() <= SNAP, "the field stands beside the combo box: {combo:?} {field:?}"); + assert!(combo.right() <= field.left() + SNAP, "the combo box ends before the field: {combo:?} {field:?}"); + assert!(field.right() <= ROOM + SNAP, "both fit the room: {field:?}"); +}