From 1d620acc5a5f8189d3471c58e1815f6bace892ef Mon Sep 17 00:00:00 2001 From: keith Date: Fri, 27 Dec 2019 23:23:10 -0800 Subject: [PATCH] horizontal scrolling and progress toward command line --- src/editor/command_line.rs | 42 +++++++++++++++++---------------- src/editor/mod.rs | 17 +++++++++++++ src/renderer/caching_shaper.rs | 15 ++++++++---- src/renderer/cursor_renderer.rs | 4 ++-- src/renderer/mod.rs | 2 +- src/window.rs | 21 +++++++++++++---- 6 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/editor/command_line.rs b/src/editor/command_line.rs index 8b18d7d..a609d6f 100644 --- a/src/editor/command_line.rs +++ b/src/editor/command_line.rs @@ -28,26 +28,28 @@ impl CommandLine { pub fn draw(&self, window_size: (u64, u64), defined_styles: &HashMap) -> Vec { let mut draw_commands = Vec::new(); - if self.content.len() > 0 { - let (width, height) = window_size; - let text_length: usize = self.content.iter().map(|(_, text)| text.len()).sum(); - - let text_width = text_length * COMMAND_SCALE as usize; - let text_height = COMMAND_SCALE; - - let x = (width / 2) - (text_width as u64 / 2); - let y = (height / 2) - (text_height as u64 / 2); - - let mut start_x = x; - let mut commands = self.content.iter().map(|(style_id, text)| { - let command_width = text.len() * 2; - let style = defined_styles.get(style_id).map(|style| style.clone()); - let mut command = DrawCommand::new(text.clone(), (start_x, y), style); - command.scale = COMMAND_SCALE; - start_x = start_x + command_width as u64; - command - }).collect::>(); - draw_commands.append(&mut commands); + if self.visible { + if self.content.len() > 0 { + let (width, height) = window_size; + let text_length: usize = self.content.iter().map(|(_, text)| text.len()).sum(); + + let text_width = text_length * COMMAND_SCALE as usize; + let text_height = COMMAND_SCALE; + + let x = (width / 2) - (text_width as u64 / 2); + let y = (height / 2) - (text_height as u64 / 2); + + let mut start_x = x; + let mut commands = self.content.iter().map(|(style_id, text)| { + let command_width = text.len() * 2; + let style = defined_styles.get(style_id).map(|style| style.clone()); + let mut command = DrawCommand::new(text.clone(), (start_x, y), style); + command.scale = COMMAND_SCALE; + start_x = start_x + command_width as u64; + command + }).collect::>(); + draw_commands.append(&mut commands); + } } draw_commands } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 43a150f..c8a6534 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -23,6 +23,20 @@ pub struct DrawCommand { pub scale: u16 } +impl DrawCommand { + pub fn set_coverage(&self, dirty: &mut Vec>) { + let (left, top) = self.grid_position; + let text_width = self.text.chars().count() * self.scale as usize; + + for y in top..(top + self.scale as u64 - 1) { + let row = &mut dirty[y as usize]; + for x in left..(left + text_width as u64 - 1) { + row[x as usize] = true; + } + } + } +} + pub struct Editor { pub grid: Vec>, pub dirty: Vec>, @@ -136,6 +150,9 @@ impl Editor { }).collect::>(); let mut command_line_draw_commands = self.command_line.draw(self.size, &self.defined_styles); + for command_line_draw_command in command_line_draw_commands.iter() { + command_line_draw_command.set_coverage(&mut self.dirty); + } draw_commands.append(&mut command_line_draw_commands); let (width, height) = self.size; diff --git a/src/renderer/caching_shaper.rs b/src/renderer/caching_shaper.rs index 19dde4c..8147f26 100644 --- a/src/renderer/caching_shaper.rs +++ b/src/renderer/caching_shaper.rs @@ -3,7 +3,7 @@ use skulpin::skia_safe::{Shaper, TextBlob, Font, Point}; pub struct CachingShaper { shaper: Shaper, - cache: LruCache + cache: LruCache<(String, u16), TextBlob> } impl CachingShaper { @@ -19,11 +19,16 @@ impl CachingShaper { blob } - pub fn shape_cached(&mut self, text: String, font: &Font) -> &TextBlob { - if !self.cache.contains(&text) { - self.cache.put(text.clone(), self.shape(&text, &font)); + pub fn shape_cached(&mut self, text: String, scale: u16, font: &Font) -> &TextBlob { + let key = (text.clone(), scale); + if !self.cache.contains(&key) { + self.cache.put(key.clone(), self.shape(&text, &font)); } - self.cache.get(&text).unwrap() + self.cache.get(&key).unwrap() + } + + pub fn clear(&mut self) { + self.cache.clear(); } } diff --git a/src/renderer/cursor_renderer.rs b/src/renderer/cursor_renderer.rs index 1fe2aab..c75f311 100644 --- a/src/renderer/cursor_renderer.rs +++ b/src/renderer/cursor_renderer.rs @@ -5,7 +5,7 @@ use skulpin::skia_safe::{Canvas, Paint, Path, Point}; use crate::renderer::{CachingShaper, FontLookup}; use crate::editor::{Colors, Cursor, CursorShape, Editor}; -const AVERAGE_MOTION_PERCENTAGE: f32 = 0.8; +const AVERAGE_MOTION_PERCENTAGE: f32 = 0.6; const MOTION_PERCENTAGE_SPREAD: f32 = 0.5; const BAR_WIDTH: f32 = 1.0 / 8.0; @@ -142,7 +142,7 @@ impl CursorRenderer { .map(|(character, _)| character) .unwrap_or(' '); canvas.draw_text_blob( - shaper.shape_cached(character.to_string(), &fonts_lookup.size(1).normal), + shaper.shape_cached(character.to_string(), 1, &fonts_lookup.size(1).normal), (cursor_x, cursor_y), &paint); } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 5eec39a..6b1b2e3 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -149,7 +149,7 @@ impl Renderer { self.paint.set_color(style.foreground(&default_colors).to_color()); let text = text.trim_end(); if text.len() > 0 { - let blob = self.shaper.shape_cached(text.to_string(), self.fonts_lookup.size(size).get(&style)); + let blob = self.shaper.shape_cached(text.to_string(), size, self.fonts_lookup.size(size).get(&style)); canvas.draw_text_blob(blob, (x, y), &self.paint); } } diff --git a/src/window.rs b/src/window.rs index ee315c1..0b74d4d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -124,20 +124,33 @@ pub fn ui_loop(editor: Arc>, nvim: Neovim, initial_size: (u64, u64 Event::WindowEvent { event: WindowEvent::MouseWheel { - delta: MouseScrollDelta::LineDelta(_, delta), + delta: MouseScrollDelta::LineDelta(horizontal, vertical), .. }, .. } => { - let input_type = if delta > 0.0 { + let vertical_input_type = if vertical > 0.0 { Some("up") - } else if delta < 0.0 { + } else if vertical < 0.0 { Some("down") } else { None }; - if let Some(input_type) = input_type { + if let Some(input_type) = vertical_input_type { + let (grid_x, grid_y) = mouse_pos; + nvim.input_mouse("wheel", input_type, "", 0, grid_y, grid_x).expect("Could not send mouse input"); + } + + let horizontal_input_type = if horizontal > 0.0 { + Some("right") + } else if horizontal < 0.0 { + Some("left") + } else { + None + }; + + if let Some(input_type) = horizontal_input_type { let (grid_x, grid_y) = mouse_pos; nvim.input_mouse("wheel", input_type, "", 0, grid_y, grid_x).expect("Could not send mouse input"); }