diff --git a/src/app/browser/window/tab/item/page/navigation/request.rs b/src/app/browser/window/tab/item/page/navigation/request.rs index 79db4341..1efe86b4 100644 --- a/src/app/browser/window/tab/item/page/navigation/request.rs +++ b/src/app/browser/window/tab/item/page/navigation/request.rs @@ -247,14 +247,20 @@ impl Request for Entry { fn identity(&self, profile: &Rc) { if let Some(uri) = self.uri() { if ["gemini", "titan"].contains(&uri.scheme().as_str()) { - return identity::default(profile, &uri, { - let profile = profile.clone(); - let this = self.clone(); - move || { - this.update(&profile); - this.emit_activate(); - } // on apply - }) + return identity::default( + profile, + &uri, + &Rc::new({ + let profile = profile.clone(); + let this = self.clone(); + move |is_reload| { + this.update(&profile); + if is_reload { + this.emit_activate(); + } + } + }), + ) .present(Some(self)); } } 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 728032fa..42d4fe5b 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 @@ -10,8 +10,12 @@ use gtk::glib::Uri; use std::rc::Rc; /// Create new identity widget for Gemini protocol match given URI -pub fn default(profile: &Rc, request: &Uri, on_apply: impl Fn() + 'static) -> Default { - Default::build(profile, request, on_apply) +pub fn default( + profile: &Rc, + request: &Uri, + callback: &Rc, +) -> Default { + Default::build(profile, request, callback) } /// Create new identity widget for unknown request diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/default.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/default.rs index 06e029a4..8e4ffd37 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/default.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/default.rs @@ -1,5 +1,5 @@ mod widget; -use widget::{form::list::item::value::Value, Widget}; +use widget::Widget; use super::Profile; use gtk::{glib::Uri, prelude::IsA}; @@ -14,59 +14,13 @@ impl Default { // Construct /// Create new `Self` for given `Profile` - pub fn build(profile: &Rc, request: &Uri, on_apply: impl Fn() + 'static) -> Self { + pub fn build( + profile: &Rc, + request: &Uri, + callback: &Rc, + ) -> Self { // Init widget - let widget = Rc::new(Widget::build(profile, request)); - - // Init events - widget.on_apply({ - let profile = profile.clone(); - let request = request.clone(); - let widget = widget.clone(); - move |response| { - // Get option match user choice - let option = match response { - Value::ProfileIdentityId(value) => Some(value), - Value::GuestSession => None, - Value::GeneratePem => Some( - profile - .identity - .make(None, &widget.form.name.value().unwrap()) - .unwrap(), // @TODO handle - ), - Value::ImportPem => Some( - profile - .identity - .add(&widget.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 - on_apply() - } - }); + let widget = Rc::new(Widget::build(profile, request, callback)); // Return activated `Self` Self { diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/default/widget.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/default/widget.rs index 3c35ec78..b4c0b93a 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/default/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/default/widget.rs @@ -24,16 +24,18 @@ const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); // Select options pub struct Widget { - // pub action: Rc, - pub form: Rc
, - pub alert_dialog: AlertDialog, + alert_dialog: AlertDialog, } impl Widget { // Constructors /// Create new `Self` - pub fn build(profile: &Rc, request: &Uri) -> Self { + pub fn build( + profile: &Rc, + request: &Uri, + callback: &Rc, + ) -> Self { // Init actions let action = Rc::new(WidgetAction::new()); @@ -49,13 +51,65 @@ impl Widget { .extra_child(&form.g_box) .build(); - // Set response variants 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); @@ -67,6 +121,7 @@ impl Widget { // Init events action.update.connect_activate({ let form = form.clone(); + let callback = callback.clone(); let alert_dialog = alert_dialog.clone(); move || { // Update child components @@ -74,6 +129,8 @@ impl Widget { // Deactivate apply button if the form values could not be processed alert_dialog.set_response_enabled(RESPONSE_APPLY.0, form.is_applicable()); + + callback(false); } }); @@ -81,30 +138,11 @@ impl Widget { action.update.activate(); // Return new activated `Self` - Self { - // action, - form, - alert_dialog, - } + Self { alert_dialog } } // Actions - /// Callback wrapper to apply - /// [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html) - pub fn on_apply(&self, callback: impl Fn(Value) + 'static) { - self.alert_dialog.connect_response(Some(RESPONSE_APPLY.0), { - let form = self.form.clone(); - move |this, response| { - // Prevent double-click action - this.set_response_enabled(response, false); - - // Result - callback(form.list.selected().value_enum()) - } - }); - } - /// Show dialog with new preset pub fn present(&self, parent: Option<&impl IsA>) { self.alert_dialog.present(parent)