Build ui object as central point of all event processing
This commit is contained in:
parent
193b4d573c
commit
f553fd5f42
@ -9,13 +9,12 @@ mod ui_model;
|
||||
mod ui;
|
||||
|
||||
use ui::Ui;
|
||||
use nvim::Nvim;
|
||||
|
||||
fn main() {
|
||||
let ui = Ui::new();
|
||||
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();
|
||||
}
|
||||
|
61
src/nvim.rs
61
src/nvim.rs
@ -1,49 +1,56 @@
|
||||
use neovim_lib::{Neovim, NeovimApi, Session};
|
||||
use std::io::{Result, Error, ErrorKind};
|
||||
use std::sync::Arc;
|
||||
use ui_mutex::UiMutex;
|
||||
use rmp::Value;
|
||||
use ui::Ui;
|
||||
|
||||
pub struct Nvim {
|
||||
nvim: Neovim,
|
||||
}
|
||||
pub type SharedUi = Arc<UiMutex<Ui>>;
|
||||
|
||||
pub trait RedrawEvents {
|
||||
}
|
||||
|
||||
impl Nvim {
|
||||
pub fn start(mut ui: Ui) -> Result<Nvim> {
|
||||
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
|
||||
let session = if cfg!(target_os = "windows") {
|
||||
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
|
||||
} else {
|
||||
Session::new_child().unwrap()
|
||||
};
|
||||
let mut nvim = Neovim::new(session);
|
||||
pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
|
||||
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
|
||||
let session = if cfg!(target_os = "windows") {
|
||||
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
|
||||
} else {
|
||||
Session::new_child().unwrap()
|
||||
};
|
||||
let 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
|
||||
nvim.input("i").unwrap();
|
||||
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>) {
|
||||
if method == "redraw" {
|
||||
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);
|
||||
} else {
|
||||
println!("Unsupported event {:?}", ev_args);
|
||||
}
|
||||
Ok(sh_ui)
|
||||
}
|
||||
|
||||
fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
|
||||
if method == "redraw" {
|
||||
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);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
11
src/ui.rs
11
src/ui.rs
@ -2,21 +2,32 @@ use cairo;
|
||||
use gtk;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Window, WindowType, DrawingArea, Grid, ToolButton, ButtonBox, Orientation, Image};
|
||||
use neovim_lib::Neovim;
|
||||
|
||||
use ui_model::UiModel;
|
||||
use nvim::RedrawEvents;
|
||||
|
||||
pub struct Ui {
|
||||
model: UiModel,
|
||||
nvim: Option<Neovim>,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn new() -> Ui {
|
||||
Ui {
|
||||
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) {
|
||||
gtk::init().expect("Failed to initialize GTK");
|
||||
let window = Window::new(WindowType::Toplevel);
|
||||
|
@ -4,18 +4,18 @@ use std::thread;
|
||||
use std::sync::Arc;
|
||||
use gtk;
|
||||
|
||||
pub struct MainLoopMutex<T: Sized> {
|
||||
pub struct UiMutex<T: Sized> {
|
||||
data: RefCell<T>,
|
||||
main_thread_name: Option<String>,
|
||||
}
|
||||
|
||||
// here sync used to mark that internal data is acessed only from main thread
|
||||
// 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> {
|
||||
pub fn new(t: T) -> MainLoopMutex<T> {
|
||||
MainLoopMutex {
|
||||
impl<T> UiMutex<T> {
|
||||
pub fn new(t: T) -> UiMutex<T> {
|
||||
UiMutex {
|
||||
data: RefCell::new(t),
|
||||
main_thread_name: thread::current().name().map(|v| v.to_owned()),
|
||||
}
|
||||
@ -29,9 +29,9 @@ impl<T> MainLoopMutex<T> {
|
||||
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,
|
||||
F: Fn(&MainLoopMutex<I>) + 'static
|
||||
F: Fn(&UiMutex<I>) + 'static
|
||||
{
|
||||
gtk::idle_add(move || {
|
||||
cb(&*mutex);
|
||||
|
Loading…
Reference in New Issue
Block a user