From b73367976017c7868946ae330c0e30cbb9d5a56c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 3 Mar 2026 13:27:56 +0100 Subject: [PATCH] Fix text color when selecting newline character (#7951) * Closes https://github.com/emilk/egui/issues/7865 --- crates/egui/src/text_selection/visuals.rs | 4 +-- crates/egui_demo_lib/tests/misc.rs | 34 +++++++++++-------- .../tests/snapshots/text_selection.png | 3 -- .../tests/snapshots/text_selection_0.png | 3 ++ .../tests/snapshots/text_selection_1.png | 3 ++ 5 files changed, 27 insertions(+), 20 deletions(-) delete mode 100644 crates/egui_demo_lib/tests/snapshots/text_selection.png create mode 100644 crates/egui_demo_lib/tests/snapshots/text_selection_0.png create mode 100644 crates/egui_demo_lib/tests/snapshots/text_selection_1.png diff --git a/crates/egui/src/text_selection/visuals.rs b/crates/egui/src/text_selection/visuals.rs index fead390fe..e41d7a436 100644 --- a/crates/egui/src/text_selection/visuals.rs +++ b/crates/egui/src/text_selection/visuals.rs @@ -67,9 +67,7 @@ pub fn paint_text_selection( let first_vertex_index = row .glyphs .get(first_glyph_index) - .map_or(row.visuals.glyph_vertex_range.start, |g| { - g.first_vertex as _ - }); + .map_or(row.visuals.glyph_vertex_range.end, |g| g.first_vertex as _); let last_vertex_index = row .glyphs .get(last_glyph_index) diff --git a/crates/egui_demo_lib/tests/misc.rs b/crates/egui_demo_lib/tests/misc.rs index 8abc69d19..d5f6a3a3c 100644 --- a/crates/egui_demo_lib/tests/misc.rs +++ b/crates/egui_demo_lib/tests/misc.rs @@ -59,21 +59,27 @@ fn test_italics() { #[test] fn test_text_selection() { - let mut harness = Harness::builder().build_ui(|ui| { - let visuals = ui.visuals_mut(); - visuals.selection.bg_fill = Color32::LIGHT_GREEN; - visuals.selection.stroke.color = Color32::DARK_BLUE; + let mut results = egui_kittest::SnapshotResults::new(); - ui.label("Some varied ☺ text :)\nAnd it has a second line!"); - }); - harness.run(); - harness.fit_contents(); + for (test_idx, drag_start_x) in [0.2_f32, 0.9].into_iter().enumerate() { + let mut harness = Harness::builder().build_ui(|ui| { + let visuals = ui.visuals_mut(); + visuals.selection.bg_fill = Color32::LIGHT_GREEN; + visuals.selection.stroke.color = Color32::RED; - // Drag to select text: - let label = harness.get_by_role(Role::Label); - harness.drag_at(label.rect().lerp_inside([0.2, 0.25])); - harness.drop_at(label.rect().lerp_inside([0.6, 0.75])); - harness.run(); + ui.label("Some varied ☺ text :)\nAnd it has a second line!"); + }); + harness.run(); + harness.fit_contents(); - harness.snapshot("text_selection"); + // Drag to select text: + let label = harness.get_by_role(Role::Label); + harness.drag_at(label.rect().lerp_inside([drag_start_x, 0.25])); + harness.drop_at(label.rect().lerp_inside([0.6, 0.75])); + harness.run(); + + harness.snapshot(format!("text_selection_{test_idx}")); + + results.extend_harness(&mut harness); + } } diff --git a/crates/egui_demo_lib/tests/snapshots/text_selection.png b/crates/egui_demo_lib/tests/snapshots/text_selection.png deleted file mode 100644 index 63a4423a3..000000000 --- a/crates/egui_demo_lib/tests/snapshots/text_selection.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0475c5ac04ab8f79b79d43cfdb985f05b61dbe90e81f898a6dc216c308a28841 -size 4707 diff --git a/crates/egui_demo_lib/tests/snapshots/text_selection_0.png b/crates/egui_demo_lib/tests/snapshots/text_selection_0.png new file mode 100644 index 000000000..7930dff48 --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/text_selection_0.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:344d90928510855dc718a2e36e31a97f084f1163ab750d0217fb8620469b621a +size 5276 diff --git a/crates/egui_demo_lib/tests/snapshots/text_selection_1.png b/crates/egui_demo_lib/tests/snapshots/text_selection_1.png new file mode 100644 index 000000000..8691211cb --- /dev/null +++ b/crates/egui_demo_lib/tests/snapshots/text_selection_1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60449af267336663304e44e254d0984e037bebfa2d1efdf32234cab4374e8c79 +size 5301