neovim-gtk/src/cmd_line.rs

622 lines
18 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2017-11-23 14:57:39 +00:00
use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;
2018-04-04 20:47:19 +00:00
use std::cmp::{max, min};
2017-11-18 12:56:37 +00:00
use gtk;
use gtk::prelude::*;
2017-11-23 14:57:39 +00:00
use cairo;
2018-04-02 20:18:29 +00:00
use pango;
2017-11-18 12:56:37 +00:00
use neovim_lib::Value;
2018-04-05 20:25:18 +00:00
use nvim::{self, NeovimClient};
use mode;
2018-01-02 22:26:04 +00:00
use ui_model::{Attrs, ModelLayout};
2017-11-23 14:57:39 +00:00
use ui::UiMutex;
2018-01-03 09:04:01 +00:00
use render::{self, CellMetrics};
2017-11-23 14:57:39 +00:00
use shell;
use cursor;
2018-04-03 19:05:59 +00:00
use popup_menu;
pub struct Level {
2018-01-02 22:26:04 +00:00
model_layout: ModelLayout,
prompt_offset: usize,
2018-01-03 09:04:01 +00:00
preferred_width: i32,
preferred_height: i32,
}
impl Level {
pub fn insert(&mut self, c: String, shift: bool, render_state: &shell::RenderState) {
self.model_layout.insert_char(c, shift);
self.update_preferred_size(render_state);
}
pub fn replace_from_ctx(&mut self, ctx: &CmdLineContext, render_state: &shell::RenderState) {
let content = ctx.get_lines();
self.replace_line(content.lines, false);
self.prompt_offset = content.prompt_offset;
self.model_layout
.set_cursor(self.prompt_offset + ctx.pos as usize);
self.update_preferred_size(render_state);
}
pub fn from_ctx(ctx: &CmdLineContext, render_state: &shell::RenderState) -> Self {
let content = ctx.get_lines();
let mut level = Level::from_lines(content.lines, ctx.max_width, render_state);
level.prompt_offset = content.prompt_offset;
level
.model_layout
.set_cursor(level.prompt_offset + ctx.pos as usize);
level.update_preferred_size(render_state);
level
}
fn replace_line(&mut self, lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>, append: bool) {
if append {
self.model_layout.layout_append(lines);
} else {
self.model_layout.layout(lines);
}
}
fn update_preferred_size(&mut self, render_state: &shell::RenderState) {
let &CellMetrics {
line_height,
char_width,
..
} = render_state.font_ctx.cell_metrics();
2018-01-13 15:02:47 +00:00
let (columns, rows) = self.model_layout.size();
let columns = max(columns, 5);
self.preferred_width = (char_width * columns as f64) as i32;
self.preferred_height = (line_height * rows as f64) as i32;
}
fn to_attributed_content(
content: &Vec<Vec<(HashMap<String, Value>, String)>>,
) -> Vec<Vec<(Option<Attrs>, Vec<char>)>> {
content
2018-01-13 18:32:21 +00:00
.iter()
.map(|line_chars| {
line_chars
.iter()
.map(|c| (Some(Attrs::from_value_map(&c.0)), c.1.chars().collect()))
2018-01-13 18:32:21 +00:00
.collect()
})
.collect()
2018-01-13 15:02:47 +00:00
}
pub fn from_multiline_content(
content: &Vec<Vec<(HashMap<String, Value>, String)>>,
max_width: i32,
render_state: &shell::RenderState,
) -> Self {
Level::from_lines(
Level::to_attributed_content(content),
max_width,
render_state,
)
2018-01-13 15:02:47 +00:00
}
2018-01-13 15:02:47 +00:00
pub fn from_lines(
lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>,
2018-01-13 15:02:47 +00:00
max_width: i32,
render_state: &shell::RenderState,
) -> Self {
let &CellMetrics { char_width, .. } = render_state.font_ctx.cell_metrics();
2018-01-03 09:04:01 +00:00
let max_width_chars = (max_width as f64 / char_width) as u64;
2018-01-03 09:04:01 +00:00
let mut model_layout = ModelLayout::new(max_width_chars);
model_layout.layout(lines);
2018-01-03 20:20:22 +00:00
let mut level = Level {
2018-01-03 20:20:22 +00:00
model_layout,
preferred_width: -1,
preferred_height: -1,
prompt_offset: 0,
};
2017-11-23 14:57:39 +00:00
level.update_preferred_size(render_state);
level
2018-01-13 15:02:47 +00:00
}
2017-11-23 14:57:39 +00:00
fn update_cache(&mut self, render_state: &shell::RenderState) {
render::shape_dirty(
&render_state.font_ctx,
2018-01-02 22:26:04 +00:00
&mut self.model_layout.model,
2017-11-23 14:57:39 +00:00
&render_state.color_model,
);
}
fn set_cursor(&mut self, render_state: &shell::RenderState, pos: usize) {
self.model_layout.set_cursor(self.prompt_offset + pos);
self.update_preferred_size(render_state);
}
}
fn prompt_lines(
firstc: &str,
prompt: &str,
indent: u64,
) -> (usize, Vec<(Option<Attrs>, Vec<char>)>) {
let prompt: Vec<(Option<Attrs>, Vec<char>)> = if !firstc.is_empty() {
if firstc.len() >= indent as usize {
vec![(None, firstc.chars().collect())]
} else {
vec![
(
None,
firstc
.chars()
.chain((firstc.len()..indent as usize).map(|_| ' '))
.collect(),
),
]
}
2017-11-19 20:13:06 +00:00
} else if !prompt.is_empty() {
2018-01-03 20:20:22 +00:00
prompt
.lines()
.map(|l| (None, l.chars().collect()))
.collect()
2017-11-19 20:13:06 +00:00
} else {
vec![]
};
let prompt_offset = prompt.last().map(|l| l.1.len()).unwrap_or(0);
(prompt_offset, prompt)
2017-11-19 20:13:06 +00:00
}
2017-11-23 14:57:39 +00:00
struct State {
2018-04-05 20:25:18 +00:00
nvim: Option<Rc<nvim::NeovimClient>>,
2017-11-23 14:57:39 +00:00
levels: Vec<Level>,
2018-01-11 19:44:19 +00:00
block: Option<Level>,
2017-11-23 14:57:39 +00:00
render_state: Rc<RefCell<shell::RenderState>>,
drawing_area: gtk::DrawingArea,
2018-01-21 20:19:00 +00:00
cursor: Option<cursor::BlinkCursor<State>>,
2017-11-23 14:57:39 +00:00
}
impl State {
fn new(drawing_area: gtk::DrawingArea, render_state: Rc<RefCell<shell::RenderState>>) -> Self {
State {
2018-04-05 20:25:18 +00:00
nvim: None,
2017-11-23 14:57:39 +00:00
levels: Vec::new(),
2018-01-11 19:44:19 +00:00
block: None,
2017-11-23 14:57:39 +00:00
render_state,
drawing_area,
2018-01-21 20:19:00 +00:00
cursor: None,
2017-11-23 14:57:39 +00:00
}
}
2018-01-14 21:18:32 +00:00
fn request_area_size(&self) {
let drawing_area = self.drawing_area.clone();
let block = self.block.as_ref();
let level = self.levels.last();
2018-01-21 11:14:07 +00:00
let (block_width, block_height) = block
.map(|b| (b.preferred_width, b.preferred_height))
.unwrap_or((0, 0));
let (level_width, level_height) = level
.map(|l| (l.preferred_width, l.preferred_height))
.unwrap_or((0, 0));
2018-01-14 21:18:32 +00:00
drawing_area.set_size_request(
max(level_width, block_width),
max(block_height + level_height, 40),
);
}
fn preferred_height(&self) -> i32 {
let level = self.levels.last();
level.map(|l| l.preferred_height).unwrap_or(0)
+ self.block.as_ref().map(|b| b.preferred_height).unwrap_or(0)
}
2017-11-23 14:57:39 +00:00
fn set_cursor(&mut self, render_state: &shell::RenderState, pos: usize, level: usize) {
debug_assert!(level > 0);
// queue old cursor position
self.queue_redraw_cursor();
self.levels
.get_mut(level - 1)
.map(|l| l.set_cursor(render_state, pos));
}
2017-11-23 14:57:39 +00:00
fn queue_redraw_cursor(&mut self) {
2018-01-21 20:19:00 +00:00
if let Some(ref level) = self.levels.last() {
let level_preferred_height = level.preferred_height;
let block_preferred_height =
self.block.as_ref().map(|b| b.preferred_height).unwrap_or(0);
let gap = self.drawing_area.get_allocated_height() - level_preferred_height
- block_preferred_height;
2018-01-21 20:19:00 +00:00
let model = &level.model_layout.model;
let mut cur_point = model.cur_point();
cur_point.extend_by_items(model);
let render_state = self.render_state.borrow();
let cell_metrics = render_state.font_ctx.cell_metrics();
let (x, y, width, height) = cur_point.to_area_extend_ink(model, cell_metrics);
if gap > 0 {
self.drawing_area
.queue_draw_area(x, y + gap / 2, width, height);
} else {
self.drawing_area
.queue_draw_area(x, y + block_preferred_height, width, height);
}
2018-01-21 20:19:00 +00:00
}
2017-11-23 14:57:39 +00:00
}
}
impl cursor::CursorRedrawCb for State {
fn queue_redraw_cursor(&mut self) {
self.queue_redraw_cursor();
}
}
2017-11-18 12:56:37 +00:00
pub struct CmdLine {
2017-11-18 20:15:03 +00:00
popover: gtk::Popover,
2018-04-02 20:18:29 +00:00
wild_tree: gtk::TreeView,
wild_scroll: gtk::ScrolledWindow,
2018-04-03 19:05:59 +00:00
wild_css_provider: gtk::CssProvider,
wild_renderer: gtk::CellRendererText,
2018-04-04 20:47:19 +00:00
wild_column: gtk::TreeViewColumn,
2017-11-23 14:57:39 +00:00
displyed: bool,
state: Arc<UiMutex<State>>,
2017-11-18 12:56:37 +00:00
}
impl CmdLine {
2017-11-23 14:57:39 +00:00
pub fn new(drawing: &gtk::DrawingArea, render_state: Rc<RefCell<shell::RenderState>>) -> Self {
2017-11-18 20:15:03 +00:00
let popover = gtk::Popover::new(Some(drawing));
popover.set_modal(false);
2018-01-03 09:04:01 +00:00
popover.set_position(gtk::PositionType::Right);
2018-01-02 22:26:04 +00:00
2018-04-02 20:18:29 +00:00
let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
2017-11-18 20:15:03 +00:00
let drawing_area = gtk::DrawingArea::new();
2018-04-02 20:18:29 +00:00
content.pack_start(&drawing_area, true, true, 0);
2017-11-18 12:56:37 +00:00
2017-11-23 14:57:39 +00:00
let state = Arc::new(UiMutex::new(State::new(drawing_area.clone(), render_state)));
let weak_cb = Arc::downgrade(&state);
2018-01-21 20:19:00 +00:00
let cursor = cursor::BlinkCursor::new(weak_cb);
state.borrow_mut().cursor = Some(cursor);
2017-11-23 14:57:39 +00:00
2018-01-21 20:19:00 +00:00
drawing_area.connect_draw(clone!(state => move |_, ctx| gtk_draw(ctx, &state)));
2017-11-23 14:57:39 +00:00
2018-04-04 20:47:19 +00:00
let (wild_scroll, wild_tree, wild_css_provider, wild_renderer, wild_column) =
2018-04-05 20:25:18 +00:00
CmdLine::create_widlmenu(&state);
2018-04-02 20:18:29 +00:00
content.pack_start(&wild_scroll, false, true, 0);
popover.add(&content);
drawing_area.show_all();
content.show();
2017-11-18 12:56:37 +00:00
CmdLine {
2017-11-18 20:15:03 +00:00
popover,
2017-11-23 14:57:39 +00:00
state,
displyed: false,
2018-04-02 20:18:29 +00:00
wild_scroll,
wild_tree,
2018-04-03 19:05:59 +00:00
wild_css_provider,
wild_renderer,
2018-04-04 20:47:19 +00:00
wild_column,
2017-11-23 14:57:39 +00:00
}
}
2018-04-05 20:25:18 +00:00
fn create_widlmenu(
state: &Arc<UiMutex<State>>,
) -> (
2018-04-03 19:05:59 +00:00
gtk::ScrolledWindow,
gtk::TreeView,
gtk::CssProvider,
gtk::CellRendererText,
2018-04-04 20:47:19 +00:00
gtk::TreeViewColumn,
2018-04-03 19:05:59 +00:00
) {
let css_provider = gtk::CssProvider::new();
2018-04-02 20:18:29 +00:00
let tree = gtk::TreeView::new();
2018-04-03 19:05:59 +00:00
let style_context = tree.get_style_context().unwrap();
style_context.add_provider(&css_provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION);
2018-04-02 20:18:29 +00:00
tree.get_selection().set_mode(gtk::SelectionMode::Single);
tree.set_headers_visible(false);
tree.set_can_focus(false);
let renderer = gtk::CellRendererText::new();
renderer.set_property_ellipsize(pango::EllipsizeMode::End);
2018-04-04 20:47:19 +00:00
let column = gtk::TreeViewColumn::new();
column.pack_start(&renderer, true);
column.add_attribute(&renderer, "text", 0);
tree.append_column(&column);
2018-04-02 20:18:29 +00:00
let scroll = gtk::ScrolledWindow::new(None, None);
scroll.set_propagate_natural_height(true);
2018-04-04 20:47:19 +00:00
scroll.set_propagate_natural_width(true);
2018-04-02 20:18:29 +00:00
scroll.add(&tree);
2018-04-05 20:25:18 +00:00
tree.connect_button_press_event(clone!(state => move |tree, ev| {
let state = state.borrow();
let nvim = state.nvim.as_ref().unwrap().nvim();
if let Some(mut nvim) = nvim {
popup_menu::tree_button_press(tree, ev, &mut *nvim, "");
}
Inhibit(false)
}));
2018-04-04 20:47:19 +00:00
(scroll, tree, css_provider, renderer, column)
2018-04-02 20:18:29 +00:00
}
2018-01-03 20:20:22 +00:00
pub fn show_level(&mut self, ctx: &CmdLineContext) {
2017-11-23 14:57:39 +00:00
let mut state = self.state.borrow_mut();
2018-04-05 20:25:18 +00:00
if state.nvim.is_none() {
state.nvim = Some(ctx.nvim.clone());
}
let render_state = state.render_state.clone();
let render_state = render_state.borrow();
2018-01-03 20:20:22 +00:00
2018-01-03 09:04:01 +00:00
if ctx.level_idx as usize == state.levels.len() {
2018-01-14 21:18:32 +00:00
let level = state.levels.last_mut().unwrap();
level.replace_from_ctx(ctx, &*render_state);
level.update_cache(&*render_state);
} else {
2018-01-14 21:18:32 +00:00
let mut level = Level::from_ctx(ctx, &*render_state);
level.update_cache(&*render_state);
state.levels.push(level);
2017-11-23 14:57:39 +00:00
}
2018-01-14 21:18:32 +00:00
state.request_area_size();
2017-11-23 14:57:39 +00:00
if !self.displyed {
self.displyed = true;
2018-01-02 22:26:04 +00:00
self.popover.set_pointing_to(&gtk::Rectangle {
2018-01-03 09:04:01 +00:00
x: ctx.x,
y: ctx.y,
width: ctx.width,
height: ctx.height,
2018-01-02 22:26:04 +00:00
});
2017-11-23 14:57:39 +00:00
self.popover.popup();
2018-01-21 20:19:00 +00:00
state.cursor.as_mut().unwrap().start();
2017-11-23 14:57:39 +00:00
} else {
2018-01-14 21:18:32 +00:00
state.drawing_area.queue_draw()
2017-11-18 12:56:37 +00:00
}
}
2018-01-02 22:26:04 +00:00
pub fn special_char(
&self,
render_state: &shell::RenderState,
c: String,
shift: bool,
level: u64,
) {
let mut state = self.state.borrow_mut();
if let Some(level) = state.levels.get_mut((level - 1) as usize) {
level.insert(c, shift, render_state);
level.update_cache(&*render_state);
} else {
error!("Level {} does not exists", level);
}
state.request_area_size();
state.drawing_area.queue_draw()
}
2018-01-02 22:26:04 +00:00
pub fn hide_level(&mut self, level_idx: u64) {
let mut state = self.state.borrow_mut();
if level_idx as usize == state.levels.len() {
state.levels.pop();
}
if state.levels.is_empty() {
self.popover.hide();
self.displyed = false;
2018-01-21 20:19:00 +00:00
state.cursor.as_mut().unwrap().leave_focus();
2018-01-02 22:26:04 +00:00
}
}
2018-01-11 19:44:19 +00:00
2018-01-13 15:02:47 +00:00
pub fn show_block(
&mut self,
content: &Vec<Vec<(HashMap<String, Value>, String)>>,
max_width: i32,
) {
let mut state = self.state.borrow_mut();
let mut block =
Level::from_multiline_content(content, max_width, &*state.render_state.borrow());
2018-01-13 15:02:47 +00:00
block.update_cache(&*state.render_state.borrow());
state.block = Some(block);
2018-01-14 21:18:32 +00:00
state.request_area_size();
2018-01-13 15:02:47 +00:00
}
2018-01-21 11:14:07 +00:00
pub fn block_append(&mut self, content: &Vec<(HashMap<String, Value>, String)>) {
2018-01-13 15:02:47 +00:00
let mut state = self.state.borrow_mut();
let render_state = state.render_state.clone();
2018-01-14 21:18:32 +00:00
{
2018-01-21 11:14:07 +00:00
let attr_content = content
.iter()
.map(|c| (Some(Attrs::from_value_map(&c.0)), c.1.chars().collect()))
2018-01-21 11:14:07 +00:00
.collect();
2018-01-14 21:18:32 +00:00
let block = state.block.as_mut().unwrap();
block.replace_line(vec![attr_content], true);
block.update_preferred_size(&*render_state.borrow());
2018-01-14 21:18:32 +00:00
block.update_cache(&*render_state.borrow());
}
state.request_area_size();
2018-01-13 15:02:47 +00:00
}
2018-01-11 19:44:19 +00:00
pub fn block_hide(&self) {
2018-01-13 15:02:47 +00:00
self.state.borrow_mut().block = None;
2018-01-11 19:44:19 +00:00
}
2018-02-10 09:51:47 +00:00
pub fn pos(&self, render_state: &shell::RenderState, pos: u64, level: u64) {
self.state
.borrow_mut()
.set_cursor(render_state, pos as usize, level as usize);
2018-02-10 09:51:47 +00:00
}
pub fn set_mode_info(&self, mode_info: Option<mode::ModeInfo>) {
self.state
.borrow_mut()
.cursor
.as_mut()
.unwrap()
.set_mode_info(mode_info);
}
2018-04-02 20:18:29 +00:00
2018-04-04 20:47:19 +00:00
pub fn show_wildmenu(
&self,
items: Vec<String>,
render_state: &shell::RenderState,
max_width: i32,
) {
// update font/color
2018-04-03 19:05:59 +00:00
self.wild_renderer
.set_property_font(Some(&render_state.font_ctx.font_description().to_string()));
self.wild_renderer
.set_property_foreground_rgba(Some(&render_state.color_model.pmenu_fg().into()));
popup_menu::update_css(&self.wild_css_provider, &render_state.color_model);
2018-04-04 20:47:19 +00:00
// set width
2018-04-04 20:47:32 +00:00
// this calculation produce width more then needed, but this is looks ok :)
2018-04-04 20:47:19 +00:00
let max_item_width = (items.iter().map(|item| item.len()).max().unwrap() as f64
* render_state.font_ctx.cell_metrics().char_width) as i32
+ self.state.borrow().levels.last().unwrap().preferred_width;
self.wild_column
.set_fixed_width(min(max_item_width, max_width));
self.wild_scroll.set_max_content_width(max_width);
// load data
2018-04-02 20:18:29 +00:00
let list_store = gtk::ListStore::new(&vec![gtk::Type::String; 1]);
for item in items {
list_store.insert_with_values(None, &[0], &[&item]);
}
self.wild_tree.set_model(&list_store);
2018-04-04 20:47:19 +00:00
// set height
let treeview_height =
popup_menu::calc_treeview_height(&self.wild_tree, &self.wild_renderer);
self.wild_scroll.set_max_content_height(treeview_height);
self.wild_scroll.show_all();
2018-04-02 20:18:29 +00:00
}
pub fn hide_wildmenu(&self) {
self.wild_scroll.hide();
}
2018-04-03 19:05:59 +00:00
pub fn wildmenu_select(&self, selected: i64) {
if selected >= 0 {
let wild_tree = self.wild_tree.clone();
idle_add(move || {
let selected_path = gtk::TreePath::new_from_string(&format!("{}", selected));
wild_tree.get_selection().select_path(&selected_path);
wild_tree.scroll_to_cell(&selected_path, None, false, 0.0, 0.0);
Continue(false)
});
} else {
self.wild_tree.get_selection().unselect_all();
}
}
2017-11-23 14:57:39 +00:00
}
2017-11-18 12:56:37 +00:00
fn gtk_draw(ctx: &cairo::Context, state: &Arc<UiMutex<State>>) -> Inhibit {
2017-11-23 14:57:39 +00:00
let state = state.borrow();
let preferred_height = state.preferred_height();
2017-11-23 14:57:39 +00:00
let level = state.levels.last();
2018-01-14 21:18:32 +00:00
let block = state.block.as_ref();
2017-11-23 14:57:39 +00:00
2018-01-14 21:18:32 +00:00
let render_state = state.render_state.borrow();
let gap = state.drawing_area.get_allocated_height() - preferred_height;
if gap > 0 {
2018-01-29 19:39:29 +00:00
ctx.translate(0.0, (gap / 2) as f64);
2018-01-14 21:18:32 +00:00
}
2017-11-23 14:57:39 +00:00
2018-01-21 20:19:00 +00:00
render::clear(ctx, &render_state.color_model);
2018-01-14 21:18:32 +00:00
if let Some(block) = block {
render::render(
ctx,
2018-01-21 20:19:00 +00:00
&cursor::EmptyCursor::new(),
2018-01-14 21:18:32 +00:00
&render_state.font_ctx,
&block.model_layout.model,
&render_state.color_model,
);
ctx.translate(0.0, block.preferred_height as f64);
}
if let Some(level) = level {
2017-11-23 14:57:39 +00:00
render::render(
ctx,
2018-01-21 20:19:00 +00:00
state.cursor.as_ref().unwrap(),
2017-11-23 14:57:39 +00:00
&render_state.font_ctx,
2018-01-02 22:26:04 +00:00
&level.model_layout.model,
2017-11-23 14:57:39 +00:00
&render_state.color_model,
);
2017-11-18 12:56:37 +00:00
}
2017-11-23 14:57:39 +00:00
Inhibit(false)
2017-11-18 12:56:37 +00:00
}
2018-01-03 09:04:01 +00:00
2018-04-05 20:25:18 +00:00
pub struct CmdLineContext<'a> {
pub nvim: &'a Rc<NeovimClient>,
2018-01-03 09:04:01 +00:00
pub content: Vec<(HashMap<String, Value>, String)>,
pub pos: u64,
pub firstc: String,
pub prompt: String,
pub indent: u64,
pub level_idx: u64,
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
pub max_width: i32,
}
2018-04-05 20:25:18 +00:00
impl<'a> CmdLineContext<'a> {
fn get_lines(&self) -> LineContent {
let content_line: Vec<(Option<Attrs>, Vec<char>)> = self.content
.iter()
.map(|c| (Some(Attrs::from_value_map(&c.0)), c.1.chars().collect()))
.collect();
let (prompt_offset, prompt_lines) = prompt_lines(&self.firstc, &self.prompt, self.indent);
let mut content: Vec<_> = prompt_lines.into_iter().map(|line| vec![line]).collect();
if content.is_empty() {
content.push(content_line);
} else {
content.last_mut().map(|line| line.extend(content_line));
}
LineContent {
lines: content,
prompt_offset,
}
}
}
struct LineContent {
lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>,
prompt_offset: usize,
}