Skip to content

Commit

Permalink
fix(pages/whoareyou): proper username checking
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Oct 29, 2024
1 parent debdeb6 commit 4e20d93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! generate_pages {
macro_rules! generate_page {
($page:ident $({$($model:tt)+})?:
$(
init($root:ident, $initsender:ident, $initmodel:ident, $initwidgets:ident) $initblock:block
init$([$($local_ref:ident)+])?($root:ident, $initsender:ident, $initmodel:ident, $initwidgets:ident) $initblock:block
)?
update($self:ident, $message:ident, $sender:ident) {
$( $msg:ident$(($($param:ident: $paramtype:ty),+$(,)?))? => $msghdl:expr ),*$(,)?
Expand Down Expand Up @@ -111,6 +111,9 @@ macro_rules! generate_page {
$sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self::default();

$($($(let $local_ref = &model.$local_ref;)+)?)?

// HACK: invoking view_output!() directly gives `()` when $init* is given.
// I don't know why this fixes the issue. — mado
let widgets = [<view _output>]!();
Expand Down
23 changes: 20 additions & 3 deletions src/pages/_01_whoareyou.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
crate::generate_page!(WhoAreYou {
pub name: String,
pub user: String,
lbl_error: gtk::Label,
}:
init(root, sender, model, widgets) {
init[lbl_error](root, sender, model, widgets) {
let s1 = sender.clone();
widgets.tf_fullname.internal_entry().connect_changed(move |en| {
s1.input(Self::Input::NotifyFullName(en.text().to_string()));
});

let s2 = sender.clone();
widgets.tf_username.internal_entry().connect_changed(move |en| {
s2.input(Self::Input::NotifyUsername(en.text().to_string()));
if en.parent().and_then(|x| x.parent()).and_then(|x| x.parent()).and_then(|x| x.parent()).unwrap().dynamic_cast::<libhelium::TextField>().unwrap().is_valid() {
sender.input(Self::Input::NotifyUsername(en.text().to_string()));
} else {
sender.input(Self::Input::InvalidUsername);
}
});

tracing::trace!(?model, ?widgets);
Expand All @@ -23,7 +27,11 @@ crate::generate_page!(WhoAreYou {
NotifyUsername(user: String) => {
tracing::trace!(?user, "Username Input");
self.user = user;
self.lbl_error.set_visible(false);
},
InvalidUsername => {
self.lbl_error.set_visible(true);
}
} => {}

gtk::Box {
Expand Down Expand Up @@ -63,6 +71,15 @@ crate::generate_page!(WhoAreYou {
set_needs_validation: true,
set_regex: &libhelium::glib::Regex::new(r"^[a-z][-a-z0-9_]*\$?$", gtk::glib::RegexCompileFlags::DEFAULT, gtk::glib::RegexMatchFlags::DEFAULT).unwrap().unwrap(),
},

#[local_ref]
lbl_error -> gtk::Label {
set_label: &gettext("Username \n- must start with lowercase letters\n- must contain only alphanumericals, underscore (<tt>_</tt>) or dash (<tt>-</tt>)\n- may optionally end with a dollar sign (<tt>$</tt>)"),
set_use_markup: true,
set_visible: false,
add_css_class: "destructive-action",
inline_css: "color: orange",
}
},

#[template] crate::ui::PrevNextBtns {
Expand Down

0 comments on commit 4e20d93

Please sign in to comment.