diff options
author | 2021-05-22 19:28:17 +0100 | |
---|---|---|
committer | 2021-07-22 19:12:56 +0700 | |
commit | fa433743b352f9a27e0669d4da41f645db8b04cb (patch) | |
tree | e384f5db7a9ffbed5c7b7afd6ace68544c67c850 | |
parent | a2b1ba522a8b90a2e539fff5936c798efc3f3807 (diff) | |
download | iced-fa433743b352f9a27e0669d4da41f645db8b04cb.tar.gz iced-fa433743b352f9a27e0669d4da41f645db8b04cb.tar.bz2 iced-fa433743b352f9a27e0669d4da41f645db8b04cb.zip |
feat: add placeholders to pick_list
see issue #726
-rw-r--r-- | graphics/src/widget/pick_list.rs | 3 | ||||
-rw-r--r-- | native/src/widget/pick_list.rs | 32 |
2 files changed, 31 insertions, 4 deletions
diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 32dfbdf9..a7fe3e93 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -31,6 +31,7 @@ where bounds: Rectangle, cursor_position: Point, selected: Option<String>, + placeholder: Option<String>, padding: Padding, text_size: u16, font: Font, @@ -68,7 +69,7 @@ where ( Primitive::Group { - primitives: if let Some(label) = selected { + primitives: if let Some(label) = selected.or(placeholder) { let label = Primitive::Text { content: label, size: f32::from(text_size), 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<T>, on_selected: Box<dyn Fn(T) -> Message>, options: Cow<'a, [T]>, + placeholder: Option<String>, selected: Option<T>, 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<String>) -> 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<String>, + placeholder: Option<String>, padding: Padding, text_size: u16, font: Self::Font, |