mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:47:25 +00:00
Single-line text sits on its part's centre line
A single-line edit takes its text's own font for egui's row and runs in a rect one row tall centred on the part, so a text row taller than the part no longer grows down from the part's top and sets the text low. Side labels follow the baseline.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<RefCell<Vec<Rect>>> = 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:?}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user