From 713ed3aca1d5fb23dc9c2a75b1f61fd8bd439e01 Mon Sep 17 00:00:00 2001 From: sumibi-yakitori Date: Mon, 27 Jan 2020 04:28:40 +0900 Subject: [PATCH] Custom fonts can be set in the text input. --- native/src/renderer/null.rs | 4 +++- native/src/widget/text_input.rs | 29 +++++++++++++++++++++----- wgpu/src/renderer/widget/text_input.rs | 14 +++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 3e02e73a..2e0b6f32 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -109,7 +109,7 @@ impl text_input::Renderer for Null { 20 } - fn measure_value(&self, _value: &str, _size: u16) -> f32 { + fn measure_value(&self, _value: &str, _size: u16, _font: Font) -> f32 { 0.0 } @@ -119,6 +119,7 @@ impl text_input::Renderer for Null { _size: u16, _value: &text_input::Value, _state: &text_input::State, + _font: Font, ) -> f32 { 0.0 } @@ -129,6 +130,7 @@ impl text_input::Renderer for Null { _text_bounds: Rectangle, _cursor_position: Point, _size: u16, + _font: Font, _placeholder: &str, _value: &text_input::Value, _state: &text_input::State, diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 25032559..7e212add 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -6,7 +6,7 @@ //! [`State`]: struct.State.html use crate::{ input::{keyboard, mouse, ButtonState}, - layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, + layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; use unicode_segmentation::UnicodeSegmentation; @@ -41,6 +41,7 @@ pub struct TextInput<'a, Message, Renderer: self::Renderer> { placeholder: String, value: Value, is_secure: bool, + font: Font, width: Length, max_width: Length, padding: u16, @@ -75,6 +76,7 @@ impl<'a, Message, Renderer: self::Renderer> TextInput<'a, Message, Renderer> { placeholder: String::from(placeholder), value: Value::new(value), is_secure: false, + font: Font::Default, width: Length::Fill, max_width: Length::Shrink, padding: 0, @@ -93,6 +95,14 @@ impl<'a, Message, Renderer: self::Renderer> TextInput<'a, Message, Renderer> { self } + /// Sets the [`Font`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`Font`]: ../../struct.Font.html + pub fn font(mut self, font: Font) -> Self { + self.font = font; + self + } /// Sets the width of the [`TextInput`]. /// /// [`TextInput`]: struct.TextInput.html @@ -211,6 +221,7 @@ where size, &value, &self.state, + self.font, ); self.state.cursor_position = find_cursor_position( @@ -220,6 +231,7 @@ where size, 0, self.value.len(), + self.font, ); } else { self.state.cursor_position = 0; @@ -368,6 +380,7 @@ where text_bounds, cursor_position, self.size.unwrap_or(renderer.default_size()), + self.font, &self.placeholder, &self.value.secure(), &self.state, @@ -379,6 +392,7 @@ where text_bounds, cursor_position, self.size.unwrap_or(renderer.default_size()), + self.font, &self.placeholder, &self.value, &self.state, @@ -418,7 +432,7 @@ pub trait Renderer: crate::Renderer + Sized { /// Returns the width of the value of the [`TextInput`]. /// /// [`TextInput`]: struct.TextInput.html - fn measure_value(&self, value: &str, size: u16) -> f32; + fn measure_value(&self, value: &str, size: u16, font: Font) -> f32; /// Returns the current horizontal offset of the value of the /// [`TextInput`]. @@ -434,6 +448,7 @@ pub trait Renderer: crate::Renderer + Sized { size: u16, value: &Value, state: &State, + font: Font, ) -> f32; /// Draws a [`TextInput`]. @@ -455,6 +470,7 @@ pub trait Renderer: crate::Renderer + Sized { text_bounds: Rectangle, cursor_position: Point, size: u16, + font: Font, placeholder: &str, value: &Value, state: &State, @@ -714,6 +730,7 @@ fn find_cursor_position( size: u16, start: usize, end: usize, + font: Font, ) -> usize { if start >= end { if start == 0 { @@ -723,8 +740,8 @@ fn find_cursor_position( let prev = value.until(start - 1); let next = value.until(start); - let prev_width = renderer.measure_value(&prev.to_string(), size); - let next_width = renderer.measure_value(&next.to_string(), size); + let prev_width = renderer.measure_value(&prev.to_string(), size, font); + let next_width = renderer.measure_value(&next.to_string(), size, font); if next_width - target > target - prev_width { return start - 1; @@ -736,7 +753,7 @@ fn find_cursor_position( let index = (end - start) / 2; let subvalue = value.until(start + index); - let width = renderer.measure_value(&subvalue.to_string(), size); + let width = renderer.measure_value(&subvalue.to_string(), size, font); if width > target { find_cursor_position( @@ -746,6 +763,7 @@ fn find_cursor_position( size, start, start + index, + font, ) } else { find_cursor_position( @@ -755,6 +773,7 @@ fn find_cursor_position( size, start + index + 1, end, + font, ) } } diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index 8b774a48..e2a1b3a9 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -14,11 +14,11 @@ impl text_input::Renderer for Renderer { 20 } - fn measure_value(&self, value: &str, size: u16) -> f32 { + fn measure_value(&self, value: &str, size: u16, font: Font) -> f32 { let (mut width, _) = self.text_pipeline.measure( value, f32::from(size), - Font::Default, + font, Size::INFINITY, ); @@ -38,6 +38,7 @@ impl text_input::Renderer for Renderer { size: u16, value: &text_input::Value, state: &text_input::State, + font: Font, ) -> f32 { if state.is_focused() { let (_, offset) = measure_cursor_and_scroll_offset( @@ -46,6 +47,7 @@ impl text_input::Renderer for Renderer { value, size, state.cursor_position(value), + font, ); offset @@ -60,6 +62,7 @@ impl text_input::Renderer for Renderer { text_bounds: Rectangle, cursor_position: Point, size: u16, + font: Font, placeholder: &str, value: &text_input::Value, state: &text_input::State, @@ -97,7 +100,7 @@ impl text_input::Renderer for Renderer { style_sheet.value_color() } .into(), - font: Font::Default, + font, bounds: Rectangle { width: f32::INFINITY, ..text_bounds @@ -114,6 +117,7 @@ impl text_input::Renderer for Renderer { value, size, state.cursor_position(value), + font, ); let cursor = Primitive::Quad { @@ -164,12 +168,14 @@ fn measure_cursor_and_scroll_offset( value: &text_input::Value, size: u16, cursor_index: usize, + font: Font, ) -> (f32, f32) { use iced_native::text_input::Renderer; let text_before_cursor = value.until(cursor_index).to_string(); - let text_value_width = renderer.measure_value(&text_before_cursor, size); + let text_value_width = + renderer.measure_value(&text_before_cursor, size, font); let offset = ((text_value_width + 5.0) - text_bounds.width).max(0.0); (text_value_width, offset)