From 4b6c73a8621b9fab92ec62f7c1d8f063ca39f02b Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 21:23:16 +0000 Subject: [PATCH] Text fields: a multi-line text starts at its box's top, and the box shows whole lines --- .../ui-contract/src/runtime/egui/text_edit.rs | 14 ++++++---- crates/ui-widgets/src/text/alt_text.rs | 19 ++++++------- crates/ui-widgets/src/text/mod.rs | 6 +++++ crates/ui-widgets/src/text/text_field.rs | 27 +++++++++++++++---- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/text_edit.rs b/crates/ui-contract/src/runtime/egui/text_edit.rs index 0d543d9b8..68cf2f019 100644 --- a/crates/ui-contract/src/runtime/egui/text_edit.rs +++ b/crates/ui-contract/src/runtime/egui/text_edit.rs @@ -209,11 +209,15 @@ pub(crate) fn show(ui: &mut Ui, env: &crate::runtime::env::Env, edit: Edit<'_>) // 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, - (false, rtl) => match (end_aligned, rtl) { - (true, false) | (false, true) => egui::Align2::RIGHT_CENTER, - (true, true) | (false, false) => egui::Align2::LEFT_CENTER, - }, + (centred, rtl) => { + let across = match (centred, end_aligned == rtl) { + (true, _) => egui::Align::Center, + (false, true) => egui::Align::Min, + (false, false) => egui::Align::Max, + }; + // A multi-line text starts at the part's top, however many of its lines the text fills. + egui::Align2([across, if multiline { egui::Align::Min } else { egui::Align::Center }]) + } }); if let Some(hint) = hint_text.clone() { edit = edit.hint_text(hint); diff --git a/crates/ui-widgets/src/text/alt_text.rs b/crates/ui-widgets/src/text/alt_text.rs index 644e2ce1c..c47f596a7 100644 --- a/crates/ui-widgets/src/text/alt_text.rs +++ b/crates/ui-widgets/src/text/alt_text.rs @@ -3,7 +3,7 @@ #[cfg(feature = "suite")] pub(crate) mod stories; -use super::{FieldStyle, text_part}; +use super::{FieldStyle, lines_inset, text_part}; use crate::choice::Checkbox; use crate::contract::{ Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, OverlayUi, PaintUi, PartsCx, Props as _, Role, WordSetCx as _, @@ -133,11 +133,12 @@ fn shown_of(shown: Option<&AltText>) -> AltText { shown.cloned().unwrap_or_else(|| AltText::parts(Mixed::Mixed, Mixed::Mixed)) } -/// The height of the description box and the size of the check. -fn sizes(cx: &MeasureCx<'_, AltTextField<'_>>) -> (f32, Vec2) { +/// The height of the description's lines, the space above and below them, and the size of the check. +fn sizes(cx: &MeasureCx<'_, AltTextField<'_>>) -> (f32, f32, Vec2) { let words = cx.words::(); let line = cx.layout(&TextRun::new("M")).size().y; - (line * LINES as f32, cx.child("decorative", &mut check_face(Mixed::Same(false), &words), Proposal::Ideal)) + let check = cx.child("decorative", &mut check_face(Mixed::Same(false), &words), Proposal::Ideal); + (line * LINES as f32, lines_inset(&cx.style.metrics, line), check) } impl Component for AltTextField<'_> { @@ -156,9 +157,9 @@ impl Component for AltTextField<'_> { } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { - let (text, check) = sizes(cx); + let (text, inset, check) = sizes(cx); let width = cx.room.unwrap_or(check.x); - Vec2::new(width, text + cx.gap() + check.y) + Vec2::new(width, 2.0 * inset + text + cx.gap() + check.y) } /// The description box (a text part, disabled while every object is decorative, showing the mixed word as its @@ -166,10 +167,10 @@ impl Component for AltTextField<'_> { /// its own value. fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) { let content = cx.content_rect; - let (text, check) = sizes(cx); + let (text, inset, check) = sizes(cx); let words = cx.words::(); - let area = Rect::from_min_size(content.min, vec2(content.width(), text)); - let check_at = Rect::from_min_size(pos2(content.min.x, area.max.y + cx.gap()), vec2(content.width(), check.y)); + let area = Rect::from_min_size(content.min + vec2(0.0, inset), vec2(content.width(), text)); + let check_at = Rect::from_min_size(pos2(content.min.x, area.max.y + inset + cx.gap()), vec2(content.width(), check.y)); let value = shown_of(cx.value()); let placeholder = if value.text.same().is_some() { words.alt_text.clone() } else { words.mixed.clone() }; let options = TextOptions { lines: Some(LINES), placeholder: Some(placeholder), enabled: !value.all_decorative(), ..TextOptions::default() }; diff --git a/crates/ui-widgets/src/text/mod.rs b/crates/ui-widgets/src/text/mod.rs index 8565cb5da..ede2d2d88 100644 --- a/crates/ui-widgets/src/text/mod.rs +++ b/crates/ui-widgets/src/text/mod.rs @@ -53,6 +53,12 @@ pub struct FieldStyle { button: f32, } +/// The space above the first and below the last of the lines of a multi-line text in a field of `metrics`, its lines +/// `line` high: that of a single line in its control, so a box of `n` lines shows them whole. +pub(crate) fn lines_inset(metrics: &Metrics, line: f32) -> f32 { + ((metrics.min_size.y - line) / 2.0).max(0.0) +} + impl FieldStyle { /// The style a text-like field reads from the theme. pub fn at(cx: &StyleCx<'_>) -> FieldStyle { diff --git a/crates/ui-widgets/src/text/text_field.rs b/crates/ui-widgets/src/text/text_field.rs index 50c11b2a8..7de2a9b04 100644 --- a/crates/ui-widgets/src/text/text_field.rs +++ b/crates/ui-widgets/src/text/text_field.rs @@ -4,13 +4,14 @@ pub(crate) mod stories; use super::adorn::{self, Adornment}; -use super::{FieldStyle, completions, text_part, words}; +use super::{FieldStyle, completions, lines_inset, text_part, words}; use crate::contract::{ Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, OverlayUi, PaintUi, PartsCx, Pointer as _, Props as _, Role, Sizing, Styled as _, WordSetCx as _, }; use crate::primitives::{Completer, IconSpec, SubmitKey, TabKey, TextOptions, TextRun, Wrap}; use crate::runtime::env::{Label, Message}; +use crate::style::Span; use craft_behavior::{Codec, EditEvent, IconId, Mixed, PartKey, PlainText, Proposal, Sense}; use egui::{Id, Rect, Vec2}; use smallvec::SmallVec; @@ -339,6 +340,18 @@ impl TextField<'_, C> { } } +impl TextField<'_, C> { + /// `text` as the editor sets it. + fn run<'t>(&self, text: impl Into>) -> TextRun<'t> { + if self.options.mono { TextRun::spans(vec![Span::new(text).mono()]) } else { TextRun::new(text).role(self.options.role) } + } + + /// The height of one line of the editor's text. + fn line(&self, cx: &MeasureCx<'_, Self>) -> f32 { + cx.layout(&self.run("M")).size().y + } +} + impl Component for TextField<'_, C> { const ROLE: Role = Role::TEXT_INPUT; const INPUT: crate::contract::Input = crate::contract::Input::Text; @@ -373,10 +386,10 @@ impl Component for TextField<'_, C> { // field's own text wrapped as the editor wraps it, not the codec's width sample, which a plain field leaves // empty. if let Some(lines) = self.options.lines.filter(|_| self.options.multiline()) { - let line = cx.layout(&TextRun::new("M").role(self.options.role)).size().y; - let height = |lines: usize| cx.style.metrics().min_size.y + line * lines.saturating_sub(1) as f32; + let line = self.line(cx); + let height = |lines: usize| 2.0 * lines_inset(&cx.style.metrics, line) + line * lines as f32; let shown = cx.value().map(|value| self.codec.format(value)).unwrap_or_default(); - let tall = cx.layout(&TextRun::new(shown).role(self.options.role).wrap(Wrap::Wrap)).size().y; + let tall = cx.layout(&self.run(shown).wrap(Wrap::Wrap)).size().y; let rows = if line > 0.0 { (tall / line).round() as usize } else { 1 }; size.y = size.y.max(height(lines.max(rows))); if let Some(most) = self.max_lines { @@ -416,12 +429,16 @@ impl Component for TextField<'_, C> { let clear = self.clear_shown(value).then(|| Self::clear_name(&cx.words::())); let trailing = self.trailing(clear, cx.env.get()); self.leading = std::mem::take(&mut self.leading).into_iter().map(|adornment| adornment.measured(cx)).collect(); - let placed = adorn::place(cx.content_rect, cx.style, &self.leading, &trailing, cx.flow.is_rtl()); + let mut placed = adorn::place(cx.content_rect, cx.style, &self.leading, &trailing, cx.flow.is_rtl()); for (adornment, rect) in trailing.iter().zip(&placed.trailing) { if adornment.part() == Some(CLEAR) { cx.part(CLEAR, *rect).sense(Sense::CLICK | Sense::KEEP_FOCUS).pointer_only("Escape clears the text"); } } + if self.options.multiline() { + let inset = lines_inset(&cx.style.metrics, self.line(cx)); + placed.text = placed.text.shrink2(Vec2::new(0.0, inset.min(placed.text.height() / 2.0))); + } if let Some(start) = value.or(self.empty.as_ref()) { let limit = self.limit.effective(self.options.multiline()); // The bound caret goes to the text part every pass; the part places it only when it differs from the caret