2bc41aeb8602
look_tests.rs
use super::*;
/// A context with the fonts and the theme in `brightness`, in English.
pub(crate) fn themed(brightness: theme::Brightness) -> egui::Context {
crate::i18n::set_current(crate::i18n::Lang::EN);
let ctx = egui::Context::default();
theme::install_fonts(&ctx);
theme::apply(&ctx, brightness);
ctx
}
/// One frame at `time` seconds of `add` in a 600 × 400 window with `events` → the shapes it
/// drew, `Shape::Vec`s flattened.
pub(crate) fn frame(ctx: &egui::Context, time: f64, events: Vec<egui::Event>, add: impl FnMut(&mut Ui)) -> Vec<egui::Shape> {
fn flat(s: egui::Shape, out: &mut Vec<egui::Shape>) {
match s {
egui::Shape::Vec(v) => v.into_iter().for_each(|s| flat(s, out)),
s => out.push(s),
}
}
let raw =
egui::RawInput { time: Some(time), screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(600.0, 400.0))), events, ..Default::default() };
let mut out = ctx.run_ui(raw, add);
out.textures_delta.clear();
let mut shapes = vec![];
out.shapes.into_iter().for_each(|c| flat(c.shape, &mut shapes));
shapes
}
// Regression test for #808: the scroll bar of the toolbar and the dock's icon column widened to
// 10 points while it was dragged or the pointer was on it, covering the group labels and the
// right edge of the tool icons. It stays a slim line.
#[test]
fn the_strip_scroll_bar_stays_a_slim_line_while_the_pointer_is_on_it() {
let ctx = themed(Default::default());
let area = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
area.set(
ui.allocate_ui(vec2(80.0, 200.0), |ui| {
strip_scroll(ui, "strip", |ui| {
for i in 0..40 {
ui.label(format!("Tool {i}"));
}
});
})
.response
.rect,
);
};
frame(&ctx, 0.0, vec![], &mut add);
let area = area.get();
assert!(area.height() >= 199.0, "the strip fills the height it was given: {area:?}");
// The pointer comes onto the bar's edge and stays there while the bar settles.
let on_the_bar = egui::Event::PointerMoved(pos2(area.right() - 1.0, area.center().y));
let mut widest = 0.0_f32;
for i in 1..=15 {
let shapes = frame(&ctx, f64::from(i) * 0.1, if i == 1 { vec![on_the_bar.clone()] } else { vec![] }, &mut add);
for s in shapes {
// The bar's track and handle are the filled rectangles.
if let egui::Shape::Rect(r) = s
&& r.fill != Color32::TRANSPARENT
{
widest = widest.max(r.rect.width());
}
}
}
assert!(widest > 0.0, "the scroll bar is drawn");
assert!(widest <= 4.5, "the scroll bar is {widest} points wide");
}
/// The filled rectangles of a scroll bar drawn at the right of `content` in `shapes`: narrow
/// rectangles that start at or past the content's right edge.
fn bar_rects(shapes: &[egui::Shape], content: Rect, widest: f32) -> Vec<(Rect, Color32)> {
shapes
.iter()
.filter_map(|s| match s {
egui::Shape::Rect(r)
if r.fill != Color32::TRANSPARENT && r.rect.width() <= widest && r.rect.height() > 4.0 && r.rect.min.x >= content.right() - 0.5 =>
{
Some((r.rect, r.fill))
}
_ => None,
})
.collect()
}
// Regression test for #5941: a scrolling list in a panel or a dialog showed no scroll bar until the
// pointer came over it, so nothing told the user that the list had more rows. In every brightness
// the bar shows while the pointer is elsewhere, in a column of its own beside the rows, and its
// handle stands out from its track.
#[test]
fn a_scroll_bar_shows_while_the_pointer_is_elsewhere() {
for brightness in theme::Brightness::ALL {
let ctx = themed(brightness);
let rows = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
ui.allocate_ui(vec2(160.0, 200.0), |ui| {
egui::ScrollArea::vertical().id_salt("list").auto_shrink([false, false]).show(ui, |ui| {
for i in 0..40 {
ui.label(format!("Row {i}"));
}
rows.set(ui.max_rect());
});
});
};
frame(&ctx, 0.0, vec![egui::Event::PointerMoved(pos2(500.0, 350.0))], &mut add);
let shapes = frame(&ctx, 1.0, vec![], &mut add);
let rows = rows.get();
assert!(rows.width() < 160.0, "{brightness:?}: the rows leave a column for the bar: {rows:?}");
let bar = bar_rects(&shapes, rows, 10.0);
let (Some(track), Some(handle)) =
(bar.iter().max_by(|a, b| a.0.height().total_cmp(&b.0.height())), bar.iter().min_by(|a, b| a.0.height().total_cmp(&b.0.height())))
else {
panic!("{brightness:?}: the scroll bar is drawn at rest: {bar:?}");
};
assert!(handle.0.height() < track.0.height(), "{brightness:?}: a handle on a track: {bar:?}");
assert_eq!(handle.1.a(), 255, "{brightness:?}: the handle is opaque: {:?}", handle.1);
let gap = (i32::from(handle.1.r()) - i32::from(track.1.r())).abs();
assert!(gap >= 24, "{brightness:?}: handle {:?} on track {:?}", handle.1, track.1);
}
}
// Regression test for #5941: a drop-down list opens with its current option in view. With its field
// in the middle of a short window, neither side has room for the list's 200 pt, and the menu is cut
// to the room beside the field; the current option still shows whole, at the end of the list and in
// its middle, also when the room beside the field is less than half the list.
#[test]
fn a_dropdown_in_a_short_window_opens_with_its_current_option_in_view() {
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, current option): about 105 pt and 65 pt on either side
// of the field.
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 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);
};
let mut run = |time: f64, events: Vec<egui::Event>| {
let raw = egui::RawInput {
time: Some(time),
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(600.0, height))),
events,
..Default::default()
};
let mut out = ctx.run_ui(raw, &mut add);
out.textures_delta.clear();
out.shapes
};
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)]);
let mut clipped = vec![];
for i in 3..10 {
clipped = run(f64::from(i) * 0.1, vec![]);
}
// Each shape with the clip rect it is painted in.
let mut shapes = vec![];
fn flat(s: egui::Shape, clip: Rect, out: &mut Vec<(egui::Shape, Rect)>) {
match s {
egui::Shape::Vec(v) => v.into_iter().for_each(|s| flat(s, clip, out)),
s => out.push((s, clip)),
}
}
clipped.into_iter().for_each(|c| flat(c.shape, c.clip_rect, &mut shapes));
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");
// The options that show whole: inside the list and inside the clip they are painted in.
let rows: Vec<(String, Rect)> = shapes
.iter()
.filter_map(|(s, clip)| match s {
egui::Shape::Text(t) if list.contains_rect(t.visual_bounding_rect()) && clip.contains_rect(t.visual_bounding_rect()) => {
Some((t.galley.text().to_owned(), t.visual_bounding_rect()))
}
_ => None,
})
.collect();
let case = format!("{height} pt window, {} current", labels[current]);
assert!(rows.len() < labels.len(), "{case}: the list does not show every option in {list:?}: {rows:?}");
assert!(rows.iter().any(|(s, _)| s == labels[current]), "{case}: the current option shows whole in {list:?}: {rows:?}");
}
}
// #5941: a drop-down list that draws its own rows (warp styles, tracing presets) has no current
// option for the drop-down to scroll to, so it opens where it was scrolled when it last closed, and
// a row picked below the first ones is still in view when the list opens again. That holds for a
// field in the middle of a short window too, where the list is taller on the pass it opens than
// beside the field, and for a list scrolled to its end.
#[test]
fn a_dropdown_that_draws_its_own_rows_opens_where_it_was_scrolled() {
// (window height, space above the field, wheel, a row the wheel brings into view)
for (height, above, wheel, row) in [(300.0, 20.0, -300.0, "Row 15"), (260.0, 117.0, -2000.0, "Row 19")] {
own_rows_reopen(height, above, wheel, row);
}
}
fn own_rows_reopen(height: f32, above: f32, wheel: f32, row: &str) {
let case = format!("{height} pt window, {above} pt above the field");
let items: Vec<String> = (0..20).map(|i| format!("Row {i:02}")).collect();
let ctx = themed(Default::default());
let field = std::cell::Cell::new(Rect::NOTHING);
let current = std::cell::RefCell::new(String::from("Row 00"));
let mut add = |ui: &mut Ui| {
ui.add_space(above);
let shown = current.borrow().clone();
let r = ui.horizontal(|ui| {
combo(ui, "own-rows", &shown, 160.0, |ui| {
let mut chosen = None;
for (i, s) in items.iter().enumerate() {
if ui.selectable_label(*s == shown, s.as_str()).clicked() {
chosen = Some(i);
}
}
chosen
})
});
if let Some(i) = r.inner {
current.replace(items[i].clone());
}
field.set(r.response.rect);
};
let mut time = 0.0;
let mut run = |events: Vec<egui::Event>| {
time += 0.1;
let raw = egui::RawInput {
time: Some(time),
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(600.0, height))),
events,
..Default::default()
};
let mut out = ctx.run_ui(raw, &mut add);
out.textures_delta.clear();
out.shapes
};
// The rows that show whole, with their tops: inside the open list and the clip they are painted
// in.
let rows = |clipped: Vec<egui::epaint::ClippedShape>| -> Vec<(String, f32)> {
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");
let mut shapes = vec![];
fn flat(s: egui::Shape, clip: Rect, out: &mut Vec<(egui::Shape, Rect)>) {
match s {
egui::Shape::Vec(v) => v.into_iter().for_each(|s| flat(s, clip, out)),
s => out.push((s, clip)),
}
}
clipped.into_iter().for_each(|c| flat(c.shape, c.clip_rect, &mut shapes));
shapes
.iter()
.filter_map(|(s, clip)| match s {
egui::Shape::Text(t) if list.contains_rect(t.visual_bounding_rect()) && clip.contains_rect(t.visual_bounding_rect()) => {
Some((t.galley.text().to_owned(), t.visual_bounding_rect().top().round()))
}
_ => None,
})
.collect()
};
let has = |rows: &[(String, f32)], name: &str| rows.iter().any(|(s, _)| s == name);
run(vec![]);
let at = field.get().center();
let press = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
let open = |run: &mut dyn FnMut(Vec<egui::Event>) -> Vec<egui::epaint::ClippedShape>| {
run(vec![egui::Event::PointerMoved(at), press(true)]);
run(vec![press(false)]);
let mut clipped = vec![];
for _ in 0..4 {
clipped = run(vec![]);
}
clipped
};
let first = rows(open(&mut run));
assert!(has(&first, "Row 00") && !has(&first, row), "{case}: the list opens at its top: {first:?}");
// Scrolled down to `row` and closed.
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");
run(vec![egui::Event::PointerMoved(list.center())]);
let wheel = egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, wheel),
modifiers: Default::default(),
phase: egui::TouchPhase::Move,
source: egui::MouseWheelSource::Unknown,
};
run(vec![wheel]);
let mut clipped = vec![];
for _ in 0..4 {
clipped = run(vec![]);
}
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.
for time in ["first", "second"] {
run(vec![egui::Event::PointerMoved(at), press(true)]);
run(vec![press(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");
}
// `row` picked: the field shows it, and the list opens again where it was when it was picked.
let top = scrolled.iter().find(|(s, _)| s == row).map(|(_, top)| *top).expect("the row shows");
let on_row = pos2(list.left() + 30.0, top + 6.0);
let click = |pressed| egui::Event::PointerButton { pos: on_row, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
run(vec![egui::Event::PointerMoved(on_row), click(true)]);
run(vec![click(false)]);
run(vec![]);
assert_eq!(*current.borrow(), row, "{case}: the click picks {row}");
let reopened = rows(open(&mut run));
assert_eq!(reopened, scrolled, "{case}: the list opens again where {row} was picked");
}
// Regression test for #5941: the toolbar's and the dock icon column's scroll bar showed nothing
// until the pointer came over the column. Its slim line shows at rest, still at most 4 points
// wide (#808).
#[test]
fn the_strip_scroll_bar_shows_while_the_pointer_is_elsewhere() {
let ctx = themed(Default::default());
let column = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
column.set(
ui.allocate_ui(vec2(80.0, 200.0), |ui| {
strip_scroll(ui, "strip", |ui| {
for i in 0..40 {
ui.label(format!("Tool {i}"));
}
});
})
.response
.rect,
);
};
frame(&ctx, 0.0, vec![egui::Event::PointerMoved(pos2(500.0, 350.0))], &mut add);
let shapes = frame(&ctx, 1.0, vec![], &mut add);
let column = column.get();
// The bar lies along the column's right edge, inside it.
let buttons = Rect::from_min_max(column.min, pos2(column.right() - 6.0, column.bottom()));
let bar = bar_rects(&shapes, buttons, 4.5);
assert!(!bar.is_empty(), "the slim scroll bar is drawn at rest");
}
// Regression test for #900: a number field with a spinner and a preset list drew as three
// boxes side by side: its text box was a point taller than the 26-point row, and each joint had
// two borders.
#[test]
fn a_spinner_field_reads_as_one_box_in_three_parts() {
let ctx = themed(Default::default());
let mut add = |ui: &mut Ui| {
ui.horizontal(|ui| spin_field(ui, "weight", Some(4.0), Unit::Points, 100.0, 1.0, 0.0, &[1.0, 2.0]));
};
frame(&ctx, 0.0, vec![], &mut add);
let shapes = frame(&ctx, 0.1, vec![], &mut add);
// The boxes of the row: the spinner, the field and the preset chevron, left to right.
let mut boxes: Vec<Rect> = vec![];
for s in &shapes {
if let egui::Shape::Rect(r) = s
&& r.rect.height() > 20.0
// egui 0.37's TextEdit paints its `Frame::NONE` as a transparent rect too: that is
// not a box of the row.
&& r.fill != Color32::TRANSPARENT
&& !boxes.contains(&r.rect)
{
boxes.push(r.rect);
}
}
boxes.sort_by(|a, b| a.min.x.total_cmp(&b.min.x));
assert_eq!(boxes.len(), 3, "{boxes:?}");
for b in &boxes {
assert_eq!((b.min.y, b.height()), (boxes[0].min.y, 26.0), "all as tall as the row: {boxes:?}");
}
for pair in boxes.windows(2) {
assert!((pair[1].min.x - pair[0].max.x + 1.0).abs() < 0.01, "neighbours share one border: {boxes:?}");
}
// The number text in the numeric field is vertically centered in the row box (#900, #4567).
let text_shape = shapes
.iter()
.find_map(|s| match s {
egui::Shape::Text(t) if t.galley.text() == "4 pt" => Some(t),
_ => None,
})
.expect("field text shape present");
let text_top = text_shape.pos.y - boxes[1].min.y;
let text_bottom = boxes[1].max.y - (text_shape.pos.y + text_shape.galley.rect.height());
assert!((text_top - text_bottom).abs() <= 1.5, "text is vertically centered in its box: top gap {text_top}, bottom gap {text_bottom}");
}
// Regression test for #4567: numbers and text in input fields (numeric fields, dash/gap fields,
// etc.) are vertically centered in their boxes, not shoved against the top edge.
#[test]
fn input_elements_are_vertically_centered_in_their_boxes() {
let ctx = themed(Default::default());
let mut add = |ui: &mut Ui| {
ui.horizontal(|ui| {
num_field(ui, "plain_num", Some(12.0), Unit::Points, 70.0);
opt_field(ui, "dash_cell", Some(12.0), Unit::Points, 40.0);
});
};
frame(&ctx, 0.0, vec![], &mut add);
let shapes = frame(&ctx, 0.1, vec![], &mut add);
let mut boxes: Vec<Rect> = vec![];
for s in &shapes {
if let egui::Shape::Rect(r) = s
&& r.rect.height() > 20.0
&& r.fill != Color32::TRANSPARENT
&& !boxes.contains(&r.rect)
{
boxes.push(r.rect);
}
}
boxes.sort_by(|a, b| a.min.x.total_cmp(&b.min.x));
assert_eq!(boxes.len(), 2, "found both field boxes: {boxes:?}");
let mut text_shapes: Vec<&egui::epaint::TextShape> = vec![];
for s in &shapes {
if let egui::Shape::Text(t) = s
&& (t.galley.text() == "12 pt" || t.galley.text() == "12")
{
text_shapes.push(t);
}
}
text_shapes.sort_by(|a, b| a.pos.x.total_cmp(&b.pos.x));
assert_eq!(text_shapes.len(), 2, "found both text shapes: {text_shapes:?}");
for (b, t) in boxes.iter().zip(text_shapes.iter()) {
let text_top = t.pos.y - b.min.y;
let text_bottom = b.max.y - (t.pos.y + t.galley.rect.height());
assert!(
(text_top - text_bottom).abs() <= 1.5,
"text '{}' is vertically centered: top gap {text_top}, bottom gap {text_bottom} in box {b:?}",
t.galley.text()
);
}
}
/// The colour of every glyph of the text in `shapes`: selected glyphs are drawn in the
/// selection stroke's colour.
pub(crate) fn glyph_colours(shapes: &[egui::Shape]) -> Vec<Color32> {
let mut colours = vec![];
for s in shapes {
if let egui::Shape::Text(t) = s {
for row in &t.galley.rows {
let visuals = &row.row.visuals;
colours.extend(visuals.mesh.vertices[visuals.glyph_vertex_range.clone()].iter().map(|v| v.color));
}
}
}
colours
}
// Regression test for #1465: the selected number in a value field was hard to read in every
// brightness: egui draws selected text in the selection stroke's colour, the accent, on a
// selection tinted with the same accent. It is in the strong text colour.
#[test]
fn selected_text_in_a_value_field_is_in_the_strong_text_colour() {
for brightness in theme::Brightness::ALL {
let ctx = themed(brightness);
let t = Tokens::get(&ctx);
let id = std::cell::Cell::new(egui::Id::NULL);
let mut add = |ui: &mut Ui| {
id.set(ui.scope_id().with("w"));
num_field(ui, "w", Some(1234.0), Unit::Points, 80.0);
};
frame(&ctx, 0.0, vec![], &mut add);
// The field takes focus, and all of its text is selected.
ctx.memory_mut(|m| m.request_focus(id.get()));
frame(&ctx, 0.1, vec![], &mut add);
assert!(ctx.memory(|m| m.has_focus(id.get())), "{brightness:?}: the field has focus");
let len = Unit::Points.format(1234.0).chars().count();
let mut state = egui::TextEdit::load_state(&ctx, id.get()).unwrap_or_default();
state.cursor.set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(len))));
state.store(&ctx, id.get());
let shapes = frame(&ctx, 0.2, vec![], &mut add);
let colours = glyph_colours(&shapes);
assert!(!colours.is_empty(), "{brightness:?}: the number is drawn");
let mut used = colours.clone();
used.dedup();
assert_eq!(used, [t.text_strong], "{brightness:?}: selected text in {used:?}");
assert_ne!(t.text_strong, t.accent);
}
}
// Regression test for #1483: the theme's widget fill is the panel's colour, so a slider in a
// dialog showed only its handle. `slider` draws the rail in the buttons' border colour and
// fills it up to the handle in the accent.
#[test]
fn a_slider_shows_its_rail_filled_up_to_the_handle() {
for brightness in theme::Brightness::ALL {
let ctx = themed(brightness);
let t = Tokens::get(&ctx);
let mut value = 30.0_f64;
let mut add = |ui: &mut Ui| {
slider(ui, egui::Slider::new(&mut value, 0.0..=100.0).show_value(false));
};
frame(&ctx, 0.0, vec![], &mut add);
let shapes = frame(&ctx, 0.1, vec![], &mut add);
let rects: Vec<(Rect, Color32)> =
shapes.iter().filter_map(|s| if let egui::Shape::Rect(r) = s { Some((r.rect, r.fill)) } else { None }).collect();
let rail = rects.iter().find(|(_, c)| *c == t.button_border).map(|(r, _)| *r);
let filled = rects.iter().find(|(_, c)| *c == t.accent).map(|(r, _)| *r);
let (Some(rail), Some(filled)) = (rail, filled) else {
panic!("{brightness:?}: the rail ({:?}) and its fill ({:?}) are drawn: {rects:?}", t.button_border, t.accent);
};
assert!(filled.width() > 1.0 && filled.width() < rail.width(), "{brightness:?}: filled {filled:?} of the rail {rail:?}");
assert!((filled.min.x - rail.min.x).abs() < 1.0, "{brightness:?}: the fill starts at the rail's start");
}
}
/// A dialog slider that a click focused shows no focus outline; once an arrow key steps it, the
/// outline shows, so a keyboard user sees where the focus is.
#[test]
fn a_slider_focused_by_a_click_shows_its_outline_only_after_a_key() {
let ctx = themed(Default::default());
let t = Tokens::get(&ctx);
let rail = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
let shown = SliderValue::number(5.0, 0.0..=10.0);
let (_, _, _) = color_slider_stepped(ui, "outline-test", "Test", Some(shown), 0.5, 180.0, &|_| Color32::GRAY);
rail.set(ui.min_rect());
};
// An outline: a stroked rect in the accent around the rail.
let outlined = |shapes: &[egui::Shape]| {
shapes
.iter()
.any(|s| matches!(s, egui::Shape::Rect(r) if r.stroke.color == t.accent && r.stroke.width > 0.0 && r.fill == Color32::TRANSPARENT))
};
frame(&ctx, 0.0, vec![], &mut add);
let at = rail.get().center();
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };
frame(&ctx, 0.1, vec![egui::Event::PointerMoved(at), button(true)], &mut add);
frame(&ctx, 0.2, vec![button(false)], &mut add);
let shapes = frame(&ctx, 0.3, vec![], &mut add);
assert!(ctx.memory(|m| m.focused().is_some()), "the click focused the rail");
assert!(!outlined(&shapes), "no outline after a click");
let arrow =
|pressed| egui::Event::Key { key: egui::Key::ArrowRight, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE };
frame(&ctx, 0.4, vec![arrow(true), arrow(false)], &mut add);
let shapes = frame(&ctx, 0.5, vec![], &mut add);
assert!(outlined(&shapes), "an arrow key shows the outline");
}
/// Regression test for #2083: the rows of the panels' menus didn't light up under the pointer, as
/// the menu bar's items do.
#[test]
fn a_panel_menu_row_lights_up_under_the_pointer() {
let ctx = themed(Default::default());
let row_at = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
row_at.set(
ui.scope(|ui| {
menu_item_name(ui, "Rename", true, false);
})
.response
.rect,
);
};
// A filled rectangle behind the row's text.
let lit = |shapes: &[egui::Shape], row: Rect| {
shapes.iter().any(|s| matches!(s, egui::Shape::Rect(r) if r.fill != Color32::TRANSPARENT && r.rect.contains(row.center())))
};
frame(&ctx, 0.0, vec![], &mut add);
let row = row_at.get();
let at_rest = frame(&ctx, 0.1, vec![], &mut add);
assert!(!lit(&at_rest, row), "no frame at rest");
frame(&ctx, 0.2, vec![egui::Event::PointerMoved(row.center())], &mut add);
let hovered = frame(&ctx, 0.3, vec![], &mut add);
assert!(lit(&hovered, row), "the row under the pointer is lit: {row:?}");
}
/// Each painted row label names its own numeric field rather than the field's internal salt.
#[test]
fn a_label_rows_input_is_named_by_the_row_label() {
use egui_kittest::kittest::Queryable;
crate::i18n::set_current(crate::i18n::Lang::EN);
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 200.0)).build_ui(|ui| {
label_row(ui, "Opacity", 70.0, |ui| {
let _ = spin_field(ui, "opacity", Some(50.0), Unit::Points, 80.0, 1.0, 0.0, &[]);
});
label_row(ui, "Angle", 70.0, |ui| {
let _ = range_field(ui, "plugin-angle", 30.0, 0.0..=360.0, "°", 1, 80.0);
});
});
h.run_steps(2);
// The painted row label is the value field's exact accessible name: not its internal id.
let _ = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Opacity");
let _ = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Angle");
assert!(h.query_by_role_and_label(egui::accesskit::Role::TextInput, "opacity").is_none(), "no internal widget id");
assert!(h.query_by_role_and_label(egui::accesskit::Role::TextInput, "plugin-angle").is_none(), "no internal widget id");
}
/// A label followed by a checkbox or other widget does not name a later field.
#[test]
fn a_label_does_not_name_a_field_after_another_widget() {
use egui_kittest::kittest::Queryable;
crate::i18n::set_current(crate::i18n::Lang::EN);
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 200.0)).with_accessibility_check(false).build_ui(|ui| {
field_label(ui, "Opacity");
let _ = check(ui, "Preview", true, true);
let _ = spin_field(ui, "unrelated", Some(50.0), Unit::Points, 80.0, 1.0, 0.0, &[]);
});
h.run_steps(2);
assert!(
h.query_by_role_and_label(egui::accesskit::Role::TextInput, "Opacity").is_none(),
"the label belongs to the next widget, not a later field"
);
assert!(h.query_by_role_and_label(egui::accesskit::Role::TextInput, "unrelated").is_none(), "the field is not named by its id");
}
/// A grid's label is in the cell before the field: egui moves the cursor to the next cell
/// without allocating a widget, so the label still names that field exactly.
#[test]
fn a_grid_fields_adjacent_label_names_it_exactly() {
use egui_kittest::kittest::Queryable;
crate::i18n::set_current(crate::i18n::Lang::EN);
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 200.0)).build_ui(|ui| {
egui::Grid::new("field-name-grid").num_columns(2).show(ui, |ui| {
dim_label(ui, "X:");
let _ = num_field(ui, "xfp-x", Some(12.0), Unit::Points, 80.0);
ui.end_row();
});
});
h.run_steps(2);
let _ = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "X:");
assert!(h.query_by_role_and_label(egui::accesskit::Role::TextInput, "xfp-x").is_none(), "no internal widget id");
}
/// A row whose visible names are painted below its fields (Stroke's dash/gap cells) passes
/// the exact translated name, never a Debug-formatted widget id.
#[test]
fn an_explicit_field_name_is_exact_and_has_no_widget_id() {
use egui_kittest::kittest::Queryable;
crate::i18n::set_current(crate::i18n::Lang::EN);
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 200.0)).build_ui(|ui| {
let _ = opt_field_named(ui, ("dash", 0), Some("dash 1"), Some(4.0), Unit::Points, 60.0);
let _ = num_field_named(ui, "xfp-x", Some("X:"), Some(12.0), Unit::Points, 80.0);
});
h.run_steps(2);
let _ = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "dash 1");
let _ = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "X:");
for leaked in ["dash 0", "xfp-x", "(\"dash\", 0)"] {
assert!(
h.query_by_role_and_label(egui::accesskit::Role::TextInput, leaked).is_none(),
"the internal widget id {leaked:?} is not an accessible name"
);
}
}
/// The Enter and focus guard's reconstructed id equals the id egui gives the ComboBox button.
#[test]
fn a_combos_keyboard_guard_reads_the_combos_own_button_id() {
crate::i18n::set_current(crate::i18n::Lang::EN);
let same = std::rc::Rc::new(std::cell::Cell::new(false));
let seen = same.clone();
let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 200.0)).with_accessibility_check(false).build_ui(move |ui| {
// The id the Enter/focus guard reconstructs before drawing, and the id egui's
// `ComboBox::from_id_salt` gives the button: they must be equal, or the guard
// reads and gives back the keyboard of nothing.
let expected = combo_button_id(ui, &"combo-guard-test");
let response = craft_ui::ComboBox::from_id_salt("combo-guard-test").selected_text("One").show_ui(ui, |_| ());
seen.set(expected == response.response.id);
});
h.run_steps(2);
assert!(same.get(), "the guard's id is the ComboBox button's id");
}
/// Regression test for #3440: the Fill/Stroke chip's popover closed on the click that ended a
/// drag between its proxies. It stays open for a click on its own anchor and while a panel
/// drag is on; a click elsewhere still closes it.
#[test]
fn a_popover_stays_open_for_a_click_on_its_anchor_or_during_a_panel_drag() {
let press = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
// Opens the popover, then clicks at `click` (with `drag` as the panel drag under way).
let open_after_click = |click: Option<Pos2>, drag: Option<PanelDrag>| -> bool {
let ctx = themed(Default::default());
let toggle = std::cell::Cell::new(true);
let shown = std::cell::Cell::new(false);
let anchor = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
let resp = ui.allocate_response(vec2(40.0, 24.0), Sense::hover());
anchor.set(resp.rect);
shown.set(popover(&resp, toggle.replace(false), |ui| ui.label("Colour")).is_some());
};
frame(&ctx, 0.0, vec![], &mut add);
assert!(shown.get(), "the toggle opens it");
frame(&ctx, 0.2, vec![], &mut add);
assert!(shown.get(), "it stays open");
if let Some(d) = drag {
egui::DragAndDrop::set_payload(&ctx, d);
}
let pos = click.unwrap_or_else(|| anchor.get().center());
frame(&ctx, 0.3, vec![egui::Event::PointerMoved(pos), press(pos, true)], &mut add);
frame(&ctx, 0.4, vec![press(pos, false)], &mut add);
frame(&ctx, 0.5, vec![], &mut add);
shown.get()
};
let far = Some(pos2(500.0, 350.0));
let panel_drag = || Some(PanelDrag::Symbol { document: 1, name: "s".into() });
// Whether it is open after: a click elsewhere; a click on its anchor that doesn't toggle it;
// a panel drag released elsewhere.
let open = [open_after_click(far, None), open_after_click(None, None), open_after_click(far, panel_drag())];
assert_eq!(open, [false, true, true]);
}
/// #4267: a click on a spinner arrow steps once; an arrow held down steps again and again after a
/// pause even past the click timeout (#6307), and letting go doesn't step once more.
#[test]
fn a_held_spinner_arrow_repeats() {
let ctx = themed(Default::default());
let value = std::cell::Cell::new(5.0);
let scroll = std::cell::Cell::new(0.0);
let spinner_rect = std::cell::Cell::new(Rect::NOTHING);
let mut add = |ui: &mut Ui| {
let area = egui::ScrollArea::vertical().max_height(100.0).scroll_source(egui::scroll_area::ScrollSource::DRAG).show(ui, |ui| {
ui.add_space(35.0);
let row = ui.horizontal(|ui| {
if let Some(v) = spin_plain(ui, "repeat", value.get(), "", 0, 100.0, 1.0, 0.0, &[]) {
value.set(v);
}
});
spinner_rect.set(Rect::from_min_size(row.response.rect.min, vec2(16.0, 26.0)));
ui.add_space(150.0);
});
scroll.set(area.state.clamped_offset().y);
};
let shapes = frame(&ctx, 0.0, vec![], &mut add);
let spinner = shapes
.iter()
.filter_map(|s| match s {
egui::Shape::Rect(r) if r.rect.height() > 20.0 && r.fill != Color32::TRANSPARENT => Some(r.rect),
_ => None,
})
.min_by(|a, b| a.min.x.total_cmp(&b.min.x))
.expect("the spinner box");
let up = spinner.center_top() + vec2(0.0, 6.0);
let button = |pressed| egui::Event::PointerButton { pos: up, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
// A click: one step, on release.
frame(&ctx, 0.1, vec![egui::Event::PointerMoved(up), button(true)], &mut add);
frame(&ctx, 0.2, vec![button(false)], &mut add);
assert_eq!(value.get(), 6.0, "a click steps once");
// Held: nothing more until the pause is over, then a step every repeat.
frame(&ctx, 1.0, vec![button(true)], &mut add);
frame(&ctx, 1.3, vec![], &mut add);
assert_eq!(value.get(), 6.0, "no step before the pause");
for i in 0..17 {
let t = 1.5 + f64::from(i) * 0.1;
frame(&ctx, t, vec![], &mut add);
assert_eq!(value.get(), 7.0 + f64::from(i), "a step every repeat at {t}");
}
frame(&ctx, 3.2, vec![button(false)], &mut add);
frame(&ctx, 3.3, vec![], &mut add);
assert_eq!(value.get(), 23.0, "letting go doesn't step once more");
let down = spinner_rect.get().center_bottom() - vec2(0.0, 6.0);
let modifiers = egui::Modifiers::SHIFT;
let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers };
frame(&ctx, 3.9, vec![egui::Event::PointerMoved(down)], &mut add);
frame(&ctx, 4.0, vec![egui::Event::ModifiersChanged(modifiers), button(down, true)], &mut add);
frame(&ctx, 4.5, vec![], &mut add);
assert_eq!(value.get(), 13.0, "Shift+down steps by ten: press {down:?}, current {:?}", spinner_rect.get());
frame(&ctx, 4.9, vec![], &mut add);
assert_eq!(value.get(), 3.0, "Shift+down repeats past the click timeout");
let outside = down - vec2(0.0, 45.0);
frame(&ctx, 5.0, vec![egui::Event::PointerMoved(outside)], &mut add);
frame(&ctx, 5.1, vec![], &mut add);
frame(&ctx, 5.2, vec![button(outside, false)], &mut add);
assert_eq!(value.get(), 3.0, "dragging away and releasing doesn't step");
assert!(scroll.get() > 0.0, "a drag starting on the spinner can still scroll the panel");
}
/// The Kerning field reads `(10+5)` as 15 whether it shows a manual value (`12`) or an automatic
/// one (`(12)`), and refuses `(10+5)*2` either way.
#[test]
fn kerning_reads_parentheses_around_its_text_with_a_manual_value_too() {
let typed = |automatic: bool, text: &str| {
let ctx = themed(Default::default());
let picked = std::cell::Cell::new(None);
let mut add = |ui: &mut Ui| {
ui.horizontal(|ui| {
if let Some(p) = spin_kerning(ui, "kern", Some(12.0), Some(automatic), Some(false), "Optical", automatic, 100.0, &[]) {
picked.set(Some(p));
}
});
};
arm_field_focus(&ctx, "kern");
frame(&ctx, 0.0, vec![], &mut add);
frame(&ctx, 0.1, vec![], &mut add);
let enter = |pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
frame(&ctx, 0.2, vec![egui::Event::Text(text.into()), enter(true), enter(false)], &mut add);
frame(&ctx, 0.3, vec![], &mut add);
picked.get()
};
for automatic in [false, true] {
assert_eq!(typed(automatic, "(10+5)"), Some(SpinPick::Value(15.0)), "automatic: {automatic}");
assert_eq!(typed(automatic, "(10+5)*2"), None, "automatic: {automatic}");
assert_eq!(typed(automatic, "(1,000)"), None, "a thousands comma, automatic: {automatic}");
assert_eq!(typed(automatic, "(1,5)"), Some(SpinPick::Value(1.5)), "a decimal comma, automatic: {automatic}");
}
}
/// 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]
fn a_range_field_refuses_text_that_reads_as_no_number() {
let typed = |text: &str| {
let ctx = themed(Default::default());
let picked = std::cell::Cell::new(None);
let mut add = |ui: &mut Ui| {
if let Some(v) = range_field(ui, "count", 12.0, 0.0..=100.0, "", 0, 80.0) {
picked.set(Some(v));
}
};
arm_field_focus(&ctx, "count");
frame(&ctx, 0.0, vec![], &mut add);
frame(&ctx, 0.1, vec![], &mut add);
let enter = |pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
frame(&ctx, 0.2, vec![egui::Event::Text(text.into()), enter(true), enter(false)], &mut add);
let refused = ctx.data_mut(|d| d.remove_temp::<bool>(dialog_reject_commit_flag())).unwrap_or(false);
(picked.get(), refused)
};
assert_eq!(typed("40"), (Some(40.0), false), "a number commits");
assert_eq!(typed("abc"), (None, true), "text that reads as no number is refused");
assert_eq!(typed("(10+5)*2"), (None, true), "parentheses the field doesn't read are refused");
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.