Edit mode cursor

This commit is contained in:
daa 2016-05-04 09:23:39 +03:00
parent a11581748c
commit 60539e0ccf
2 changed files with 33 additions and 1 deletions

View File

@ -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<Value>) {
Ok(())
});
}
"mode_change" => {
safe_call(move |ui| {
ui.on_mode_change(try_str!(args[0]));
Ok(())
});
}
_ => println!("Event {}({:?})", method, args),
};
}

View File

@ -37,6 +37,13 @@ thread_local!(pub static UI: RefCell<Ui> = {
RefCell::new(Ui::new())
});
#[derive(PartialEq)]
enum NvimMode {
Normal,
Insert,
Other,
}
pub struct Ui {
pub model: UiModel,
nvim: Option<Neovim>,
@ -48,6 +55,7 @@ pub struct Ui {
line_height: Option<f64>,
char_width: Option<f64>,
resize_timer: Option<u32>,
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 {