small speed improvements, undercurl render improvement

This commit is contained in:
daa84 2017-03-10 12:32:58 +03:00
parent fd5ad2a97c
commit fbdf897d48
3 changed files with 61 additions and 56 deletions

View File

@ -1,7 +1,6 @@
use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer}; use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer};
use std::io::{Result, Error, ErrorKind}; use std::io::{Result, Error, ErrorKind};
use std::result; use std::result;
use std::collections::HashMap;
use ui_model::UiModel; use ui_model::UiModel;
use ui; use ui;
use ui::Ui; use ui::Ui;
@ -18,7 +17,7 @@ pub trait RedrawEvents {
fn on_redraw(&self); fn on_redraw(&self);
fn on_highlight_set(&mut self, attrs: &HashMap<&str, &Value>); fn on_highlight_set(&mut self, attrs: &Vec<(Value, Value)>);
fn on_eol_clear(&mut self); fn on_eol_clear(&mut self);
@ -149,13 +148,7 @@ fn call(ui: &mut Ui, method: &str, args: &Vec<Value>) -> result::Result<(), Stri
"resize" => ui.on_resize(try_uint!(args[0]), try_uint!(args[1])), "resize" => ui.on_resize(try_uint!(args[0]), try_uint!(args[1])),
"highlight_set" => { "highlight_set" => {
if let Value::Map(ref attrs) = args[0] { if let Value::Map(ref attrs) = args[0] {
let attrs_map = attrs.iter() ui.on_highlight_set(attrs);
.map(|v| match v {
&(Value::String(ref key), ref value) => (key.as_str(), value),
_ => panic!("attribute key must be string"),
})
.collect();
ui.on_highlight_set(&attrs_map);
} else { } else {
panic!("Supports only map value as argument"); panic!("Supports only map value as argument");
} }

103
src/ui.rs
View File

@ -1,6 +1,5 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::thread; use std::thread;
use std::collections::HashMap;
use std::string::String; use std::string::String;
use cairo; use cairo;
@ -313,7 +312,24 @@ fn draw(ui: &Ui, ctx: &cairo::Context) {
let current_point = ctx.get_current_point(); let current_point = ctx.get_current_point();
if let Some(ref bg) = cell.attrs.background { let mut bg = if let Some(ref bg) = cell.attrs.background {
bg
} else {
&ui.bg_color
};
let mut fg = if let Some(ref fg) = cell.attrs.foreground {
fg
} else {
&ui.fg_color
};
if cell.attrs.reverse {
let tmp = fg;
fg = bg;
bg = tmp;
}
if cell.attrs.background.is_some() || cell.attrs.reverse {
ctx.set_source_rgb(bg.0, bg.1, bg.2); ctx.set_source_rgb(bg.0, bg.1, bg.2);
ctx.rectangle(current_point.0, line_y, char_width, line_height); ctx.rectangle(current_point.0, line_y, char_width, line_height);
ctx.fill(); ctx.fill();
@ -321,17 +337,6 @@ fn draw(ui: &Ui, ctx: &cairo::Context) {
ctx.move_to(current_point.0, current_point.1); ctx.move_to(current_point.0, current_point.1);
} }
let fg = if let Some(ref fg) = cell.attrs.foreground {
fg
} else {
&ui.fg_color
};
let bg = if let Some(ref bg) = cell.attrs.background {
bg
} else {
&ui.bg_color
};
if row == line_idx && col == col_idx { 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); ctx.set_source_rgba(1.0 - bg.0, 1.0 - bg.1, 1.0 - bg.2, 0.5);
@ -366,7 +371,7 @@ fn draw(ui: &Ui, ctx: &cairo::Context) {
// so it is not possible to find right position for underline or undercurl position // so it is not possible to find right position for underline or undercurl position
// > update_font_description(&mut desc, &cell.attrs); // > update_font_description(&mut desc, &cell.attrs);
// > layout.get_context().unwrap().get_metrics(); // > layout.get_context().unwrap().get_metrics();
let top_offset = line_height - 2.0; let top_offset = line_height * 0.9;
let sp = if let Some(ref sp) = cell.attrs.special { let sp = if let Some(ref sp) = cell.attrs.special {
sp sp
@ -376,18 +381,18 @@ fn draw(ui: &Ui, ctx: &cairo::Context) {
ctx.set_source_rgba(sp.0, sp.1, sp.2, 0.7); ctx.set_source_rgba(sp.0, sp.1, sp.2, 0.7);
if cell.attrs.undercurl { if cell.attrs.undercurl {
ctx.set_dash(&[4.0, 2.0], 0.0);
ctx.set_line_width(2.0); ctx.set_line_width(2.0);
ctx.move_to(current_point.0, line_y + top_offset); ctx.move_to(current_point.0, line_y + top_offset);
ctx.line_to(current_point.0 + char_width, ctx.line_to(current_point.0 + char_width, line_y + top_offset);
line_y + top_offset); ctx.stroke();
} ctx.set_dash(&[], 0.0);
else if cell.attrs.underline { } else if cell.attrs.underline {
ctx.set_line_width(1.0); ctx.set_line_width(1.0);
ctx.move_to(current_point.0, line_y + top_offset); ctx.move_to(current_point.0, line_y + top_offset);
ctx.line_to(current_point.0 + char_width, ctx.line_to(current_point.0 + char_width, line_y + top_offset);
line_y + top_offset); ctx.stroke();
} }
ctx.stroke();
} }
ctx.move_to(current_point.0 + char_width, current_point.1); ctx.move_to(current_point.0 + char_width, current_point.1);
@ -478,35 +483,39 @@ impl RedrawEvents for Ui {
self.model.scroll(count); self.model.scroll(count);
} }
fn on_highlight_set(&mut self, attrs: &HashMap<&str, &Value>) { fn on_highlight_set(&mut self, attrs: &Vec<(Value, Value)>) {
let mut model_attrs = Attrs::new(); let mut model_attrs = Attrs::new();
if let Some(&&Value::Integer(Integer::U64(fg))) = attrs.get("foreground") {
model_attrs.foreground = Some(split_color(fg)); for &(ref key_val, ref val) in attrs {
} if let &Value::String(ref key) = key_val {
if let Some(&&Value::Integer(Integer::U64(bg))) = attrs.get("background") { match key.as_ref() {
model_attrs.background = Some(split_color(bg)); "foreground" => {
} if let &Value::Integer(Integer::U64(fg)) = val {
if let Some(&&Value::Integer(Integer::U64(bg))) = attrs.get("special") { model_attrs.foreground = Some(split_color(fg));
model_attrs.special = Some(split_color(bg)); }
} },
if attrs.contains_key("reverse") { "background" => {
let fg = if let Some(ref fg) = model_attrs.foreground { if let &Value::Integer(Integer::U64(bg)) = val {
fg.clone() model_attrs.background = Some(split_color(bg));
}
},
"special" => {
if let &Value::Integer(Integer::U64(bg)) = val {
model_attrs.special = Some(split_color(bg));
}
},
"reverse" => model_attrs.reverse = true,
"bold" => model_attrs.bold = true,
"italic" => model_attrs.italic = true,
"underline" => model_attrs.underline = true,
"undercurl" => model_attrs.undercurl = true,
attr_key => println!("unknown attribute {}", attr_key),
};
} else { } else {
self.fg_color.clone() panic!("attr key must be string");
}; }
let bg = if let Some(ref bg) = model_attrs.background {
bg.clone()
} else {
self.bg_color.clone()
};
model_attrs.foreground = Some(bg);
model_attrs.background = Some(fg);
} }
model_attrs.bold = attrs.contains_key("bold");
model_attrs.italic = attrs.contains_key("italic");
model_attrs.underline = attrs.contains_key("underline");
model_attrs.undercurl = attrs.contains_key("undercurl");
self.cur_attrs = Some(model_attrs); self.cur_attrs = Some(model_attrs);
} }

View File

@ -15,6 +15,7 @@ pub struct Attrs {
pub foreground: Option<Color>, pub foreground: Option<Color>,
pub background: Option<Color>, pub background: Option<Color>,
pub special: Option<Color>, pub special: Option<Color>,
pub reverse: bool,
} }
impl Attrs { impl Attrs {
@ -27,6 +28,7 @@ impl Attrs {
bold: false, bold: false,
underline: false, underline: false,
undercurl: false, undercurl: false,
reverse: false,
} }
} }
@ -35,6 +37,7 @@ impl Attrs {
self.bold = false; self.bold = false;
self.underline = false; self.underline = false;
self.undercurl = false; self.undercurl = false;
self.reverse = false;
self.foreground = None; self.foreground = None;
self.background = None; self.background = None;
self.special = None; self.special = None;