First event
This commit is contained in:
parent
f553fd5f42
commit
fae82271d1
45
src/nvim.rs
45
src/nvim.rs
@ -1,13 +1,31 @@
|
||||
use neovim_lib::{Neovim, NeovimApi, Session};
|
||||
use std::io::{Result, Error, ErrorKind};
|
||||
use std::sync::Arc;
|
||||
use std::result;
|
||||
use ui_mutex::UiMutex;
|
||||
use rmp::Value;
|
||||
use rmp::value::Integer;
|
||||
use ui::Ui;
|
||||
use gtk;
|
||||
|
||||
pub type SharedUi = Arc<UiMutex<Ui>>;
|
||||
|
||||
pub trait RedrawEvents {
|
||||
fn on_cursor_goto(&mut self, row: u64, col: u64);
|
||||
}
|
||||
|
||||
macro_rules! try_str {
|
||||
($exp:expr) => (match $exp {
|
||||
Value::String(ref val) => val.to_owned(),
|
||||
_ => return Err("Can't convert argument to string".to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
macro_rules! try_int {
|
||||
($exp:expr) => (match $exp {
|
||||
Value::Integer(Integer::U64(val)) => val,
|
||||
_ => return Err("Can't convert argument to int".to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
|
||||
@ -42,7 +60,9 @@ fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
|
||||
for ev in params {
|
||||
if let Value::Array(ev_args) = ev {
|
||||
if let Value::String(ref ev_name) = ev_args[0] {
|
||||
println!("Event {}", ev_name);
|
||||
let mut args = vec![];
|
||||
args.extend_from_slice(&ev_args[1..]);
|
||||
call(ui, ev_name, args);
|
||||
} else {
|
||||
println!("Unsupported event {:?}", ev_args);
|
||||
}
|
||||
@ -54,3 +74,26 @@ fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
|
||||
println!("Notification {}", method);
|
||||
}
|
||||
}
|
||||
|
||||
fn call(ui: &SharedUi, method: &str, args: Vec<Value>) {
|
||||
match method {
|
||||
"cursor_goto" => {
|
||||
safe_call(ui.clone(), move |ui| {
|
||||
ui.borrow_mut().on_cursor_goto(try_int!(args[0]), try_int!(args[1]));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
_ => println!("Event {}", method),
|
||||
};
|
||||
}
|
||||
|
||||
fn safe_call<F>(mutex: SharedUi, cb: F)
|
||||
where F: Fn(&UiMutex<Ui>) -> result::Result<(), String> + 'static
|
||||
{
|
||||
gtk::idle_add(move || {
|
||||
if let Err(msg) = cb(&*mutex) {
|
||||
println!("Error call function: {}", msg);
|
||||
}
|
||||
gtk::Continue(false)
|
||||
});
|
||||
}
|
||||
|
@ -79,5 +79,8 @@ impl Ui {
|
||||
}
|
||||
|
||||
impl RedrawEvents for Ui {
|
||||
fn on_cursor_goto(&mut self, row: u64, col: u64) {
|
||||
self.model.set_cursor(row, col);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ impl UiModel {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_cursor(&mut self, col: u64, row: u64) {
|
||||
pub fn set_cursor(&mut self, row: u64, col: u64) {
|
||||
self.cur_col = col;
|
||||
self.cur_row = row;
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::thread;
|
||||
use std::sync::Arc;
|
||||
use gtk;
|
||||
|
||||
pub struct UiMutex<T: Sized> {
|
||||
data: RefCell<T>,
|
||||
@ -28,16 +26,6 @@ impl<T> UiMutex<T> {
|
||||
|
||||
self.data.borrow_mut()
|
||||
}
|
||||
|
||||
pub fn safe_call<F, I>(mutex: Arc<UiMutex<I>>, cb: F)
|
||||
where I: 'static,
|
||||
F: Fn(&UiMutex<I>) + 'static
|
||||
{
|
||||
gtk::idle_add(move || {
|
||||
cb(&*mutex);
|
||||
gtk::Continue(false)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user