implement widget update action

This commit is contained in:
yggverse 2024-12-07 08:15:29 +02:00
parent 4b5e260792
commit 073e035bb2
3 changed files with 37 additions and 42 deletions

View file

@ -68,6 +68,9 @@ impl Widget {
let form = form.clone(); let form = form.clone();
let alert_dialog = alert_dialog.clone(); let alert_dialog = alert_dialog.clone();
move || { move || {
// Update child components
form.update();
// Deactivate apply button if the form values could not be processed // Deactivate apply button if the form values could not be processed
alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable()); alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable());
} }

View file

@ -19,12 +19,12 @@ use std::rc::Rc;
pub struct Form { pub struct Form {
// pub widget_action: Rc<Action>, // pub widget_action: Rc<Action>,
// pub drop: Rc<Drop>, pub drop: Rc<Drop>,
// pub exit: Rc<Exit>, pub exit: Rc<Exit>,
pub file: Rc<File>, pub file: Rc<File>,
pub list: Rc<List>, pub list: Rc<List>,
pub name: Rc<Name>, pub name: Rc<Name>,
// pub save: Rc<Save>, pub save: Rc<Save>,
pub g_box: Box, pub g_box: Box,
} }
@ -34,7 +34,7 @@ impl Form {
/// Create new `Self` /// Create new `Self`
pub fn new(profile: Rc<Profile>, widget_action: Rc<Action>, auth_url: &str) -> Self { pub fn new(profile: Rc<Profile>, widget_action: Rc<Action>, auth_url: &str) -> Self {
// Init components // Init components
let list = Rc::new(List::new(profile.clone(), auth_url)); let list = Rc::new(List::new(widget_action.clone(), profile.clone(), auth_url));
let file = Rc::new(File::new(widget_action.clone())); let file = Rc::new(File::new(widget_action.clone()));
let name = Rc::new(Name::new(widget_action.clone())); let name = Rc::new(Name::new(widget_action.clone()));
let save = Rc::new(Save::new(profile.clone(), list.clone())); let save = Rc::new(Save::new(profile.clone(), list.clone()));
@ -51,49 +51,15 @@ impl Form {
g_box.append(&drop.button); g_box.append(&drop.button);
g_box.append(&save.button); g_box.append(&save.button);
// Connect events
list.dropdown.connect_selected_notify({
let list = list.clone();
let name = name.clone();
let file = file.clone();
// let drop = drop.clone();
// let exit = exit.clone();
// let save = save.clone();
move |_| {
// Get selected item
let item = list.selected();
// Update name entry visibility
name.set_visible(matches!(item.value_enum(), Value::GeneratePem));
// Update file choose button visibility
file.set_visible(matches!(item.value_enum(), Value::ImportPem));
// Update ID-related components
match item.value_enum() {
Value::ProfileIdentityGeminiId(_) => {
drop.set_visible(true);
exit.set_visible(true);
save.set_visible(true);
}
_ => {
drop.set_visible(false);
exit.set_visible(false);
save.set_visible(false);
}
}
}
});
// Return activated `Self` // Return activated `Self`
Self { Self {
// widget_action, // widget_action,
// drop, drop,
// exit, exit,
file, file,
list, list,
name, name,
// save, save,
g_box, g_box,
} }
} }
@ -109,4 +75,26 @@ impl Form {
_ => true, _ => true,
} }
} }
pub fn update(&self) {
// Get selected item value
let value = self.list.selected().value_enum();
// Toggle visibility for children components
self.name.set_visible(matches!(value, Value::GeneratePem));
self.file.set_visible(matches!(value, Value::ImportPem));
match value {
Value::ProfileIdentityGeminiId(_) => {
self.drop.set_visible(true);
self.exit.set_visible(true);
self.save.set_visible(true);
}
_ => {
self.drop.set_visible(false);
self.exit.set_visible(false);
self.save.set_visible(false);
}
}
}
} }

View file

@ -3,6 +3,7 @@ use std::rc::Rc;
use item::Item; use item::Item;
use super::Action;
use crate::profile::Profile; use crate::profile::Profile;
use gtk::{ use gtk::{
gdk::Cursor, gdk::Cursor,
@ -24,7 +25,7 @@ impl List {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new(profile: Rc<Profile>, auth_url: &str) -> Self { pub fn new(widget_action: Rc<Action>, profile: Rc<Profile>, auth_url: &str) -> Self {
// Init model // Init model
let list_store = ListStore::new::<Item>(); let list_store = ListStore::new::<Item>();
@ -132,6 +133,9 @@ impl List {
.factory(&factory) .factory(&factory)
.build(); .build();
// Connect events
dropdown.connect_selected_notify(move |_| widget_action.update.activate());
// Return activated `Self` // Return activated `Self`
Self { Self {
dropdown, dropdown,