Extract mode struct

This commit is contained in:
daa 2017-07-08 23:45:55 +03:00
parent 27b498a36d
commit 5a6f7d7581
5 changed files with 74 additions and 24 deletions

View File

@ -2,7 +2,7 @@ use cairo;
use ui_model::Color;
use ui::UiMutex;
use shell;
use shell::NvimMode;
use mode;
use nvim::{RepaintMode, RedrawEvents};
use std::sync::{Arc, Weak};
@ -123,7 +123,7 @@ impl Cursor {
let current_point = ctx.get_current_point();
ctx.set_source_rgba(1.0 - bg.0, 1.0 - bg.1, 1.0 - bg.2, 0.6 * state.alpha.0);
let cursor_width = if shell.mode == NvimMode::Insert {
let cursor_width = if shell.mode.is(&mode::NvimMode::Insert) {
char_width / 5.0
} else {
if double_width {

View File

@ -20,6 +20,7 @@ extern crate serde;
extern crate toml;
mod value;
mod mode;
mod ui_model;
#[macro_use]
mod ui;

46
src/mode.rs Normal file
View File

@ -0,0 +1,46 @@
use nvim;
#[derive(PartialEq)]
pub enum NvimMode {
Normal,
Insert,
Other,
}
pub struct Mode {
mode: NvimMode,
idx: usize,
info: Option<Vec<nvim::ModeInfo>>,
}
impl Mode {
pub fn new() -> Self {
Mode {
mode: NvimMode::Normal,
idx: 0,
info: None,
}
}
pub fn is(&self, mode: &NvimMode) -> bool {
self.mode == *mode
}
pub fn update(&mut self, mode: &str, idx: usize) {
match mode {
"normal" => self.mode = NvimMode::Normal,
"insert" => self.mode = NvimMode::Insert,
_ => self.mode = NvimMode::Other,
}
self.idx = idx;
}
pub fn set_info(&mut self, cursor_style_enabled: bool, info: Vec<nvim::ModeInfo>) {
self.info = if cursor_style_enabled {
Some(info)
} else {
None
};
}
}

View File

@ -41,7 +41,7 @@ pub trait RedrawEvents {
fn on_update_sp(&mut self, sp: i64) -> RepaintMode;
fn on_mode_change(&mut self, mode: &str) -> RepaintMode;
fn on_mode_change(&mut self, mode: &str, idx: u64) -> RepaintMode;
fn on_mouse(&mut self, on: bool) -> RepaintMode;
@ -106,6 +106,7 @@ macro_rules! map_array {
);
}
#[derive(Debug)]
pub enum CursorShape {
Block,
Horizontal,
@ -131,8 +132,10 @@ impl CursorShape {
}
}
#[derive(Debug)]
pub struct ModeInfo {
cursor_shape: Option<CursorShape>,
cell_percentage: Option<u64>,
}
impl ModeInfo {
@ -145,7 +148,16 @@ impl ModeInfo {
None
};
Ok(ModeInfo { cursor_shape })
let cell_percentage = if let Some(cell_percentage) = mode_info_map.get("cell_percentage") {
cell_percentage.as_u64()
} else {
None
};
Ok(ModeInfo {
cursor_shape,
cell_percentage,
})
}
}
@ -376,7 +388,7 @@ fn call(ui: &mut shell::State,
"update_bg" => ui.on_update_bg(try_int!(args[0])),
"update_fg" => ui.on_update_fg(try_int!(args[0])),
"update_sp" => ui.on_update_sp(try_int!(args[0])),
"mode_change" => ui.on_mode_change(try_str!(args[0])),
"mode_change" => ui.on_mode_change(try_str!(args[0]), try_uint!(args[1])),
"mouse_on" => ui.on_mouse(true),
"mouse_off" => ui.on_mouse(false),
"busy_start" => ui.on_busy(true),

View File

@ -28,6 +28,7 @@ use ui::UiMutex;
use popup_menu::PopupMenu;
use tabline::Tabline;
use error;
use mode;
const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12";
pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2";
@ -44,21 +45,12 @@ macro_rules! idle_cb_call {
)
}
#[derive(PartialEq)]
pub enum NvimMode {
Normal,
Insert,
Other,
}
pub struct State {
pub model: UiModel,
bg_color: Color,
fg_color: Color,
sp_color: Color,
cur_attrs: Option<Attrs>,
pub mode: NvimMode,
mouse_enabled: bool,
nvim: Rc<RefCell<NeovimClient>>,
font_desc: FontDescription,
@ -66,6 +58,8 @@ pub struct State {
popup_menu: RefCell<PopupMenu>,
settings: Rc<RefCell<Settings>>,
pub mode: mode::Mode,
stack: gtk::Stack,
drawing_area: gtk::DrawingArea,
tabs: Tabline,
@ -93,13 +87,15 @@ impl State {
bg_color: COLOR_BLACK,
fg_color: COLOR_WHITE,
sp_color: COLOR_RED,
mode: NvimMode::Normal,
mouse_enabled: true,
font_desc: FontDescription::from_string(DEFAULT_FONT_NAME),
cursor: None,
popup_menu,
settings: settings,
mode: mode::Mode::new(),
// UI
stack: gtk::Stack::new(),
drawing_area,
tabs: Tabline::new(),
@ -414,7 +410,7 @@ impl Shell {
pub fn edit_paste(&self) {
let state = self.state.borrow();
let paste_command = if state.mode == NvimMode::Normal {
let paste_command = if state.mode.is(&mode::NvimMode::Normal) {
"\"*p"
} else {
"<Esc>\"*pa"
@ -949,13 +945,8 @@ impl RedrawEvents for State {
RepaintMode::Nothing
}
fn on_mode_change(&mut self, mode: &str) -> RepaintMode {
match mode {
"normal" => self.mode = NvimMode::Normal,
"insert" => self.mode = NvimMode::Insert,
_ => self.mode = NvimMode::Other,
}
fn on_mode_change(&mut self, mode: &str, idx: u64) -> RepaintMode {
self.mode.update(mode, idx as usize);
RepaintMode::Area(self.model.cur_point())
}
@ -1018,7 +1009,7 @@ impl RedrawEvents for State {
fn mode_info_set(&mut self,
cursor_style_enabled: bool,
mode_info: Vec<nvim::ModeInfo>) -> RepaintMode {
// TODO: mode info handle
self.mode.set_info(cursor_style_enabled, mode_info);
RepaintMode::Nothing
}
}