2017-11-19 09:44:28 +00:00
|
|
|
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-19 09:44:28 +00:00
|
|
|
|
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
|
|
|
|
2017-11-19 09:44:28 +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;
|
|
|
|
use render;
|
|
|
|
use shell;
|
|
|
|
use cursor;
|
2017-11-19 09:44:28 +00:00
|
|
|
|
|
|
|
pub struct Level {
|
2018-01-02 22:26:04 +00:00
|
|
|
model_layout: ModelLayout,
|
2017-11-19 09:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
|
|
|
|
|
|
|
pub fn from(
|
|
|
|
content: Vec<(HashMap<String, Value>, String)>,
|
|
|
|
pos: u64,
|
|
|
|
firstc: String,
|
|
|
|
prompt: String,
|
|
|
|
indent: u64,
|
|
|
|
) -> Self {
|
|
|
|
//TODO: double width chars
|
2017-11-23 14:57:39 +00:00
|
|
|
//TODO: im
|
2017-11-19 09:44:28 +00:00
|
|
|
|
2018-01-02 22:26:04 +00:00
|
|
|
let content_line: Vec<(Option<Attrs>, Vec<char>)> = content
|
2017-11-19 09:44:28 +00:00
|
|
|
.iter()
|
2018-01-02 22:26:04 +00:00
|
|
|
.map(|c| (Some(Attrs::from_value_map(&c.0)), c.1.chars().collect()))
|
2017-11-19 09:44:28 +00:00
|
|
|
.collect();
|
2018-01-02 22:26:04 +00:00
|
|
|
let prompt_lines = prompt_lines(firstc, prompt, indent);
|
2017-11-19 09:44:28 +00:00
|
|
|
|
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
|
|
|
}
|
2017-11-19 09:44:28 +00:00
|
|
|
|
2018-01-02 22:26:04 +00:00
|
|
|
let mut model_layout = ModelLayout::new();
|
|
|
|
// TODO: calculate width
|
|
|
|
model_layout.layout(content, 5);
|
|
|
|
|
|
|
|
Level { model_layout }
|
2017-11-19 09:44:28 +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,
|
|
|
|
);
|
|
|
|
}
|
2017-11-19 09:44:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 22:26:04 +00:00
|
|
|
fn prompt_lines(firstc: String, prompt: String, indent: u64) -> Vec<(Option<Attrs>, Vec<char>)> {
|
2017-11-19 20:13:06 +00:00
|
|
|
if !firstc.is_empty() {
|
2018-01-02 22:26:04 +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-02 22:26:04 +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: >k::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-02 22:26:04 +00:00
|
|
|
popover.set_position(gtk::PositionType::Top);
|
|
|
|
|
2017-11-18 20:15:03 +00:00
|
|
|
let edit_frame = gtk::Frame::new(None);
|
|
|
|
edit_frame.set_shadow_type(gtk::ShadowType::In);
|
|
|
|
let drawing_area = gtk::DrawingArea::new();
|
2018-01-02 22:26:04 +00:00
|
|
|
drawing_area.set_size_request(150, 50);
|
2017-11-18 20:15:03 +00:00
|
|
|
edit_frame.add(&drawing_area);
|
|
|
|
edit_frame.show_all();
|
|
|
|
|
|
|
|
popover.add(&edit_frame);
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn show_level(
|
|
|
|
&mut self,
|
|
|
|
content: Vec<(HashMap<String, Value>, String)>,
|
|
|
|
pos: u64,
|
|
|
|
firstc: String,
|
|
|
|
prompt: String,
|
|
|
|
indent: u64,
|
|
|
|
level_idx: u64,
|
|
|
|
) {
|
|
|
|
let mut state = self.state.borrow_mut();
|
|
|
|
|
|
|
|
let mut level = Level::from(content, pos, firstc, prompt, indent);
|
|
|
|
level.update_cache(&*state.render_state.borrow());
|
|
|
|
|
|
|
|
if level_idx as usize == state.levels.len() {
|
|
|
|
// TODO: update level
|
|
|
|
state.levels.pop();
|
|
|
|
}
|
|
|
|
state.levels.push(level);
|
|
|
|
if !self.displyed {
|
|
|
|
self.displyed = true;
|
2018-01-02 22:26:04 +00:00
|
|
|
let allocation = self.popover.get_relative_to().unwrap().get_allocation();
|
|
|
|
self.popover.set_pointing_to(>k::Rectangle {
|
|
|
|
x: allocation.width / 2,
|
|
|
|
y: allocation.height / 2,
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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
|
|
|
}
|