feat: add placeholders to pick_list

see issue #726
This commit is contained in:
Jon Pacheco 2021-05-22 19:28:17 +01:00 committed by Héctor Ramón Jiménez
parent a2b1ba522a
commit fa433743b3
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 31 additions and 4 deletions

View file

@ -31,6 +31,7 @@ where
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
selected: Option<String>, selected: Option<String>,
placeholder: Option<String>,
padding: Padding, padding: Padding,
text_size: u16, text_size: u16,
font: Font, font: Font,
@ -68,7 +69,7 @@ where
( (
Primitive::Group { Primitive::Group {
primitives: if let Some(label) = selected { primitives: if let Some(label) = selected.or(placeholder) {
let label = Primitive::Text { let label = Primitive::Text {
content: label, content: label,
size: f32::from(text_size), size: f32::from(text_size),

View file

@ -25,6 +25,7 @@ where
last_selection: &'a mut Option<T>, last_selection: &'a mut Option<T>,
on_selected: Box<dyn Fn(T) -> Message>, on_selected: Box<dyn Fn(T) -> Message>,
options: Cow<'a, [T]>, options: Cow<'a, [T]>,
placeholder: Option<String>,
selected: Option<T>, selected: Option<T>,
width: Length, width: Length,
padding: Padding, padding: Padding,
@ -82,6 +83,7 @@ where
last_selection, last_selection,
on_selected: Box::new(on_selected), on_selected: Box::new(on_selected),
options: options.into(), options: options.into(),
placeholder: None,
selected, selected,
width: Length::Shrink, width: Length::Shrink,
text_size: None, 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`]. /// Sets the width of the [`PickList`].
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: Length) -> Self {
self.width = width; self.width = width;
@ -158,8 +166,7 @@ where
let max_width = match self.width { let max_width = match self.width {
Length::Shrink => { Length::Shrink => {
let labels = self.options.iter().map(ToString::to_string); let labels = self.options.iter().map(ToString::to_string);
let labels_width = labels
labels
.map(|label| { .map(|label| {
let (width, _) = renderer.measure( let (width, _) = renderer.measure(
&label, &label,
@ -171,7 +178,24 @@ where
width.round() as u32 width.round() as u32
}) })
.max() .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, _ => 0,
}; };
@ -265,6 +289,7 @@ where
layout.bounds(), layout.bounds(),
cursor_position, cursor_position,
self.selected.as_ref().map(ToString::to_string), self.selected.as_ref().map(ToString::to_string),
self.placeholder.clone(),
self.padding, self.padding,
self.text_size.unwrap_or(renderer.default_size()), self.text_size.unwrap_or(renderer.default_size()),
self.font, self.font,
@ -325,6 +350,7 @@ pub trait Renderer: text::Renderer + menu::Renderer {
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
selected: Option<String>, selected: Option<String>,
placeholder: Option<String>,
padding: Padding, padding: Padding,
text_size: u16, text_size: u16,
font: Self::Font, font: Self::Font,