Skip to content

Commit

Permalink
Tweak label size and update example text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed May 5, 2024
1 parent 2243237 commit 9819240
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
15 changes: 12 additions & 3 deletions crates/egui_form/src/form_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::form::FormFieldState;
use crate::{EguiValidationReport, Form};
use egui::{Response, RichText, Widget};
use egui::{Response, RichText, TextStyle, Widget};
use std::borrow::Cow;

/// A form field that can be validated.
Expand Down Expand Up @@ -61,11 +61,20 @@ impl<'a, 'f, Errors: EguiValidationReport> FormField<'a, 'f, Errors> {
}

if let Some(label) = self.label {
let mut rich_text = RichText::new(label.as_ref());
let mut rich_text = RichText::new(label);
if show_error {
rich_text = rich_text.color(error_color);
}
ui.label(rich_text);
ui.label(
rich_text.size(
ui.style()
.text_styles
.get(&TextStyle::Body)
.map(|s| s.size)
.unwrap_or(16.0)
* 0.9,
),
);
}

let response = content.ui(ui);
Expand Down
14 changes: 9 additions & 5 deletions fancy-example/src/signup_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl SignupForm {
}
}

fn text_edit_singleline(value: &mut String) -> TextEdit {
TextEdit::singleline(value).margin(8.0)
}

impl Example for SignupForm {
fn name(&self) -> &'static str {
"Signup Form"
Expand Down Expand Up @@ -191,31 +195,31 @@ Errors will show up after editing a field or after trying to submit.
&mut |ui, form_ref| {
FormField::new(form_ref, field_path!("first_name"))
.label("First Name")
.ui(ui, TextEdit::singleline(first_name));
.ui(ui, text_edit_singleline(first_name));
},
&mut |ui, form_ref| {
FormField::new(form_ref, field_path!("last_name"))
.label("Last Name")
.ui(ui, TextEdit::singleline(last_name));
.ui(ui, text_edit_singleline(last_name));
},
);

FormField::new(form_ref, field_path!("email"))
.label("Email")
.ui(ui, TextEdit::singleline(email));
.ui(ui, text_edit_singleline(email));

horizontal_fields(
ui,
form_ref,
&mut |ui, form_ref| {
FormField::new(form_ref, field_path!("password"))
.label("Password")
.ui(ui, TextEdit::singleline(password).password(true));
.ui(ui, text_edit_singleline(password).password(true));
},
&mut |ui, form_ref| {
FormField::new(form_ref, field_path!("repeat_password"))
.label("Repeat Password")
.ui(ui, TextEdit::singleline(repeat_password).password(true));
.ui(ui, text_edit_singleline(repeat_password).password(true));
},
);

Expand Down

0 comments on commit 9819240

Please sign in to comment.