Add events callback

This commit is contained in:
daa 2016-03-23 14:59:18 +03:00
parent dc1bf55a46
commit 3fc2c3322d
4 changed files with 21 additions and 15 deletions

3
Cargo.lock generated
View File

@ -5,7 +5,7 @@ dependencies = [
"cairo-rs 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "cairo-rs 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"neovim-lib 0.1.0", "neovim-lib 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -195,6 +195,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "neovim-lib" name = "neovim-lib"
version = "0.1.0" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rmp-serialize 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "rmp-serialize 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -11,7 +11,8 @@ use ui::Ui;
use nvim::Nvim; use nvim::Nvim;
fn main() { fn main() {
let nvim = Nvim::start().expect("Can't start nvim instance"); let ui = Ui::new();
Ui::new().show(); let nvim = Nvim::start(ui).expect("Can't start nvim instance");
ui.show();
} }

View File

@ -3,38 +3,37 @@ use std::io::{Result, Error, ErrorKind};
use rmp::Value; use rmp::Value;
pub struct Nvim { pub struct Nvim {
nvim: Neovim nvim: Neovim,
}
pub trait RedrawEvents {
} }
impl Nvim { impl Nvim {
pub fn start() -> Result<Nvim> { pub fn start<F: RedrawEvents + Send + 'static> (redraw_cb: F) -> Result<Nvim> {
//let mut session = try!(Session::new_tcp("127.0.0.1:6666")); // let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
let mut session = try!(Session::new_child()); let mut session = try!(Session::new_child());
session.start_event_loop_cb(Nvim::cb); session.start_event_loop_cb(move |m, p| Nvim::cb(&redraw_cb, m, p));
let mut nvim = Neovim::new(session); let mut nvim = Neovim::new(session);
// fix neovim --embed bug to start embed mode // fix neovim --embed bug to start embed mode
nvim.input("i").unwrap(); nvim.input("i").unwrap();
try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e))); try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e)));
Ok(Nvim { Ok(Nvim { nvim: nvim })
nvim: nvim,
})
} }
fn cb(method: &str, params: Vec<Value>) { fn cb<F: RedrawEvents>(readraw_cb: &F, method: &str, params: Vec<Value>) {
if method == "redraw" { if method == "redraw" {
for ev in params { for ev in params {
if let Value::Array(ev_args) = ev { if let Value::Array(ev_args) = ev {
if let Value::String(ref ev_name) = ev_args[0] { if let Value::String(ref ev_name) = ev_args[0] {
println!("Event {}", ev_name); println!("Event {}", ev_name);
} } else {
else {
println!("Unsupported event {:?}", ev_args); println!("Unsupported event {:?}", ev_args);
} }
} } else {
else {
println!("Unsupported event type {:?}", ev); println!("Unsupported event type {:?}", ev);
} }
} }

View File

@ -4,6 +4,7 @@ use gtk::prelude::*;
use gtk::{Window, WindowType, DrawingArea, Grid, Button, ButtonBox, Orientation}; use gtk::{Window, WindowType, DrawingArea, Grid, Button, ButtonBox, Orientation};
use ui_model::UiModel; use ui_model::UiModel;
use nvim::RedrawEvents;
pub struct Ui { pub struct Ui {
model: UiModel, model: UiModel,
@ -53,3 +54,7 @@ impl Ui {
Inhibit(true) Inhibit(true)
} }
} }
impl RedrawEvents for Ui {
}