mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 10:23:16 +01:00
Merge pull request #8705 from dotCooCoo/feat/dropdown-list-fits-window
Part of #5941: drop-down lists show every option when the window has room
This commit is contained in:
4 files changed
+808
-31
No files matched your search
@@ -259,11 +259,12 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_with_the_chosen_format
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let labels: Vec<&str> = export_as::formats().iter().map(|f| f.label).collect();
|
||||
// A built-in format six from the end of the built-in ones: out of view when the list opens at
|
||||
// its top, and far enough from the end that a plug-in format another test adds after the
|
||||
// built-in ones does not move the rows shown around it.
|
||||
// built-in ones does not move the rows shown around it. The window is short enough that the
|
||||
// list centered on it stops short of its end.
|
||||
let built_in: Vec<_> = fileio::FORMATS.iter().filter(|f| f.write && !matches!(f.id, "vectorcraft" | "ai")).collect();
|
||||
let labels: Vec<&str> = built_in.iter().map(|f| f.label).collect();
|
||||
let chosen = built_in.len().checked_sub(6).and_then(|i| built_in.get(i)).copied().expect("Export As lists formats");
|
||||
let (mut app, _) = app(1);
|
||||
app.run("file.exportAs", json!({"format": chosen.id})).unwrap();
|
||||
@@ -272,31 +273,38 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_with_the_chosen_format
|
||||
theme::apply(&ctx, Default::default());
|
||||
// Every scroll area takes the bar at rest from the theme, whatever style the list sets itself.
|
||||
assert_eq!(ctx.global_style().spacing.scroll, crate::widgets::list_scroll_style());
|
||||
let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1024.0, 480.0)));
|
||||
let screen_rect = Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1024.0, 280.0)));
|
||||
let mut time = 0.0;
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
// The texts and bars painted on the open list (without the dialog under it), and every text.
|
||||
let mut run = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
|
||||
time += 0.25;
|
||||
let mut out = ctx.run_ui(egui::RawInput { screen_rect, events, time: Some(time), ..Default::default() }, |ui| show(app, ui.ctx()));
|
||||
let mut out = ctx.run_ui(egui::RawInput { screen_rect, events, time: Some(time), ..Default::default() }, |ui| {
|
||||
show(app, ui.ctx());
|
||||
popup.replace(crate::widgets::top_popup_shapes(ui.ctx()));
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
let (mut texts, mut bars) = (vec![], vec![]);
|
||||
out.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut texts, &mut bars));
|
||||
(texts, bars)
|
||||
let (mut texts, mut bars, mut all) = (vec![], vec![], vec![]);
|
||||
popup.take().iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut texts, &mut bars));
|
||||
out.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut all, &mut vec![]));
|
||||
(texts, bars, all)
|
||||
};
|
||||
let away = egui::Event::PointerMoved(egui::pos2(1.0, 1.0));
|
||||
run(&mut app, vec![away.clone()]);
|
||||
let (texts, _) = run(&mut app, vec![]);
|
||||
let (_, _, texts) = run(&mut app, vec![]);
|
||||
let field = texts.iter().find(|(s, ..)| s == chosen.label).map(|(_, r, _)| r.center()).expect("the Format field shows the chosen format");
|
||||
let click_at = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
// Opens the list, moves the pointer away and returns the list's rect, the format names drawn in
|
||||
// it with their clip rects, and the bars drawn.
|
||||
type Run<'r> = dyn FnMut(&mut VectorcraftApp, Vec<egui::Event>) -> (Vec<(String, egui::Rect, egui::Rect)>, Vec<egui::Rect>) + 'r;
|
||||
type Texts = Vec<(String, egui::Rect, egui::Rect)>;
|
||||
type Run<'r> = dyn FnMut(&mut VectorcraftApp, Vec<egui::Event>) -> (Texts, Vec<egui::Rect>, Texts) + 'r;
|
||||
let open = |run: &mut Run<'_>, app: &mut VectorcraftApp| {
|
||||
run(app, vec![egui::Event::PointerMoved(field), click_at(field, true)]);
|
||||
run(app, vec![click_at(field, false)]);
|
||||
run(app, vec![away.clone()]);
|
||||
let (mut texts, mut bars) = (vec![], vec![]);
|
||||
for _ in 0..4 {
|
||||
(texts, bars) = run(app, vec![]);
|
||||
(texts, bars, _) = run(app, vec![]);
|
||||
}
|
||||
let list = ctx
|
||||
.memory(|m| {
|
||||
@@ -334,11 +342,13 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_with_the_chosen_format
|
||||
run(&mut app, vec![wheel]);
|
||||
let mut scrolled = vec![];
|
||||
for _ in 0..4 {
|
||||
(scrolled, _) = run(&mut app, vec![]);
|
||||
(scrolled, ..) = run(&mut app, vec![]);
|
||||
}
|
||||
let scrolled: Vec<_> = scrolled.into_iter().filter(|(s, r, _)| labels.contains(&s.as_str()) && list.contains_rect(*r)).collect();
|
||||
run(&mut app, vec![egui::Event::PointerMoved(field), click_at(field, true)]);
|
||||
run(&mut app, vec![click_at(field, false)]);
|
||||
// Escape closes it: a list taller than the room beside its field covers the field.
|
||||
let escape = |pressed| egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
|
||||
run(&mut app, vec![escape(true)]);
|
||||
run(&mut app, vec![escape(false)]);
|
||||
let (_, reopened, _) = open(&mut run, &mut app);
|
||||
if rows.len() < labels.len() {
|
||||
assert_ne!(tops(&scrolled), tops(&rows), "the wheel scrolled the list");
|
||||
@@ -347,6 +357,83 @@ fn the_format_list_shows_its_scroll_bar_at_rest_and_opens_with_the_chosen_format
|
||||
assert!(whole(&reopened, chosen.label), "the chosen format shows whole: {:?}", tops(&reopened));
|
||||
}
|
||||
|
||||
/// #5941: the Format list stopped at the room on one side of its field, so a window with room for
|
||||
/// every format still cut the list short. In windows of 1280×900 and 1000×856 the list shows every
|
||||
/// built-in format whole, the grayed-out DWG row after them included, inside the window, without a
|
||||
/// scroll bar. A plug-in format that another test adds or removes meanwhile is not counted.
|
||||
#[test]
|
||||
fn the_format_list_shows_every_format_when_the_window_has_room() {
|
||||
fn walk(shape: &egui::Shape, clip: egui::Rect, texts: &mut Vec<(String, egui::Rect, egui::Rect)>, bars: &mut Vec<egui::Rect>) {
|
||||
match shape {
|
||||
egui::Shape::Text(t) => texts.push((t.galley.text().into(), t.visual_bounding_rect(), clip)),
|
||||
egui::Shape::Rect(r) if r.fill.a() > 0 && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => bars.push(r.rect),
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, clip, texts, bars)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let labels: Vec<&str> =
|
||||
fileio::FORMATS.iter().filter(|f| f.write && !matches!(f.id, "vectorcraft" | "ai")).map(|f| f.label).chain(["DWG (use DXF)"]).collect();
|
||||
for (width, height) in [(1280.0, 900.0), (1000.0, 856.0)] {
|
||||
let case = format!("{width}×{height}");
|
||||
let (mut app, _) = app(1);
|
||||
app.run("file.exportAs", json!({"format": "png"})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
theme::install_fonts(&ctx);
|
||||
theme::apply(&ctx, Default::default());
|
||||
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(width, height));
|
||||
let mut time = 0.0;
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
// The texts and bars painted on the open list (without the dialog under it), and every text.
|
||||
let mut run = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
|
||||
time += 0.25;
|
||||
let mut out = ctx.run_ui(egui::RawInput { screen_rect: Some(screen), events, time: Some(time), ..Default::default() }, |ui| {
|
||||
show(app, ui.ctx());
|
||||
popup.replace(crate::widgets::top_popup_shapes(ui.ctx()));
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
let (mut texts, mut bars, mut all) = (vec![], vec![], vec![]);
|
||||
popup.take().iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut texts, &mut bars));
|
||||
out.shapes.iter().for_each(|c| walk(&c.shape, c.clip_rect, &mut all, &mut vec![]));
|
||||
(texts, bars, all)
|
||||
};
|
||||
let away = egui::Event::PointerMoved(egui::pos2(1.0, 1.0));
|
||||
run(&mut app, vec![away.clone()]);
|
||||
let (_, _, all) = run(&mut app, vec![]);
|
||||
let field = all.iter().find(|(s, ..)| s == "PNG").map(|(_, r, _)| r.center()).expect("the Format field shows PNG");
|
||||
let click = |pressed| egui::Event::PointerButton { pos: field, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(&mut app, vec![egui::Event::PointerMoved(field), click(true)]);
|
||||
run(&mut app, vec![click(false)]);
|
||||
run(&mut app, vec![away.clone()]);
|
||||
let list_rect = || {
|
||||
ctx.memory(|m| {
|
||||
m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id))
|
||||
})
|
||||
};
|
||||
// A plug-in format that another test installs or removes changes the list's height, and the
|
||||
// list is placed for its new height on the pass after. Read it once it keeps its rect.
|
||||
let (mut texts, mut bars, mut list) = (vec![], vec![], None);
|
||||
for _ in 0..12 {
|
||||
(texts, bars, _) = run(&mut app, vec![]);
|
||||
let now = list_rect();
|
||||
if now.is_some() && now == list {
|
||||
break;
|
||||
}
|
||||
list = now;
|
||||
}
|
||||
let list = list.expect("the Format list is open");
|
||||
assert!(screen.contains_rect(list), "{case}: the list {list:?} is inside the window");
|
||||
let whole: Vec<&str> = labels
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|name| {
|
||||
texts.iter().any(|(s, r, clip)| s == name && list.contains_rect(*r) && r.top() >= clip.top() && r.bottom() <= clip.bottom())
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(whole, labels, "{case}: every format shows whole in {list:?}");
|
||||
assert!(bars.is_empty(), "{case}: a list that shows every format draws no scroll bar: {bars:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svg_continues_in_svg_options() {
|
||||
let (mut app, written) = app(2);
|
||||
|
||||
@@ -49,6 +49,8 @@ pub use drag::{
|
||||
};
|
||||
#[cfg(test)]
|
||||
use dropdowns::combo_button_id;
|
||||
#[cfg(test)]
|
||||
pub(crate) use dropdowns::top_popup_shapes;
|
||||
pub(crate) use dropdowns::{combo, combo_menu, combo_menu_field_named, combo_named, field_focused_in, menu_min_width, popover_is_open};
|
||||
pub use dropdowns::{
|
||||
dropdown, dropdown_icon_names, dropdown_icon_names_sized, dropdown_named, dropdown_names, dropdown_names_named, dropdown_with,
|
||||
|
||||
@@ -149,9 +149,10 @@ fn option_row<'a>(ui: &mut Ui, enabled: bool, selected: bool, atoms: impl egui::
|
||||
response
|
||||
}
|
||||
|
||||
/// The id [`craft_ui::ComboBox::from_id_salt`] gives its button in this `Ui`: `from_id_salt`
|
||||
/// stores `IdSalt::new(salt)` and hashes it once more with the scope, so the app must hash `id`
|
||||
/// the same way to read the button's focus and open state before it is drawn.
|
||||
/// The id of the button of [`combo_atoms_named`] in this `Ui`, the one
|
||||
/// [`craft_ui::ComboBox::from_id_salt`] gives its button: `from_id_salt` stores
|
||||
/// `IdSalt::new(salt)` and hashes it once more with the scope. The app reads the button's focus and
|
||||
/// open state with it before the button is drawn.
|
||||
pub(super) fn combo_button_id(ui: &Ui, id: &impl std::hash::Hash) -> egui::Id {
|
||||
ui.make_persistent_id(egui::IdSalt::new(HashOnly(id)))
|
||||
}
|
||||
@@ -206,9 +207,8 @@ fn combo_atoms_named<'a, R>(
|
||||
if opening {
|
||||
ui.data_mut(|d| d.insert_temp(opened_key, pass));
|
||||
}
|
||||
// The menu measures itself against the whole screen on the pass it opens and takes
|
||||
// the room beside its field on the next one, so the current option is scrolled into
|
||||
// view on both.
|
||||
// The list is laid out unseen to measure it on the pass it opens and placed on the
|
||||
// next one, so the current option is scrolled into view on both.
|
||||
let settling = ui.data(|d| d.get_temp::<u64>(opened_key)).is_some_and(|p| pass <= p + 1);
|
||||
// A list drawn inside this one's rows finds its own record, and this one's comes back after it.
|
||||
let outer = if settling { OPENING.replace(Some(Opening::default())) } else { OPENING.take() };
|
||||
@@ -238,17 +238,14 @@ fn combo_atoms_named<'a, R>(
|
||||
chosen
|
||||
};
|
||||
let enter_for_combo = ui.memory(|m| m.has_focus(button_id)) || craft_ui::ComboBox::is_open(ui.ctx(), button_id);
|
||||
let response = craft_ui::ComboBox::from_id_salt(HashOnly(&id))
|
||||
.selected_text(current)
|
||||
.width(width - 4.0)
|
||||
.height(ui.spacing().combo_height)
|
||||
.show_ui(ui, list);
|
||||
let response = combo_button(ui, button_id, current, width - 4.0);
|
||||
let shown = combo_list(ui, &response, list);
|
||||
// The button or open list handles Enter before the modal can accept it.
|
||||
if enter_for_combo {
|
||||
ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
|
||||
}
|
||||
let _ = name_field(label_field(response.response.clone(), label), name);
|
||||
let chosen = response.inner.flatten();
|
||||
let _ = name_field(label_field(response, label), name);
|
||||
let chosen = shown.flatten();
|
||||
// egui closes the list on a click of the pointer only: an option picked with Space or
|
||||
// Enter closes it here, and the combo gives the keyboard back, so that Enter reaches the
|
||||
// dialog's OK (#2789).
|
||||
@@ -261,6 +258,178 @@ fn combo_atoms_named<'a, R>(
|
||||
.inner
|
||||
}
|
||||
|
||||
/// The button of [`combo_atoms_named`] with the id `button_id`, at least `width` wide, showing
|
||||
/// `current`: [`craft_ui::ComboBox`]'s button, with its look, its tooltip for a value cut short and
|
||||
/// its role for screen readers. It is drawn here so that its list opens in [`combo_list`].
|
||||
fn combo_button<'a>(ui: &mut Ui, button_id: egui::Id, current: impl egui::IntoAtoms<'a>, width: f32) -> Response {
|
||||
use egui::class::HasClasses as _;
|
||||
use egui::{AtomExt as _, NumExt as _};
|
||||
let open = egui::Popup::is_id_open(ui.ctx(), button_id.with("popup"));
|
||||
let mut classes = egui::class::Classes::default();
|
||||
classes.add_class(craft_ui::ComboBox::CLASS);
|
||||
classes.set_class(craft_ui::ComboBox::CLASS_OPEN, open);
|
||||
let egui::widget_style::ButtonStyle { atom_layout } = ui.widget_style(button_id, &classes);
|
||||
let visuals = if open {
|
||||
ui.visuals().widgets.open
|
||||
} else if let Some(response) = ui.ctx().read_response(button_id) {
|
||||
*ui.style().interact(&response)
|
||||
} else {
|
||||
ui.visuals().widgets.inactive
|
||||
};
|
||||
let min_size = vec2(width, 0.0).at_least(atom_layout.min_size);
|
||||
let current = current.into_atoms();
|
||||
let text = current.text().map(String::from).unwrap_or_default();
|
||||
let rtl = ui.style().right_to_left;
|
||||
// The combo box's arrow: a downward triangle.
|
||||
let arrow = egui::Atom::paint(Vec2::splat(ui.spacing().icon_width), move |ui, args| {
|
||||
let rect = args.rect.expand(visuals.expansion);
|
||||
let rect = Rect::from_center_size(rect.center(), vec2(rect.width() * 0.7, rect.height() * 0.45));
|
||||
ui.painter().add(egui::Shape::rotated_triangle(rect, 0.0, visuals.fg_stroke.color));
|
||||
})
|
||||
.atom_grow(true)
|
||||
.atom_align(if rtl { egui::Align2::LEFT_CENTER } else { egui::Align2::RIGHT_CENTER });
|
||||
let button = atom_layout
|
||||
.apply(
|
||||
egui::WidgetAtom::new((current, arrow))
|
||||
.id(button_id)
|
||||
.sense(Sense::click())
|
||||
.fallback_font(egui::TextStyle::Button)
|
||||
.wrap_mode(ui.wrap_mode())
|
||||
.direction(if rtl { egui::Direction::RightToLeft } else { egui::Direction::LeftToRight }),
|
||||
)
|
||||
.min_size(min_size)
|
||||
.measure(ui, ui.available_size());
|
||||
let elided = button.container.iter_texts().any(|galley| galley.elided);
|
||||
let response = button.allocate(ui).paint(ui).response;
|
||||
// A value cut short shows in full on hover, as a truncated `Label`'s does.
|
||||
let response = if elided {
|
||||
response.on_hover_ui(|ui| {
|
||||
ui.set_max_width(ui.spacing().tooltip_width);
|
||||
ui.add(egui::Label::new(text.as_str()).wrap());
|
||||
})
|
||||
} else {
|
||||
response
|
||||
};
|
||||
response.widget_info(|| egui::WidgetInfo {
|
||||
enabled: ui.is_enabled(),
|
||||
current_text_value: Some(text.clone()),
|
||||
..egui::WidgetInfo::new(egui::Role::ComboBox)
|
||||
});
|
||||
response
|
||||
}
|
||||
|
||||
/// The list of [`combo_atoms_named`] while it is open, drawn by `list` in a popup of its own that
|
||||
/// opens from `button` and takes the whole list whenever the window has room for it (#5941): below
|
||||
/// the button when it fits there, above it when it fits there instead, and otherwise moved up from
|
||||
/// below, over the button, until it ends at the bottom bars. It stays above the bottom bars
|
||||
/// ([`menu_floor`]), and a list taller than the room above them scrolls. A click closes
|
||||
/// the list, except a click on its scroll bar, which scrolls it. Returns what `list` returned, or
|
||||
/// `None` while the list is closed.
|
||||
fn combo_list<R>(ui: &Ui, button: &Response, list: impl FnOnce(&mut Ui) -> R) -> Option<R> {
|
||||
let ctx = ui.ctx();
|
||||
let id = button.id.with("popup");
|
||||
let style = egui::style::StyleModifier::new(|style| {
|
||||
egui::containers::menu::menu_style(style);
|
||||
style.spacing.scroll = list_scroll_style();
|
||||
});
|
||||
// The id of the list's scroll area. egui senses its vertical bar under the area's id with the
|
||||
// axis, 1, and names the widget a click went to at the start of the pass.
|
||||
let area_key = id.with("scroll-area-id");
|
||||
let on_bar =
|
||||
ctx.data(|d| d.get_temp::<egui::Id>(area_key)).is_some_and(|area| ctx.interaction_snapshot(|s| s.clicked == Some(area.with(1_usize))));
|
||||
let close = if on_bar { egui::PopupCloseBehavior::IgnoreClicks } else { egui::PopupCloseBehavior::CloseOnClick };
|
||||
let popup = egui::Popup::menu(button).id(id).width(button.rect.width()).style(style.clone()).close_behavior(close);
|
||||
let screen = ctx.content_rect();
|
||||
// The list stops above the bottom bars, as other menus do (#2086).
|
||||
let floor = menu_floor(ctx).min(screen.bottom());
|
||||
// The room on each side of the rect the popup opens from: the part of the button in view.
|
||||
let anchor = popup.get_anchor_rect().unwrap_or(button.rect);
|
||||
let below = floor - anchor.bottom();
|
||||
let above = anchor.top() - screen.top();
|
||||
// The list's size from its last pass: egui lays a popup out unseen on the pass it opens, so the
|
||||
// list is measured before it is placed. A right-to-left layout lines the list up with the
|
||||
// button's right edge, as egui's menus do.
|
||||
let rtl = ui.style().right_to_left;
|
||||
let expected = popup.get_expected_size();
|
||||
let (align, popup) = match expected {
|
||||
Some(size) if size.y > below && size.y <= above => (if rtl { egui::RectAlign::TOP_END } else { egui::RectAlign::TOP_START }, popup),
|
||||
// A list too tall for either side moves up from below until it ends at the floor.
|
||||
Some(size) if size.y > below => {
|
||||
let top = (floor - size.y).max(screen.top());
|
||||
let from = Rect::from_min_max(egui::pos2(anchor.left(), top), egui::pos2(anchor.right(), top));
|
||||
(if rtl { egui::RectAlign::BOTTOM_END } else { egui::RectAlign::BOTTOM_START }, popup.anchor(from))
|
||||
}
|
||||
_ => (if rtl { egui::RectAlign::BOTTOM_END } else { egui::RectAlign::BOTTOM_START }, popup),
|
||||
};
|
||||
let mut popup_style = (*ctx.global_style()).clone();
|
||||
style.apply(&mut popup_style);
|
||||
let margins = egui::Frame::popup(&popup_style).total_margin().sum();
|
||||
let height = (floor - screen.top() - margins.y).max(ui.spacing().interact_size.y);
|
||||
// A list that was wider than the window keeps its scroll bar in view: until it closes, it is no
|
||||
// wider than it measured or than the window, its names cut short and shown in full on hover.
|
||||
// Only a size measured while the list is open counts: egui keeps the last one after it closes.
|
||||
// The width it needs is its rows' width on the last pass before it was narrowed, with its
|
||||
// margins, and the bar's column while the rows scroll.
|
||||
let too_wide_key = id.with("too-wide");
|
||||
let natural_key = id.with("natural-width");
|
||||
let scrolls_key = id.with("scrolls");
|
||||
let open_last_pass = ctx.read_response(id).is_some();
|
||||
if popup.get_expected_size().is_some_and(|size| open_last_pass && size.x > screen.width()) {
|
||||
// The first measurement is kept: later ones are of the list already narrowed.
|
||||
ctx.data_mut(|d| {
|
||||
if d.get_temp::<f32>(too_wide_key).is_none() {
|
||||
let rows = d.get_temp::<f32>(natural_key).unwrap_or(screen.width());
|
||||
d.insert_temp(too_wide_key, rows);
|
||||
}
|
||||
});
|
||||
}
|
||||
let (measured, scrolls) = ctx.data(|d| (d.get_temp::<f32>(too_wide_key), d.get_temp::<bool>(scrolls_key).unwrap_or(false)));
|
||||
let too_wide = measured.is_some();
|
||||
let shown = popup.align(align).align_alternatives(&[]).show(|ui| {
|
||||
if let Some(rows) = measured {
|
||||
let bar = if scrolls { list_scroll_style().allocated_width() } else { 0.0 };
|
||||
ui.set_max_width((rows + bar).min(screen.width() - margins.x).max(0.0));
|
||||
}
|
||||
ui.set_min_width(ui.available_width());
|
||||
// A popup offers its contents the height it had on its last pass; the list is offered the
|
||||
// window's instead.
|
||||
ui.set_max_height(height);
|
||||
// Labels widen a narrow list rather than wrap, as in egui's combo box.
|
||||
ui.style_mut().wrap_mode = Some(if too_wide { egui::TextWrapMode::Truncate } else { egui::TextWrapMode::Extend });
|
||||
let painted = ui.ctx().graphics(|g| g.get(ui.layer_id()).map_or(0, |shapes| shapes.all_entries().len()));
|
||||
let shown = egui::ScrollArea::vertical().max_height(height).show(ui, list);
|
||||
if too_wide {
|
||||
show_shortened_rows(ui, painted);
|
||||
}
|
||||
ui.data_mut(|d| {
|
||||
if !too_wide {
|
||||
d.insert_temp(natural_key, shown.content_size.x);
|
||||
}
|
||||
d.insert_temp(scrolls_key, shown.content_size.y > shown.inner_rect.height());
|
||||
d.insert_temp(area_key, shown.id);
|
||||
});
|
||||
shown.inner
|
||||
});
|
||||
if shown.is_some() {
|
||||
ctx.accesskit_node_builder(id, |node| node.set_role(egui::Role::ListBox));
|
||||
} else {
|
||||
ctx.data_mut(|d| {
|
||||
d.remove::<f32>(too_wide_key);
|
||||
d.remove::<f32>(natural_key);
|
||||
d.remove::<bool>(scrolls_key);
|
||||
});
|
||||
}
|
||||
shown.map(|r| r.inner)
|
||||
}
|
||||
|
||||
/// The shapes painted so far in this pass on the topmost popup that was shown in the last one, such
|
||||
/// as an open dropdown list, without what lies under it. Read at the end of a test's UI.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn top_popup_shapes(ctx: &egui::Context) -> Vec<egui::epaint::ClippedShape> {
|
||||
let top = ctx.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last());
|
||||
top.map_or_else(Vec::new, |layer| ctx.graphics(|g| g.get(layer).map(|p| p.all_entries().cloned().collect()).unwrap_or_default()))
|
||||
}
|
||||
|
||||
/// The recessed combo box of [`dropdown`] showing `current`, whose menu keeps a search field and
|
||||
/// filters in place over a list that scrolls (the font menu and the shortcut import's target
|
||||
/// picker, #555). `menu` shows the menu with one of [`craft_ui::combo::Menu`]'s header forms and
|
||||
|
||||
@@ -133,10 +133,13 @@ fn a_dropdown_in_a_short_window_opens_with_its_current_option_in_view() {
|
||||
for (height, above, current) in [(260.0, 117.0, 19), (260.0, 117.0, 10), (180.0, 77.0, 10), (180.0, 77.0, 3)] {
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(above);
|
||||
field.set(ui.horizontal(|ui| dropdown_names(ui, "short-list", labels[current], &labels, 160.0)).response.rect);
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
// The shapes of the open list, without the field under it.
|
||||
let mut run = |time: f64, events: Vec<egui::Event>| {
|
||||
let raw = egui::RawInput {
|
||||
time: Some(time),
|
||||
@@ -146,7 +149,7 @@ fn a_dropdown_in_a_short_window_opens_with_its_current_option_in_view() {
|
||||
};
|
||||
let mut out = ctx.run_ui(raw, &mut add);
|
||||
out.textures_delta.clear();
|
||||
out.shapes
|
||||
popup.take()
|
||||
};
|
||||
run(0.0, vec![]);
|
||||
let at = field.get().center();
|
||||
@@ -206,6 +209,7 @@ fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let current = std::cell::RefCell::new(String::from("Row 00"));
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(above);
|
||||
let shown = current.borrow().clone();
|
||||
@@ -224,8 +228,10 @@ fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
|
||||
current.replace(items[i].clone());
|
||||
}
|
||||
field.set(r.response.rect);
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
let mut time = 0.0;
|
||||
// The shapes of the open list, without the field under it.
|
||||
let mut run = |events: Vec<egui::Event>| {
|
||||
time += 0.1;
|
||||
let raw = egui::RawInput {
|
||||
@@ -236,7 +242,7 @@ fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
|
||||
};
|
||||
let mut out = ctx.run_ui(raw, &mut add);
|
||||
out.textures_delta.clear();
|
||||
out.shapes
|
||||
popup.take()
|
||||
};
|
||||
// The rows that show whole, with their tops: inside the open list and the clip they are painted
|
||||
// in.
|
||||
@@ -298,10 +304,12 @@ fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
|
||||
}
|
||||
let scrolled = rows(clipped);
|
||||
assert!(has(&scrolled, row), "{case}: the wheel scrolls {row} into view: {scrolled:?}");
|
||||
// Closed and opened again twice: each time its rows are where they were when it closed.
|
||||
// Closed and opened again twice: each time its rows are where they were when it closed. Escape
|
||||
// closes it: a list taller than the room beside its field covers the field.
|
||||
let escape = |pressed| egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
|
||||
for time in ["first", "second"] {
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![escape(true)]);
|
||||
run(vec![escape(false)]);
|
||||
run(vec![]);
|
||||
let reopened = rows(open(&mut run));
|
||||
assert_eq!(reopened, scrolled, "{case}: the list opens again where it was scrolled, the {time} time");
|
||||
@@ -804,6 +812,517 @@ fn kerning_reads_parentheses_around_its_text_with_a_manual_value_too() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Long dropdown lists use the window's room instead of stopping at `combo_height` (200 pt), and
|
||||
/// show a solid scroll bar without hovering when the list still overflows.
|
||||
#[test]
|
||||
fn a_long_dropdown_uses_window_room_and_shows_its_scroll_bar_when_clipped() {
|
||||
let items: Vec<String> = (0..20).map(|i| format!("Format {i:02}")).collect();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
let run = |win_h: f32| {
|
||||
let ctx = themed(Default::default());
|
||||
let button_rect = std::cell::Cell::new(Rect::NOTHING);
|
||||
let mut draw = |ui: &mut Ui| {
|
||||
ui.add_space(16.0);
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(16.0);
|
||||
let before = ui.next_widget_position();
|
||||
let _ = dropdown_names(ui, "long-formats", labels[0], &labels, 160.0);
|
||||
button_rect.set(Rect::from_min_size(before, vec2(160.0, 26.0)));
|
||||
});
|
||||
};
|
||||
let mut step = |time: f64, events: Vec<egui::Event>| {
|
||||
let raw = egui::RawInput {
|
||||
time: Some(time),
|
||||
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(400.0, win_h))),
|
||||
events,
|
||||
..Default::default()
|
||||
};
|
||||
let mut out = ctx.run_ui(raw, &mut draw);
|
||||
out.textures_delta.clear();
|
||||
out.shapes
|
||||
};
|
||||
step(0.0, vec![]);
|
||||
let at = button_rect.get().center();
|
||||
let click = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
step(0.1, vec![egui::Event::PointerMoved(at), click(true)]);
|
||||
step(0.2, vec![click(false)]);
|
||||
// Move the pointer away so the scroll bar is dormant, not hovered, and let the popup settle.
|
||||
step(0.3, vec![egui::Event::PointerMoved(pos2(350.0, 10.0))]);
|
||||
let mut clipped = vec![];
|
||||
for i in 4..10 {
|
||||
clipped = step(f64::from(i) * 0.1, vec![]);
|
||||
}
|
||||
(ctx, clipped)
|
||||
};
|
||||
|
||||
// In a tall window, all 20 items show without stopping at the 200 pt cap.
|
||||
let (_tall_ctx, tall_shapes) = run(720.0);
|
||||
let mut texts = vec![];
|
||||
fn collect_text(s: &egui::Shape, out: &mut Vec<String>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| collect_text(s, out)),
|
||||
egui::Shape::Text(t) => out.push(t.galley.text().to_string()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for c in &tall_shapes {
|
||||
collect_text(&c.shape, &mut texts);
|
||||
}
|
||||
assert!(texts.iter().any(|t| t == "Format 19"), "the 20th option is shown without scrolling: {texts:?}");
|
||||
|
||||
// In a short window where the list overflows, a solid scroll bar is drawn even while dormant.
|
||||
let (short_ctx, short_shapes) = run(220.0);
|
||||
let mut short_texts = vec![];
|
||||
for c in &short_shapes {
|
||||
collect_text(&c.shape, &mut short_texts);
|
||||
}
|
||||
assert!(!short_texts.iter().any(|t| t == "Format 19"), "the list overflows the 220 pt window");
|
||||
let popup = short_ctx
|
||||
.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id)))
|
||||
.expect("the popup is open");
|
||||
let mut bar_rects = vec![];
|
||||
fn collect_rects(s: &egui::Shape, out: &mut Vec<egui::epaint::RectShape>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| collect_rects(s, out)),
|
||||
egui::Shape::Rect(r) => out.push(r.clone()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for c in &short_shapes {
|
||||
collect_rects(&c.shape, &mut bar_rects);
|
||||
}
|
||||
let has_scroll_bar = bar_rects.iter().any(|r| {
|
||||
r.fill != Color32::TRANSPARENT
|
||||
&& r.rect.width() <= list_scroll_style().bar_width + 1.0
|
||||
&& r.rect.height() >= 12.0
|
||||
&& r.rect.height() < popup.height()
|
||||
&& r.rect.right() <= popup.right()
|
||||
&& r.rect.left() >= popup.right() - list_scroll_style().allocated_width() - 8.0
|
||||
});
|
||||
assert!(has_scroll_bar, "the overflowing dropdown draws its scroll bar without hover in {popup:?}");
|
||||
}
|
||||
|
||||
// #5941: a drop-down list stopped at the room on one side of its field, so a window with room for
|
||||
// the whole list still cut it short. The list opens below its field when it fits there, above it
|
||||
// when it fits there instead, and otherwise moves up from below into the window over its field;
|
||||
// each time every option shows whole, inside the window.
|
||||
#[test]
|
||||
fn a_dropdown_list_shows_every_option_when_the_window_has_room() {
|
||||
let items: Vec<String> = (0..20).map(|i| format!("Option {i:02}")).collect();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
// (window height, space above the field, where the list goes)
|
||||
for (height, above, side) in [(700.0, 16.0, "below"), (700.0, 650.0, "above"), (640.0, 300.0, "over")] {
|
||||
let case = format!("{height} pt window, field {above} pt down");
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(above);
|
||||
field.set(ui.horizontal(|ui| dropdown_names(ui, "fit-list", labels[0], &labels, 160.0)).response.rect);
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(400.0, height));
|
||||
// The shapes of the open list, without the field under it.
|
||||
let mut run = |time: f64, events: Vec<egui::Event>| {
|
||||
let mut out = ctx.run_ui(egui::RawInput { time: Some(time), screen_rect: Some(screen), events, ..Default::default() }, &mut add);
|
||||
out.textures_delta.clear();
|
||||
popup.take()
|
||||
};
|
||||
run(0.0, vec![]);
|
||||
let at = field.get().center();
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(0.1, vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(0.2, vec![press(false)]);
|
||||
run(0.3, vec![egui::Event::PointerMoved(pos2(390.0, 2.0))]);
|
||||
let mut clipped = vec![];
|
||||
for i in 4..10 {
|
||||
clipped = run(f64::from(i) * 0.1, vec![]);
|
||||
}
|
||||
let list = ctx
|
||||
.memory(|m| {
|
||||
m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id))
|
||||
})
|
||||
.expect("the list is open");
|
||||
assert!(screen.contains_rect(list), "{case}: the list {list:?} is inside the window");
|
||||
let field = field.get();
|
||||
// The list opens from the button, 1 pt inside the field's border.
|
||||
match side {
|
||||
"below" => assert!(list.top() >= field.bottom() - 1.5, "{case}: the list {list:?} opens below the field {field:?}"),
|
||||
"above" => assert!(list.bottom() <= field.top() + 1.5, "{case}: the list {list:?} opens above the field {field:?}"),
|
||||
_ => assert!(list.top() < field.bottom() && list.bottom() > field.top(), "{case}: the list {list:?} covers the field {field:?}"),
|
||||
}
|
||||
let mut texts = vec![];
|
||||
fn flat(s: &egui::Shape, clip: Rect, out: &mut Vec<(String, Rect, Rect)>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| flat(s, clip, out)),
|
||||
egui::Shape::Text(t) => out.push((t.galley.text().to_owned(), t.visual_bounding_rect(), clip)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
clipped.iter().for_each(|c| flat(&c.shape, c.clip_rect, &mut texts));
|
||||
let whole: Vec<&str> = labels
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|name| {
|
||||
texts.iter().any(|(s, r, clip)| s == name && list.contains_rect(*r) && r.top() >= clip.top() && r.bottom() <= clip.bottom())
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(whole, labels, "{case}: every option shows whole in {list:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// #5941: a drop-down list stops above the bottom bars, as other menus do (#2086). In a 700 pt window
|
||||
// whose bars start at 640 pt, the list opens below a field near the top, and from a field in the
|
||||
// middle it moves up over the field until it ends at the bars; each time every option shows whole.
|
||||
#[test]
|
||||
fn a_dropdown_list_stops_above_the_bottom_bars() {
|
||||
let items: Vec<String> = (0..20).map(|i| format!("Option {i:02}")).collect();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
let floor = 640.0;
|
||||
for above in [16.0, 300.0] {
|
||||
let case = format!("field {above} pt down");
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
set_menu_floor(ui.ctx(), floor);
|
||||
ui.add_space(above);
|
||||
field.set(ui.horizontal(|ui| dropdown_names(ui, "floor-list", labels[0], &labels, 160.0)).response.rect);
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 700.0));
|
||||
let mut run = |time: f64, events: Vec<egui::Event>| {
|
||||
let mut out = ctx.run_ui(egui::RawInput { time: Some(time), screen_rect: Some(screen), events, ..Default::default() }, &mut add);
|
||||
out.textures_delta.clear();
|
||||
popup.take()
|
||||
};
|
||||
run(0.0, vec![]);
|
||||
let at = field.get().center();
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(0.1, vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(0.2, vec![press(false)]);
|
||||
run(0.3, vec![egui::Event::PointerMoved(pos2(390.0, 2.0))]);
|
||||
let mut clipped = vec![];
|
||||
for i in 4..10 {
|
||||
clipped = run(f64::from(i) * 0.1, vec![]);
|
||||
}
|
||||
let list = ctx
|
||||
.memory(|m| {
|
||||
m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id))
|
||||
})
|
||||
.expect("the list is open");
|
||||
assert!(list.top() >= 0.0 && list.bottom() <= floor + 0.5, "{case}: the list {list:?} ends above the bars at {floor}");
|
||||
let mut texts = vec![];
|
||||
fn flat(s: &egui::Shape, clip: Rect, out: &mut Vec<(String, Rect, Rect)>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| flat(s, clip, out)),
|
||||
egui::Shape::Text(t) => out.push((t.galley.text().to_owned(), t.visual_bounding_rect(), clip)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
clipped.iter().for_each(|c| flat(&c.shape, c.clip_rect, &mut texts));
|
||||
let whole: Vec<&str> = labels
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|name| {
|
||||
texts.iter().any(|(s, r, clip)| s == name && list.contains_rect(*r) && r.top() >= clip.top() && r.bottom() <= clip.bottom())
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(whole, labels, "{case}: every option shows whole in {list:?}");
|
||||
}
|
||||
}
|
||||
|
||||
// #5941: a click on the scroll bar's track of an open drop-down list scrolls it and leaves it open,
|
||||
// so an option it scrolled into view can be picked. A list closed by that click would open again
|
||||
// with its current option in view.
|
||||
#[test]
|
||||
fn a_click_on_a_dropdown_lists_scroll_bar_scrolls_it_and_leaves_it_open() {
|
||||
let items: Vec<String> = (0..40).map(|i| format!("Option {i:02}")).collect();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let picked = std::cell::Cell::new(None);
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(16.0);
|
||||
let r = ui.horizontal(|ui| dropdown_names(ui, "bar-click", labels[0], &labels, 160.0));
|
||||
field.set(r.response.rect);
|
||||
if let Some(i) = r.inner {
|
||||
picked.set(Some(i));
|
||||
}
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));
|
||||
let mut time = 0.0;
|
||||
// The options that show whole on the open list, with their rects, and the scroll bar's rects.
|
||||
let mut run = |events: Vec<egui::Event>| {
|
||||
time += 0.1;
|
||||
let mut out = ctx.run_ui(egui::RawInput { time: Some(time), screen_rect: Some(screen), events, ..Default::default() }, &mut add);
|
||||
out.textures_delta.clear();
|
||||
let (mut texts, mut bars) = (vec![], vec![]);
|
||||
fn flat(s: &egui::Shape, clip: Rect, out: &mut Vec<(String, Rect)>, bars: &mut Vec<Rect>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| flat(s, clip, out, bars)),
|
||||
egui::Shape::Text(t) => {
|
||||
let r = t.visual_bounding_rect();
|
||||
if r.top() >= clip.top() && r.bottom() <= clip.bottom() {
|
||||
out.push((t.galley.text().to_owned(), r));
|
||||
}
|
||||
}
|
||||
egui::Shape::Rect(r) if r.fill.a() > 0 && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => bars.push(r.rect),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
popup.take().iter().for_each(|c| flat(&c.shape, c.clip_rect, &mut texts, &mut bars));
|
||||
(texts, bars)
|
||||
};
|
||||
let list_rect = || {
|
||||
ctx.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id)))
|
||||
};
|
||||
let click =
|
||||
|at: Pos2, pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(vec![]);
|
||||
let at = field.get().center();
|
||||
run(vec![egui::Event::PointerMoved(at), click(at, true)]);
|
||||
run(vec![click(at, false)]);
|
||||
let (mut before, mut bars) = (vec![], vec![]);
|
||||
for _ in 0..4 {
|
||||
(before, bars) = run(vec![]);
|
||||
}
|
||||
assert!(list_rect().is_some(), "the list is open");
|
||||
assert!(before.iter().any(|(s, _)| s == "Option 00"), "the list opens at its first option: {before:?}");
|
||||
// The track, below the handle: near the bottom of the tallest bar rect drawn (the track).
|
||||
let bar = bars
|
||||
.iter()
|
||||
.copied()
|
||||
.max_by(|a, b| a.height().total_cmp(&b.height()))
|
||||
.unwrap_or_else(|| panic!("the list {:?} draws its scroll bar: {before:?}", list_rect()));
|
||||
let track = pos2(bar.center().x, bar.bottom() - 6.0);
|
||||
run(vec![egui::Event::PointerMoved(track), click(track, true)]);
|
||||
run(vec![]);
|
||||
// The button is let go just past the bar's edge, closer than a drag, and the pointer then moves
|
||||
// sideways onto the rows, in the same pass.
|
||||
let let_go = pos2(bar.right() + 1.0, track.y);
|
||||
let onto_row = before.first().map(|(_, r)| pos2(r.center().x, track.y)).expect("a row shows");
|
||||
run(vec![egui::Event::PointerMoved(let_go), click(let_go, false), egui::Event::PointerMoved(onto_row)]);
|
||||
let mut after = vec![];
|
||||
for _ in 0..4 {
|
||||
(after, _) = run(vec![]);
|
||||
}
|
||||
assert!(list_rect().is_some(), "a click on the scroll bar leaves the list open");
|
||||
let new = after
|
||||
.iter()
|
||||
.find(|(s, _)| !before.iter().any(|(b, _)| b == s))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| panic!("the click scrolled options into view: {after:?}"));
|
||||
let row = new.1.center();
|
||||
run(vec![egui::Event::PointerMoved(row), click(row, true)]);
|
||||
run(vec![click(row, false)]);
|
||||
run(vec![]);
|
||||
let index = labels.iter().position(|l| *l == new.0);
|
||||
assert_eq!(picked.get(), index, "the option scrolled into view is picked: {}", new.0);
|
||||
assert!(list_rect().is_none(), "picking an option closes the list");
|
||||
}
|
||||
|
||||
// #5941: a drop-down list with an option wider than the window ran past the window's right edge,
|
||||
// and its scroll bar with it. The list stays inside the window and its bar shows; the long option's
|
||||
// name is cut short.
|
||||
#[test]
|
||||
fn a_dropdown_list_wider_than_the_window_keeps_its_scroll_bar_in_view() {
|
||||
let mut items: Vec<String> = (0..40).map(|i| format!("Option {i:02}")).collect();
|
||||
items[1] = "Option 01, whose name is much wider than this window".into();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
let short: Vec<&str> = labels.iter().copied().filter(|l| l.len() < 10).collect();
|
||||
let two: Vec<&str> = labels[..2].to_vec();
|
||||
// Which list the field offers: 0 all, 1 the short names, 2 the first two.
|
||||
let offered = std::cell::Cell::new(0);
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let popup = std::cell::RefCell::new(vec![]);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(16.0);
|
||||
let options = [&labels, &short, &two][offered.get()];
|
||||
field.set(ui.horizontal(|ui| dropdown_names(ui, "wide-list", options[0], options, 160.0)).response.rect);
|
||||
popup.replace(super::top_popup_shapes(ui.ctx()));
|
||||
};
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(220.0, 300.0));
|
||||
let window = std::cell::Cell::new(screen);
|
||||
let mut time = 0.0;
|
||||
// The texts painted on the open list with their clip rects, and the scroll bar's rects.
|
||||
let mut run = |events: Vec<egui::Event>| {
|
||||
time += 0.1;
|
||||
let mut out = ctx.run_ui(egui::RawInput { time: Some(time), screen_rect: Some(window.get()), events, ..Default::default() }, &mut add);
|
||||
out.textures_delta.clear();
|
||||
let (mut texts, mut bars) = (vec![], vec![]);
|
||||
fn flat(s: &egui::Shape, clip: Rect, texts: &mut Vec<(String, bool)>, bars: &mut Vec<Rect>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| flat(s, clip, texts, bars)),
|
||||
egui::Shape::Text(t) => texts.push((t.galley.text().to_owned(), t.galley.elided)),
|
||||
egui::Shape::Rect(r) if r.fill.a() > 0 && r.rect.width() <= 8.0 && r.rect.height() > 40.0 => bars.push(r.rect.intersect(clip)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
popup.take().iter().for_each(|c| flat(&c.shape, c.clip_rect, &mut texts, &mut bars));
|
||||
(texts, bars)
|
||||
};
|
||||
run(vec![]);
|
||||
let at = field.get().center();
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![egui::Event::PointerMoved(pos2(210.0, 290.0))]);
|
||||
let (mut texts, mut bars) = (vec![], vec![]);
|
||||
for _ in 0..6 {
|
||||
(texts, bars) = run(vec![]);
|
||||
}
|
||||
let list_rect = || {
|
||||
ctx.memory(|m| m.layer_ids().filter(|l| l.order == egui::Order::Foreground && m.areas().is_visible(l)).last().and_then(|l| m.area_rect(l.id)))
|
||||
};
|
||||
let list = list_rect().expect("the list is open");
|
||||
assert!(screen.contains_rect(list), "the list {list:?} is inside the window");
|
||||
assert!(
|
||||
bars.iter().any(|b| b.width() > 0.0 && screen.contains_rect(*b) && list.contains_rect(*b)),
|
||||
"the bar shows in the list {list:?}: {bars:?}"
|
||||
);
|
||||
assert!(texts.iter().any(|(s, elided)| *elided && s.starts_with("Option 01")), "the long name is cut short: {texts:?}");
|
||||
// The window grows while the list is open: the list grows as wide as its names need, and no
|
||||
// wider.
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, vec2(1000.0, 300.0)));
|
||||
for _ in 0..6 {
|
||||
(texts, _) = run(vec![]);
|
||||
}
|
||||
let grown = list_rect().expect("the list is still open");
|
||||
assert!(grown.width() < 500.0, "the list {grown:?} stays as wide as its names need");
|
||||
assert!(texts.iter().any(|(s, elided)| !*elided && s == labels[1]), "the long name shows in full: {texts:?}");
|
||||
// Narrowed and widened again, it comes back to the same width.
|
||||
for size in [screen.size(), vec2(1000.0, 300.0)] {
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, size));
|
||||
for _ in 0..6 {
|
||||
run(vec![]);
|
||||
}
|
||||
}
|
||||
let again = list_rect().expect("the list is still open");
|
||||
assert!((again.width() - grown.width()).abs() < 0.5, "the list is as wide as before: {again:?}, {grown:?}");
|
||||
// Closed, the window narrowed again and the long option gone: the list opens as narrow as its
|
||||
// names, not as wide as the window.
|
||||
let escape = |pressed| egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
|
||||
run(vec![escape(true)]);
|
||||
run(vec![escape(false)]);
|
||||
assert!(list_rect().is_none(), "Escape closes the list");
|
||||
window.set(screen);
|
||||
offered.set(1);
|
||||
run(vec![]);
|
||||
let at = field.get().center();
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![egui::Event::PointerMoved(pos2(210.0, 290.0))]);
|
||||
let mut widths = vec![];
|
||||
for _ in 0..4 {
|
||||
run(vec![]);
|
||||
widths.push(list_rect().expect("the list is open").width());
|
||||
}
|
||||
assert!(widths.iter().all(|w| *w < 180.0), "the list of short names stays narrow: {widths:?}");
|
||||
// Opened in a wide window, narrowed and widened again: it comes back to the width it opened at,
|
||||
// the long list that scrolls and a list of two that does not.
|
||||
for (which, count) in [(0, "40"), (2, "2")] {
|
||||
run(vec![escape(true)]);
|
||||
run(vec![escape(false)]);
|
||||
offered.set(which);
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, vec2(1000.0, 300.0)));
|
||||
run(vec![]);
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![egui::Event::PointerMoved(pos2(990.0, 290.0))]);
|
||||
for _ in 0..6 {
|
||||
run(vec![]);
|
||||
}
|
||||
let opened = list_rect().expect("the list is open");
|
||||
for size in [screen.size(), vec2(1000.0, 300.0)] {
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, size));
|
||||
for _ in 0..6 {
|
||||
run(vec![]);
|
||||
}
|
||||
}
|
||||
let restored = list_rect().expect("the list is still open");
|
||||
// Within the half point that egui rounds a popup's position to.
|
||||
assert!((restored.width() - opened.width()).abs() < 0.5, "{count} options: the list is as wide as it opened: {restored:?}, {opened:?}");
|
||||
}
|
||||
// Opened in a narrow window tall enough for every option, then made wide and short: the list
|
||||
// scrolls now, and its bar's column comes on top of the long name, which shows in full.
|
||||
run(vec![escape(true)]);
|
||||
run(vec![escape(false)]);
|
||||
offered.set(0);
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, vec2(220.0, 1400.0)));
|
||||
run(vec![]);
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![egui::Event::PointerMoved(pos2(210.0, 1390.0))]);
|
||||
for _ in 0..6 {
|
||||
run(vec![]);
|
||||
}
|
||||
window.set(Rect::from_min_size(Pos2::ZERO, vec2(1000.0, 300.0)));
|
||||
for _ in 0..6 {
|
||||
(texts, _) = run(vec![]);
|
||||
}
|
||||
assert!(texts.iter().any(|(s, elided)| !*elided && s == labels[1]), "the long name shows in full beside the bar: {texts:?}");
|
||||
}
|
||||
|
||||
// #5941: in a drop-down list narrowed to the window, an option whose name is cut short shows the
|
||||
// full name in a tooltip while the pointer rests on it.
|
||||
#[test]
|
||||
fn a_dropdown_option_cut_short_shows_its_full_name_on_hover() {
|
||||
let mut items: Vec<String> = (0..10).map(|i| format!("Option {i:02}")).collect();
|
||||
items[1] = "Option 01, whose name is much wider than this window".into();
|
||||
let labels: Vec<&str> = items.iter().map(String::as_str).collect();
|
||||
let ctx = themed(Default::default());
|
||||
let field = std::cell::Cell::new(Rect::NOTHING);
|
||||
let mut add = |ui: &mut Ui| {
|
||||
ui.add_space(16.0);
|
||||
field.set(ui.horizontal(|ui| dropdown_names(ui, "hover-list", labels[0], &labels, 160.0)).response.rect);
|
||||
};
|
||||
let screen = Rect::from_min_size(Pos2::ZERO, vec2(220.0, 300.0));
|
||||
let mut time = 0.0;
|
||||
// Every text painted, on any layer, with its rect and whether it was cut short.
|
||||
let mut run = |events: Vec<egui::Event>| {
|
||||
time += 0.1;
|
||||
let mut out = ctx.run_ui(egui::RawInput { time: Some(time), screen_rect: Some(screen), events, ..Default::default() }, &mut add);
|
||||
out.textures_delta.clear();
|
||||
let mut texts = vec![];
|
||||
fn flat(s: &egui::Shape, out: &mut Vec<(String, Rect, bool)>) {
|
||||
match s {
|
||||
egui::Shape::Vec(v) => v.iter().for_each(|s| flat(s, out)),
|
||||
egui::Shape::Text(t) => out.push((t.galley.text().to_owned(), t.visual_bounding_rect(), t.galley.elided)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out.shapes.iter().for_each(|c| flat(&c.shape, &mut texts));
|
||||
texts
|
||||
};
|
||||
run(vec![]);
|
||||
let at = field.get().center();
|
||||
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
run(vec![egui::Event::PointerMoved(at), press(true)]);
|
||||
run(vec![press(false)]);
|
||||
run(vec![egui::Event::PointerMoved(pos2(210.0, 290.0))]);
|
||||
let mut texts = vec![];
|
||||
for _ in 0..6 {
|
||||
texts = run(vec![]);
|
||||
}
|
||||
let full = |texts: &[(String, Rect, bool)]| texts.iter().any(|(s, _, elided)| !*elided && s == labels[1]);
|
||||
assert!(!full(&texts), "the long name is not shown in full before the hover: {texts:?}");
|
||||
let row = texts
|
||||
.iter()
|
||||
.find(|(s, _, elided)| *elided && s.starts_with("Option 01"))
|
||||
.map(|(_, r, _)| r.center())
|
||||
.unwrap_or_else(|| panic!("the long name is cut short: {texts:?}"));
|
||||
run(vec![egui::Event::PointerMoved(row)]);
|
||||
for _ in 0..10 {
|
||||
texts = run(vec![]);
|
||||
}
|
||||
assert!(full(&texts), "the full name shows on hover: {texts:?}");
|
||||
}
|
||||
|
||||
/// Enter on text that reads as no number in a range field refuses it as a unit field does: the
|
||||
/// field flashes and an open dialog is not confirmed with the old value.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user