From 60539e0ccfd604e8c4f8b2794dedd92493d4ddf1 Mon Sep 17 00:00:00 2001 From: daa Date: Wed, 4 May 2016 09:23:39 +0300 Subject: [PATCH] Edit mode cursor --- src/nvim.rs | 8 ++++++++ src/ui.rs | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/nvim.rs b/src/nvim.rs index ef022eb..0b62bde 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -31,6 +31,8 @@ pub trait RedrawEvents { fn on_update_bg(&mut self, bg: i64); fn on_update_fg(&mut self, fg: i64); + + fn on_mode_change(&mut self, mode: &str); } macro_rules! try_str { @@ -187,6 +189,12 @@ fn call(method: &str, args: Vec) { Ok(()) }); } + "mode_change" => { + safe_call(move |ui| { + ui.on_mode_change(try_str!(args[0])); + Ok(()) + }); + } _ => println!("Event {}({:?})", method, args), }; } diff --git a/src/ui.rs b/src/ui.rs index b9e6a72..579a858 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -37,6 +37,13 @@ thread_local!(pub static UI: RefCell = { RefCell::new(Ui::new()) }); +#[derive(PartialEq)] +enum NvimMode { + Normal, + Insert, + Other, +} + pub struct Ui { pub model: UiModel, nvim: Option, @@ -48,6 +55,7 @@ pub struct Ui { line_height: Option, char_width: Option, resize_timer: Option, + mode: NvimMode, } impl Ui { @@ -63,6 +71,7 @@ impl Ui { line_height: None, char_width: None, resize_timer: None, + mode: NvimMode::Insert, } } @@ -236,9 +245,16 @@ fn draw(ui: &Ui, ctx: &cairo::Context) { if row == line_idx && col == col_idx { ctx.set_source_rgba(1.0 - bg.0, 1.0 - bg.1, 1.0 - bg.2, 0.5); + + let cursor_width = if ui.mode == NvimMode::Insert { + char_width / 5.0 + } else { + char_width + }; + ctx.rectangle(current_point.0, line_y - line_height, - char_width, + cursor_width, line_height); ctx.fill(); ctx.move_to(current_point.0, current_point.1); @@ -344,6 +360,14 @@ impl RedrawEvents for Ui { self.fg_color = COLOR_WHITE; } } + + fn on_mode_change(&mut self, mode: &str) { + match mode { + "normal" => self.mode = NvimMode::Normal, + "insert" => self.mode = NvimMode::Insert, + _ => self.mode = NvimMode::Other, + } + } } fn split_color(indexed_color: u64) -> Color {