Implement flexible `TextInput::draw` helper
This commit is contained in:
parent
62295f554b
commit
df712f9ccf
|
@ -165,6 +165,7 @@ where
|
||||||
/// Sets the style of the [`TextInput`].
|
/// Sets the style of the [`TextInput`].
|
||||||
///
|
///
|
||||||
/// [`TextInput`]: struct.TextInput.html
|
/// [`TextInput`]: struct.TextInput.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
|
pub fn style(mut self, style: impl Into<Renderer::Style>) -> Self {
|
||||||
self.style = style.into();
|
self.style = style.into();
|
||||||
self
|
self
|
||||||
|
@ -173,11 +174,63 @@ where
|
||||||
/// Returns the current [`State`] of the [`TextInput`].
|
/// Returns the current [`State`] of the [`TextInput`].
|
||||||
///
|
///
|
||||||
/// [`TextInput`]: struct.TextInput.html
|
/// [`TextInput`]: struct.TextInput.html
|
||||||
|
/// [`State`]: struct.State.html
|
||||||
pub fn state(&self) -> &State {
|
pub fn state(&self) -> &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>
|
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||||
for TextInput<'a, Message, Renderer>
|
for TextInput<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
|
@ -541,36 +594,7 @@ where
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
_viewport: &Rectangle,
|
_viewport: &Rectangle,
|
||||||
) -> Renderer::Output {
|
) -> Renderer::Output {
|
||||||
let bounds = layout.bounds();
|
self.draw(renderer, layout, cursor_position, None)
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
|
|
@ -21,6 +21,15 @@ impl Value {
|
||||||
Self { graphemes }
|
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`].
|
/// Returns the total amount of graphemes in the [`Value`].
|
||||||
///
|
///
|
||||||
/// [`Value`]: struct.Value.html
|
/// [`Value`]: struct.Value.html
|
||||||
|
|
Loading…
Reference in New Issue