Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Dropdowns: open lists start at their widest option's width (#2607) (#2634)

Valentin Kleinc908f76827a5
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 5ab403447..d868db5b2 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -873,6 +873,7 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o
let reveal_id = wheel_id.with("reveal");
let mut reveal = wheel && ui.data_mut(|d| d.remove_temp::<bool>(reveal_id)).unwrap_or(false);
let response = egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).show_ui(ui, |ui| {
+ fit_widest_option(ui, options.iter().map(|(_, l)| tl!(l)));
// Over the open list the wheel steps the value too, instead of scrolling the list.
over_list = wheel && ui.rect_contains_pointer(ui.clip_rect());
if over_list {
@@ -912,6 +913,16 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o
(chosen, hovered)
}
+/// Makes an open dropdown list as wide as its widest option is when hovered, from the start. egui
+/// takes a plain row's frame stroke off its padding but draws a hovered row with the stroke, so a
+/// row grows by two stroke widths under the pointer, and the list never shrinks back: scrolling
+/// the widest option under the pointer widened the list (#2607).
+fn fit_widest_option<'a>(ui: &mut Ui, labels: impl Iterator<Item = &'a str>) {
+ let extend = Some(egui::TextWrapMode::Extend);
+ let widest = labels.map(|l| egui::WidgetText::from(l).into_galley(ui, extend, f32::INFINITY, egui::FontSelection::Default).size().x).fold(0.0, f32::max);
+ ui.set_min_width(widest + 2.0 * ui.spacing().button_padding.x);
+}
+
/// Whether the pointer still rests where the wheel last stepped dropdown `id` (`wheeled`: it did
/// this frame). The option under it then isn't the one to preview: the value moved on without it.
fn wheel_rests(ui: &Ui, id: egui::Id, wheeled: bool) -> bool {
@@ -1032,6 +1043,7 @@ pub fn dropdown_with_tooltips<T: PartialEq + Clone>(ui: &mut Ui, id: &str, curre
let label = options.iter().find(|(v, _, _)| v == current).map(|(_, l, _)| tl!(l)).unwrap_or("—");
let mut changed = false;
let response = egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).show_ui(ui, |ui| {
+ fit_widest_option(ui, options.iter().map(|(_, l, _)| tl!(l)));
for (v, l, tip) in options {
if ui.selectable_label(v == current, tl!(l)).on_hover_text(tl!(tip)).clicked() {
*current = v.clone();
@@ -1388,6 +1400,38 @@ mod tests {
assert_eq!(*h.state(), 0);
}
+ /// Scrolling rows under the pointer doesn't widen the list: it is as wide as the widest
+ /// option hovered from the start (#2607).
+ #[test]
+ fn dropdown_list_keeps_its_width_while_scrolling() {
+ use photocraft_color::BlendMode;
+ let mut h = Harness::builder().with_size(egui::vec2(400.0, 700.0)).build_ui_state(
+ |ui, mode: &mut BlendMode| {
+ let opts: Vec<(BlendMode, &str)> = BlendMode::LAYER_MODES.iter().map(|m| (*m, m.label())).collect();
+ super::dropdown(ui, "brush-mode", mode, &opts, 96.0);
+ },
+ BlendMode::Normal,
+ );
+ // The app's theme strokes plain rows, which is what made hovered rows wider.
+ crate::theme::apply(&h.ctx, crate::theme::ThemeKind::ProMedium);
+ h.run();
+ h.get_by_role(egui::accesskit::Role::ComboBox).click();
+ h.run();
+ let list = |h: &Harness<'_, BlendMode>| h.ctx.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground).find_map(|l| m.area_rect(l.id)));
+ let Some(open) = list(&h) else { panic!("the list is open") };
+ for _ in 0..30 {
+ h.hover_at(open.center());
+ h.event(egui::Event::MouseWheel {
+ unit: egui::MouseWheelUnit::Line,
+ delta: egui::vec2(0.0, -1.0),
+ phase: egui::TouchPhase::Move,
+ modifiers: Modifiers::NONE,
+ });
+ h.run();
+ assert_eq!(list(&h).map(|r| r.width()), Some(open.width()));
+ }
+ }
+
const MODES: [(usize, &str); 4] = [(0, "Normal"), (1, "Dissolve"), (2, "Darken"), (3, "Multiply")];
/// A wheel dropdown (or a plain one) of [`MODES`], whose state is the chosen index.