Skip to content

Commit

Permalink
Add an option to set selected text color
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche authored and jackpot51 committed Jun 10, 2024
1 parent 3c94352 commit 10ae9a9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/editor-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 });
},
Expand Down
2 changes: 2 additions & 0 deletions examples/rich-text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 55 in src/edit/editor.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> src/edit/editor.rs:46:5 | 46 | / pub fn draw<F>( 47 | | &self, 48 | | font_system: &mut FontSystem, 49 | | cache: &mut crate::SwashCache, ... | 54 | | mut f: F, 55 | | ) where | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand All @@ -890,6 +904,7 @@ impl<'font_system, 'buffer> BorrowedWithFontSystem<'font_system, Editor<'buffer>
text_color,
cursor_color,
selection_color,
selected_text_color,
f,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/edit/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down

0 comments on commit 10ae9a9

Please sign in to comment.