Skip to content

Commit

Permalink
Merge pull request #138 from StaffEngineer/allow-cursor
Browse files Browse the repository at this point in the history
allow setting cursor color
  • Loading branch information
jackpot51 authored Jun 8, 2023
2 parents 9062cce + 0eb08d2 commit a93ec8a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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),
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a93ec8a

Please sign in to comment.