From fc8c967891dbe2353f3d56d5edf52ff07982b7ef Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 29 Jan 2025 19:34:30 +0200 Subject: [PATCH] implement Common identity as trait for AlertDialog --- .../item/page/navigation/request/identity.rs | 4 +- .../navigation/request/identity/common.rs | 152 +++++++++++++++--- .../identity/common/{widget => }/action.rs | 0 .../common/{widget => }/action/update.rs | 0 .../identity/common/{widget => }/form.rs | 0 .../identity/common/{widget => }/form/drop.rs | 0 .../identity/common/{widget => }/form/exit.rs | 0 .../identity/common/{widget => }/form/file.rs | 0 .../identity/common/{widget => }/form/list.rs | 0 .../common/{widget => }/form/list/item.rs | 0 .../{widget => }/form/list/item/error.rs | 0 .../common/{widget => }/form/list/item/imp.rs | 0 .../{widget => }/form/list/item/is_active.rs | 0 .../{widget => }/form/list/item/subtitle.rs | 0 .../{widget => }/form/list/item/title.rs | 0 .../{widget => }/form/list/item/tooltip.rs | 0 .../{widget => }/form/list/item/value.rs | 0 .../identity/common/{widget => }/form/name.rs | 0 .../identity/common/{widget => }/form/save.rs | 0 .../{widget => }/form/save/certificate.rs | 0 .../form/save/certificate/error.rs | 0 .../request/identity/common/widget.rs | 150 ----------------- 22 files changed, 131 insertions(+), 175 deletions(-) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/action.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/action/update.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/drop.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/exit.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/file.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/error.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/imp.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/is_active.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/subtitle.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/title.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/tooltip.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/list/item/value.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/name.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/save.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/save/certificate.rs (100%) rename src/app/browser/window/tab/item/page/navigation/request/identity/common/{widget => }/form/save/certificate/error.rs (100%) delete mode 100644 src/app/browser/window/tab/item/page/navigation/request/identity/common/widget.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity.rs b/src/app/browser/window/tab/item/page/navigation/request/identity.rs index f2025ff8..f4577ed1 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity.rs @@ -14,8 +14,8 @@ pub fn common( profile: &Rc, request: &Uri, callback: &Rc, -) -> Common { - Common::build(profile, request, callback) +) -> AlertDialog { + AlertDialog::common(profile, request, callback) } /// Create new identity widget for unknown request diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs index c27ac36a..ea77c6e4 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common.rs @@ -1,38 +1,144 @@ -mod widget; -use widget::Widget; +mod action; +pub mod form; -use super::Profile; -use gtk::{glib::Uri, prelude::IsA}; +use action::Action as WidgetAction; +use form::{list::item::value::Value, Form}; + +use crate::Profile; +use adw::{ + prelude::{AlertDialogExt, AlertDialogExtManual}, + AlertDialog, ResponseAppearance, +}; +use gtk::glib::Uri; use std::rc::Rc; -pub struct Common { - // profile: Rc, - widget: Rc, +// Defaults +const HEADING: &str = "Identity"; +const BODY: &str = "Select identity certificate"; + +// Response variants +const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply"); +const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); +// const RESPONSE_MANAGE: (&str, &str) = ("manage", "Manage"); + +// Select options + +pub trait Common { + fn common(profile: &Rc, request: &Uri, callback: &Rc) + -> Self; } -impl Common { - // Construct +impl Common for AlertDialog { + // Constructors - /// Create new `Self` for given `Profile` - pub fn build( + /// Create new `Self` + fn common( profile: &Rc, request: &Uri, callback: &Rc, ) -> Self { - // Init widget - let widget = Rc::new(Widget::build(profile, request, callback)); + // Init actions + let action = Rc::new(WidgetAction::new()); - // Return activated `Self` - Self { - // profile, - widget, - } - } + // Init child container + let form = Rc::new(Form::build(&action, profile, request)); - // Actions + // Init main widget + let alert_dialog = AlertDialog::builder() + .heading(HEADING) + .body(BODY) + .close_response(RESPONSE_CANCEL.0) + .default_response(RESPONSE_APPLY.0) + .extra_child(&form.g_box) + .build(); - /// Show dialog for parent [Widget](https://docs.gtk.org/gtk4/class.Widget.html) - pub fn present(&self, parent: Option<&impl IsA>) { - self.widget.present(parent); + alert_dialog.add_responses(&[ + RESPONSE_CANCEL, + // RESPONSE_MANAGE, + RESPONSE_APPLY, + ]); + + alert_dialog.connect_response(Some(RESPONSE_APPLY.0), { + let callback = callback.clone(); + let form = form.clone(); + let profile = profile.clone(); + let request = request.clone(); + move |this, response| { + // Prevent double-click action + this.set_response_enabled(response, false); + + // Get option match user choice + let option = match form.list.selected().value_enum() { + Value::ProfileIdentityId(value) => Some(value), + Value::GuestSession => None, + Value::GeneratePem => Some( + profile + .identity + .make(None, &form.name.value().unwrap()) + .unwrap(), // @TODO handle + ), + Value::ImportPem => Some( + profile + .identity + .add(&form.file.pem.take().unwrap()) + .unwrap(), // @TODO handle + ), + }; + + // Apply auth + match option { + // Activate identity for `scope` + Some(profile_identity_id) => { + if profile + .identity + .auth + .apply(profile_identity_id, &request.to_string()) + .is_err() + { + panic!() // unexpected @TODO + } + } + // Remove all identity auths for `scope` + None => { + if profile.identity.auth.remove(&request.to_string()).is_err() { + panic!() // unexpected @TODO + } + } + } + + // Run callback function + callback(true) + } + }); + + // Deactivate not implemented feature @TODO + // alert_dialog.set_response_enabled(RESPONSE_MANAGE.0, false); + + // Decorate default response preset + alert_dialog.set_response_appearance(RESPONSE_APPLY.0, ResponseAppearance::Suggested); + /* contrast issue with Ubuntu orange accents + alert_dialog.set_response_appearance(RESPONSE_CANCEL.0, ResponseAppearance::Destructive); */ + + // Init events + action.update.connect_activate({ + let form = form.clone(); + let callback = callback.clone(); + let alert_dialog = alert_dialog.clone(); + move || { + // Update child components + form.update(); + + // Deactivate apply button if the form values could not be processed + alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable()); + + callback(false); + } + }); + + // Make initial update + action.update.activate(); + + // Return new activated `Self` + alert_dialog } } diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/action.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/action.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/action.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/action.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/action/update.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/action/update.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/action/update.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/action/update.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/drop.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/drop.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/drop.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/exit.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/exit.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/file.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/file.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/file.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/file.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/error.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/error.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/error.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/error.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/imp.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/imp.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/imp.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/imp.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/is_active.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/is_active.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/is_active.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/subtitle.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/subtitle.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/subtitle.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/subtitle.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/title.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/title.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/title.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/title.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/tooltip.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/tooltip.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/tooltip.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/tooltip.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/value.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/value.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/list/item/value.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/list/item/value.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/name.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/name.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/name.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/name.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save/certificate.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save/certificate.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save/certificate/error.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate/error.rs similarity index 100% rename from src/app/browser/window/tab/item/page/navigation/request/identity/common/widget/form/save/certificate/error.rs rename to src/app/browser/window/tab/item/page/navigation/request/identity/common/form/save/certificate/error.rs diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget.rs deleted file mode 100644 index b4c0b93a..00000000 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/widget.rs +++ /dev/null @@ -1,150 +0,0 @@ -mod action; -pub mod form; - -use action::Action as WidgetAction; -use form::{list::item::value::Value, Form}; - -use crate::Profile; -use adw::{ - prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual}, - AlertDialog, ResponseAppearance, -}; -use gtk::{glib::Uri, prelude::IsA}; -use std::rc::Rc; - -// Defaults -const HEADING: &str = "Identity"; -const BODY: &str = "Select identity certificate"; - -// Response variants -const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply"); -const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); -// const RESPONSE_MANAGE: (&str, &str) = ("manage", "Manage"); - -// Select options - -pub struct Widget { - alert_dialog: AlertDialog, -} - -impl Widget { - // Constructors - - /// Create new `Self` - pub fn build( - profile: &Rc, - request: &Uri, - callback: &Rc, - ) -> Self { - // Init actions - let action = Rc::new(WidgetAction::new()); - - // Init child container - let form = Rc::new(Form::build(&action, profile, request)); - - // Init main widget - let alert_dialog = AlertDialog::builder() - .heading(HEADING) - .body(BODY) - .close_response(RESPONSE_CANCEL.0) - .default_response(RESPONSE_APPLY.0) - .extra_child(&form.g_box) - .build(); - - alert_dialog.add_responses(&[ - RESPONSE_CANCEL, - // RESPONSE_MANAGE, - RESPONSE_APPLY, - ]); - - alert_dialog.connect_response(Some(RESPONSE_APPLY.0), { - let callback = callback.clone(); - let form = form.clone(); - let profile = profile.clone(); - let request = request.clone(); - move |this, response| { - // Prevent double-click action - this.set_response_enabled(response, false); - - // Get option match user choice - let option = match form.list.selected().value_enum() { - Value::ProfileIdentityId(value) => Some(value), - Value::GuestSession => None, - Value::GeneratePem => Some( - profile - .identity - .make(None, &form.name.value().unwrap()) - .unwrap(), // @TODO handle - ), - Value::ImportPem => Some( - profile - .identity - .add(&form.file.pem.take().unwrap()) - .unwrap(), // @TODO handle - ), - }; - - // Apply auth - match option { - // Activate identity for `scope` - Some(profile_identity_id) => { - if profile - .identity - .auth - .apply(profile_identity_id, &request.to_string()) - .is_err() - { - panic!() // unexpected @TODO - } - } - // Remove all identity auths for `scope` - None => { - if profile.identity.auth.remove(&request.to_string()).is_err() { - panic!() // unexpected @TODO - } - } - } - - // Run callback function - callback(true) - } - }); - - // Deactivate not implemented feature @TODO - // alert_dialog.set_response_enabled(RESPONSE_MANAGE.0, false); - - // Decorate default response preset - alert_dialog.set_response_appearance(RESPONSE_APPLY.0, ResponseAppearance::Suggested); - /* contrast issue with Ubuntu orange accents - alert_dialog.set_response_appearance(RESPONSE_CANCEL.0, ResponseAppearance::Destructive); */ - - // Init events - action.update.connect_activate({ - let form = form.clone(); - let callback = callback.clone(); - let alert_dialog = alert_dialog.clone(); - move || { - // Update child components - form.update(); - - // Deactivate apply button if the form values could not be processed - alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable()); - - callback(false); - } - }); - - // Make initial update - action.update.activate(); - - // Return new activated `Self` - Self { alert_dialog } - } - - // Actions - - /// Show dialog with new preset - pub fn present(&self, parent: Option<&impl IsA>) { - self.alert_dialog.present(parent) - } -}