diff --git a/src/editor/cursor.rs b/src/editor/cursor.rs index 2db8c1d..475e5e0 100644 --- a/src/editor/cursor.rs +++ b/src/editor/cursor.rs @@ -4,6 +4,8 @@ use skia_safe::Color4f; use crate::editor::style::{Colors, Style}; +use super::grid::GridCell; + #[derive(Debug, Clone, PartialEq)] pub enum CursorShape { Block, @@ -44,7 +46,7 @@ pub struct Cursor { pub style: Option>, pub enabled: bool, pub double_width: bool, - pub character: String, + pub grid_cell: GridCell, } impl Cursor { @@ -60,7 +62,7 @@ impl Cursor { blinkoff: None, enabled: true, double_width: false, - character: " ".to_string(), + grid_cell: (" ".to_string(), None), } } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index c187f45..d69b271 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -387,12 +387,12 @@ impl Editor { fn send_cursor_info(&mut self) { let (grid_left, grid_top) = self.cursor.grid_position; if let Some(window) = self.windows.get(&self.cursor.parent_window_id) { - let (character, double_width) = window.get_cursor_character(grid_left, grid_top); - self.cursor.character = character; + let (character, style, double_width) = window.get_cursor_grid_cell(grid_left, grid_top); + self.cursor.grid_cell = (character, style); self.cursor.double_width = double_width; } else { self.cursor.double_width = false; - self.cursor.character = " ".to_string(); + self.cursor.grid_cell = (" ".to_string(), None); } self.draw_command_batcher .queue(DrawCommand::UpdateCursor(self.cursor.clone())) diff --git a/src/editor/window.rs b/src/editor/window.rs index 4264a57..9fa9021 100644 --- a/src/editor/window.rs +++ b/src/editor/window.rs @@ -63,10 +63,14 @@ impl Window { }); } - pub fn get_cursor_character(&self, window_left: u64, window_top: u64) -> (String, bool) { - let character = match self.grid.get_cell(window_left, window_top) { - Some((character, _)) => character.clone(), - _ => ' '.to_string(), + pub fn get_cursor_grid_cell( + &self, + window_left: u64, + window_top: u64, + ) -> (String, Option>, bool) { + let grid_cell = match self.grid.get_cell(window_left, window_top) { + Some((character, style)) => (character.clone(), style.clone()), + _ => (' '.to_string(), None), }; let double_width = match self.grid.get_cell(window_left + 1, window_top) { @@ -74,7 +78,7 @@ impl Window { _ => false, }; - (character, double_width) + (grid_cell.0, grid_cell.1, double_width) } pub fn get_width(&self) -> u64 { diff --git a/src/renderer/cursor_renderer/mod.rs b/src/renderer/cursor_renderer/mod.rs index 9ba773c..ebb08f7 100644 --- a/src/renderer/cursor_renderer/mod.rs +++ b/src/renderer/cursor_renderer/mod.rs @@ -273,7 +273,7 @@ impl CursorRenderer { let mut paint = Paint::new(skia_safe::colors::WHITE, None); paint.set_anti_alias(settings.antialiasing); - let character = self.cursor.character.clone(); + let character = self.cursor.grid_cell.0.clone(); let mut cursor_width = grid_renderer.font_dimensions.width; if self.cursor.double_width && self.cursor.shape == CursorShape::Block { @@ -341,52 +341,58 @@ impl CursorRenderer { self.previous_editor_mode = current_mode.clone(); } - if self.cursor.enabled && render { - // Draw Background - let background_color = self - .cursor - .background(&grid_renderer.default_style.colors) - .to_color(); - paint.set_color(background_color); - - // The cursor is made up of four points, so I create a path with each of the four - // corners. - let mut path = Path::new(); - - path.move_to(self.corners[0].current_position); - path.line_to(self.corners[1].current_position); - path.line_to(self.corners[2].current_position); - path.line_to(self.corners[3].current_position); - path.close(); - - canvas.draw_path(&path, &paint); - - // Draw foreground - let foreground_color = self - .cursor - .foreground(&grid_renderer.default_style.colors) - .to_color(); - paint.set_color(foreground_color); - - canvas.save(); - canvas.clip_path(&path, None, Some(false)); - - let y_adjustment = grid_renderer.shaper.y_adjustment(); - let blobs = &grid_renderer.shaper.shape_cached(character, false, false); - - for blob in blobs.iter() { - canvas.draw_text_blob( - &blob, - (self.destination.x, self.destination.y + y_adjustment as f32), - &paint, - ); - } + if !(self.cursor.enabled && render) { + return; + } + // Draw Background + let background_color = self + .cursor + .background(&grid_renderer.default_style.colors) + .to_color(); + paint.set_color(background_color); + + // The cursor is made up of four points, so I create a path with each of the four + // corners. + let mut path = Path::new(); + + path.move_to(self.corners[0].current_position); + path.line_to(self.corners[1].current_position); + path.line_to(self.corners[2].current_position); + path.line_to(self.corners[3].current_position); + path.close(); + + canvas.draw_path(&path, &paint); + + // Draw foreground + let foreground_color = self + .cursor + .foreground(&grid_renderer.default_style.colors) + .to_color(); + paint.set_color(foreground_color); + + canvas.save(); + canvas.clip_path(&path, None, Some(false)); + + let y_adjustment = grid_renderer.shaper.y_adjustment(); + let style = &self.cursor.grid_cell.1; + + let bold = style.as_ref().map(|x| x.bold).unwrap_or(false); + let italic = style.as_ref().map(|x| x.italic).unwrap_or(false); + + let blobs = &grid_renderer.shaper.shape_cached(character, bold, italic); + + for blob in blobs.iter() { + canvas.draw_text_blob( + &blob, + (self.destination.x, self.destination.y + y_adjustment as f32), + &paint, + ); + } - canvas.restore(); + canvas.restore(); - if let Some(vfx) = self.cursor_vfx.as_ref() { - vfx.render(&settings, canvas, grid_renderer, &self.cursor); - } + if let Some(vfx) = self.cursor_vfx.as_ref() { + vfx.render(&settings, canvas, grid_renderer, &self.cursor); } } }