diff --git a/crates/ui-contract/src/runtime/egui/text_edit.rs b/crates/ui-contract/src/runtime/egui/text_edit.rs index a61d4a144..b47b11333 100644 --- a/crates/ui-contract/src/runtime/egui/text_edit.rs +++ b/crates/ui-contract/src/runtime/egui/text_edit.rs @@ -124,6 +124,14 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown { ui.fonts_mut(|fonts| crate::runtime::egui::text_layout::layout_with(fonts, job)) }; let hint_text = hint.map(|hint| egui::RichText::new(hint).font(FontId::proportional(font_id.size))); + // A single-line edit runs in a rect one text row tall, or the part's height when that is taller, centred on the + // part's centre line: the row's centre is the part's whatever row egui allocates from the top of its rect. + let rect = if multiline || galley.is_some() { + rect + } else { + let row = ui.fonts_mut(|fonts| fonts.row_height(&font_id)); + Rect::from_center_size(rect.center(), egui::vec2(rect.width(), rect.height().max(row))) + }; // The component's root took its place in the flow already: the editor runs in a child that takes none, so the // parent's cursor stays past the root, not past the text part inside it. let output = { @@ -146,6 +154,8 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown { let mut edit = if multiline { egui::TextEdit::multiline($buffer) } else { egui::TextEdit::singleline($buffer) }; edit = edit .id(id) + // egui sizes the text row by this font: the text's own, not egui's default. + .font(font_id.clone()) .interactive(focusable && pointer) .frame(egui::Frame::NONE) .desired_width(rect.width()) @@ -156,8 +166,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown { .event_filter(filter) .return_key(if multiline { Some(egui::KeyboardShortcut::new(egui::Modifiers::NONE, egui::Key::Enter)) } else { None }) .layouter(&mut layouter) - // A laid-out galley sits at the rect's corner, as a painted run does: egui's own row height - // (its default font's) would otherwise centre it lower. + // A laid-out galley sits at the rect's corner, as a painted run does. .align(match (centred, rtl) { _ if galley.is_some() => egui::Align2::LEFT_TOP, (true, _) => egui::Align2::CENTER_CENTER, diff --git a/crates/ui-widgets/tests/it/text/number.rs b/crates/ui-widgets/tests/it/text/number.rs index 6e71c08d7..cadcef035 100644 --- a/crates/ui-widgets/tests/it/text/number.rs +++ b/crates/ui-widgets/tests/it/text/number.rs @@ -352,3 +352,30 @@ fn the_unit_keeps_its_place_at_the_end_of_the_field() { .collect(); assert!((ends[0] - ends[1]).abs() < 0.5, "the units end at one place from the field's end: {ends:?}"); } + +/// A field's digits sit on its centre line at both control sizes when the text row is taller than the part (here +/// egui's own fonts, far larger than the field's; on a Mac, the OS interface face's tall row in a small field): the +/// row is centred on the part, not allocated from the part's top. The side label follows the digits' baseline, so +/// this keeps it level too. +#[trial] +fn the_digits_sit_on_the_field_centre_line() { + use craft_theme::ControlSize; + use craft_ui::testing::Fixture; + let boxes: std::rc::Rc>> = std::rc::Rc::default(); + let b = boxes.clone(); + let mut fixture = Fixture::new(move |ui| { + let mut v = b.borrow_mut(); + v.clear(); + for (at, size) in [ControlSize::Sm, ControlSize::Md].into_iter().enumerate() { + v.push(NumberField::new("N", Bind::bound(&mut Some(100.0))).size(size).id_salt(at).show(ui).response.rect); + } + }); + fixture.ctx().all_styles_mut(|style| style.text_styles.values_mut().for_each(|font| font.size *= 3.0)); + fixture.settle(); + fixture.idle(3); + for field in boxes.borrow().iter() { + let digits = fixture.texts().iter().find(|text| field.intersects(text.rect) && text.text == "100").expect("the digits are painted"); + let off = digits.rect.center().y - field.center().y; + assert!(off.abs() <= 0.5, "the digits sit {off} pt off the centre line of {field:?}"); + } +}