From 0eb08d218c0584f113194a05d46437cb1d726749 Mon Sep 17 00:00:00 2001 From: Dima Rets Date: Thu, 8 Jun 2023 15:32:32 +0100 Subject: [PATCH] allow setting cursor color --- src/attrs.rs | 2 +- src/buffer.rs | 18 ++++++++++++++---- src/edit/editor.rs | 23 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/attrs.rs b/src/attrs.rs index 3d69db0882..92d90e1019 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -11,7 +11,7 @@ pub use fontdb::{Family, Stretch, Style, Weight}; use rangemap::RangeMap; /// Text color -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, PartialOrd, Ord, Eq, Hash, PartialEq)] pub struct Color(pub u32); impl Color { diff --git a/src/buffer.rs b/src/buffer.rs index e459e723e4..7ddd20d873 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -8,11 +8,9 @@ use alloc::{ use core::{cmp, fmt}; use unicode_segmentation::UnicodeSegmentation; -#[cfg(feature = "swash")] -use crate::Color; use crate::{ - Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, FontSystem, LayoutGlyph, LayoutLine, - ShapeLine, Shaping, Wrap, + Attrs, AttrsList, BorrowedWithFontSystem, BufferLine, Color, FontSystem, LayoutGlyph, + LayoutLine, ShapeLine, Shaping, Wrap, }; /// Current cursor location @@ -25,6 +23,8 @@ pub struct Cursor { /// Whether to associate the cursor with the run before it or the run after it if placed at the /// boundary between two runs pub affinity: Affinity, + /// Cursor color + pub color: Option, } impl Cursor { @@ -39,6 +39,16 @@ impl Cursor { line, index, affinity, + color: None, + } + } + /// Create a new cursor, specifying the color + pub const fn new_with_color(line: usize, index: usize, color: Color) -> Self { + Self { + line, + index, + affinity: Affinity::Before, + color: Some(color), } } } diff --git a/src/edit/editor.rs b/src/edit/editor.rs index ff2f00b4d3..2c7d5c3f3e 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -36,6 +36,17 @@ impl Editor { } } + /// Create a new [`Editor`] with the provided [`Buffer`] and [`Cursor`] + pub fn new_with_cursor(buffer: Buffer, cursor: Cursor) -> Self { + Self { + buffer, + cursor, + cursor_x_opt: None, + select_opt: None, + cursor_moved: false, + } + } + fn set_layout_cursor(&mut self, font_system: &mut FontSystem, cursor: LayoutCursor) { let layout = self .buffer @@ -556,7 +567,9 @@ impl Edit for Editor { if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) { if new_cursor != self.cursor { + let color = self.cursor.color; self.cursor = new_cursor; + self.cursor.color = color; self.buffer.set_redraw(true); } } @@ -569,7 +582,9 @@ impl Edit for Editor { if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) { if new_cursor != self.cursor { + let color = self.cursor.color; self.cursor = new_cursor; + self.cursor.color = color; self.buffer.set_redraw(true); } } @@ -833,7 +848,13 @@ impl Edit for Editor { }, }; - f(x, (line_y - font_size) as i32, 1, line_height as u32, color); + f( + x, + (line_y - font_size) as i32, + 1, + line_height as u32, + self.cursor.color.unwrap_or(color), + ); } for glyph in run.glyphs.iter() {