From 51dcd7f4e846f4537e81fa86c27940244fdea9ed Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 18 Mar 2017 01:18:41 +0300 Subject: [PATCH] Repaint event on given area --- src/nvim.rs | 14 ++++++++++---- src/settings.rs | 5 ++++- src/shell.rs | 26 ++++++++++++++++++++++---- src/ui_model.rs | 6 ++++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/nvim.rs b/src/nvim.rs index 4ad463c..0305d88 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -1,7 +1,7 @@ use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer, UiAttachOptions, CallError}; use std::io::{Result, Error, ErrorKind}; use std::result; -use ui_model::UiModel; +use ui_model::{UiModel, ModelRect}; use ui; use shell::Shell; use glib; @@ -15,7 +15,7 @@ pub trait RedrawEvents { fn on_resize(&mut self, columns: u64, rows: u64); - fn on_redraw(&self); + fn on_redraw(&self, mode: &RepaintMode); fn on_highlight_set(&mut self, attrs: &Vec<(Value, Value)>); @@ -114,7 +114,7 @@ fn nvim_cb(method: &str, params: Vec) { } } - ui.on_redraw(); + ui.on_redraw(&RepaintMode::All); Ok(()) }); } @@ -124,7 +124,7 @@ fn nvim_cb(method: &str, params: Vec) { let args = params.iter().skip(1).cloned().collect(); safe_call(move |ui| { call_gui_event(ui, &ev_name, &args)?; - ui.on_redraw(); + ui.on_redraw(&RepaintMode::All); Ok(()) }); } else { @@ -204,3 +204,9 @@ impl ErrorReport for result::Result { } } } + +pub enum RepaintMode { + All, + Area(ModelRect), +} + diff --git a/src/settings.rs b/src/settings.rs index 4da9491..860aec8 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,6 +1,9 @@ #[cfg(unix)] use ui::{UI, SET}; +#[cfg(unix)] +use nvim::RepaintMode; + #[cfg(unix)] use nvim::RedrawEvents; @@ -77,7 +80,7 @@ fn monospace_font_changed() { // rpc is priority for font if set.font_source != FontSource::Rpc { set.update_font(&mut ui.shell); - ui.shell.on_redraw(); + ui.shell.on_redraw(&RepaintMode::All); } }); }); diff --git a/src/shell.rs b/src/shell.rs index b8242ed..eea40c3 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -14,19 +14,25 @@ use neovim_lib::{Neovim, NeovimApi, Value, Integer}; use settings; use ui_model::{UiModel, Cell, Attrs, Color, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; -use nvim::{RedrawEvents, GuiApi}; +use nvim::{RedrawEvents, GuiApi, RepaintMode}; use input::{convert_key, keyval_to_input_string}; use ui::{UI, Ui, SET}; const DEFAULT_FONT_NAME: &'static str = "DejaVu Sans Mono 12"; macro_rules! SHELL { + (&$id:ident = $expr:expr) => ( + UI.with(|ui_cell| { + let $id = &ui_cell.borrow().shell; + $expr + }); + ); ($id:ident = $expr:expr) => ( UI.with(|ui_cell| { let mut $id = &mut ui_cell.borrow_mut().shell; $expr }); - ) + ); } #[derive(PartialEq)] @@ -473,8 +479,20 @@ impl RedrawEvents for Shell { self.model = UiModel::new(rows, columns); } - fn on_redraw(&self) { - self.drawing_area.queue_draw(); + fn on_redraw(&self, mode: &RepaintMode) { + match mode { + &RepaintMode::All => self.drawing_area.queue_draw(), + &RepaintMode::Area(ref rect) => { + SHELL!(&shell = { + if let Some(line_height) = shell.line_height { + if let Some(char_width) = shell.char_width { + let (x, y, width, height) = rect.to_area(line_height as i32, char_width as i32); + self.drawing_area.queue_draw_area(x, y, width, height); + } + } + }); + }, + } } fn on_set_scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64) { diff --git a/src/ui_model.rs b/src/ui_model.rs index 59e1ee6..d77a53d 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -258,4 +258,10 @@ impl ModelRect { rect.right }; } + + pub fn to_area(&self, line_height: i32, char_width: i32) -> (i32, i32, i32, i32) { + let y = self.top as i32 * line_height; + let x = self.left as i32 * char_width; + (x, y, self.right as i32 * char_width - x, self.bot as i32 * line_height - y) + } }