mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
replace Option with Value enum
This commit is contained in:
parent
69b879540d
commit
f4cef86438
@ -1,5 +1,5 @@
|
|||||||
mod widget;
|
mod widget;
|
||||||
use widget::Widget;
|
use widget::{form::list::item::value::Value, Widget};
|
||||||
|
|
||||||
use crate::app::browser::window::Action;
|
use crate::app::browser::window::Action;
|
||||||
use crate::profile::Profile;
|
use crate::profile::Profile;
|
||||||
@ -27,10 +27,11 @@ impl Gemini {
|
|||||||
let auth_url = auth_uri.to_string();
|
let auth_url = auth_uri.to_string();
|
||||||
|
|
||||||
// Add new identity option
|
// Add new identity option
|
||||||
widget
|
widget.form.list.append(
|
||||||
.form
|
Value::CREATE_NEW_AUTH,
|
||||||
.list
|
"Create new..",
|
||||||
.append(None, "Create new..", "Auto-generated certificate");
|
"Auto-generated certificate",
|
||||||
|
);
|
||||||
|
|
||||||
// Collect additional options from database
|
// Collect additional options from database
|
||||||
match profile.identity.gemini.database.records() {
|
match profile.identity.gemini.database.records() {
|
||||||
@ -44,7 +45,7 @@ impl Gemini {
|
|||||||
|
|
||||||
// Append record option
|
// Append record option
|
||||||
widget.form.list.append(
|
widget.form.list.append(
|
||||||
Some(identity.id),
|
Value::PROFILE_IDENTITY_GEMINI_ID(identity.id),
|
||||||
&certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix
|
&certificate.subject_name().unwrap().replace("CN=", ""), // trim prefix
|
||||||
&format!(
|
&format!(
|
||||||
"valid: {} | auth: {}",
|
"valid: {} | auth: {}",
|
||||||
@ -76,10 +77,9 @@ impl Gemini {
|
|||||||
move |response| {
|
move |response| {
|
||||||
// Get record ID depending of user selection
|
// Get record ID depending of user selection
|
||||||
let profile_identity_gemini_id = match response {
|
let profile_identity_gemini_id = match response {
|
||||||
// Use selected identity
|
Value::PROFILE_IDENTITY_GEMINI_ID(value) => value,
|
||||||
Some(id) => id,
|
Value::REMOVE_CURRENT_AUTH => todo!(),
|
||||||
// Create new identity, get last insert ID
|
Value::CREATE_NEW_AUTH => profile
|
||||||
None => profile
|
|
||||||
.identity
|
.identity
|
||||||
.gemini
|
.gemini
|
||||||
.create(None, widget.form.name.value().as_deref())
|
.create(None, widget.form.name.value().as_deref())
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
mod form;
|
pub mod form;
|
||||||
|
use form::{list::item::value::Value, Form};
|
||||||
use form::Form;
|
|
||||||
|
|
||||||
use adw::{
|
use adw::{
|
||||||
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
|
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
|
||||||
@ -63,8 +62,8 @@ impl Widget {
|
|||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Callback wrapper for `apply` [response](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/signal.AlertDialog.response.html)
|
/// 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`
|
/// * return `Value` enum or new record request on `None`
|
||||||
pub fn on_apply(&self, callback: impl Fn(Option<i64>) + 'static) {
|
pub fn on_apply(&self, callback: impl Fn(Value) + 'static) {
|
||||||
self.gobject.connect_response(Some(RESPONSE_APPLY.0), {
|
self.gobject.connect_response(Some(RESPONSE_APPLY.0), {
|
||||||
let form = self.form.clone();
|
let form = self.form.clone();
|
||||||
move |_, _| callback(form.list.selected())
|
move |_, _| callback(form.list.selected())
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
mod list;
|
pub mod list;
|
||||||
mod name;
|
mod name;
|
||||||
|
|
||||||
use list::List;
|
use list::{item::value::Value, List};
|
||||||
use name::Name;
|
use name::Name;
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
@ -34,8 +34,12 @@ impl Form {
|
|||||||
// Connect events
|
// Connect events
|
||||||
list.on_select({
|
list.on_select({
|
||||||
let name = name.clone();
|
let name = name.clone();
|
||||||
// Show name entry on new identity option selected
|
move |key| {
|
||||||
move |key| name.gobject.set_visible(key.is_none())
|
name.gobject.set_visible(match key {
|
||||||
|
Value::CREATE_NEW_AUTH => true,
|
||||||
|
_ => false,
|
||||||
|
})
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return activated `Self`
|
// Return activated `Self`
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
mod item;
|
pub mod item;
|
||||||
use item::Item;
|
use item::{value::Value, Item};
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gio::{
|
gio::{
|
||||||
@ -84,34 +84,33 @@ impl List {
|
|||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/// Append new item
|
/// Append new item
|
||||||
pub fn append(&self, profile_identity_gemini_id: Option<i64>, title: &str, subtitle: &str) {
|
pub fn append(&self, value: Value, title: &str, subtitle: &str) {
|
||||||
self.model
|
self.model.append(&Item::new(value, title, subtitle));
|
||||||
.append(&Item::new(profile_identity_gemini_id, title, subtitle));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
|
|
||||||
/// Run callback function on `connect_selected_notify` event
|
/// Run callback function on `connect_selected_notify` event
|
||||||
/// * return formatted `profile_identity_gemini_id` match selected item
|
/// * return `Value` enum match selected item
|
||||||
pub fn on_select(&self, callback: impl Fn(Option<i64>) + 'static) {
|
pub fn on_select(&self, callback: impl Fn(Value) + 'static) {
|
||||||
self.gobject.connect_selected_notify(move |list| {
|
self.gobject.connect_selected_notify(move |list| {
|
||||||
callback(
|
callback(
|
||||||
list.selected_item()
|
list.selected_item()
|
||||||
.and_downcast::<Item>()
|
.and_downcast::<Item>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.profile_identity_gemini_id_option(),
|
.value_enum(),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
/// Get formatted `profile_identity_gemini_id` **option** match selected item
|
/// Get formatted `value` match selected item
|
||||||
pub fn selected(&self) -> Option<i64> {
|
pub fn selected(&self) -> Value {
|
||||||
self.gobject
|
self.gobject
|
||||||
.selected_item()
|
.selected_item()
|
||||||
.and_downcast::<Item>()
|
.and_downcast::<Item>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.profile_identity_gemini_id_option()
|
.value_enum()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,29 @@
|
|||||||
mod imp;
|
mod imp;
|
||||||
|
pub mod value;
|
||||||
|
|
||||||
use gtk::glib::{self, Object};
|
use gtk::glib::{self, Object};
|
||||||
|
use value::Value;
|
||||||
|
|
||||||
glib::wrapper! {
|
glib::wrapper! {
|
||||||
pub struct Item(ObjectSubclass<imp::Item>);
|
pub struct Item(ObjectSubclass<imp::Item>);
|
||||||
}
|
}
|
||||||
|
|
||||||
// C-type conversion
|
// C-type property `value` conversion for `Item`
|
||||||
const OPTION_NEW: i64 = -1;
|
const CREATE_NEW_AUTH: i64 = 0;
|
||||||
|
const REMOVE_CURRENT_AUTH: i64 = -1;
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
/// Create new `GObject` with formatted properties
|
/// Create new `GObject` with formatted properties
|
||||||
pub fn new(profile_identity_gemini_id: Option<i64>, title: &str, subtitle: &str) -> Self {
|
pub fn new(value: Value, title: &str, subtitle: &str) -> Self {
|
||||||
Object::builder()
|
Object::builder()
|
||||||
.property(
|
.property(
|
||||||
"profile_identity_gemini_id",
|
"value",
|
||||||
match profile_identity_gemini_id {
|
match value {
|
||||||
Some(value) => value,
|
Value::CREATE_NEW_AUTH => CREATE_NEW_AUTH,
|
||||||
None => OPTION_NEW,
|
Value::REMOVE_CURRENT_AUTH => REMOVE_CURRENT_AUTH,
|
||||||
|
Value::PROFILE_IDENTITY_GEMINI_ID(value) => value,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.property("title", title)
|
.property("title", title)
|
||||||
@ -28,11 +33,12 @@ impl Item {
|
|||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
/// Additional `profile_identity_gemini_id` wrapper with `Option` value support
|
/// Get `value` as enum `Value`
|
||||||
pub fn profile_identity_gemini_id_option(&self) -> Option<i64> {
|
pub fn value_enum(&self) -> Value {
|
||||||
match self.profile_identity_gemini_id() {
|
match self.value() {
|
||||||
OPTION_NEW => None,
|
CREATE_NEW_AUTH => Value::CREATE_NEW_AUTH,
|
||||||
value => Some(value),
|
REMOVE_CURRENT_AUTH => Value::REMOVE_CURRENT_AUTH,
|
||||||
|
value => Value::PROFILE_IDENTITY_GEMINI_ID(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ use std::cell::{Cell, RefCell};
|
|||||||
#[properties(wrapper_type = super::Item)]
|
#[properties(wrapper_type = super::Item)]
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
#[property(get, set)]
|
#[property(get, set)]
|
||||||
profile_identity_gemini_id: Cell<i64>,
|
value: Cell<i64>,
|
||||||
#[property(get, set)]
|
#[property(get, set)]
|
||||||
title: RefCell<String>,
|
title: RefCell<String>,
|
||||||
#[property(get, set)]
|
#[property(get, set)]
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Value {
|
||||||
|
CREATE_NEW_AUTH,
|
||||||
|
REMOVE_CURRENT_AUTH,
|
||||||
|
PROFILE_IDENTITY_GEMINI_ID(i64),
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user