Skip to content

Commit

Permalink
Add support for localized placeholders.
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra committed Jan 14, 2025
1 parent dface87 commit 7497c10
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ language-es-es = Spanish (Spain)
#
form-example-hinted-label = Hinted
form-example-not-hinted-label = Not hinted
form-example-input-placeholder = Enter some text
# Specific validation reason
form-input-invalid-non-whitespace-required = This field must have at least one non-whitespace character
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ language-es-es = Español (España)
#
form-example-hinted-label = Insinuado
form-example-not-hinted-label = No insinuado
form-example-input-placeholder = Introduzca un texto
# Razón de validación específica
form-input-invalid-non-whitespace-required = Este campo debe tener al menos un carácter que no sea un espacio en blanco
Expand Down
2 changes: 2 additions & 0 deletions examples/validation-localized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ fn form() -> impl MakeWidget {
localize!("form-example-hinted-label")
.and(
text.to_input()
.placeholder(localize!("form-example-input-placeholder"))
.validation(validations.validate(&text, validate_input))
.hint(localize!("form-hint-field-required")),
)
.and(localize!("form-example-not-hinted-label"))
.and(
text.to_input()
.placeholder(localize!("form-example-input-placeholder"))
.validation(validations.validate(&text, validate_input)),
)
.and(
Expand Down
2 changes: 2 additions & 0 deletions examples/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ fn main() -> cushy::Result {
"Hinted"
.and(
text.to_input()
.placeholder("Enter some text")
.validation(validations.validate(&text, validate_input))
.hint("* required"),
)
.and("Not Hinted")
.and(
text.to_input()
.placeholder("Enter some text")
.validation(validations.validate(&text, validate_input)),
)
.and(
Expand Down
12 changes: 8 additions & 4 deletions src/widgets/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::utils::ModifiersExt;
use crate::value::{Destination, Dynamic, Generation, IntoDynamic, IntoValue, Source, Value};
use crate::widget::{Callback, EventHandling, Widget, HANDLED, IGNORED};
use crate::window::KeyEvent;
use crate::{ConstraintLimit, FitMeasuredSize, Lazy};
use crate::{ConstraintLimit, FitMeasuredSize, Lazy, MaybeLocalized};

const CURSOR_BLINK_DURATION: Duration = Duration::from_millis(500);

Expand All @@ -38,7 +38,7 @@ pub struct Input<Storage = String> {
/// The value of this widget.
pub value: Dynamic<Storage>,
/// The placeholder text to display when no value is present.
pub placeholder: Value<String>,
pub placeholder: Value<MaybeLocalized>,
mask_symbol: Value<CowString>,
mask: CowString,
on_key: Option<Callback<KeyEvent, EventHandling>>,
Expand Down Expand Up @@ -127,7 +127,7 @@ where

/// Sets the `placeholder` text, which is displayed when the field has an
/// empty value.
pub fn placeholder(mut self, placeholder: impl IntoValue<String>) -> Self {
pub fn placeholder(mut self, placeholder: impl IntoValue<MaybeLocalized>) -> Self {
self.placeholder = placeholder.into_value();
self
}
Expand Down Expand Up @@ -605,7 +605,11 @@ where
}

let placeholder_color = context.theme().surface.on_color_variant;
let placeholder = self.placeholder.map(|placeholder| context.gfx.measure_text(Text::new(placeholder, placeholder_color)));
let placeholder = self.placeholder.map(|placeholder| {
let text = placeholder.localize(context).to_string();

context.gfx.measure_text(Text::new(&text, placeholder_color))
});
(bytes, context.gfx.measure_text(text), placeholder)
});
self.cache = Some(CachedLayout {
Expand Down

0 comments on commit 7497c10

Please sign in to comment.