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

View file

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

View file

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