summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2021-07-22 20:32:43 +0700
committerLibravatar GitHub <noreply@github.com>2021-07-22 20:32:43 +0700
commitf076649fbb575ee036ab0b3f4511690e3379c115 (patch)
tree805377d47609364a579597c1c3966250d4f2e03a /native
parenta2b1ba522a8b90a2e539fff5936c798efc3f3807 (diff)
parenta866f8742e4ddf5714455519790fed0f961fad66 (diff)
downloadiced-f076649fbb575ee036ab0b3f4511690e3379c115.tar.gz
iced-f076649fbb575ee036ab0b3f4511690e3379c115.tar.bz2
iced-f076649fbb575ee036ab0b3f4511690e3379c115.zip
Merge pull request #888 from Ace4896/picklist-placeholder
Add Placeholders to PickList
Diffstat (limited to '')
-rw-r--r--native/src/widget/pick_list.rs48
1 files changed, 35 insertions, 13 deletions
diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs
index 92c183f3..4f4e751e 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;
@@ -154,24 +162,34 @@ 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);
- 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(String::as_str)
+ .map(measure)
+ .unwrap_or(100);
+
+ labels_width.max(placeholder_width)
}
_ => 0,
};
@@ -195,6 +213,8 @@ where
match self.width {
Length::Shrink => {
+ self.placeholder.hash(state);
+
self.options
.iter()
.map(ToString::to_string)
@@ -265,6 +285,7 @@ where
layout.bounds(),
cursor_position,
self.selected.as_ref().map(ToString::to_string),
+ self.placeholder.as_ref().map(String::as_str),
self.padding,
self.text_size.unwrap_or(renderer.default_size()),
self.font,
@@ -325,6 +346,7 @@ pub trait Renderer: text::Renderer + menu::Renderer {
bounds: Rectangle,
cursor_position: Point,
selected: Option<String>,
+ placeholder: Option<&str>,
padding: Padding,
text_size: u16,
font: Self::Font,