Merge pull request #888 from Ace4896/picklist-placeholder

Add Placeholders to PickList
This commit is contained in:
Héctor Ramón 2021-07-22 20:32:43 +07:00 committed by GitHub
commit f076649fbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 18 deletions

View file

@ -171,6 +171,7 @@ impl pick_list::StyleSheet for PickList {
}, },
border_radius: 2.0, border_radius: 2.0,
icon_size: 0.5, icon_size: 0.5,
..pick_list::Style::default()
} }
} }

View file

@ -11,7 +11,7 @@ pub fn main() -> iced::Result {
struct Example { struct Example {
scroll: scrollable::State, scroll: scrollable::State,
pick_list: pick_list::State<Language>, pick_list: pick_list::State<Language>,
selected_language: Language, selected_language: Option<Language>,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -33,7 +33,7 @@ impl Sandbox for Example {
fn update(&mut self, message: Message) { fn update(&mut self, message: Message) {
match message { match message {
Message::LanguageSelected(language) => { Message::LanguageSelected(language) => {
self.selected_language = language; self.selected_language = Some(language);
} }
} }
} }
@ -42,9 +42,10 @@ impl Sandbox for Example {
let pick_list = PickList::new( let pick_list = PickList::new(
&mut self.pick_list, &mut self.pick_list,
&Language::ALL[..], &Language::ALL[..],
Some(self.selected_language), self.selected_language,
Message::LanguageSelected, Message::LanguageSelected,
); )
.placeholder("Choose a language...");
let mut content = Scrollable::new(&mut self.scroll) let mut content = Scrollable::new(&mut self.scroll)
.width(Length::Fill) .width(Length::Fill)

View file

@ -31,12 +31,14 @@ where
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
selected: Option<String>, selected: Option<String>,
placeholder: Option<&str>,
padding: Padding, padding: Padding,
text_size: u16, text_size: u16,
font: Font, font: Font,
style: &Box<dyn StyleSheet>, style: &Box<dyn StyleSheet>,
) -> Self::Output { ) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
let is_selected = selected.is_some();
let style = if is_mouse_over { let style = if is_mouse_over {
style.hovered() style.hovered()
@ -68,12 +70,16 @@ where
( (
Primitive::Group { Primitive::Group {
primitives: if let Some(label) = selected { primitives: if let Some(label) =
selected.or_else(|| placeholder.map(str::to_string))
{
let label = Primitive::Text { let label = Primitive::Text {
content: label, content: label,
size: f32::from(text_size), size: f32::from(text_size),
font, font,
color: style.text_color, color: is_selected
.then(|| style.text_color)
.unwrap_or(style.placeholder_color),
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + f32::from(padding.left), x: bounds.x + f32::from(padding.left),
y: bounds.center_y(), y: bounds.center_y(),

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;
@ -154,24 +162,34 @@ where
.pad(self.padding); .pad(self.padding);
let text_size = self.text_size.unwrap_or(renderer.default_size()); let text_size = self.text_size.unwrap_or(renderer.default_size());
let font = self.font;
let max_width = match self.width { let max_width = match self.width {
Length::Shrink => { 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 = self.options.iter().map(ToString::to_string);
labels let labels_width =
.map(|label| { labels.map(|label| measure(&label)).max().unwrap_or(100);
let (width, _) = renderer.measure(
&label,
text_size,
self.font,
Size::new(f32::INFINITY, f32::INFINITY),
);
width.round() as u32 let placeholder_width = self
}) .placeholder
.max() .as_ref()
.unwrap_or(100) .map(String::as_str)
.map(measure)
.unwrap_or(100);
labels_width.max(placeholder_width)
} }
_ => 0, _ => 0,
}; };
@ -195,6 +213,8 @@ where
match self.width { match self.width {
Length::Shrink => { Length::Shrink => {
self.placeholder.hash(state);
self.options self.options
.iter() .iter()
.map(ToString::to_string) .map(ToString::to_string)
@ -265,6 +285,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.as_ref().map(String::as_str),
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 +346,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<&str>,
padding: Padding, padding: Padding,
text_size: u16, text_size: u16,
font: Self::Font, font: Self::Font,

View file

@ -5,6 +5,7 @@ use iced_core::{Background, Color};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Style { pub struct Style {
pub text_color: Color, pub text_color: Color,
pub placeholder_color: Color,
pub background: Background, pub background: Background,
pub border_radius: f32, pub border_radius: f32,
pub border_width: f32, pub border_width: f32,
@ -16,6 +17,7 @@ impl std::default::Default for Style {
fn default() -> Self { fn default() -> Self {
Self { Self {
text_color: Color::BLACK, text_color: Color::BLACK,
placeholder_color: [0.4, 0.4, 0.4].into(),
background: Background::Color([0.87, 0.87, 0.87].into()), background: Background::Color([0.87, 0.87, 0.87].into()),
border_radius: 0.0, border_radius: 0.0,
border_width: 1.0, border_width: 1.0,