1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Enables every combination of TextEdit and LayoutJob alignments (#7831)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

This is a fix/improvement that makes all kinds of alignments work in
`TextEdit` when a custom `LayoutJob` with halign is used.


I used the simplest approach possible to avoid unwanted bugs as I wasn't
sure what's safe to change, but there's potentially better ways to
achieve this. In particular, I'm not sure I fully understand the
rationale behind aligning rows in a `Galley` based on the latter's
leftmost border, considering the size is what's ultimately used in
widgets (in `TextEdit` at least).

Regardless, here's a demo of this PR:


https://github.com/user-attachments/assets/5d9801d7-73af-4576-80c5-47f169700462


* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
RndUsr123
2026-03-24 10:35:37 +00:00
committed by GitHub
parent 7fbd1315ec
commit 2a03ae1348
4 changed files with 63 additions and 4 deletions

View File

@@ -820,7 +820,11 @@ impl TextEdit<'_> {
paint_text_selection(&mut galley, ui.visuals(), &cursor_range, None);
}
painter.galley(galley_pos, Arc::clone(&galley), text_color);
painter.galley(
galley_pos - vec2(galley.rect.left(), 0.0),
Arc::clone(&galley),
text_color,
);
if has_focus && let Some(cursor_range) = state.cursor.range(&galley) {
let primary_cursor_rect = cursor_rect(&galley, &cursor_range.primary, row_height)

View File

@@ -1047,7 +1047,7 @@ impl Galley {
return self.end_pos();
};
let x = row.x_offset(layout_cursor.column);
let x = row.x_offset(layout_cursor.column) + row.pos.x - self.rect.left();
Rect::from_min_max(pos2(x, row.min_y()), pos2(x, row.max_y()))
}
@@ -1092,7 +1092,7 @@ impl Galley {
if is_pos_within_row || y_dist < best_y_dist {
best_y_dist = y_dist;
// char_at is `Row` not `PlacedRow` relative which means we have to subtract the pos.
let column = row.char_at(pos.x - row.pos.x);
let column = row.char_at(pos.x - row.pos.x + self.rect.left());
let prefer_next_row = column < row.char_count_excluding_newline();
cursor = CCursor {
index: ccursor_index + column,