diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 8a35afc42e..0c4f46f786 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -17,6 +17,7 @@ fn redraw( let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF); let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33); + let selected_text_color = Color::rgb(0xF0, 0xF0, 0xFF); editor.shape_as_needed(true); if editor.redraw() { @@ -29,6 +30,7 @@ fn redraw( font_color, cursor_color, selection_color, + selected_text_color, |x, y, w, h, color| { window.rect(x, y, w, h, orbclient::Color { data: color.0 }); }, diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index 2c42b73eb2..1044ea8015 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -138,6 +138,7 @@ fn main() { let font_color = Color::rgb(0xFF, 0xFF, 0xFF); let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF); let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33); + let selected_text_color = Color::rgb(0xA0, 0xA0, 0xFF); event_loop .run(|event, elwt| { @@ -193,6 +194,7 @@ fn main() { font_color, cursor_color, selection_color, + selected_text_color, |x, y, w, h, color| { // Note: due to softbuffer and tiny_skia having incompatible internal color representations we swap // the red and blue channels here diff --git a/src/edit/editor.rs b/src/edit/editor.rs index d2d6c04fcb..cdd2a68b4f 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -50,10 +50,12 @@ impl<'buffer> Editor<'buffer> { text_color: Color, cursor_color: Color, selection_color: Color, + selected_text_color: Color, mut f: F, ) where F: FnMut(i32, i32, u32, u32, Color), { + let selection_bounds = self.selection_bounds(); self.with_buffer(|buffer| { for run in buffer.layout_runs() { let line_i = run.line_i; @@ -98,7 +100,7 @@ impl<'buffer> Editor<'buffer> { }; // Highlight selection - if let Some((start, end)) = self.selection_bounds() { + if let Some((start, end)) = selection_bounds { if line_i >= start.line && line_i <= end.line { let mut range_opt = None; for glyph in run.glyphs.iter() { @@ -191,10 +193,21 @@ impl<'buffer> Editor<'buffer> { for glyph in run.glyphs.iter() { let physical_glyph = glyph.physical((0., 0.), 1.0); - let glyph_color = match glyph.color_opt { + let mut glyph_color = match glyph.color_opt { Some(some) => some, None => text_color, }; + if text_color != selected_text_color { + if let Some((start, end)) = selection_bounds { + if line_i >= start.line + && line_i <= end.line + && (start.line != line_i || glyph.end > start.index) + && (end.line != line_i || glyph.start < end.index) + { + glyph_color = selected_text_color; + } + } + } cache.with_pixels( font_system, @@ -880,6 +893,7 @@ impl<'font_system, 'buffer> BorrowedWithFontSystem<'font_system, Editor<'buffer> text_color: Color, cursor_color: Color, selection_color: Color, + selected_text_color: Color, f: F, ) where F: FnMut(i32, i32, u32, u32, Color), @@ -890,6 +904,7 @@ impl<'font_system, 'buffer> BorrowedWithFontSystem<'font_system, Editor<'buffer> text_color, cursor_color, selection_color, + selected_text_color, f, ); } diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 5ad7b0add4..d63c0e573e 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -208,6 +208,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> { self.foreground_color(), self.cursor_color(), self.selection_color(), + self.foreground_color(), f, ); }