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

View File

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