Change state management
This commit is contained in:
parent
ea85f78350
commit
2cefb06063
12
src/dirs.rs
12
src/dirs.rs
@ -4,13 +4,14 @@ use std::path::PathBuf;
|
|||||||
pub fn get_app_config_dir_create() -> Result<PathBuf, String> {
|
pub fn get_app_config_dir_create() -> Result<PathBuf, String> {
|
||||||
let config_dir = get_app_config_dir()?;
|
let config_dir = get_app_config_dir()?;
|
||||||
|
|
||||||
std::fs::create_dir_all(&config_dir)
|
std::fs::create_dir_all(&config_dir).map_err(
|
||||||
.map_err(|e| format!("{}", e))?;
|
|e| format!("{}", e),
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(config_dir)
|
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()?;
|
let mut config_dir = get_xdg_config_dir()?;
|
||||||
|
|
||||||
config_dir.push("nvim-gtk");
|
config_dir.push("nvim-gtk");
|
||||||
@ -23,8 +24,9 @@ fn get_xdg_config_dir() -> Result<PathBuf, String> {
|
|||||||
return Ok(PathBuf::from(config_path));
|
return Ok(PathBuf::from(config_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut home_dir = std::env::home_dir()
|
let mut home_dir = std::env::home_dir().ok_or(
|
||||||
.ok_or("Impossible to get your home dir!")?;
|
"Impossible to get your home dir!",
|
||||||
|
)?;
|
||||||
home_dir.push(".config");
|
home_dir.push(".config");
|
||||||
Ok(home_dir)
|
Ok(home_dir)
|
||||||
}
|
}
|
||||||
|
@ -6,29 +6,42 @@ use super::store::Store;
|
|||||||
use nvim::NeovimClient;
|
use nvim::NeovimClient;
|
||||||
|
|
||||||
pub struct Manager {
|
pub struct Manager {
|
||||||
pub vim_plug: vim_plug::Manager,
|
vim_plug: vim_plug::Manager,
|
||||||
|
pub plug_manage_state: PlugManageState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Manager {
|
impl Manager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Manager {
|
Manager {
|
||||||
vim_plug: vim_plug::Manager::new(),
|
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);
|
self.vim_plug.initialize(nvim);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_store(&self, vim_plug_state: &vim_plug::State) -> Store {
|
pub fn update_state(&mut self) {
|
||||||
match *vim_plug_state {
|
if self.vim_plug.is_loaded() {
|
||||||
vim_plug::State::AlreadyLoaded => {
|
if let PlugManageState::Unknown = self.plug_manage_state {
|
||||||
let store = Store::load_from_plug(&self.vim_plug);
|
self.plug_manage_state = PlugManageState::Configuration(Store::load_from_plug(&self.vim_plug));
|
||||||
store
|
|
||||||
}
|
|
||||||
vim_plug::State::Unknown => {
|
|
||||||
Store::load()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum PlugManageState {
|
||||||
|
NvimGtk(Store),
|
||||||
|
Configuration(Store),
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
@ -8,6 +8,10 @@ pub struct Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Store {
|
impl Store {
|
||||||
|
pub fn is_config_exists() -> bool {
|
||||||
|
Settings::is_file_exists()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load() -> Self {
|
pub fn load() -> Self {
|
||||||
Store { settings: Settings::load() }
|
Store { settings: Settings::load() }
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,25 @@
|
|||||||
use std::rc::Rc;
|
use std::sync::Arc;
|
||||||
use std::cell::RefCell;
|
|
||||||
|
use ui::UiMutex;
|
||||||
|
|
||||||
use gtk;
|
use gtk;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
|
|
||||||
use super::manager;
|
use super::manager;
|
||||||
use super::vim_plug;
|
|
||||||
use super::store::Store;
|
use super::store::Store;
|
||||||
|
|
||||||
pub struct Ui<'a> {
|
pub struct Ui<'a> {
|
||||||
manager: &'a manager::Manager,
|
manager: &'a Arc<UiMutex<manager::Manager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Ui<'a> {
|
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 }
|
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;
|
const OK_ID: i32 = 0;
|
||||||
|
|
||||||
let dlg = gtk::Dialog::new_with_buttons(
|
let dlg = gtk::Dialog::new_with_buttons(
|
||||||
@ -31,41 +33,52 @@ impl<'a> Ui<'a> {
|
|||||||
let content = dlg.get_content_area();
|
let content = dlg.get_content_area();
|
||||||
let tabs = gtk::Notebook::new();
|
let tabs = gtk::Notebook::new();
|
||||||
|
|
||||||
let vim_plug_state = self.get_state();
|
match self.manager.borrow_mut().plug_manage_state {
|
||||||
match vim_plug_state {
|
manager::PlugManageState::Unknown => {
|
||||||
vim_plug::State::AlreadyLoaded => {
|
|
||||||
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
|
let help = gtk::Box::new(gtk::Orientation::Vertical, 3);
|
||||||
let warn_lbl = gtk::Label::new(None);
|
let warn_lbl = gtk::Label::new(None);
|
||||||
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> <b>vim-plug</b> manager already loaded!\n\
|
warn_lbl.set_markup("<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!");
|
||||||
NeovimGtk plugins manager will be <b>disabled</b>.\n\
|
help.pack_start(&warn_lbl, true, false, 0);
|
||||||
To enable it please disable vim-plug in your configuration.\n\
|
|
||||||
NeovimGtk manages plugins use vim-plug as backend.\n\
|
let enable_btn = gtk::Button::new_with_label("Enable NeovimGtk plugin manager");
|
||||||
You can convert vim-plug configuration to NeovimGtk configuration using button below.\n\
|
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.");
|
List of current vim-plug plugins can be found in 'Plugins' tab.");
|
||||||
help.pack_start(&warn_lbl, true, false, 0);
|
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 =
|
let copy_btn =
|
||||||
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
|
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
|
||||||
help.pack_start(©_btn, false, false, 0);
|
help.pack_start(©_btn, false, false, 0);
|
||||||
|
|
||||||
let get_plugins_lbl = gtk::Label::new("Help");
|
let get_plugins_lbl = gtk::Label::new("Help");
|
||||||
tabs.append_page(&help, Some(&get_plugins_lbl));
|
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 = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||||
let get_plugins_lbl = gtk::Label::new("Get Plugins");
|
let get_plugins_lbl = gtk::Label::new("Get Plugins");
|
||||||
tabs.append_page(&get_plugins, Some(&get_plugins_lbl));
|
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);
|
tabs.set_tab_pos(gtk::PositionType::Left);
|
||||||
content.pack_start(&tabs, true, true, 0);
|
content.pack_start(&tabs, true, true, 0);
|
||||||
@ -82,12 +95,21 @@ impl<'a> Ui<'a> {
|
|||||||
dlg.destroy();
|
dlg.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_plugin_list(panel: >k::Box, store: &Rc<RefCell<Store>>) {
|
fn add_plugin_list_tab(&self, tabs: >k::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: >k::Box, store: &Store) {
|
||||||
let scroll = gtk::ScrolledWindow::new(None, None);
|
let scroll = gtk::ScrolledWindow::new(None, None);
|
||||||
let plugs_panel = gtk::ListBox::new();
|
let plugs_panel = gtk::ListBox::new();
|
||||||
plugs_panel.set_selection_mode(gtk::SelectionMode::None);
|
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 = gtk::ListBoxRow::new();
|
||||||
let row_container = gtk::Box::new(gtk::Orientation::Vertical, 5);
|
let row_container = gtk::Box::new(gtk::Orientation::Vertical, 5);
|
||||||
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);
|
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);
|
||||||
@ -106,11 +128,21 @@ impl<'a> Ui<'a> {
|
|||||||
let row_ref = row.clone();
|
let row_ref = row.clone();
|
||||||
remove_btn.connect_clicked(move |_| {
|
remove_btn.connect_clicked(move |_| {
|
||||||
// store_ref.borrow_mut().remove(idx);
|
// 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(&hbox, true, true, 0);
|
||||||
row_container.pack_start(>k::Separator::new(gtk::Orientation::Horizontal), true, true, 0);
|
row_container.pack_start(
|
||||||
|
>k::Separator::new(gtk::Orientation::Horizontal),
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
0,
|
||||||
|
);
|
||||||
vbox.pack_start(&name_lbl, true, true, 0);
|
vbox.pack_start(&name_lbl, true, true, 0);
|
||||||
vbox.pack_start(&url_lbl, true, true, 0);
|
vbox.pack_start(&url_lbl, true, true, 0);
|
||||||
hbox.pack_start(&vbox, true, true, 0);
|
hbox.pack_start(&vbox, true, true, 0);
|
||||||
@ -123,12 +155,12 @@ impl<'a> Ui<'a> {
|
|||||||
scroll.add(&plugs_panel);
|
scroll.add(&plugs_panel);
|
||||||
panel.pack_start(&scroll, true, true, 0);
|
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 =
|
let copy_btn =
|
||||||
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
|
gtk::Button::new_with_label("Copy plugins from current vim-plug configuration");
|
||||||
panel.add(©_btn);
|
panel.add(©_btn);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_state(&self) -> vim_plug::State {
|
|
||||||
self.manager.vim_plug.get_state()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
if let Some(mut nvim) = self.nvim() {
|
||||||
let loaded_plug = nvim.eval("exists('g:loaded_plug')");
|
let loaded_plug = nvim.eval("exists('g:loaded_plug')");
|
||||||
loaded_plug
|
loaded_plug
|
||||||
.ok_and_report(&mut *nvim)
|
.ok_and_report(&mut *nvim)
|
||||||
.and_then(|loaded_plug| loaded_plug.as_i64())
|
.and_then(|loaded_plug| loaded_plug.as_i64())
|
||||||
.map_or(State::Unknown, |loaded_plug| if loaded_plug > 0 {
|
.map_or(false, |loaded_plug| if loaded_plug > 0 {
|
||||||
State::AlreadyLoaded
|
true
|
||||||
} else {
|
} else {
|
||||||
State::Unknown
|
false
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
State::Unknown
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +101,3 @@ impl VimPlugInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum State {
|
|
||||||
Unknown,
|
|
||||||
AlreadyLoaded,
|
|
||||||
}
|
|
||||||
|
@ -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) {
|
fn save(&self) {
|
||||||
match save_err(self) {
|
match save_err(self) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(e) => println!("{}", e),
|
Err(e) => error!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
src/ui.rs
11
src/ui.rs
@ -132,8 +132,13 @@ impl Ui {
|
|||||||
let comps_ref = self.comps.clone();
|
let comps_ref = self.comps.clone();
|
||||||
let plug_manager_ref = self.plug_manager.clone();
|
let plug_manager_ref = self.plug_manager.clone();
|
||||||
plug_btn.connect_clicked(move |_| {
|
plug_btn.connect_clicked(move |_| {
|
||||||
plug_manager::Ui::new(&mut *plug_manager_ref.borrow_mut())
|
plug_manager::Ui::new(&plug_manager_ref).show(
|
||||||
.show(comps_ref.borrow().window.as_ref().unwrap())
|
comps_ref
|
||||||
|
.borrow()
|
||||||
|
.window
|
||||||
|
.as_ref()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
header_bar.pack_start(&plug_btn);
|
header_bar.pack_start(&plug_btn);
|
||||||
|
|
||||||
@ -168,7 +173,7 @@ impl Ui {
|
|||||||
let state_ref = self.shell.borrow().state.clone();
|
let state_ref = self.shell.borrow().state.clone();
|
||||||
let plug_manager_ref = self.plug_manager.clone();
|
let plug_manager_ref = self.plug_manager.clone();
|
||||||
shell.set_nvim_started_cb(Some(move || {
|
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(),
|
state_ref.borrow().nvim_clone(),
|
||||||
);
|
);
|
||||||
}));
|
}));
|
||||||
|
Loading…
Reference in New Issue
Block a user