diff --git a/crates/ui-egui/src/panels/typography/picker.rs b/crates/ui-egui/src/panels/typography/picker.rs index 1dc884569..8c0773ac4 100644 --- a/crates/ui-egui/src/panels/typography/picker.rs +++ b/crates/ui-egui/src/panels/typography/picker.rs @@ -96,8 +96,7 @@ 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))); + let search = widgets::search_edit(ui, &mut q, tr("Search fonts"), regular(11.5)); crate::widgets::select_all_on_focus(ui, &search, &q); 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..2679c94f3 100644 --- a/crates/ui-egui/src/select_all_tests.rs +++ b/crates/ui-egui/src/select_all_tests.rs @@ -140,3 +140,59 @@ 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 mut mid_box = 0.0; + let mut f = |ui: &mut Ui| { + let r = widgets::search_edit(ui, &mut q, "Search fonts", egui::FontId::proportional(11.5)); + mid_box = r.rect.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}"); +} diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 8bd0b47c8..84a98bc21 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -1381,7 +1381,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); @@ -1472,3 +1475,17 @@ pub fn sublabels(ui: &mut Ui, a: &str, b: Option<&str>) { pub fn col_width(w: f32) -> f32 { ((w - PAD - PAD_RIGHT - GUTTER * 2.0 - 24.0) / 2.0).max(40.0) } + +/// The search box of a menu (the font list's): one line, as wide as the menu, its hint and text +/// centred in the box. [`egui::TextEdit`] puts the text at the top of a box taller than a line +/// unless told otherwise (#2318). +pub(crate) fn search_edit(ui: &mut Ui, text: &mut String, hint: &str, font: egui::FontId) -> Response { + ui.add( + egui::TextEdit::singleline(text) + .hint_text(hint) + .desired_width(ui.available_width()) + .font(font) + .align(Align2::LEFT_CENTER) + .min_size(vec2(0.0, FIELD_H)), + ) +}