diff --git a/src/app/browser/window/tab/item/identity/gemini.rs b/src/app/browser/window/tab/item/identity/gemini.rs index 4f5496e5..58870721 100644 --- a/src/app/browser/window/tab/item/identity/gemini.rs +++ b/src/app/browser/window/tab/item/identity/gemini.rs @@ -1,5 +1,5 @@ mod widget; -use widget::Widget; +use widget::{form::list::item::value::Value, Widget}; use crate::app::browser::window::Action; use crate::profile::Profile; @@ -27,10 +27,11 @@ impl Gemini { let auth_url = auth_uri.to_string(); // Add new identity option - widget - .form - .list - .append(None, "Create new..", "Auto-generated certificate"); + widget.form.list.append( + Value::CREATE_NEW_AUTH, + "Create new..", + "Auto-generated certificate", + ); // Collect additional options from database match profile.identity.gemini.database.records() { @@ -44,7 +45,7 @@ impl Gemini { // Append record option widget.form.list.append( - Some(identity.id), + Value::PROFILE_IDENTITY_GEMINI_ID(identity.id), &certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix &format!( "valid: {} | auth: {}", @@ -76,10 +77,9 @@ impl Gemini { move |response| { // Get record ID depending of user selection let profile_identity_gemini_id = match response { - // Use selected identity - Some(id) => id, - // Create new identity, get last insert ID - None => profile + Value::PROFILE_IDENTITY_GEMINI_ID(value) => value, + Value::REMOVE_CURRENT_AUTH => todo!(), + Value::CREATE_NEW_AUTH => profile .identity .gemini .create(None, widget.form.name.value().as_deref()) diff --git a/src/app/browser/window/tab/item/identity/gemini/widget.rs b/src/app/browser/window/tab/item/identity/gemini/widget.rs index 22aebdd1..660a8ab8 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget.rs @@ -1,6 +1,5 @@ -mod form; - -use form::Form; +pub mod form; +use form::{list::item::value::Value, Form}; use adw::{ prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual}, @@ -63,8 +62,8 @@ impl Widget { // Actions /// Callback wrapper for `apply` [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html) - /// * return `profile_identity_gemini_id` or new record request on `None` - pub fn on_apply(&self, callback: impl Fn(Option) + 'static) { + /// * return `Value` enum or new record request on `None` + pub fn on_apply(&self, callback: impl Fn(Value) + 'static) { self.gobject.connect_response(Some(RESPONSE_APPLY.0), { let form = self.form.clone(); move |_, _| callback(form.list.selected()) diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form.rs index de54dcc2..5c219829 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form.rs @@ -1,7 +1,7 @@ -mod list; +pub mod list; mod name; -use list::List; +use list::{item::value::Value, List}; use name::Name; use gtk::{ @@ -34,8 +34,12 @@ impl Form { // Connect events list.on_select({ let name = name.clone(); - // Show name entry on new identity option selected - move |key| name.gobject.set_visible(key.is_none()) + move |key| { + name.gobject.set_visible(match key { + Value::CREATE_NEW_AUTH => true, + _ => false, + }) + } }); // Return activated `Self` diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs index 32425943..14235b88 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs @@ -1,5 +1,5 @@ -mod item; -use item::Item; +pub mod item; +use item::{value::Value, Item}; use gtk::{ gio::{ @@ -84,34 +84,33 @@ impl List { // Actions /// Append new item - pub fn append(&self, profile_identity_gemini_id: Option, title: &str, subtitle: &str) { - self.model - .append(&Item::new(profile_identity_gemini_id, title, subtitle)); + pub fn append(&self, value: Value, title: &str, subtitle: &str) { + self.model.append(&Item::new(value, title, subtitle)); } // Events /// Run callback function on `connect_selected_notify` event - /// * return formatted `profile_identity_gemini_id` match selected item - pub fn on_select(&self, callback: impl Fn(Option) + 'static) { + /// * return `Value` enum match selected item + pub fn on_select(&self, callback: impl Fn(Value) + 'static) { self.gobject.connect_selected_notify(move |list| { callback( list.selected_item() .and_downcast::() .unwrap() - .profile_identity_gemini_id_option(), + .value_enum(), ) }); } // Getters - /// Get formatted `profile_identity_gemini_id` **option** match selected item - pub fn selected(&self) -> Option { + /// Get formatted `value` match selected item + pub fn selected(&self) -> Value { self.gobject .selected_item() .and_downcast::() .unwrap() - .profile_identity_gemini_id_option() + .value_enum() } } diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs index 41abf82d..181924b6 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs @@ -1,24 +1,29 @@ mod imp; +pub mod value; + use gtk::glib::{self, Object}; +use value::Value; glib::wrapper! { pub struct Item(ObjectSubclass); } -// C-type conversion -const OPTION_NEW: i64 = -1; +// C-type property `value` conversion for `Item` +const CREATE_NEW_AUTH: i64 = 0; +const REMOVE_CURRENT_AUTH: i64 = -1; impl Item { // Constructors /// Create new `GObject` with formatted properties - pub fn new(profile_identity_gemini_id: Option, title: &str, subtitle: &str) -> Self { + pub fn new(value: Value, title: &str, subtitle: &str) -> Self { Object::builder() .property( - "profile_identity_gemini_id", - match profile_identity_gemini_id { - Some(value) => value, - None => OPTION_NEW, + "value", + match value { + Value::CREATE_NEW_AUTH => CREATE_NEW_AUTH, + Value::REMOVE_CURRENT_AUTH => REMOVE_CURRENT_AUTH, + Value::PROFILE_IDENTITY_GEMINI_ID(value) => value, }, ) .property("title", title) @@ -28,11 +33,12 @@ impl Item { // Getters - /// Additional `profile_identity_gemini_id` wrapper with `Option` value support - pub fn profile_identity_gemini_id_option(&self) -> Option { - match self.profile_identity_gemini_id() { - OPTION_NEW => None, - value => Some(value), + /// Get `value` as enum `Value` + pub fn value_enum(&self) -> Value { + match self.value() { + CREATE_NEW_AUTH => Value::CREATE_NEW_AUTH, + REMOVE_CURRENT_AUTH => Value::REMOVE_CURRENT_AUTH, + value => Value::PROFILE_IDENTITY_GEMINI_ID(value), } } } diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/imp.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/imp.rs index 1cf8791b..70dfc8b6 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/imp.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/imp.rs @@ -12,7 +12,7 @@ use std::cell::{Cell, RefCell}; #[properties(wrapper_type = super::Item)] pub struct Item { #[property(get, set)] - profile_identity_gemini_id: Cell, + value: Cell, #[property(get, set)] title: RefCell, #[property(get, set)] diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/value.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/value.rs new file mode 100644 index 00000000..61ae53b6 --- /dev/null +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/value.rs @@ -0,0 +1,6 @@ +#[derive(Debug)] +pub enum Value { + CREATE_NEW_AUTH, + REMOVE_CURRENT_AUTH, + PROFILE_IDENTITY_GEMINI_ID(i64), +}