neovim-gtk/src/cmd_line.rs

308 lines
8.2 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
2018-01-13 15:02:47 +00:00
pub fn append_line(&mut self,
content: &Vec<Vec<(HashMap<String, Value>, String)>>) {
2018-01-13 18:32:21 +00:00
let lines: Vec<Vec<(Option<Attrs>, Vec<char>)>> = content
.iter()
.map(|line_chars| {
line_chars
.iter()
.map(|c| {
(Some(Attrs::from_value_map(&c.0)), c.1.chars().collect())
})
.collect()
})
.collect();
//TODO: implement
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 {
let lines: Vec<Vec<(Option<Attrs>, Vec<char>)>> = content
.iter()
2018-01-13 15:02:47 +00:00
.map(|line_chars| {
line_chars
.iter()
.map(|c| {
(Some(Attrs::from_value_map(&c.0)), c.1.chars().collect())
})
.collect()
2018-01-03 20:20:22 +00:00
})
.collect();
2017-11-19 20:13:06 +00:00
2018-01-13 15:02:47 +00:00
Level::from_lines(lines, max_width, render_state)
}
2018-01-13 15:02:47 +00:00
pub fn from_lines(
lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>,
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();
2018-01-13 18:32:21 +00:00
let max_width_chars = (max_width as f64 / char_width) as usize;
2018-01-03 09:04:01 +00:00
2018-01-02 22:26:04 +00:00
let mut model_layout = ModelLayout::new();
2018-01-13 15:02:47 +00:00
let (columns, rows) = model_layout.layout(lines, max_width_chars);
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 {
let content_line: Vec<(Option<Attrs>, Vec<char>)> = ctx.content
.iter()
.map(|c| {
(Some(Attrs::from_value_map(&c.0)), c.1.chars().collect())
})
.collect();
let prompt_lines = prompt_lines(&ctx.firstc, &ctx.prompt, ctx.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));
}
Level::from_lines(content, ctx.max_width, render_state)
}
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,
}
}
}
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();
2018-01-11 19:44:19 +00:00
let mut level = Level::from_ctx(ctx, &*state.render_state.borrow());
2017-11-23 14:57:39 +00:00
level.update_cache(&*state.render_state.borrow());
2018-01-13 15:02:47 +00:00
// TODO: request size depends on leve size + block size
state
.drawing_area
.set_size_request(level.preferred_width, max(level.preferred_height, 40));
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() {
2017-11-23 14:57:39 +00:00
// TODO: update level
state.levels.pop();
}
state.levels.push(level);
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 {
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());
block.update_cache(&*state.render_state.borrow());
state.block = Some(block);
}
pub fn block_append(
&mut self,
content: &Vec<Vec<(HashMap<String, Value>, String)>>,
) {
let mut state = self.state.borrow_mut();
let render_state = state.render_state.clone();
let block = state.block.as_mut().unwrap();
block.append_line(content);
block.update_cache(& *render_state.borrow());
}
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-13 15:02:47 +00:00
//TODO: draw block
2017-11-23 14:57:39 +00:00
if let Some(level) = level {
let render_state = state.render_state.borrow();
2018-01-03 20:20:22 +00:00
let gap = state.drawing_area.get_allocated_height() - level.preferred_height;
if gap > 0 {
ctx.translate(0.0, gap as f64 / 2.0);
}
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,
}