From 49909080ec1f71249790beb9588bb80294e81d57 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Mon, 16 Nov 2026 11:33:10 -0600 Subject: [PATCH] Resolve logical text alignment above the egui runtime layer --- .../ui-contract/src/primitives/text_adapter.rs | 12 ++++++++---- .../ui-contract/src/runtime/egui/text_edit.rs | 18 +++++------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index cce3cd8a0..f25de8807 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -408,8 +408,13 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut filter, char_limit: input.options.limit, preedit: preedit.clone(), - rtl: input.rtl, - align: input.options.align, + // Resolve the same logical placement as text::free_room_before at this layer: the low-level editor + // receives only egui's physical alignment, not a dependency on the text primitives. + align: match (input.options.align, input.rtl) { + (HAlign::Center, _) => egui::Align2::CENTER_CENTER, + (HAlign::Start, true) | (HAlign::End, false) => egui::Align2::RIGHT_CENTER, + (HAlign::Start, false) | (HAlign::End, true) => egui::Align2::LEFT_CENTER, + }, caret_visible: input.caret_visible, underline: input.underline, galley: None, @@ -521,8 +526,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) { filter: egui_focus::event_filter(keep, false), char_limit: None, preedit: None, - rtl: false, - align: HAlign::Start, + align: egui::Align2::LEFT_TOP, caret_visible: false, underline: 0.0, galley: Some(layout.galley.clone()), diff --git a/crates/ui-contract/src/runtime/egui/text_edit.rs b/crates/ui-contract/src/runtime/egui/text_edit.rs index 058057328..dbfe43418 100644 --- a/crates/ui-contract/src/runtime/egui/text_edit.rs +++ b/crates/ui-contract/src/runtime/egui/text_edit.rs @@ -1,6 +1,5 @@ //! Building `TextEdit` and its state: the one caller of egui's text editing. -use crate::primitives::HAlign; use craft_theme::TypeRole; use egui::text::{CCursor, CCursorRange, LayoutJob, TextFormat}; use egui::text_edit::TextEditState; @@ -31,9 +30,8 @@ pub(crate) struct Edit<'a> { pub char_limit: Option, /// The bytes of `text` that are IME preedit, underlined. pub preedit: Option>, - pub rtl: bool, - /// Where the text sits in the rect, relative to the interface's reading direction. - pub align: HAlign, + /// Physical placement resolved by the text adapter, which owns the logical reading direction. + pub align: egui::Align2, /// Whether the caret is drawn in this pass. pub caret_visible: bool, /// The width of the preedit underline. @@ -75,7 +73,6 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown { filter, char_limit, preedit, - rtl, align, caret_visible, underline, @@ -167,14 +164,9 @@ 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. Other text follows the same logical placement - // rule as primitives::text::free_room_before, with the row centred vertically. - .align(match (align, rtl) { - _ if galley.is_some() => egui::Align2::LEFT_TOP, - (HAlign::Center, _) => egui::Align2::CENTER_CENTER, - (HAlign::Start, true) | (HAlign::End, false) => egui::Align2::RIGHT_CENTER, - (HAlign::Start, false) | (HAlign::End, true) => egui::Align2::LEFT_CENTER, - }); + // A pre-laid galley sits at the rect's corner. Other text uses the physical placement + // the adapter resolved, without the runtime depending on its higher-level text types. + .align(if galley.is_some() { egui::Align2::LEFT_TOP } else { align }); if let Some(hint) = hint_text.clone() { edit = edit.hint_text(hint); }