diff --git a/src/popup_menu.rs b/src/popup_menu.rs index 720c59e..a8e1ce4 100644 --- a/src/popup_menu.rs +++ b/src/popup_menu.rs @@ -5,13 +5,12 @@ use std::cmp::min; use gtk; use gtk::prelude::*; use glib; -use pango::FontDescription; use gdk::{EventButton, EventType}; use neovim_lib::{Neovim, NeovimApi}; use nvim::ErrorReport; - +use shell; use input; const MAX_VISIBLE_ROWS: i32 = 10; @@ -34,25 +33,26 @@ impl State { } fn before_show(&mut self, - nvim: &Rc>, - font_desc: &FontDescription, + shell: &shell::State, menu_items: &Vec>, selected: i64) { if self.nvim.is_none() { - self.nvim = Some(nvim.clone()); + self.nvim = Some(shell.nvim_clone()); } - self.update_tree(menu_items, font_desc); + self.update_tree(menu_items, shell); self.select(selected); } - fn update_tree(&self, menu: &Vec>, font_desc: &FontDescription) { + fn update_tree(&self, menu: &Vec>, shell: &shell::State) { if menu.is_empty() { return; } self.renderer - .set_property_font(Some(&font_desc.to_string())); + .set_property_font(Some(&shell.get_font_desc().to_string())); + self.renderer.set_property_foreground_rgba(Some(&shell.get_foreground().into())); + self.renderer.set_property_background_rgba(Some(&shell.get_background().into())); let col_count = menu.get(0).unwrap().len(); let columns = self.tree.get_columns(); @@ -164,8 +164,7 @@ impl PopupMenu { } pub fn show(&mut self, - nvim: &Rc>, - font_desc: &FontDescription, + shell: &shell::State, menu_items: &Vec>, selected: i64, x: i32, @@ -184,13 +183,16 @@ impl PopupMenu { }); self.state .borrow_mut() - .before_show(&nvim, font_desc, menu_items, selected); - self.popover.popup(); + .before_show(shell, menu_items, selected); + self.popover.popup() } pub fn hide(&mut self) { self.open = false; - self.popover.popdown(); + // popdown() in case of fast hide/show + // situation does not work and just close popup window + // so hide() is important here + self.popover.hide(); } pub fn select(&self, selected: i64) { diff --git a/src/shell.rs b/src/shell.rs index 05ae541..f30ad17 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -48,7 +48,7 @@ pub struct State { nvim: Option>>, font_desc: FontDescription, cursor: Option, - popup_menu: PopupMenu, + popup_menu: RefCell, settings: Rc>, line_height: Option, @@ -62,7 +62,7 @@ pub struct State { impl State { pub fn new(settings: Rc>, parent: &Arc>) -> State { let drawing_area = DrawingArea::new(); - let popup_menu = PopupMenu::new(&drawing_area); + let popup_menu = RefCell::new(PopupMenu::new(&drawing_area)); State { model: UiModel::new(24, 80), @@ -88,10 +88,22 @@ impl State { } } + pub fn get_foreground(&self) -> &Color { + &self.fg_color + } + + pub fn get_background(&self) -> &Color { + &self.bg_color + } + pub fn nvim(&self) -> RefMut { self.nvim.as_ref().unwrap().borrow_mut() } + pub fn nvim_clone(&self) -> Rc> { + self.nvim.as_ref().unwrap().clone() + } + fn create_pango_font(&self) -> FontDescription { self.font_desc.clone() } @@ -115,6 +127,10 @@ impl State { } } + pub fn get_font_desc(&self) -> &FontDescription { + &self.font_desc + } + pub fn set_font_desc(&mut self, desc: &str) { self.font_desc = FontDescription::from_string(desc); self.line_height = None; @@ -138,7 +154,7 @@ impl State { } fn close_popup_menu(&self) { - if self.popup_menu.is_open() { + if self.popup_menu.borrow().is_open() { let mut nvim = self.nvim(); nvim.input("").report_err(&mut *nvim); } @@ -825,8 +841,7 @@ impl RedrawEvents for State { let point = ModelRect::point(col as usize, row as usize); let (x, y, width, height) = point.to_area(line_height, char_width); - self.popup_menu.show(self.nvim.as_ref().unwrap(), - &self.font_desc, + self.popup_menu.borrow_mut().show(&self, menu, selected, x, @@ -841,12 +856,12 @@ impl RedrawEvents for State { } fn popupmenu_hide(&mut self) -> RepaintMode { - self.popup_menu.hide(); + self.popup_menu.borrow_mut().hide(); RepaintMode::Nothing } fn popupmenu_select(&mut self, selected: i64) -> RepaintMode { - self.popup_menu.select(selected); + self.popup_menu.borrow().select(selected); RepaintMode::Nothing } } diff --git a/src/ui_model.rs b/src/ui_model.rs index f16682b..ca0c66b 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -1,5 +1,7 @@ use std::slice::Iter; +use gdk; + #[derive(Clone, PartialEq)] pub struct Color(pub f64, pub f64, pub f64); @@ -7,6 +9,17 @@ pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0); pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0); pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0); +impl <'a> Into for &'a Color { + fn into(self) -> gdk::RGBA { + gdk::RGBA { + red: self.0, + green: self.1, + blue: self.2, + alpha: 1.0, + } + } +} + #[derive(Clone)] pub struct Attrs { pub italic: bool,