From fa433743b352f9a27e0669d4da41f645db8b04cb Mon Sep 17 00:00:00 2001 From: Jon Pacheco Date: Sat, 22 May 2021 19:28:17 +0100 Subject: feat: add placeholders to pick_list see issue #726 --- native/src/widget/pick_list.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 92c183f3..f83a2e8d 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -25,6 +25,7 @@ where last_selection: &'a mut Option, on_selected: Box Message>, options: Cow<'a, [T]>, + placeholder: Option, selected: Option, width: Length, padding: Padding, @@ -82,6 +83,7 @@ where last_selection, on_selected: Box::new(on_selected), options: options.into(), + placeholder: None, selected, width: Length::Shrink, text_size: None, @@ -91,6 +93,12 @@ where } } + /// Sets the placeholder of the [`PickList`]. + pub fn placeholder(mut self, placeholder: impl Into) -> Self { + self.placeholder = Some(placeholder.into()); + self + } + /// Sets the width of the [`PickList`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -158,8 +166,7 @@ where let max_width = match self.width { Length::Shrink => { let labels = self.options.iter().map(ToString::to_string); - - labels + let labels_width = labels .map(|label| { let (width, _) = renderer.measure( &label, @@ -171,7 +178,24 @@ where width.round() as u32 }) .max() - .unwrap_or(100) + .unwrap_or(100); + + let placeholder_width = self + .placeholder + .as_ref() + .map(|placeholder| { + let (width, _) = renderer.measure( + placeholder, + text_size, + self.font, + Size::new(f32::INFINITY, f32::INFINITY), + ); + + width.round() as u32 + }) + .unwrap_or(100); + + labels_width.max(placeholder_width) } _ => 0, }; @@ -265,6 +289,7 @@ where layout.bounds(), cursor_position, self.selected.as_ref().map(ToString::to_string), + self.placeholder.clone(), self.padding, self.text_size.unwrap_or(renderer.default_size()), self.font, @@ -325,6 +350,7 @@ pub trait Renderer: text::Renderer + menu::Renderer { bounds: Rectangle, cursor_position: Point, selected: Option, + placeholder: Option, padding: Padding, text_size: u16, font: Self::Font, -- cgit From 45bd685f7c12ccb4c03a756b3d694672948c1a03 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 18:55:55 +0700 Subject: Hash `placeholder` in `hash_layout` implementation for `PickList` --- native/src/widget/pick_list.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'native/src') diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index f83a2e8d..fbc091ad 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -219,6 +219,8 @@ where match self.width { Length::Shrink => { + self.placeholder.hash(state); + self.options .iter() .map(ToString::to_string) -- cgit From 26b2a824a930b8f98f4510aa2d2f3aaef7b669b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:09:13 +0700 Subject: Remove duplication of measuring logic in `PickList` --- native/src/widget/pick_list.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index fbc091ad..21c0c153 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -162,37 +162,31 @@ where .pad(self.padding); let text_size = self.text_size.unwrap_or(renderer.default_size()); + let font = self.font; let max_width = match self.width { Length::Shrink => { + let measure = |label: &str| -> u32 { + let (width, _) = renderer.measure( + label, + text_size, + font, + Size::new(f32::INFINITY, f32::INFINITY), + ); + + width.round() as u32 + }; + let labels = self.options.iter().map(ToString::to_string); - let labels_width = labels - .map(|label| { - let (width, _) = renderer.measure( - &label, - text_size, - self.font, - Size::new(f32::INFINITY, f32::INFINITY), - ); - - width.round() as u32 - }) - .max() - .unwrap_or(100); + + let labels_width = + labels.map(|label| measure(&label)).max().unwrap_or(100); let placeholder_width = self .placeholder .as_ref() - .map(|placeholder| { - let (width, _) = renderer.measure( - placeholder, - text_size, - self.font, - Size::new(f32::INFINITY, f32::INFINITY), - ); - - width.round() as u32 - }) + .map(String::as_str) + .map(measure) .unwrap_or(100); labels_width.max(placeholder_width) -- cgit From a866f8742e4ddf5714455519790fed0f961fad66 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:16:53 +0700 Subject: Avoid cloning `placeholder` for `PickList` unnecessarily during `draw` --- native/src/widget/pick_list.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 21c0c153..4f4e751e 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -285,7 +285,7 @@ where layout.bounds(), cursor_position, self.selected.as_ref().map(ToString::to_string), - self.placeholder.clone(), + self.placeholder.as_ref().map(String::as_str), self.padding, self.text_size.unwrap_or(renderer.default_size()), self.font, @@ -346,7 +346,7 @@ pub trait Renderer: text::Renderer + menu::Renderer { bounds: Rectangle, cursor_position: Point, selected: Option, - placeholder: Option, + placeholder: Option<&str>, padding: Padding, text_size: u16, font: Self::Font, -- cgit