add file chooser widget

This commit is contained in:
yggverse 2024-11-21 13:17:35 +02:00
parent 83d474a192
commit d0f61aab5e
2 changed files with 54 additions and 0 deletions

View File

@ -1,6 +1,8 @@
mod file;
pub mod list;
mod name;
use file::File;
use list::{item::value::Value, List};
use name::Name;
@ -10,6 +12,7 @@ use std::rc::Rc;
pub struct Form {
// pub action: Rc<Action>,
pub file: Rc<File>,
pub list: Rc<List>,
pub name: Rc<Name>,
pub gobject: Box,
@ -23,15 +26,18 @@ impl Form {
// Init components
let list = Rc::new(List::new());
let name = Rc::new(Name::new(action.clone()));
let file = Rc::new(File::new(action.clone()));
// Init main container
let gobject = Box::builder().orientation(Orientation::Vertical).build();
gobject.append(&list.gobject);
gobject.append(&name.gobject);
gobject.append(&file.gobject);
// Connect events
list.on_select({
let file = file.clone();
let name = name.clone();
let update = action.update.clone();
move |key| {
@ -41,6 +47,12 @@ impl Form {
_ => false,
});
// Change name entry visibility
file.show(match key {
Value::IMPORT_PEM => true,
_ => false,
});
// Update widget
update.activate();
}
@ -50,6 +62,7 @@ impl Form {
Self {
// action,
gobject,
file,
list,
name,
}

View File

@ -0,0 +1,41 @@
use super::Action;
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
const LABEL: &str = "Select file..";
const MARGIN: i32 = 8;
pub struct File {
pub gobject: Button,
}
impl File {
// Constructors
/// Create new `Self`
pub fn new(action: Rc<Action>) -> Self {
// Init `GObject`
let gobject = Button::builder().label(LABEL).margin_top(MARGIN).build();
// Init events
gobject.connect_clicked(move |_| todo!());
// Return activated `Self`
Self { gobject }
}
// Actions
/// Change visibility status
/// * grab focus on `is_visible`
pub fn show(&self, is_visible: bool) {
self.gobject.set_visible(is_visible);
if is_visible {
self.gobject.grab_focus();
}
}
}