neovim-gtk/src/cmd_line.rs

240 lines
6.1 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;
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-03 09:04:01 +00:00
pub fn from(ctx: &CmdLineContext, render_state: &shell::RenderState) -> Self {
//TODO: double width chars render, also note in text wrapping
2017-11-23 14:57:39 +00:00
//TODO: im
2018-01-03 09:04:01 +00:00
let content_line: Vec<(Option<Attrs>, Vec<char>)> = ctx.content
.iter()
2018-01-03 20:20:22 +00:00
.map(|c| {
(Some(Attrs::from_value_map(&c.0)), c.1.chars().collect())
})
.collect();
2018-01-03 09:04:01 +00:00
let prompt_lines = prompt_lines(&ctx.firstc, &ctx.prompt, ctx.indent);
2018-01-02 22:26:04 +00:00
let mut content: Vec<_> = prompt_lines.into_iter().map(|line| vec![line]).collect();
2017-11-19 20:13:06 +00:00
2018-01-02 22:26:04 +00:00
if content.is_empty() {
content.push(content_line);
2017-11-23 14:57:39 +00:00
} else {
2018-01-02 22:26:04 +00:00
content.last_mut().map(|line| line.extend(content_line));
2017-11-23 14:57:39 +00:00
}
2018-01-03 09:04:01 +00:00
let &CellMetrics {
line_height,
char_width,
..
} = render_state.font_ctx.cell_metrics();
let max_width_chars = (ctx.max_width as f64 / char_width) as u64;
2018-01-02 22:26:04 +00:00
let mut model_layout = ModelLayout::new();
2018-01-03 09:04:01 +00:00
let (columns, rows) = model_layout.layout(content, max_width_chars);
2018-01-02 22:26:04 +00:00
2018-01-03 20:20:22 +00:00
let columns = if columns < 5 {
5
} else {
columns
};
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
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,
firstc.chars().chain((0..indent).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![]
}
}
2017-11-23 14:57:39 +00:00
struct State {
levels: Vec<Level>,
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(),
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);
drawing_area.connect_draw(
clone!(state => move |_, ctx| gtk_draw(ctx, &state, &cursor)),
);
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-03 09:04:01 +00:00
let mut level = Level::from(ctx, &*state.render_state.borrow());
2017-11-23 14:57:39 +00:00
level.update_cache(&*state.render_state.borrow());
2018-01-03 20:20:22 +00:00
let preferred_height = if level.preferred_height < 40 {
40
} else {
level.preferred_height
};
state.drawing_area.set_size_request(level.preferred_width, preferred_height);
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;
}
}
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();
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,
}