neovim-gtk/src/cmd_line.rs

373 lines
10 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-01-11 19:44:19 +00:00
use std::cmp::max;
2017-11-18 12:56:37 +00:00
use gtk;
use gtk::prelude::*;
2017-11-23 14:57:39 +00:00
use cairo;
2017-11-18 12:56:37 +00:00
use neovim_lib::Value;
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;
pub struct Level {
2018-01-02 22:26:04 +00:00
model_layout: ModelLayout,
2018-01-03 09:04:01 +00:00
preferred_width: i32,
preferred_height: i32,
}
impl Level {
2018-01-13 15:02:47 +00:00
//TODO: double width chars render, also note in text wrapping
//TODO: im
//TODO: cursor
pub fn replace_from_ctx(&mut self, ctx: &CmdLineContext, render_state: &shell::RenderState) {
self.replace_line(&ctx.get_lines(), render_state, false);
}
fn replace_line(
&mut self,
lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>,
render_state: &shell::RenderState,
append: bool,
) {
let &CellMetrics {
line_height,
char_width,
..
} = render_state.font_ctx.cell_metrics();
2018-01-13 15:02:47 +00:00
let (columns, rows) = if append {
self.model_layout.layout_append(lines)
} else {
self.model_layout.layout(lines)
};
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())
})
.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 {
2018-01-03 09:04:01 +00:00
let &CellMetrics {
line_height,
char_width,
..
} = render_state.font_ctx.cell_metrics();
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);
let (columns, rows) = model_layout.layout(lines);
2018-01-02 22:26:04 +00:00
2018-01-11 19:44:19 +00:00
let columns = max(columns, 5);
2018-01-03 20:20:22 +00:00
2018-01-03 09:04:01 +00:00
let preferred_width = (char_width * columns as f64) as i32;
let preferred_height = (line_height * rows as f64) as i32;
2018-01-03 20:20:22 +00:00
Level {
model_layout,
preferred_width,
preferred_height,
}
}
2017-11-23 14:57:39 +00:00
2018-01-13 15:02:47 +00:00
pub fn from_ctx(ctx: &CmdLineContext, render_state: &shell::RenderState) -> Self {
Level::from_lines(&ctx.get_lines(), ctx.max_width, render_state)
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,
);
}
}
2018-01-03 09:04:01 +00:00
fn prompt_lines(firstc: &str, prompt: &str, indent: u64) -> Vec<(Option<Attrs>, Vec<char>)> {
2017-11-19 20:13:06 +00:00
if !firstc.is_empty() {
2018-01-03 20:20:22 +00:00
vec![
(
None,
2018-01-13 15:02:47 +00:00
firstc.chars().chain((0..indent).map(|_| ' ')).collect(),
2018-01-03 20:20:22 +00:00
),
]
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![]
}
}
2017-11-23 14:57:39 +00:00
struct State {
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,
}
impl State {
fn new(drawing_area: gtk::DrawingArea, render_state: Rc<RefCell<shell::RenderState>>) -> Self {
State {
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-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();
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));
drawing_area.set_size_request(
max(level_width, block_width),
max(block_height + level_height, 40),
);
}
2017-11-23 14:57:39 +00:00
}
impl cursor::CursorRedrawCb for State {
fn queue_redraw_cursor(&mut self) {
// TODO: implement
}
}
2017-11-18 12:56:37 +00:00
pub struct CmdLine {
2017-11-18 20:15:03 +00:00
popover: gtk::Popover,
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
2017-11-18 20:15:03 +00:00
let drawing_area = gtk::DrawingArea::new();
2018-01-03 20:20:22 +00:00
drawing_area.show_all();
popover.add(&drawing_area);
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);
let cursor = cursor::Cursor::new(weak_cb);
2018-01-13 15:02:47 +00:00
drawing_area.connect_draw(clone!(state => move |_, ctx| gtk_draw(ctx, &state, &cursor)));
2017-11-23 14:57:39 +00:00
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-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();
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();
} 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 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-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
}
pub fn block_append(&mut self, content: &Vec<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
{
let block = state.block.as_mut().unwrap();
block.replace_line(
&Level::to_attributed_content(content),
&*render_state.borrow(),
true,
);
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
2018-01-13 15:02:47 +00:00
pub fn block_hide(&mut self) {
self.state.borrow_mut().block = None;
2018-01-11 19:44:19 +00:00
}
2017-11-23 14:57:39 +00:00
}
2017-11-18 12:56:37 +00:00
2017-11-23 14:57:39 +00:00
fn gtk_draw(
ctx: &cairo::Context,
state: &Arc<UiMutex<State>>,
cursor: &cursor::Cursor<State>,
) -> Inhibit {
let state = state.borrow();
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 preferred_height = level.map(|l| l.preferred_height).unwrap_or(0)
+ block.as_ref().map(|b| b.preferred_height).unwrap_or(0);
let render_state = state.render_state.borrow();
let gap = state.drawing_area.get_allocated_height() - preferred_height;
if gap > 0 {
ctx.translate(0.0, gap as f64 / 2.0);
}
2017-11-23 14:57:39 +00:00
2018-01-14 21:18:32 +00:00
if let Some(block) = block {
// TODO: disable cursor
render::render(
ctx,
cursor,
&render_state.font_ctx,
&block.model_layout.model,
&render_state.color_model,
&render_state.mode,
);
ctx.translate(0.0, block.preferred_height as f64);
}
if let Some(level) = level {
//TODO: limit model to row filled
2017-11-23 14:57:39 +00:00
render::render(
ctx,
cursor,
&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,
&render_state.mode,
);
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
pub struct CmdLineContext {
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,
}
impl CmdLineContext {
fn get_lines(&self) -> Vec<Vec<(Option<Attrs>, Vec<char>)>> {
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_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));
}
content
}
}