implement name entry as trait

This commit is contained in:
yggverse 2025-01-29 20:57:32 +02:00
parent c6f5de634d
commit 93942dd83d
3 changed files with 30 additions and 32 deletions

View file

@ -4,7 +4,7 @@ mod form;
use crate::Profile; use crate::Profile;
use action::Action as WidgetAction; use action::Action as WidgetAction;
use adw::AlertDialog; use adw::AlertDialog;
use gtk::glib::Uri; use gtk::{glib::Uri, prelude::EditableExt};
use std::rc::Rc; use std::rc::Rc;
// Select options // Select options
@ -69,10 +69,7 @@ impl Common for AlertDialog {
Value::ProfileIdentityId(value) => Some(value), Value::ProfileIdentityId(value) => Some(value),
Value::GuestSession => None, Value::GuestSession => None,
Value::GeneratePem => Some( Value::GeneratePem => Some(
profile profile.identity.make(None, &form.name.text()).unwrap(), // @TODO handle
.identity
.make(None, &form.name.value().unwrap())
.unwrap(), // @TODO handle
), ),
Value::ImportPem => Some( Value::ImportPem => Some(
profile profile

View file

@ -17,7 +17,7 @@ use crate::Profile;
use gtk::{ use gtk::{
glib::Uri, glib::Uri,
prelude::{BoxExt, WidgetExt}, prelude::{BoxExt, WidgetExt},
Box, Button, Orientation, Box, Button, Entry, Orientation,
}; };
use std::rc::Rc; use std::rc::Rc;
@ -27,7 +27,7 @@ pub struct Form {
pub exit: Button, pub exit: Button,
pub file: Rc<File>, pub file: Rc<File>,
pub list: Rc<List>, pub list: Rc<List>,
pub name: Rc<Name>, pub name: Entry,
pub save: Rc<Save>, pub save: Rc<Save>,
pub g_box: Box, pub g_box: Box,
profile: Rc<Profile>, profile: Rc<Profile>,
@ -41,7 +41,7 @@ impl Form {
// Init components // Init components
let list = Rc::new(List::build(widget_action, profile, request)); let list = Rc::new(List::build(widget_action, profile, request));
let file = Rc::new(File::build(widget_action)); let file = Rc::new(File::build(widget_action));
let name = Rc::new(Name::build(widget_action)); let name = Entry::name(widget_action);
let save = Rc::new(Save::build(profile, &list)); let save = Rc::new(Save::build(profile, &list));
let drop = Rc::new(Drop::build(profile, &list)); let drop = Rc::new(Drop::build(profile, &list));
let exit = Button::exit(widget_action, profile, &list, request); let exit = Button::exit(widget_action, profile, &list, request);
@ -50,7 +50,7 @@ impl Form {
let g_box = Box::builder().orientation(Orientation::Vertical).build(); let g_box = Box::builder().orientation(Orientation::Vertical).build();
g_box.append(&list.dropdown); g_box.append(&list.dropdown);
g_box.append(&name.entry); g_box.append(&name);
g_box.append(&file.button); g_box.append(&file.button);
g_box.append(&exit); g_box.append(&exit);
g_box.append(&drop.button); g_box.append(&drop.button);

View file

@ -1,25 +1,35 @@
use super::WidgetAction; use super::WidgetAction;
use gtk::{ use gtk::{
glib::GString,
prelude::{EditableExt, EntryExt, WidgetExt}, prelude::{EditableExt, EntryExt, WidgetExt},
Entry, Entry,
}; };
use std::rc::Rc; use std::rc::Rc;
const PLACEHOLDER_TEXT: &str = "Identity name (required)";
const MARGIN: i32 = 8;
const MIN_LENGTH: u16 = 1; const MIN_LENGTH: u16 = 1;
const MAX_LENGTH: u16 = 36; const MAX_LENGTH: u16 = 36;
pub struct Name { pub trait Name {
pub entry: Entry, // Constructors
fn name(widget_action: &Rc<WidgetAction>) -> Self;
// Actions
fn update(&self, is_visible: bool);
// Getters
fn is_valid(&self) -> bool;
} }
impl Name { impl Name for Entry {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn build(widget_action: &Rc<WidgetAction>) -> Self { fn name(widget_action: &Rc<WidgetAction>) -> Self {
const PLACEHOLDER_TEXT: &str = "Identity name (required)";
const MARGIN: i32 = 8;
// Init main gobject // Init main gobject
let entry = Entry::builder() let entry = Entry::builder()
.margin_top(MARGIN) .margin_top(MARGIN)
@ -35,32 +45,23 @@ impl Name {
}); });
// Return activated `Self` // Return activated `Self`
Self { entry } entry
} }
// Actions // Actions
/// Change visibility status /// Change visibility status
/// * grab focus on `is_visible` is `true` /// * grab focus on `is_visible` is `true`
pub fn update(&self, is_visible: bool) { fn update(&self, is_visible: bool) {
self.entry.set_visible(is_visible); self.set_visible(is_visible);
if is_visible && self.entry.focus_child().is_none() { if is_visible && self.focus_child().is_none() {
self.entry.grab_focus(); self.grab_focus();
} }
} }
// Getters // Getters
pub fn is_valid(&self) -> bool { fn is_valid(&self) -> bool {
self.entry.text_length() >= MIN_LENGTH && self.entry.text_length() <= MAX_LENGTH self.text_length() >= MIN_LENGTH && self.text_length() <= MAX_LENGTH
}
pub fn value(&self) -> Option<GString> {
let text = self.entry.text();
if text.is_empty() {
None
} else {
Some(text)
}
} }
} }