From df712f9ccfddebafdc7a7f97953f7249176d28e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 17 Nov 2020 05:13:06 +0100 Subject: [PATCH] Implement flexible `TextInput::draw` helper --- native/src/widget/text_input.rs | 84 +++++++++++++++++---------- native/src/widget/text_input/value.rs | 9 +++ 2 files changed, 63 insertions(+), 30 deletions(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index cf95e7e8..6d194db5 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -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) -> 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 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) { diff --git a/native/src/widget/text_input/value.rs b/native/src/widget/text_input/value.rs index 1e9ba45b..8df74e0c 100644 --- a/native/src/widget/text_input/value.rs +++ b/native/src/widget/text_input/value.rs @@ -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