Merge pull request #138 from StaffEngineer/allow-cursor

allow setting cursor color
This commit is contained in:
Jeremy Soller 2023-06-08 13:12:35 -06:00 committed by GitHub
commit a93ec8adf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View file

@ -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 {

View file

@ -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<Color>,
}
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),
}
}
}

View file

@ -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() {