Plugs reordering, load config on window start

This commit is contained in:
daa84 2017-11-02 17:31:46 +03:00
parent edebb8b673
commit afa282833a
3 changed files with 106 additions and 21 deletions

View File

@ -40,7 +40,32 @@ impl Manager {
self.vim_plug.initialize(nvim); self.vim_plug.initialize(nvim);
} }
pub fn update_state(&mut self) { pub fn reload_store(&mut self) {
match self.plug_manage_state {
PlugManageState::Unknown => {
if self.vim_plug.is_loaded() {
self.store = Store::load_from_plug(&self.vim_plug);
self.plug_manage_state = PlugManageState::VimPlug;
} else {
self.store = Store::empty();
}
}
PlugManageState::NvimGtk => {
if Store::is_config_exists() {
self.store = Store::load();
} else {
self.store = Store::empty();
}
}
PlugManageState::VimPlug => {
if Store::is_config_exists() {
self.store = Store::load();
self.plug_manage_state = PlugManageState::NvimGtk;
} else {
self.store = Store::empty();
}
}
}
if let PlugManageState::Unknown = self.plug_manage_state { if let PlugManageState::Unknown = self.plug_manage_state {
if self.vim_plug.is_loaded() { if self.vim_plug.is_loaded() {
self.store = Store::load_from_plug(&self.vim_plug); self.store = Store::load_from_plug(&self.vim_plug);
@ -60,6 +85,10 @@ impl Manager {
pub fn add_plug(&mut self, plug: PlugInfo) -> bool { pub fn add_plug(&mut self, plug: PlugInfo) -> bool {
self.store.add_plug(plug) self.store.add_plug(plug)
} }
pub fn move_item(&mut self, idx: usize, offset: i32) {
self.store.move_item(idx, offset);
}
} }
pub enum PlugManageState { pub enum PlugManageState {

View File

@ -78,6 +78,11 @@ impl Store {
pub fn plugs_count(&self) -> usize { pub fn plugs_count(&self) -> usize {
self.settings.plugs.len() self.settings.plugs.len()
} }
pub fn move_item(&mut self, idx: usize, offset: i32) {
let plug = self.settings.plugs.remove(idx);
self.settings.plugs.insert((idx as i32 + offset) as usize, plug);
}
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]

View File

@ -7,6 +7,7 @@ use ui::UiMutex;
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use gtk_sys;
use super::manager; use super::manager;
use super::store::{Store, PlugInfo}; use super::store::{Store, PlugInfo};
@ -19,7 +20,7 @@ pub struct Ui<'a> {
impl<'a> Ui<'a> { impl<'a> Ui<'a> {
pub fn new(manager: &'a Arc<UiMutex<manager::Manager>>) -> Ui<'a> { pub fn new(manager: &'a Arc<UiMutex<manager::Manager>>) -> Ui<'a> {
manager.borrow_mut().update_state(); manager.borrow_mut().reload_store();
Ui { manager } Ui { manager }
} }
@ -29,7 +30,10 @@ impl<'a> Ui<'a> {
Some("Plug"), Some("Plug"),
Some(parent), Some(parent),
gtk::DIALOG_DESTROY_WITH_PARENT, gtk::DIALOG_DESTROY_WITH_PARENT,
&[("Ok", gtk::ResponseType::Ok.into())], &[
("Cancel", gtk::ResponseType::Cancel.into()),
("Ok", gtk::ResponseType::Ok.into()),
],
); );
dlg.set_default_size(800, 600); dlg.set_default_size(800, 600);
@ -68,27 +72,25 @@ impl<'a> Ui<'a> {
add_vimawesome_tab(&pages, &self.manager, &plugs_panel); add_vimawesome_tab(&pages, &self.manager, &plugs_panel);
match self.manager.borrow().plug_manage_state {
manager::PlugManageState::Unknown => {
add_help_tab(
&pages,
"<span foreground=\"red\">Note:</span> NeovimGtk plugin manager <b>disabled</b>!",
);
}
manager::PlugManageState::VimPlug => {
add_help_tab(
&pages,
"<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\
Current configuration taken from your vim-plug",
);
}
manager::PlugManageState::NvimGtk => {}
}
let plugins_lbl = gtk::Label::new("Plugins"); let plugins_lbl = gtk::Label::new("Plugins");
pages.add_page(&plugins_lbl, &plugins, "plugins"); pages.add_page(&plugins_lbl, &plugins, "plugins");
add_help_tab(
&pages,
&format!("NeovimGtk plugin manager is a GUI for vim-plug.\n\
It can load plugins from vim-plug configuration if vim-plug sarted and self settings is empty.\n\
When enabled it generate and load vim-plug as simple vim file at startup before init.vim is processed.\n\
So after enabling this manager <b>you must disable vim-plug</b> configuration in init.vim.\n\
This manager currently only manage vim-plug configuration and do not any actions on plugin management.\n\
So you must call all vim-plug (PlugInstall, PlugUpdate, PlugClean) commands manually.\n\
Current configuration source is <b>{}</b>", match self.manager.borrow().plug_manage_state {
manager::PlugManageState::NvimGtk => "config file",
manager::PlugManageState::VimPlug => "loaded from vim-plug",
manager::PlugManageState::Unknown => "Unknown",
}),
);
let manager_ref = self.manager.clone(); let manager_ref = self.manager.clone();
enable_swc.connect_state_set(move |_, state| { enable_swc.connect_state_set(move |_, state| {
manager_ref.borrow_mut().store.set_enabled(state); manager_ref.borrow_mut().store.set_enabled(state);
@ -126,12 +128,61 @@ 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, 5);
panel.pack_start(
&create_up_down_btns(&plugs_panel, &self.manager),
false,
true,
0,
);
plugs_panel plugs_panel
} }
} }
fn create_up_down_btns(
plugs_panel: &gtk::ListBox,
manager: &Arc<UiMutex<manager::Manager>>,
) -> gtk::Box {
let buttons_panel = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let up_btn = gtk::Button::new_from_icon_name("go-up", gtk_sys::GTK_ICON_SIZE_BUTTON as i32);
let down_btn = gtk::Button::new_from_icon_name("go-down", gtk_sys::GTK_ICON_SIZE_BUTTON as i32);
up_btn.connect_clicked(clone!(plugs_panel, manager => move |_| {
if let Some(row) = plugs_panel.get_selected_row() {
let idx = row.get_index();
if idx > 0 {
plugs_panel.unselect_row(&row);
plugs_panel.remove(&row);
plugs_panel.insert(&row, idx - 1);
plugs_panel.select_row(&row);
manager.borrow_mut().move_item(idx as usize, -1);
}
}
}));
down_btn.connect_clicked(clone!(plugs_panel, manager => move |_| {
if let Some(row) = plugs_panel.get_selected_row() {
let idx = row.get_index();
let mut manager = manager.borrow_mut();
if idx >= 0 && idx < manager.store.plugs_count() as i32 {
plugs_panel.unselect_row(&row);
plugs_panel.remove(&row);
plugs_panel.insert(&row, idx + 1);
plugs_panel.select_row(&row);
manager.move_item(idx as usize, 1);
}
}
}));
buttons_panel.pack_start(&up_btn, false, true, 0);
buttons_panel.pack_start(&down_btn, false, true, 0);
buttons_panel
}
fn populate_get_plugins( fn populate_get_plugins(
query: Option<String>, query: Option<String>,
get_plugins: &gtk::Box, get_plugins: &gtk::Box,