gpui: Fix text-align with nowrap mode (#24116)

Release Notes:

- N/A


------

- Continue #24090 to fix text align for when used `whitespace_nowrap`.
- Fix wrapped line length calculation.

And add example

```
cargo run -p gpui --example text_layout
```

<img width="760" alt="image"
src="https://github.com/user-attachments/assets/a087c300-0e0e-4a80-98c6-90161a9b0905"
/>

---------

Co-authored-by: Owen Law <owenlaw222@gmail.com>
This commit is contained in:
Jason Lee
2025-02-06 18:51:00 -08:00
committed by GitHub
co-authored by Owen Law
parent e689c8c01b
commit c5913899d9
4 changed files with 83 additions and 16 deletions
+1 -1
View File
@@ -1684,7 +1684,7 @@ impl Interactivity {
.ok()
.and_then(|mut text| text.pop())
{
text.paint(hitbox.origin, FONT_SIZE, TextAlign::Left, window, cx)
text.paint(hitbox.origin, FONT_SIZE, TextAlign::Left, None, window, cx)
.ok();
let text_bounds = crate::Bounds {
+9 -2
View File
@@ -392,8 +392,15 @@ impl TextLayout {
let mut line_origin = bounds.origin;
let text_style = window.text_style();
for line in &element_state.lines {
line.paint(line_origin, line_height, text_style.text_align, window, cx)
.log_err();
line.paint(
line_origin,
line_height,
text_style.text_align,
Some(bounds),
window,
cx,
)
.log_err();
line_origin.y += line.size(line_height).height;
}
}
+9 -13
View File
@@ -107,15 +107,21 @@ impl WrappedLine {
origin: Point<Pixels>,
line_height: Pixels,
align: TextAlign,
bounds: Option<Bounds<Pixels>>,
window: &mut Window,
cx: &mut App,
) -> Result<()> {
let align_width = match bounds {
Some(bounds) => Some(bounds.size.width),
None => self.layout.wrap_width,
};
paint_line(
origin,
&self.layout.unwrapped_layout,
line_height,
align,
self.layout.wrap_width,
align_width,
&self.decoration_runs,
&self.wrap_boundaries,
window,
@@ -222,7 +228,7 @@ fn paint_line(
glyph_origin.x = aligned_origin_x(
origin,
align_width.unwrap_or(layout.width),
prev_glyph_position.x,
glyph.position.x,
&align,
layout,
wraps.peek(),
@@ -426,17 +432,7 @@ fn aligned_origin_x(
wrap_boundary: Option<&&WrapBoundary>,
) -> Pixels {
let end_of_line = if let Some(WrapBoundary { run_ix, glyph_ix }) = wrap_boundary {
if layout.runs[*run_ix].glyphs.len() == glyph_ix + 1 {
// Next glyph is in next run
layout
.runs
.get(run_ix + 1)
.and_then(|run| run.glyphs.first())
.map_or(layout.width, |glyph| glyph.position.x)
} else {
// Get next glyph
layout.runs[*run_ix].glyphs[*glyph_ix + 1].position.x
}
layout.runs[*run_ix].glyphs[*glyph_ix].position.x
} else {
layout.width
};