Implement flexible `TextInput::draw` helper

This commit is contained in:
Héctor Ramón Jiménez 2020-11-17 05:13:06 +01:00
parent 62295f554b
commit df712f9ccf
2 changed files with 63 additions and 30 deletions

View File

@ -165,6 +165,7 @@ where
/// Sets the style of the [`TextInput`].
///
/// [`TextInput`]: struct.TextInput.html
/// [`State`]: struct.State.html
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
self.style = style.into();
self
@ -173,11 +174,63 @@ where
/// Returns the current [`State`] of the [`TextInput`].
///
/// [`TextInput`]: struct.TextInput.html
/// [`State`]: struct.State.html
pub fn state(&self) -> &State {
self.state
}
}
impl<'a, Message, Renderer> TextInput<'a, Message, Renderer>
where
Renderer: self::Renderer,
{
/// Draws the [`TextInput`] with the given [`Renderer`], overriding its
/// [`Value`] if provided.
///
/// [`TextInput`]: struct.TextInput.html
/// [`Renderer`]: trait.Render.html
/// [`Value`]: struct.Value.html
pub fn draw(
&self,
renderer: &mut Renderer,
layout: Layout<'_>,
cursor_position: Point,
value: Option<&Value>,
) -> Renderer::Output {
let value = value.unwrap_or(&self.value);
let bounds = layout.bounds();
let text_bounds = layout.children().next().unwrap().bounds();
if self.is_secure {
self::Renderer::draw(
renderer,
bounds,
text_bounds,
cursor_position,
self.font,
self.size.unwrap_or(renderer.default_size()),
&self.placeholder,
&value.secure(),
&self.state,
&self.style,
)
} else {
self::Renderer::draw(
renderer,
bounds,
text_bounds,
cursor_position,
self.font,
self.size.unwrap_or(renderer.default_size()),
&self.placeholder,
value,
&self.state,
&self.style,
)
}
}
}
impl<'a, Message, Renderer> Widget<Message, Renderer>
for TextInput<'a, Message, Renderer>
where
@ -541,36 +594,7 @@ where
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let bounds = layout.bounds();
let text_bounds = layout.children().next().unwrap().bounds();
if self.is_secure {
self::Renderer::draw(
renderer,
bounds,
text_bounds,
cursor_position,
self.font,
self.size.unwrap_or(renderer.default_size()),
&self.placeholder,
&self.value.secure(),
&self.state,
&self.style,
)
} else {
self::Renderer::draw(
renderer,
bounds,
text_bounds,
cursor_position,
self.font,
self.size.unwrap_or(renderer.default_size()),
&self.placeholder,
&self.value,
&self.state,
&self.style,
)
}
self.draw(renderer, layout, cursor_position, None)
}
fn hash_layout(&self, state: &mut Hasher) {

View File

@ -21,6 +21,15 @@ impl Value {
Self { graphemes }
}
/// Returns whether the [`Value`] is empty or not.
///
/// A [`Value`] is empty when it contains no graphemes.
///
/// [`Value`]: struct.Value.html
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns the total amount of graphemes in the [`Value`].
///
/// [`Value`]: struct.Value.html