From 4610b7c67313ac31bf858960d824b021cd2d2699 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Fri, 17 Apr 2026 08:55:58 +0200 Subject: [PATCH] Don't hide whitespaces in centered and right aligned text edits (#8102) When enabled, trailing whitespace is included in the row width used for horizontal alignment. This is useful for text editors where hiding trailing spaces feels wrong when typing. By default this is `false`, preserving existing behavior where trailing whitespace is stripped for alignment (so "Hello " centers the same as "Hello"). * follow up to https://github.com/emilk/egui/pull/8082 --- crates/egui/src/widgets/text_edit/builder.rs | 2 ++ crates/epaint/src/text/fonts.rs | 1 + crates/epaint/src/text/text_layout.rs | 4 ++++ crates/epaint/src/text/text_layout_types.rs | 11 +++++++++++ 4 files changed, 18 insertions(+) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 416cd8c9b..7cb28d8f8 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -486,6 +486,8 @@ impl TextEdit<'_> { LayoutJob::simple_singleline(text, font_id_clone.clone(), text_color) }; layout_job.halign = align.x(); + // We want to keep the trailing whitespace, since hiding it feels really weird when typing + layout_job.keep_trailing_whitespace = true; ui.fonts_mut(|f| f.layout_job(layout_job)) }; diff --git a/crates/epaint/src/text/fonts.rs b/crates/epaint/src/text/fonts.rs index f1a6e63b4..44b68f5b2 100644 --- a/crates/epaint/src/text/fonts.rs +++ b/crates/epaint/src/text/fonts.rs @@ -1035,6 +1035,7 @@ impl GalleyCache { 0.0 }, round_output_to_gui: job.round_output_to_gui, + keep_trailing_whitespace: job.keep_trailing_whitespace, }; // Add overlapping sections: diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs index 21dd7996c..44ee3577f 100644 --- a/crates/epaint/src/text/text_layout.rs +++ b/crates/epaint/src/text/text_layout.rs @@ -162,6 +162,7 @@ pub fn layout(fonts: &mut FontsImpl, pixels_per_point: f32, job: Arc) job.halign, job.wrap.max_width, justify_row, + job.keep_trailing_whitespace, ); } } @@ -855,6 +856,7 @@ fn halign_and_justify_row( halign: Align, wrap_width: f32, justify: bool, + keep_trailing_whitespace: bool, ) { #![expect(clippy::useless_let_if_seq)] // False positive @@ -873,6 +875,8 @@ fn halign_and_justify_row( let glyph_range = if num_leading_spaces == row.glyphs.len() { // There is only whitespace (0, row.glyphs.len()) + } else if keep_trailing_whitespace { + (num_leading_spaces, row.glyphs.len()) } else { let num_trailing_spaces = row .glyphs diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs index fc987890d..e15028fec 100644 --- a/crates/epaint/src/text/text_layout_types.rs +++ b/crates/epaint/src/text/text_layout_types.rs @@ -79,6 +79,14 @@ pub struct LayoutJob { /// Round output sizes using [`emath::GuiRounding`], to avoid rounding errors in layout code. pub round_output_to_gui: bool, + + /// If `false` (default), trailing whitespace is ignored when computing + /// horizontal alignment ([`Self::halign`]). + /// This is desirable for labels so that e.g. "Hello " centers the same as "Hello". + /// + /// If `true`, trailing whitespace is included in the row width used for alignment. + /// This is desirable for text editors where the user expects to see their spaces. + pub keep_trailing_whitespace: bool, } impl Default for LayoutJob { @@ -93,6 +101,7 @@ impl Default for LayoutJob { halign: Align::LEFT, justify: false, round_output_to_gui: true, + keep_trailing_whitespace: false, } } } @@ -216,6 +225,7 @@ impl std::hash::Hash for LayoutJob { halign, justify, round_output_to_gui, + keep_trailing_whitespace, } = self; text.hash(state); @@ -226,6 +236,7 @@ impl std::hash::Hash for LayoutJob { halign.hash(state); justify.hash(state); round_output_to_gui.hash(state); + keep_trailing_whitespace.hash(state); } }