add set_visible wrapper with grab_focus

This commit is contained in:
yggverse 2024-11-21 12:13:53 +02:00
parent eb21c4d844
commit c62bcfbe05
2 changed files with 16 additions and 9 deletions

View File

@ -5,10 +5,7 @@ use list::{item::value::Value, List};
use name::Name;
use super::Action;
use gtk::{
prelude::{BoxExt, WidgetExt},
Box, Orientation,
};
use gtk::{prelude::BoxExt, Box, Orientation};
use std::rc::Rc;
pub struct Form {
@ -39,7 +36,7 @@ impl Form {
let update = action.update.clone();
move |key| {
// Change name entry visibility
name.gobject.set_visible(match key {
name.show(match key {
Value::GENERATE_NEW_AUTH => true,
_ => false,
});

View File

@ -1,7 +1,7 @@
use super::Action;
use gtk::{
glib::GString,
prelude::{EditableExt, EntryExt},
prelude::{EditableExt, EntryExt, WidgetExt},
Entry,
};
use std::rc::Rc;
@ -22,9 +22,10 @@ impl Name {
pub fn new(action: Rc<Action>) -> Self {
// Init `GObject`
let gobject = Entry::builder()
.margin_top(MARGIN)
.max_length(MAX_LENGTH as i32)
.placeholder_text(PLACEHOLDER_TEXT)
.margin_top(MARGIN)
.visible(false)
.build();
// Init events
@ -36,12 +37,21 @@ impl Name {
// Actions
pub fn is_valid(&self) -> bool {
self.gobject.text_length() >= MIN_LENGTH && self.gobject.text_length() <= MAX_LENGTH
/// Change visibility status
/// * grab focus on `is_visible`
pub fn show(&self, is_visible: bool) {
self.gobject.set_visible(is_visible);
if is_visible {
self.gobject.grab_focus();
}
}
// Getters
pub fn is_valid(&self) -> bool {
self.gobject.text_length() >= MIN_LENGTH && self.gobject.text_length() <= MAX_LENGTH
}
pub fn value(&self) -> Option<GString> {
let text = self.gobject.text();
if text.is_empty() {