Change state management

This commit is contained in:
daa84 2017-10-24 18:03:34 +03:00
parent ea85f78350
commit 2cefb06063
7 changed files with 120 additions and 59 deletions

View File

@ -4,13 +4,14 @@ use std::path::PathBuf;
pub fn get_app_config_dir_create() -> Result<PathBuf, String> {
let config_dir = get_app_config_dir()?;
std::fs::create_dir_all(&config_dir)
.map_err(|e| format!("{}", e))?;
std::fs::create_dir_all(&config_dir).map_err(
|e| format!("{}", e),
)?;
Ok(config_dir)
}
fn get_app_config_dir() -> Result<PathBuf, String> {
pub fn get_app_config_dir() -> Result<PathBuf, String> {
let mut config_dir = get_xdg_config_dir()?;
config_dir.push("nvim-gtk");
@ -23,8 +24,9 @@ fn get_xdg_config_dir() -> Result<PathBuf, String> {
return Ok(PathBuf::from(config_path));
}
let mut home_dir = std::env::home_dir()
.ok_or("Impossible to get your home dir!")?;
let mut home_dir = std::env::home_dir().ok_or(
"Impossible to get your home dir!",
)?;
home_dir.push(".config");
Ok(home_dir)
}

View File

@ -6,29 +6,42 @@ use super::store::Store;
use nvim::NeovimClient;
pub struct Manager {
pub vim_plug: vim_plug::Manager,
vim_plug: vim_plug::Manager,
pub plug_manage_state: PlugManageState,
}
impl Manager {
pub fn new() -> Self {
Manager {
vim_plug: vim_plug::Manager::new(),
plug_manage_state: PlugManageState::Unknown,
}
}
pub fn initialize(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
pub fn generate_plug_config(&mut self) -> Option<String> {
if Store::is_config_exists() {
self.plug_manage_state = PlugManageState::NvimGtk(Store::load());
Some("TODO".to_owned())
} else {
None
}
}
pub fn init_nvim_client(&mut self, nvim: Rc<RefCell<NeovimClient>>) {
self.vim_plug.initialize(nvim);
}
pub fn load_store(&self, vim_plug_state: &vim_plug::State) -> Store {
match *vim_plug_state {
vim_plug::State::AlreadyLoaded => {
let store = Store::load_from_plug(&self.vim_plug);
store
}
vim_plug::State::Unknown => {
Store::load()
pub fn update_state(&mut self) {
if self.vim_plug.is_loaded() {
if let PlugManageState::Unknown = self.plug_manage_state {
self.plug_manage_state = PlugManageState::Configuration(Store::load_from_plug(&self.vim_plug));
}
}
}
}
pub enum PlugManageState {
NvimGtk(Store),
Configuration(Store),
Unknown,
}

View File

@ -8,6 +8,10 @@ pub struct Store {
}
impl Store {
pub fn is_config_exists() -> bool {
Settings::is_file_exists()
}
pub fn load() -> Self {
Store { settings: Settings::load() }
}

View File

@ -1,23 +1,25 @@
use std::rc::Rc;
use std::cell::RefCell;
use std::sync::Arc;
use ui::UiMutex;
use gtk;
use gtk::prelude::*;
use super::manager;
use super::vim_plug;
use super::store::Store;
pub struct Ui<'a> {
manager: &'a manager::Manager,
manager: &'a Arc<UiMutex<manager::Manager>>,
}
impl<'a> Ui<'a> {
pub fn new(manager: &'a manager::Manager) -> Ui<'a> {
pub fn new(manager: &'a Arc<UiMutex<manager::Manager>>) -> Ui<'a> {
manager.borrow_mut().update_state();
Ui { manager }
}
pub fn show<T: IsA<gtk::Window>>(&self, parent: &T) {
pub fn show<T: IsA<gtk::Window>>(&mut self, parent: &T) {
const OK_ID: i32 = 0;
let dlg = gtk::Dialog::new_with_buttons(
@ -31,41 +33,52 @@ impl<'a> Ui<'a> {
let content = dlg.get_content_area();
let tabs = gtk::Notebook::new();
let vim_plug_state = self.get_state();
match vim_plug_state {
vim_plug::State::AlreadyLoaded => {
match self.manager.borrow_mut().plug_manage_state {
manager::PlugManageState::Unknown => {
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
let warn_lbl = gtk::Label::new(None);
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> <b>vim-plug</b> manager already loaded!\n\
NeovimGtk plugins manager will be <b>disabled</b>.\n\
To enable it please disable vim-plug in your configuration.\n\
NeovimGtk manages plugins use vim-plug as backend.\n\
You can convert vim-plug configuration to NeovimGtk configuration using button below.\n\
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!");
help.pack_start(&warn_lbl, true, false, 0);
let enable_btn = gtk::Button::new_with_label("Enable NeovimGtk plugin manager");
help.pack_start(&enable_btn, false, false, 0);
let get_plugins_lbl = gtk::Label::new("Help");
tabs.append_page(&help, Some(&get_plugins_lbl));
}
manager::PlugManageState::Configuration(ref store) => {
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
let warn_lbl = gtk::Label::new(None);
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!\n\
NeovimGtk manages plugins use vim-plug as backend, so enable it disables vim-plug configuration.\n\
You can convert current vim-plug configuration to NeovimGtk configuration using button below.\n\
List of current vim-plug plugins can be found in 'Plugins' tab.");
help.pack_start(&warn_lbl, true, false, 0);
let enable_btn = gtk::Button::new_with_label(
"Enable NeovimGtk plugin manager, empty configuration",
);
help.pack_start(&enable_btn, false, false, 0);
let copy_btn =
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
help.pack_start(&copy_btn, false, false, 0);
let get_plugins_lbl = gtk::Label::new("Help");
tabs.append_page(&help, Some(&get_plugins_lbl));
self.add_plugin_list_tab(&tabs, store);
}
vim_plug::State::Unknown => {
manager::PlugManageState::NvimGtk(ref store) => {
let get_plugins = gtk::Box::new(gtk::Orientation::Vertical, 0);
let get_plugins_lbl = gtk::Label::new("Get Plugins");
tabs.append_page(&get_plugins, Some(&get_plugins_lbl));
self.add_plugin_list_tab(&tabs, store);
}
}
let plugins = gtk::Box::new(gtk::Orientation::Vertical, 3);
let store = self.manager.load_store(&vim_plug_state);
let store = Rc::new(RefCell::new(store));
Ui::fill_plugin_list(&plugins, &store);
let plugins_lbl = gtk::Label::new("Plugins");
tabs.append_page(&plugins, Some(&plugins_lbl));
tabs.set_tab_pos(gtk::PositionType::Left);
content.pack_start(&tabs, true, true, 0);
@ -82,12 +95,21 @@ impl<'a> Ui<'a> {
dlg.destroy();
}
fn fill_plugin_list(panel: &gtk::Box, store: &Rc<RefCell<Store>>) {
fn add_plugin_list_tab(&self, tabs: &gtk::Notebook, store: &Store) {
// Plugins
let plugins = gtk::Box::new(gtk::Orientation::Vertical, 3);
self.fill_plugin_list(&plugins, store);
let plugins_lbl = gtk::Label::new("Plugins");
tabs.append_page(&plugins, Some(&plugins_lbl));
}
fn fill_plugin_list(&self, panel: &gtk::Box, store: &Store) {
let scroll = gtk::ScrolledWindow::new(None, None);
let plugs_panel = gtk::ListBox::new();
plugs_panel.set_selection_mode(gtk::SelectionMode::None);
for (idx, plug_info) in store.borrow().get_plugs().iter().enumerate() {
for (idx, plug_info) in store.get_plugs().iter().enumerate() {
let row = gtk::ListBoxRow::new();
let row_container = gtk::Box::new(gtk::Orientation::Vertical, 5);
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);
@ -106,11 +128,21 @@ impl<'a> Ui<'a> {
let row_ref = row.clone();
remove_btn.connect_clicked(move |_| {
// store_ref.borrow_mut().remove(idx);
panel_ref.remove(&row_ref);
row_ref.remove(row_ref.get_child().as_ref().unwrap());
let undo_btn = gtk::Button::new_with_label("Undo");
let row_container = gtk::Box::new(gtk::Orientation::Horizontal, 5);
row_container.pack_end(&undo_btn, false, true, 0);
row_ref.add(&row_container);
row_container.show_all();
});
row_container.pack_start(&hbox, true, true, 0);
row_container.pack_start(&gtk::Separator::new(gtk::Orientation::Horizontal), true, true, 0);
row_container.pack_start(
&gtk::Separator::new(gtk::Orientation::Horizontal),
true,
true,
0,
);
vbox.pack_start(&name_lbl, true, true, 0);
vbox.pack_start(&url_lbl, true, true, 0);
hbox.pack_start(&vbox, true, true, 0);
@ -123,12 +155,12 @@ impl<'a> Ui<'a> {
scroll.add(&plugs_panel);
panel.pack_start(&scroll, true, true, 0);
let enable_btn =
gtk::Button::new_with_label("Enable NeovimGtk plugin manager, empty configuration");
panel.add(&enable_btn);
let copy_btn =
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
panel.add(&copy_btn);
}
fn get_state(&self) -> vim_plug::State {
self.manager.vim_plug.get_state()
}
}

View File

@ -72,19 +72,19 @@ impl Manager {
}
}
pub fn get_state(&self) -> State {
pub fn is_loaded(&self) -> bool {
if let Some(mut nvim) = self.nvim() {
let loaded_plug = nvim.eval("exists('g:loaded_plug')");
loaded_plug
.ok_and_report(&mut *nvim)
.and_then(|loaded_plug| loaded_plug.as_i64())
.map_or(State::Unknown, |loaded_plug| if loaded_plug > 0 {
State::AlreadyLoaded
.map_or(false, |loaded_plug| if loaded_plug > 0 {
true
} else {
State::Unknown
false
})
} else {
State::Unknown
false
}
}
}
@ -101,7 +101,3 @@ impl VimPlugInfo {
}
}
pub enum State {
Unknown,
AlreadyLoaded,
}

View File

@ -128,10 +128,19 @@ pub trait SettingsLoader: Sized + serde::Serialize {
}
}
fn is_file_exists() -> bool {
if let Ok(mut toml_path) = dirs::get_app_config_dir() {
toml_path.push(Self::SETTINGS_FILE);
toml_path.is_file()
} else {
false
}
}
fn save(&self) {
match save_err(self) {
Ok(()) => (),
Err(e) => println!("{}", e),
Err(e) => error!("{}", e),
}
}
}

View File

@ -132,8 +132,13 @@ impl Ui {
let comps_ref = self.comps.clone();
let plug_manager_ref = self.plug_manager.clone();
plug_btn.connect_clicked(move |_| {
plug_manager::Ui::new(&mut *plug_manager_ref.borrow_mut())
.show(comps_ref.borrow().window.as_ref().unwrap())
plug_manager::Ui::new(&plug_manager_ref).show(
comps_ref
.borrow()
.window
.as_ref()
.unwrap(),
)
});
header_bar.pack_start(&plug_btn);
@ -168,7 +173,7 @@ impl Ui {
let state_ref = self.shell.borrow().state.clone();
let plug_manager_ref = self.plug_manager.clone();
shell.set_nvim_started_cb(Some(move || {
plug_manager_ref.borrow_mut().initialize(
plug_manager_ref.borrow_mut().init_nvim_client(
state_ref.borrow().nvim_clone(),
);
}));