Build ui object as central point of all event processing

This commit is contained in:
daa84 2016-03-28 13:09:31 +03:00
parent 193b4d573c
commit f553fd5f42
4 changed files with 53 additions and 36 deletions

View File

@ -9,13 +9,12 @@ mod ui_model;
mod ui; mod ui;
use ui::Ui; use ui::Ui;
use nvim::Nvim;
fn main() { fn main() {
let ui = Ui::new(); let ui = Ui::new();
ui.show(); ui.show();
let nvim = Nvim::start(ui).expect("Can't start nvim instance"); let nvim = nvim::initialize(ui).expect("Can't start nvim instance");
gtk::main(); gtk::main();
} }

View File

@ -1,49 +1,56 @@
use neovim_lib::{Neovim, NeovimApi, Session}; use neovim_lib::{Neovim, NeovimApi, Session};
use std::io::{Result, Error, ErrorKind}; use std::io::{Result, Error, ErrorKind};
use std::sync::Arc; use std::sync::Arc;
use ui_mutex::UiMutex;
use rmp::Value; use rmp::Value;
use ui::Ui; use ui::Ui;
pub struct Nvim { pub type SharedUi = Arc<UiMutex<Ui>>;
nvim: Neovim,
}
pub trait RedrawEvents { pub trait RedrawEvents {
} }
impl Nvim { pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
pub fn start(mut ui: Ui) -> 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 session = if cfg!(target_os = "windows") {
let session = if cfg!(target_os = "windows") { Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap() } else {
} else { Session::new_child().unwrap()
Session::new_child().unwrap() };
}; let nvim = Neovim::new(session);
let mut nvim = Neovim::new(session); ui.set_nvim(nvim);
nvim.session.start_event_loop_cb(move |m, p| Nvim::cb(&mut ui, m, p)); let sh_ui = Arc::new(UiMutex::new(ui));
{
let mut ui = (*sh_ui).borrow_mut();
let mut nvim = ui.nvim();
let moved_sh_ui = sh_ui.clone();
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(&moved_sh_ui, m, p));
// 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 { nvim: nvim })
} }
fn cb(ui: &mut Ui, method: &str, params: Vec<Value>) { Ok(sh_ui)
if method == "redraw" { }
for ev in params {
if let Value::Array(ev_args) = ev { fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
if let Value::String(ref ev_name) = ev_args[0] { if method == "redraw" {
println!("Event {}", ev_name); for ev in params {
} else { if let Value::Array(ev_args) = ev {
println!("Unsupported event {:?}", ev_args); if let Value::String(ref ev_name) = ev_args[0] {
} println!("Event {}", ev_name);
} else { } else {
println!("Unsupported event type {:?}", ev); println!("Unsupported event {:?}", ev_args);
} }
} else {
println!("Unsupported event type {:?}", ev);
} }
} else {
println!("Notification {}", method);
} }
} else {
println!("Notification {}", method);
} }
} }

View File

@ -2,21 +2,32 @@ use cairo;
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image}; use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image};
use neovim_lib::Neovim;
use ui_model::UiModel; use ui_model::UiModel;
use nvim::RedrawEvents; use nvim::RedrawEvents;
pub struct Ui { pub struct Ui {
model: UiModel, model: UiModel,
nvim: Option<Neovim>,
} }
impl Ui { impl Ui {
pub fn new() -> Ui { pub fn new() -> Ui {
Ui { Ui {
model: UiModel::empty(), model: UiModel::empty(),
nvim: None,
} }
} }
pub fn set_nvim(&mut self, nvim: Neovim) {
self.nvim = Some(nvim);
}
pub fn nvim(&mut self) -> &mut Neovim {
self.nvim.as_mut().unwrap()
}
pub fn show(&self) { pub fn show(&self) {
gtk::init().expect("Failed to initialize GTK"); gtk::init().expect("Failed to initialize GTK");
let window = Window::new(WindowType::Toplevel); let window = Window::new(WindowType::Toplevel);

View File

@ -4,18 +4,18 @@ use std::thread;
use std::sync::Arc; use std::sync::Arc;
use gtk; use gtk;
pub struct MainLoopMutex<T: Sized> { pub struct UiMutex<T: Sized> {
data: RefCell<T>, data: RefCell<T>,
main_thread_name: Option<String>, main_thread_name: Option<String>,
} }
// here sync used to mark that internal data is acessed only from main thread // here sync used to mark that internal data is acessed only from main thread
// this behaviour works because of borrow_mut check thread name // this behaviour works because of borrow_mut check thread name
unsafe impl<T: Sized + Send> Sync for MainLoopMutex<T> {} unsafe impl<T: Sized + Send> Sync for UiMutex<T> {}
impl<T> MainLoopMutex<T> { impl<T> UiMutex<T> {
pub fn new(t: T) -> MainLoopMutex<T> { pub fn new(t: T) -> UiMutex<T> {
MainLoopMutex { UiMutex {
data: RefCell::new(t), data: RefCell::new(t),
main_thread_name: thread::current().name().map(|v| v.to_owned()), main_thread_name: thread::current().name().map(|v| v.to_owned()),
} }
@ -29,9 +29,9 @@ impl<T> MainLoopMutex<T> {
self.data.borrow_mut() self.data.borrow_mut()
} }
pub fn safe_call<F, I>(mutex: Arc<MainLoopMutex<I>>, cb: F) pub fn safe_call<F, I>(mutex: Arc<UiMutex<I>>, cb: F)
where I: 'static, where I: 'static,
F: Fn(&MainLoopMutex<I>) + 'static F: Fn(&UiMutex<I>) + 'static
{ {
gtk::idle_add(move || { gtk::idle_add(move || {
cb(&*mutex); cb(&*mutex);