2017-10-29 20:45:05 +00:00
|
|
|
use gtk;
|
|
|
|
use gtk::prelude::*;
|
|
|
|
|
|
|
|
use super::store;
|
|
|
|
|
|
|
|
pub struct Builder<'a> {
|
2017-10-30 15:25:23 +00:00
|
|
|
title: &'a str,
|
2017-10-29 20:45:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 15:25:23 +00:00
|
|
|
impl<'a> Builder<'a> {
|
2017-10-29 20:45:05 +00:00
|
|
|
pub fn new(title: &'a str) -> Self {
|
|
|
|
Builder { title }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn show<F: IsA<gtk::Window>>(&self, parent: &F) -> Option<store::PlugInfo> {
|
|
|
|
let dlg = gtk::Dialog::new_with_buttons(
|
|
|
|
Some(self.title),
|
|
|
|
Some(parent),
|
|
|
|
gtk::DIALOG_USE_HEADER_BAR | gtk::DIALOG_DESTROY_WITH_PARENT,
|
2017-10-30 15:25:23 +00:00
|
|
|
&[
|
|
|
|
("Cancel", gtk::ResponseType::Cancel.into()),
|
|
|
|
("Ok", gtk::ResponseType::Ok.into()),
|
|
|
|
],
|
2017-10-29 20:45:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let content = dlg.get_content_area();
|
2017-11-05 17:52:29 +00:00
|
|
|
let border = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
|
|
|
border.set_border_width(12);
|
|
|
|
|
2017-10-30 15:25:23 +00:00
|
|
|
let list = gtk::ListBox::new();
|
|
|
|
list.set_selection_mode(gtk::SelectionMode::None);
|
2017-10-29 20:45:05 +00:00
|
|
|
|
2017-11-05 17:52:29 +00:00
|
|
|
let path = gtk::Box::new(gtk::Orientation::Horizontal, 5);
|
|
|
|
path.set_border_width(5);
|
2017-11-04 20:04:03 +00:00
|
|
|
let path_lbl = gtk::Label::new("Repo");
|
|
|
|
let path_e = gtk::Entry::new();
|
2017-11-05 17:52:29 +00:00
|
|
|
path_e.set_placeholder_text("user_name/repo_name");
|
2017-10-29 20:45:05 +00:00
|
|
|
|
2017-11-04 20:04:03 +00:00
|
|
|
path.pack_start(&path_lbl, true, true, 0);
|
|
|
|
path.pack_end(&path_e, false, true, 0);
|
2017-10-30 15:25:23 +00:00
|
|
|
|
|
|
|
list.add(&path);
|
|
|
|
|
|
|
|
|
2017-11-05 17:52:29 +00:00
|
|
|
let name = gtk::Box::new(gtk::Orientation::Horizontal, 5);
|
|
|
|
name.set_border_width(5);
|
2017-11-04 20:04:03 +00:00
|
|
|
let name_lbl = gtk::Label::new("Name");
|
|
|
|
let name_e = gtk::Entry::new();
|
|
|
|
|
|
|
|
name.pack_start(&name_lbl, true, true, 0);
|
|
|
|
name.pack_end(&name_e, false, true, 0);
|
|
|
|
|
|
|
|
list.add(&name);
|
|
|
|
|
2017-11-05 17:52:29 +00:00
|
|
|
border.pack_start(&list, true, true, 0);
|
|
|
|
content.add(&border);
|
2017-10-29 20:45:05 +00:00
|
|
|
content.show_all();
|
|
|
|
|
2017-11-05 17:52:29 +00:00
|
|
|
path_e.connect_changed(clone!(name_e => move |p| {
|
|
|
|
if let Some(name) = p.get_text().and_then(|t| extract_name(&t)) {
|
|
|
|
name_e.set_text(&name);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2017-10-30 15:25:23 +00:00
|
|
|
let ok: i32 = gtk::ResponseType::Ok.into();
|
|
|
|
let res = if dlg.run() == ok {
|
2017-11-04 20:04:03 +00:00
|
|
|
path_e.get_text().map(|path| {
|
|
|
|
let name = name_e
|
|
|
|
.get_text()
|
|
|
|
.and_then(|name| if name.trim().is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(name)
|
|
|
|
})
|
2017-11-05 17:52:29 +00:00
|
|
|
.or_else(|| extract_name(&path))
|
2017-11-04 20:04:03 +00:00
|
|
|
.unwrap_or_else(|| path.clone());
|
|
|
|
|
|
|
|
store::PlugInfo::new(name.to_owned(), path.to_owned())
|
2017-10-30 15:25:23 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-10-29 20:45:05 +00:00
|
|
|
|
|
|
|
dlg.destroy();
|
|
|
|
|
2017-10-30 15:25:23 +00:00
|
|
|
res
|
2017-10-29 20:45:05 +00:00
|
|
|
}
|
2017-11-05 17:52:29 +00:00
|
|
|
}
|
2017-11-04 20:04:03 +00:00
|
|
|
|
2017-11-05 17:52:29 +00:00
|
|
|
fn extract_name(path: &str) -> Option<String> {
|
|
|
|
if let Some(idx) = path.rfind(|c| c == '/' || c == '\\') {
|
|
|
|
if idx < path.len() - 1 {
|
|
|
|
let path = path.trim_right_matches(".git");
|
|
|
|
Some(path[idx + 1..].to_owned())
|
2017-11-04 20:04:03 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2017-11-05 17:52:29 +00:00
|
|
|
} else {
|
|
|
|
None
|
2017-11-04 20:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_name() {
|
|
|
|
assert_eq!(
|
2017-11-05 10:06:12 +00:00
|
|
|
Some("plugin_name".to_owned()),
|
2017-11-04 20:04:03 +00:00
|
|
|
Builder::extract_name("http://github.com/somebody/plugin_name.git")
|
|
|
|
);
|
|
|
|
}
|
2017-10-29 20:45:05 +00:00
|
|
|
}
|