diff options
author | 2019-12-08 06:53:54 +0100 | |
---|---|---|
committer | 2019-12-08 06:53:54 +0100 | |
commit | 9b84b6e40336543380312aa1b2b1091791ec25cd (patch) | |
tree | c14ed67cfc38e1a22617716c361e4684a64e7fac /wgpu | |
parent | 48145ba51e045f8b0b4788f3a75d20b9d9b7e6ad (diff) | |
parent | f942fc3b68ecbbe136c54922109c7e2e4732735b (diff) | |
download | iced-9b84b6e40336543380312aa1b2b1091791ec25cd.tar.gz iced-9b84b6e40336543380312aa1b2b1091791ec25cd.tar.bz2 iced-9b84b6e40336543380312aa1b2b1091791ec25cd.zip |
Merge branch 'master' into feature/event-subscriptions
Diffstat (limited to '')
-rw-r--r-- | wgpu/src/renderer/widget/text_input.rs | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index d64fca6d..c6c64b88 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -12,6 +12,24 @@ impl text_input::Renderer for Renderer { 20 } + fn measure_value(&self, value: &str, size: u16) -> f32 { + let (mut width, _) = self.text_pipeline.measure( + value, + f32::from(size), + Font::Default, + Size::INFINITY, + ); + + let spaces_at_the_end = value.len() - value.trim_end().len(); + + if spaces_at_the_end > 0 { + let space_width = self.text_pipeline.space_width(size as f32); + width += spaces_at_the_end as f32 * space_width; + } + + width + } + fn draw( &mut self, bounds: Rectangle, @@ -48,7 +66,6 @@ impl text_input::Renderer for Renderer { border_radius: 4, }; - let size = f32::from(size); let text = value.to_string(); let text_value = Primitive::Text { @@ -68,7 +85,7 @@ impl text_input::Renderer for Renderer { width: f32::INFINITY, ..text_bounds }, - size, + size: f32::from(size), horizontal_alignment: HorizontalAlignment::Left, vertical_alignment: VerticalAlignment::Center, }; @@ -77,20 +94,8 @@ impl text_input::Renderer for Renderer { let text_before_cursor = value.until(state.cursor_position(value)).to_string(); - let (mut text_value_width, _) = self.text_pipeline.measure( - &text_before_cursor, - size, - Font::Default, - Size::new(f32::INFINITY, text_bounds.height), - ); - - let spaces_at_the_end = - text_before_cursor.len() - text_before_cursor.trim_end().len(); - - if spaces_at_the_end > 0 { - let space_width = self.text_pipeline.space_width(size); - text_value_width += spaces_at_the_end as f32 * space_width; - } + let text_value_width = + self.measure_value(&text_before_cursor, size); let cursor = Primitive::Quad { bounds: Rectangle { |