Skip to content

Commit

Permalink
implement name entry as trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 29, 2025
1 parent c6f5de6 commit 93942dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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>,
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
}

0 comments on commit 93942dd

Please sign in to comment.