From f58e398c48aa5d13be88004d496206e5eb1592ad Mon Sep 17 00:00:00 2001 From: daa Date: Sun, 19 Nov 2017 12:44:28 +0300 Subject: [PATCH] Starting point on conversion cmdline input to UiModel --- src/cmd_line.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- src/shell.rs | 10 ++++++---- src/ui_model/mod.rs | 6 +++--- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/cmd_line.rs b/src/cmd_line.rs index 4180bb2..3b82c6a 100644 --- a/src/cmd_line.rs +++ b/src/cmd_line.rs @@ -1,9 +1,46 @@ +use std::collections::HashMap; + use gtk; use gtk::prelude::*; -//TODO: levels +use neovim_lib::Value; + +use ui_model::{UiModel, Attrs}; + +pub struct Level { + model: UiModel, +} + +impl Level { + const COLUMNS_STEP: u64 = 50; + + pub fn from( + content: Vec<(HashMap, String)>, + pos: u64, + firstc: String, + prompt: String, + indent: u64, + level: u64, + ) -> Self { + //TODO: double width chars + //TODO: multiline prompt + + let content: Vec<(Attrs, Vec)> = content + .iter() + .map(|c| (Attrs::from_value_map(&c.0), c.1.chars().collect())) + .collect(); + + let width = content.iter().map(|c| c.1.len()).count() as u64 + indent + 1/*firstc*/; + let columns = ((width / Level::COLUMNS_STEP) + 1) * Level::COLUMNS_STEP; + + let model = UiModel::new(1, columns); + Level { model } + } +} + pub struct CmdLine { popover: gtk::Popover, + levels: Vec, } impl CmdLine { @@ -19,11 +56,13 @@ impl CmdLine { popover.add(&edit_frame); CmdLine { + levels: Vec::new(), popover, } } - pub fn show(&self) { + pub fn show_level(&mut self, level: Level) { + self.levels.push(level); self.popover.popup(); } } diff --git a/src/shell.rs b/src/shell.rs index 5fcd907..b5d1d9c 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -30,7 +30,7 @@ use cursor::Cursor; use ui::UiMutex; use popup_menu::{self, PopupMenu}; use tabline::Tabline; -use cmd_line::CmdLine; +use cmd_line::{self, CmdLine}; use error; use mode; use render; @@ -915,7 +915,9 @@ impl RedrawEvents for State { } fn on_put(&mut self, text: String) -> RepaintMode { - RepaintMode::Area(self.model.put(&text, self.cur_attrs.as_ref())) + let ch = text.chars().last().unwrap_or(' '); + let double_width = text.is_empty(); + RepaintMode::Area(self.model.put(ch, double_width, self.cur_attrs.as_ref())) } fn on_clear(&mut self) -> RepaintMode { @@ -1090,8 +1092,8 @@ impl RedrawEvents for State { indent: u64, level: u64, ) -> RepaintMode { - self.cmd_line.show(); - // TODO: implement + let level = cmd_line::Level::from(content, pos, firstc, prompt, indent, level); + self.cmd_line.show_level(level); RepaintMode::Nothing } } diff --git a/src/ui_model/mod.rs b/src/ui_model/mod.rs index ec71f26..6e67adc 100644 --- a/src/ui_model/mod.rs +++ b/src/ui_model/mod.rs @@ -91,16 +91,16 @@ impl UiModel { (self.cur_row, self.cur_col) } - pub fn put(&mut self, text: &str, attrs: Option<&Attrs>) -> ModelRect { + pub fn put(&mut self, ch: char, double_width: bool, attrs: Option<&Attrs>) -> ModelRect { let mut changed_region = self.cur_point(); let line = &mut self.model[self.cur_row]; line.dirty_line = true; let cell = &mut line[self.cur_col]; - cell.ch = text.chars().last().unwrap_or(' '); + cell.ch = ch; cell.attrs = attrs.map(Attrs::clone).unwrap_or_else(Attrs::new); - cell.attrs.double_width = text.is_empty(); + cell.attrs.double_width = double_width; cell.dirty = true; self.cur_col += 1; if self.cur_col >= self.columns {