show identity details as dropdown item tooltip

This commit is contained in:
yggverse 2024-12-05 12:50:49 +02:00
parent 763c59ed4f
commit cb08217ffa
4 changed files with 72 additions and 23 deletions

View File

@ -33,6 +33,7 @@ impl Gemini {
Value::UseGuestSession, Value::UseGuestSession,
"Guest session", "Guest session",
"No identity for this request", "No identity for this request",
None,
false, false,
); );
@ -41,6 +42,7 @@ impl Gemini {
Value::GenerateNewAuth, Value::GenerateNewAuth,
"Create new", "Create new",
"Generate long-term certificate", "Generate long-term certificate",
None,
false, false,
); );
@ -49,6 +51,7 @@ impl Gemini {
Value::ImportPem, Value::ImportPem,
"Import identity", "Import identity",
"Use existing certificate", "Use existing certificate",
None,
false, false,
); );
@ -63,13 +66,47 @@ impl Gemini {
Err(reason) => todo!("{reason}"), Err(reason) => todo!("{reason}"),
}; };
// Get auth details for tooltip
let mut auth_scope = Vec::new();
for auth in profile
.identity
.gemini
.auth
.database
.records_scope(None)
.unwrap()
.iter()
.filter(|this| this.profile_identity_gemini_id == identity.id)
{
auth_scope.push(auth.scope.clone())
}
// Build tooltip
let mut tooltip = format!(
"Valid:\n{}\n{}",
certificate
.not_valid_before()
.unwrap()
.format_iso8601()
.unwrap(),
certificate
.not_valid_after()
.unwrap()
.format_iso8601()
.unwrap()
);
if auth_scope.len() > 0 {
tooltip.push_str(&format!("\n\nScope:\n{}", auth_scope.join("\n")));
}
// Append record option // Append record option
widget.form.list.append( widget.form.list.append(
Value::ProfileIdentityGeminiId(identity.id), Value::ProfileIdentityGeminiId(identity.id),
&certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix &certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix
&format!( &format!(
"{} - {} | scope: {}", "{} - {} | scope: {}",
// certificate validity time
certificate certificate
.not_valid_before() .not_valid_before()
.unwrap() .unwrap()
@ -80,26 +117,16 @@ impl Gemini {
.unwrap() .unwrap()
.format(DATE_FORMAT) .format(DATE_FORMAT)
.unwrap(), .unwrap(),
// count active certificate sessions auth_scope.len(),
profile
.identity
.gemini
.auth
.database
.records_scope(None)
.unwrap()
.iter()
.filter(|this| this.profile_identity_gemini_id == identity.id)
.count(),
), ),
// is selected Some(&tooltip),
profile profile
.identity .identity
.gemini .gemini
.auth .auth
.memory .memory
.match_scope(&url) .match_scope(&url)
.is_some_and(|auth| auth.profile_identity_gemini_id == identity.id), .is_some_and(|auth| auth.profile_identity_gemini_id == identity.id), // is selected
); );
} }
} }

View File

@ -63,12 +63,10 @@ impl List {
title.set_css_classes(if item.is_active() { &["success"] } else { &[] }); title.set_css_classes(if item.is_active() { &["success"] } else { &[] });
// Update `subtitle` (expected as the last child) // Update `subtitle` (expected as the last child)
child let subtitle = child.last_child().unwrap().downcast::<Label>().unwrap();
.last_child()
.unwrap() subtitle.set_label(&item.subtitle());
.downcast::<Label>() subtitle.set_tooltip_text(Some(&item.tooltip()));
.unwrap()
.set_label(&item.subtitle());
}); });
// Init main `DropDown` // Init main `DropDown`
@ -87,9 +85,18 @@ impl List {
// Actions // Actions
/// Append new item /// Append new item
pub fn append(&self, value: Value, title: &str, subtitle: &str, is_active: bool) { pub fn append(
let item = Item::new(value, title, subtitle, is_active); &self,
value: Value,
title: &str,
subtitle: &str,
tooltip: Option<&str>,
is_active: bool,
) {
let item = Item::new(value, title, subtitle, tooltip, is_active);
self.list_store.append(&item); self.list_store.append(&item);
if is_active { if is_active {
self.dropdown self.dropdown
.set_selected(self.list_store.find(&item).unwrap()); // @TODO panic or handle? .set_selected(self.list_store.find(&item).unwrap()); // @TODO panic or handle?

View File

@ -18,7 +18,13 @@ impl Item {
// Constructors // Constructors
/// Create new `GObject` /// Create new `GObject`
pub fn new(value: Value, title: &str, subtitle: &str, is_active: bool) -> Self { pub fn new(
value: Value,
title: &str,
subtitle: &str,
tooltip: Option<&str>,
is_active: bool,
) -> Self {
Object::builder() Object::builder()
.property( .property(
"value", "value",
@ -31,6 +37,13 @@ impl Item {
) )
.property("title", title) .property("title", title)
.property("subtitle", subtitle) .property("subtitle", subtitle)
.property(
"tooltip",
match tooltip {
Some(text) => text,
None => "", // NULL
},
)
.property("is_active", is_active) .property("is_active", is_active)
.build() .build()
} }

View File

@ -18,6 +18,8 @@ pub struct Item {
#[property(get, set)] #[property(get, set)]
subtitle: RefCell<String>, subtitle: RefCell<String>,
#[property(get, set)] #[property(get, set)]
tooltip: RefCell<String>,
#[property(get, set)]
is_active: Cell<bool>, is_active: Cell<bool>,
} }