diff --git a/crates/ui-egui/src/font_menu_regrow_tests.rs b/crates/ui-egui/src/font_menu_regrow_tests.rs index b4a9d8804..5e095f394 100644 --- a/crates/ui-egui/src/font_menu_regrow_tests.rs +++ b/crates/ui-egui/src/font_menu_regrow_tests.rs @@ -75,13 +75,13 @@ fn centre(n: &Node) -> Pos2 { Pos2::new(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32) } -/// The top of the search field: the text input as wide as `menu` (the panel's own fields are narrower). +/// The top of the search field: the text input nearly as wide as `menu`, less the search box's margin and magnifier (the panel's own fields are narrower). fn search_top(nodes: &[Node], menu: Rect) -> f32 { let field = nodes .iter() .filter(|n| n.role() == Role::TextInput) .filter_map(|n| n.bounds()) - .find(|b| menu.contains(Pos2::new(b.x0 as f32 + 1.0, b.y0 as f32 + 1.0)) && b.x1 - b.x0 > f64::from(menu.width()) - 40.0) + .find(|b| menu.contains(Pos2::new(b.x0 as f32 + 1.0, b.y0 as f32 + 1.0)) && b.x1 - b.x0 > f64::from(menu.width()) - 60.0) .expect("the menu has a search field"); field.y0 as f32 } diff --git a/crates/ui-egui/src/panels/typography/picker.rs b/crates/ui-egui/src/panels/typography/picker.rs index 1dc884569..11c040805 100644 --- a/crates/ui-egui/src/panels/typography/picker.rs +++ b/crates/ui-egui/src/panels/typography/picker.rs @@ -75,6 +75,7 @@ pub(crate) fn font_picker( let just_opened = anchor.clicked(); let hi_id = Id::unique("font_highlight"); let q_id = Id::unique("font_query"); + let search_id = Id::unique("font_search"); craft_ui::combo::Menu::new(ui, anchor) .height(MENU_HEIGHT) .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside) @@ -96,9 +97,8 @@ pub(crate) fn font_picker( i.count_and_consume_key(egui::Modifiers::NONE, egui::Key::ArrowDown) as i64 - i.count_and_consume_key(egui::Modifiers::NONE, egui::Key::ArrowUp) as i64 }); - let search = - ui.add(egui::TextEdit::singleline(&mut q).hint_text(tr("Search fonts")).desired_width(ui.available_width()).font(regular(11.5))); - crate::widgets::select_all_on_focus(ui, &search, &q); + let (field, _) = ui.allocate_exact_size(vec2(ui.available_width(), widgets::FIELD_H), egui::Sense::hover()); + let search = widgets::search_box(ui, field, search_id, &mut q, tr("Search fonts")); if just_opened { search.request_focus(); } diff --git a/crates/ui-egui/src/select_all_tests.rs b/crates/ui-egui/src/select_all_tests.rs index d86de12df..ecb77001a 100644 --- a/crates/ui-egui/src/select_all_tests.rs +++ b/crates/ui-egui/src/select_all_tests.rs @@ -140,3 +140,83 @@ fn a_field_left_mid_edit_selects_all_on_the_next_click() { assert!(ctx.memory(|m| m.has_focus(id.with("te")))); assert_eq!(selection(&ctx, id.with("te")), Some((0, 6))); } + +/// The vertical middle of the first painted text that contains `needle`, from one frame's shapes. +fn text_mid_y(shapes: &[egui::epaint::ClippedShape], needle: &str) -> Option { + fn find(s: &egui::Shape, needle: &str) -> Option { + match s { + egui::Shape::Text(t) if t.galley.text().contains(needle) => Some(t.pos.y + t.galley.rect.center().y), + egui::Shape::Vec(v) => v.iter().find_map(|s| find(s, needle)), + _ => None, + } + } + shapes.iter().find_map(|c| find(&c.shape, needle)) +} + +/// Runs one more quiet frame of `f` and returns its shapes. +fn shapes_of(ctx: &egui::Context, f: &mut impl FnMut(&mut Ui)) -> Vec { + let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0))), ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + let mut child = ui.new_child(UiBuilder::new().max_rect(FIELD)); + f(&mut child); + }); + out.textures_delta.clear(); + out.shapes +} + +/// A rename in a row one line tall (the Layers panel's rows, a frame's name label): the typed text +/// sits in the middle of the row, not hanging below it (#2318). The theme's least height for an +/// editor is more than that row, which pushed the text down by the difference. +#[test] +fn a_rename_field_in_a_short_row_centres_its_text() { + let ctx = egui::Context::default(); + let id = Id::unique("short_rename"); + let row = Rect::from_min_size(pos2(20.0, 20.0), vec2(120.0, 16.0)); + let mut f = |ui: &mut Ui| { + widgets::rename_field(ui, id, "Frame 1", row, egui::FontId::proportional(11.0), false); + }; + click(&ctx, pos2(60.0, 28.0), None, &mut f); + assert!(ctx.memory(|m| m.has_focus(id.with("te")))); + let mid = text_mid_y(&shapes_of(&ctx, &mut f), "Frame 1").expect("the text is painted"); + assert!((mid - row.center().y).abs() <= 1.0, "text centre {mid} vs row centre {}", row.center().y); +} + +/// The font menu's search box shows its hint (and what is typed) in the middle of the box, not +/// along its top edge (#2318). +#[test] +fn the_font_search_box_centres_its_hint() { + let ctx = egui::Context::default(); + let mut q = String::new(); + let id = Id::unique("font_search"); + let mut mid_box = 0.0; + let mut f = |ui: &mut Ui| { + let (field, _) = ui.allocate_exact_size(vec2(120.0, widgets::FIELD_H), egui::Sense::hover()); + widgets::search_box(ui, field, id, &mut q, "Search fonts"); + mid_box = field.center().y; + }; + let shapes = shapes_of(&ctx, &mut f); + let mid = text_mid_y(&shapes, "Search fonts").expect("the hint is painted"); + assert!((mid - mid_box).abs() <= 1.0, "hint centre {mid} vs box centre {mid_box}"); +} + +/// The font menu's search field is the shared search box: its text starts after the magnifier, +/// not at the field's left edge as a bare editor's would (#2318). +#[test] +fn the_font_search_box_leaves_room_for_the_magnifier() { + fn left(s: &egui::Shape) -> Option { + match s { + egui::Shape::Text(t) if t.galley.text().contains("Search fonts") => Some(t.pos.x), + egui::Shape::Vec(v) => v.iter().find_map(left), + _ => None, + } + } + let ctx = egui::Context::default(); + let mut q = String::new(); + let id = Id::unique("font_search_icon"); + let mut f = |ui: &mut Ui| { + let (field, _) = ui.allocate_exact_size(vec2(120.0, widgets::FIELD_H), egui::Sense::hover()); + widgets::search_box(ui, field, id, &mut q, "Search fonts"); + }; + let x = shapes_of(&ctx, &mut f).iter().find_map(|c| left(&c.shape)).expect("the hint is painted"); + assert!(x >= FIELD.min.x + 20.0, "hint starts at {x}, field at {}", FIELD.min.x); +} diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 5ad0e198b..3cd1bf3f3 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -1386,7 +1386,10 @@ pub fn text_field_inline(ui: &mut Ui, id: Id, value: &str, rect: Rect, font: egu Some(mut text) => { ui.painter().rect_filled(rect.expand(2.0), CornerRadius::same(4), t.panel); ui.painter().rect_stroke(rect.expand(2.0), CornerRadius::same(4), Stroke::new(1.0, crate::theme::ACCENT), StrokeKind::Inside); - let mut child = ui.new_child(egui::UiBuilder::new().max_rect(rect)); + // The editor is at least the theme's field height; centred on a shorter row it + // keeps its text on the row's middle line instead of hanging below it (#2318). + let edit = Rect::from_center_size(rect.center(), vec2(rect.width(), rect.height().max(FIELD_H))); + let mut child = ui.new_child(egui::UiBuilder::new().max_rect(edit)); let r = crate::bidi::single_line(&mut child, te_id, &mut text, font, rect.width()); a11y::own_focus(&r); keep_focus(&r, started);