Extract mode struct
This commit is contained in:
parent
27b498a36d
commit
5a6f7d7581
@ -2,7 +2,7 @@ use cairo;
|
|||||||
use ui_model::Color;
|
use ui_model::Color;
|
||||||
use ui::UiMutex;
|
use ui::UiMutex;
|
||||||
use shell;
|
use shell;
|
||||||
use shell::NvimMode;
|
use mode;
|
||||||
use nvim::{RepaintMode, RedrawEvents};
|
use nvim::{RepaintMode, RedrawEvents};
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
@ -123,7 +123,7 @@ impl Cursor {
|
|||||||
let current_point = ctx.get_current_point();
|
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);
|
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
|
char_width / 5.0
|
||||||
} else {
|
} else {
|
||||||
if double_width {
|
if double_width {
|
||||||
|
@ -20,6 +20,7 @@ extern crate serde;
|
|||||||
extern crate toml;
|
extern crate toml;
|
||||||
|
|
||||||
mod value;
|
mod value;
|
||||||
|
mod mode;
|
||||||
mod ui_model;
|
mod ui_model;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod ui;
|
mod ui;
|
||||||
|
46
src/mode.rs
Normal file
46
src/mode.rs
Normal 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
18
src/nvim.rs
18
src/nvim.rs
@ -41,7 +41,7 @@ pub trait RedrawEvents {
|
|||||||
|
|
||||||
fn on_update_sp(&mut self, sp: i64) -> RepaintMode;
|
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;
|
fn on_mouse(&mut self, on: bool) -> RepaintMode;
|
||||||
|
|
||||||
@ -106,6 +106,7 @@ macro_rules! map_array {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum CursorShape {
|
pub enum CursorShape {
|
||||||
Block,
|
Block,
|
||||||
Horizontal,
|
Horizontal,
|
||||||
@ -131,8 +132,10 @@ impl CursorShape {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct ModeInfo {
|
pub struct ModeInfo {
|
||||||
cursor_shape: Option<CursorShape>,
|
cursor_shape: Option<CursorShape>,
|
||||||
|
cell_percentage: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModeInfo {
|
impl ModeInfo {
|
||||||
@ -145,7 +148,16 @@ impl ModeInfo {
|
|||||||
None
|
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_bg" => ui.on_update_bg(try_int!(args[0])),
|
||||||
"update_fg" => ui.on_update_fg(try_int!(args[0])),
|
"update_fg" => ui.on_update_fg(try_int!(args[0])),
|
||||||
"update_sp" => ui.on_update_sp(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_on" => ui.on_mouse(true),
|
||||||
"mouse_off" => ui.on_mouse(false),
|
"mouse_off" => ui.on_mouse(false),
|
||||||
"busy_start" => ui.on_busy(true),
|
"busy_start" => ui.on_busy(true),
|
||||||
|
29
src/shell.rs
29
src/shell.rs
@ -28,6 +28,7 @@ use ui::UiMutex;
|
|||||||
use popup_menu::PopupMenu;
|
use popup_menu::PopupMenu;
|
||||||
use tabline::Tabline;
|
use tabline::Tabline;
|
||||||
use error;
|
use error;
|
||||||
|
use mode;
|
||||||
|
|
||||||
const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12";
|
const DEFAULT_FONT_NAME: &str = "DejaVu Sans Mono 12";
|
||||||
pub const MINIMUM_SUPPORTED_NVIM_VERSION: &str = "0.2";
|
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 struct State {
|
||||||
pub model: UiModel,
|
pub model: UiModel,
|
||||||
bg_color: Color,
|
bg_color: Color,
|
||||||
fg_color: Color,
|
fg_color: Color,
|
||||||
sp_color: Color,
|
sp_color: Color,
|
||||||
cur_attrs: Option<Attrs>,
|
cur_attrs: Option<Attrs>,
|
||||||
pub mode: NvimMode,
|
|
||||||
mouse_enabled: bool,
|
mouse_enabled: bool,
|
||||||
nvim: Rc<RefCell<NeovimClient>>,
|
nvim: Rc<RefCell<NeovimClient>>,
|
||||||
font_desc: FontDescription,
|
font_desc: FontDescription,
|
||||||
@ -66,6 +58,8 @@ pub struct State {
|
|||||||
popup_menu: RefCell<PopupMenu>,
|
popup_menu: RefCell<PopupMenu>,
|
||||||
settings: Rc<RefCell<Settings>>,
|
settings: Rc<RefCell<Settings>>,
|
||||||
|
|
||||||
|
pub mode: mode::Mode,
|
||||||
|
|
||||||
stack: gtk::Stack,
|
stack: gtk::Stack,
|
||||||
drawing_area: gtk::DrawingArea,
|
drawing_area: gtk::DrawingArea,
|
||||||
tabs: Tabline,
|
tabs: Tabline,
|
||||||
@ -93,13 +87,15 @@ impl State {
|
|||||||
bg_color: COLOR_BLACK,
|
bg_color: COLOR_BLACK,
|
||||||
fg_color: COLOR_WHITE,
|
fg_color: COLOR_WHITE,
|
||||||
sp_color: COLOR_RED,
|
sp_color: COLOR_RED,
|
||||||
mode: NvimMode::Normal,
|
|
||||||
mouse_enabled: true,
|
mouse_enabled: true,
|
||||||
font_desc: FontDescription::from_string(DEFAULT_FONT_NAME),
|
font_desc: FontDescription::from_string(DEFAULT_FONT_NAME),
|
||||||
cursor: None,
|
cursor: None,
|
||||||
popup_menu,
|
popup_menu,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
|
|
||||||
|
mode: mode::Mode::new(),
|
||||||
|
|
||||||
|
// UI
|
||||||
stack: gtk::Stack::new(),
|
stack: gtk::Stack::new(),
|
||||||
drawing_area,
|
drawing_area,
|
||||||
tabs: Tabline::new(),
|
tabs: Tabline::new(),
|
||||||
@ -414,7 +410,7 @@ impl Shell {
|
|||||||
|
|
||||||
pub fn edit_paste(&self) {
|
pub fn edit_paste(&self) {
|
||||||
let state = self.state.borrow();
|
let state = self.state.borrow();
|
||||||
let paste_command = if state.mode == NvimMode::Normal {
|
let paste_command = if state.mode.is(&mode::NvimMode::Normal) {
|
||||||
"\"*p"
|
"\"*p"
|
||||||
} else {
|
} else {
|
||||||
"<Esc>\"*pa"
|
"<Esc>\"*pa"
|
||||||
@ -949,13 +945,8 @@ impl RedrawEvents for State {
|
|||||||
RepaintMode::Nothing
|
RepaintMode::Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_mode_change(&mut self, mode: &str) -> RepaintMode {
|
fn on_mode_change(&mut self, mode: &str, idx: u64) -> RepaintMode {
|
||||||
match mode {
|
self.mode.update(mode, idx as usize);
|
||||||
"normal" => self.mode = NvimMode::Normal,
|
|
||||||
"insert" => self.mode = NvimMode::Insert,
|
|
||||||
_ => self.mode = NvimMode::Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
RepaintMode::Area(self.model.cur_point())
|
RepaintMode::Area(self.model.cur_point())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1018,7 +1009,7 @@ impl RedrawEvents for State {
|
|||||||
fn mode_info_set(&mut self,
|
fn mode_info_set(&mut self,
|
||||||
cursor_style_enabled: bool,
|
cursor_style_enabled: bool,
|
||||||
mode_info: Vec<nvim::ModeInfo>) -> RepaintMode {
|
mode_info: Vec<nvim::ModeInfo>) -> RepaintMode {
|
||||||
// TODO: mode info handle
|
self.mode.set_info(cursor_style_enabled, mode_info);
|
||||||
RepaintMode::Nothing
|
RepaintMode::Nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user