neovim-gtk/src/main.rs

67 lines
1.6 KiB
Rust
Raw Normal View History

2016-03-16 14:39:53 +00:00
extern crate gtk;
2017-03-09 08:44:22 +00:00
extern crate gtk_sys;
2017-03-06 13:58:10 +00:00
extern crate gio;
2016-03-31 13:52:22 +00:00
extern crate gdk;
2016-05-05 07:23:04 +00:00
extern crate gdk_sys;
2016-03-28 14:14:10 +00:00
extern crate glib;
2016-03-16 14:39:53 +00:00
extern crate cairo;
2017-03-05 22:28:07 +00:00
extern crate pango;
extern crate pangocairo;
2016-03-19 10:27:39 +00:00
extern crate neovim_lib;
2016-04-02 20:00:18 +00:00
extern crate phf;
2016-03-16 14:39:53 +00:00
2016-03-19 10:27:39 +00:00
mod nvim;
2016-03-19 08:47:23 +00:00
mod ui_model;
2016-03-16 15:25:25 +00:00
mod ui;
2016-04-04 10:14:57 +00:00
mod input;
2017-03-13 15:03:32 +00:00
mod settings;
2016-03-16 14:39:53 +00:00
2016-05-05 14:27:45 +00:00
use std::thread;
2017-03-06 13:58:10 +00:00
use std::env;
use gio::ApplicationExt;
2016-05-05 14:27:45 +00:00
const BIN_PATH_ARG: &'static str = "--nvim-bin-path";
2016-03-16 15:25:25 +00:00
fn main() {
2017-03-06 13:58:10 +00:00
let app = gtk::Application::new(Some("org.gtk.neovim-gtk"), gio::ApplicationFlags::empty()).expect("Failed to initialize GTK application");
app.connect_activate(activate);
let args: Vec<String> = env::args().collect();
let argv: Vec<&str> = args.iter().filter(|a| !a.starts_with(BIN_PATH_ARG)).map(String::as_str).collect();
2017-03-06 13:58:10 +00:00
app.run(argv.len() as i32, &argv);
}
fn activate(app: &gtk::Application) {
2016-03-31 10:09:34 +00:00
ui::UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
2017-03-06 13:58:10 +00:00
ui.init(app);
let path = nvim_bin_path();
nvim::initialize(&mut *ui, path.as_ref()).expect("Can't start nvim instance");
2016-05-05 14:27:45 +00:00
guard_dispatch_thread(&mut *ui);
2016-03-31 10:09:34 +00:00
});
2016-03-16 14:39:53 +00:00
}
fn nvim_bin_path() -> Option<String> {
std::env::args()
.skip_while(|a| !a.starts_with(BIN_PATH_ARG))
.map(|p| p.split('=').nth(1).map(str::to_owned))
.nth(0)
.unwrap_or(None)
}
2016-05-05 14:27:45 +00:00
fn guard_dispatch_thread(ui: &mut ui::Ui) {
let guard = ui.nvim().session.take_dispatch_guard();
thread::spawn(move || {
guard.join().expect("Can't join dispatch thread");
glib::idle_add(move || {
2017-03-06 13:58:10 +00:00
ui::UI.with(|ui_cell| {
ui_cell.borrow().destroy();
});
2016-05-05 14:27:45 +00:00
glib::Continue(false)
});
});
}