diff --git a/src/main.rs b/src/main.rs index 29da5d6..1b089ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod shell; mod input; mod settings; mod cursor; +mod shell_dlg; use std::thread; use std::env; diff --git a/src/shell_dlg.rs b/src/shell_dlg.rs new file mode 100644 index 0000000..9504bf6 --- /dev/null +++ b/src/shell_dlg.rs @@ -0,0 +1,62 @@ +use ui::{SH, Ui}; +use neovim_lib::{NeovimApi, CallError, Value}; +use gtk; +use gtk_sys; +use gtk::{Dialog, DialogExt}; + +pub fn can_close_window(ui: &Ui) -> bool { + match get_changed_buffers() { + Ok(vec) => { + if !vec.is_empty() { + show_not_saved_dlg(ui, &vec) + } else { + true + } + }, + Err(ref err) => { + println!("Error getting info from nvim: {}", err); + true + } + } +} + +fn show_not_saved_dlg(ui: &Ui, changed_bufs: &Vec) -> bool { + let flags = gtk::DIALOG_MODAL | gtk::DIALOG_DESTROY_WITH_PARENT; + let dlg = Dialog::new_with_buttons(Some("Question"), ui.window.as_ref(), flags, &[ + ("_OK", gtk_sys::GTK_RESPONSE_ACCEPT as i32), + ("_Cancel", + gtk_sys::GTK_RESPONSE_REJECT as i32) + ]); + + dlg.run(); + + true +} + +fn get_changed_buffers() -> Result, CallError> { + SHELL!(shell = { + let mut nvim = shell.nvim(); + let buffers = nvim.get_buffers()?; + + Ok(buffers.iter() + .map(|buf| { + (match buf.get_option(&mut nvim, "modified") { + Ok(Value::Boolean(val)) => val, + _ => { + println!("Value must be boolean"); + false + }, + }, + match buf.get_name(&mut nvim) { + Ok(name) => name, + Err(ref err) => { + println!("Something going wrong while getting buffer name: {}", err); + "".to_owned() + }, + }) + }) + .filter(|e| e.0) + .map(|e| e.1) + .collect()) + }) +} diff --git a/src/ui.rs b/src/ui.rs index 93e476d..dc90fb3 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -13,6 +13,7 @@ use neovim_lib::NeovimApi; use settings; use shell::{Shell, NvimMode}; +use shell_dlg; use nvim::ErrorReport; macro_rules! ui_thread_var { @@ -169,16 +170,17 @@ fn edit_save_all() { }); } -fn quit() { - UI.with(|ui_cell| { - let mut ui = ui_cell.borrow_mut(); - ui.destroy(); - }); -} fn gtk_delete(_: &ApplicationWindow, _: &Event) -> Inhibit { - quit(); - Inhibit(false) + Inhibit(UI.with(|ui_cell| { + let mut ui = ui_cell.borrow_mut(); + if shell_dlg::can_close_window(&ui) { + ui.destroy(); + false + } else { + true + } + })) }