mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 14:49:06 -04:00
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
This commit is contained in:
@@ -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))
|
||||
};
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -162,6 +162,7 @@ pub fn layout(fonts: &mut FontsImpl, pixels_per_point: f32, job: Arc<LayoutJob>)
|
||||
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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user