From 50603b432501dcc42f4471ce58b88bea0b58a68b Mon Sep 17 00:00:00 2001 From: daa84 Date: Thu, 16 Nov 2017 16:53:58 +0300 Subject: [PATCH] Use colors for selected items, enable popup menu by default --- src/color.rs | 13 +++++++++++-- src/nvim/mod.rs | 2 +- src/popup_menu.rs | 33 ++++++++++++++++++++++++++++++--- src/shell.rs | 2 +- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/color.rs b/src/color.rs index e57e0bd..a5de61a 100644 --- a/src/color.rs +++ b/src/color.rs @@ -36,6 +36,15 @@ impl Color { (std::u16::MAX as f64 * self.2) as u16, ) } + + pub fn to_hex(&self) -> String { + format!( + "#{:X}{:X}{:X}", + (self.0 * 255.0) as u8, + (self.1 * 255.0) as u8, + (self.2 * 255.0) as u8 + ) + } } pub struct ColorModel { @@ -93,7 +102,7 @@ impl ColorModel { } } - pub fn actual_cell_bg<'a>(&'a self, cell: &'a Cell) -> &'a Color { + pub fn actual_cell_bg<'a>(&'a self, cell: &'a Cell) -> &'a Color { if !cell.attrs.reverse { cell.attrs.background.as_ref().unwrap_or(&self.bg_color) } else { @@ -102,7 +111,7 @@ impl ColorModel { } #[inline] - pub fn actual_cell_sp<'a>(&'a self, cell: &'a Cell) -> &'a Color { + pub fn actual_cell_sp<'a>(&'a self, cell: &'a Cell) -> &'a Color { cell.attrs.special.as_ref().unwrap_or(&self.sp_color) } diff --git a/src/nvim/mod.rs b/src/nvim/mod.rs index 030c544..504f8fe 100644 --- a/src/nvim/mod.rs +++ b/src/nvim/mod.rs @@ -137,7 +137,7 @@ pub fn post_start_init( rows: u64, ) -> result::Result<(), NvimInitError> { let mut opts = UiAttachOptions::new(); - opts.set_popupmenu_external(false); + opts.set_popupmenu_external(true); opts.set_tabline_external(true); nvim.borrow().unwrap().ui_attach(cols, rows, &opts).map_err( NvimInitError::new_post_init, diff --git a/src/popup_menu.rs b/src/popup_menu.rs index 00cf641..fdb7224 100644 --- a/src/popup_menu.rs +++ b/src/popup_menu.rs @@ -9,8 +9,8 @@ use gdk::{EventButton, EventType}; use neovim_lib::{Neovim, NeovimApi}; -use nvim; -use nvim::ErrorReport; +use color::ColorModel; +use nvim::{self, ErrorReport}; use shell; use input; @@ -21,15 +21,23 @@ struct State { renderer: gtk::CellRendererText, tree: gtk::TreeView, scroll: gtk::ScrolledWindow, + css_provider: gtk::CssProvider, } impl State { pub fn new() -> Self { + let tree = gtk::TreeView::new(); + let css_provider = gtk::CssProvider::new(); + + let style_context = tree.get_style_context().unwrap(); + style_context.add_provider(&css_provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION); + State { nvim: None, renderer: gtk::CellRendererText::new(), - tree: gtk::TreeView::new(), + tree, scroll: gtk::ScrolledWindow::new(None, None), + css_provider, } } @@ -59,6 +67,8 @@ impl State { Some(&color_model.pmenu_bg().into()), ); + self.update_css(color_model); + let col_count = menu[0].len(); let columns = self.tree.get_columns(); @@ -84,6 +94,23 @@ impl State { self.tree.set_model(Some(&list_store)); } + fn update_css(&self, color_model: &ColorModel) { + let bg = color_model.pmenu_bg_sel(); + let fg = color_model.pmenu_fg_sel(); + + match gtk::CssProviderExtManual::load_from_data( + &self.css_provider, + &format!( + ".view {{ color: {}; background-color: {};}}", + fg.to_hex(), + bg.to_hex() + ), + ) { + Err(e) => error!("Can't update css {}", e), + Ok(_) => (), + }; + } + fn append_column(&self, id: i32) { let renderer = &self.renderer; diff --git a/src/shell.rs b/src/shell.rs index ba8fe75..6726608 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -34,7 +34,7 @@ use render; use render::CellMetrics; const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12"; -pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2"; +pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2.1"; macro_rules! idle_cb_call { ($state:ident.$cb:ident($( $x:expr ),*)) => (