iced-yoda/native/src/widget/pick_list.rs

454 lines
13 KiB
Rust
Raw Normal View History

//! Display a dropdown list of selectable values.
use crate::alignment;
use crate::event::{self, Event};
use crate::keyboard;
use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::overlay::menu::{self, Menu};
use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
2020-04-18 14:42:48 +02:00
use crate::{
2020-11-23 17:19:21 +00:00
Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
Size, Widget,
2020-04-18 14:42:48 +02:00
};
use std::borrow::Cow;
pub use iced_style::pick_list::{Style, StyleSheet};
/// A widget for selecting a single value from a list of options.
#[allow(missing_debug_implementations)]
pub struct PickList<'a, T, Message, Renderer: text::Renderer>
2020-04-18 14:42:48 +02:00
where
[T]: ToOwned<Owned = Vec<T>>,
{
menu: &'a mut menu::State,
keyboard_modifiers: &'a mut keyboard::Modifiers,
is_open: &'a mut bool,
hovered_option: &'a mut Option<usize>,
last_selection: &'a mut Option<T>,
on_selected: Box<dyn Fn(T) -> Message>,
2020-04-18 14:42:48 +02:00
options: Cow<'a, [T]>,
placeholder: Option<String>,
2020-04-18 14:42:48 +02:00
selected: Option<T>,
width: Length,
2020-11-23 17:19:21 +00:00
padding: Padding,
2020-04-18 14:42:48 +02:00
text_size: Option<u16>,
2020-07-08 07:04:20 +02:00
font: Renderer::Font,
style_sheet: Box<dyn StyleSheet + 'a>,
2020-04-18 14:42:48 +02:00
}
2020-07-10 02:50:47 +02:00
/// The local state of a [`PickList`].
#[derive(Debug, Clone)]
pub struct State<T> {
2020-04-18 14:42:48 +02:00
menu: menu::State,
keyboard_modifiers: keyboard::Modifiers,
is_open: bool,
hovered_option: Option<usize>,
last_selection: Option<T>,
2020-04-18 14:42:48 +02:00
}
impl<T> Default for State<T> {
fn default() -> Self {
Self {
menu: menu::State::default(),
keyboard_modifiers: keyboard::Modifiers::default(),
is_open: bool::default(),
hovered_option: Option::default(),
last_selection: Option::default(),
}
}
}
impl<'a, T: 'a, Message, Renderer: text::Renderer>
2020-07-10 02:50:47 +02:00
PickList<'a, T, Message, Renderer>
2020-04-18 14:42:48 +02:00
where
T: ToString + Eq,
2020-04-18 14:42:48 +02:00
[T]: ToOwned<Owned = Vec<T>>,
{
/// The default padding of a [`PickList`].
pub const DEFAULT_PADDING: Padding = Padding::new(5);
2020-07-10 02:50:47 +02:00
/// Creates a new [`PickList`] with the given [`State`], a list of options,
/// the current selected value, and the message to produce when an option is
/// selected.
2020-04-18 14:42:48 +02:00
pub fn new(
state: &'a mut State<T>,
2020-04-18 14:42:48 +02:00
options: impl Into<Cow<'a, [T]>>,
selected: Option<T>,
on_selected: impl Fn(T) -> Message + 'static,
) -> Self {
let State {
menu,
keyboard_modifiers,
is_open,
hovered_option,
last_selection,
} = state;
2020-07-08 08:25:56 +02:00
2020-04-18 14:42:48 +02:00
Self {
menu,
keyboard_modifiers,
is_open,
hovered_option,
last_selection,
on_selected: Box::new(on_selected),
2020-04-18 14:42:48 +02:00
options: options.into(),
placeholder: None,
2020-04-18 14:42:48 +02:00
selected,
width: Length::Shrink,
text_size: None,
padding: Self::DEFAULT_PADDING,
2020-07-08 07:04:20 +02:00
font: Default::default(),
style_sheet: Default::default(),
2020-04-18 14:42:48 +02:00
}
}
/// Sets the placeholder of the [`PickList`].
pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = Some(placeholder.into());
self
}
2020-07-10 02:50:47 +02:00
/// Sets the width of the [`PickList`].
2020-04-18 14:42:48 +02:00
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
2020-11-23 17:19:21 +00:00
/// Sets the [`Padding`] of the [`PickList`].
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.padding = padding.into();
2020-04-18 14:42:48 +02:00
self
}
2020-07-10 02:50:47 +02:00
/// Sets the text size of the [`PickList`].
2020-04-18 14:42:48 +02:00
pub fn text_size(mut self, size: u16) -> Self {
self.text_size = Some(size);
self
}
2020-07-10 02:50:47 +02:00
/// Sets the font of the [`PickList`].
2020-07-08 07:04:20 +02:00
pub fn font(mut self, font: Renderer::Font) -> Self {
self.font = font;
self
}
2020-07-10 02:50:47 +02:00
/// Sets the style of the [`PickList`].
pub fn style(
mut self,
style_sheet: impl Into<Box<dyn StyleSheet + 'a>>,
) -> Self {
self.style_sheet = style_sheet.into();
self
}
2020-04-18 14:42:48 +02:00
}
impl<'a, T: 'a, Message, Renderer> Widget<Message, Renderer>
2020-07-10 02:50:47 +02:00
for PickList<'a, T, Message, Renderer>
2020-04-18 14:42:48 +02:00
where
T: Clone + ToString + Eq,
[T]: ToOwned<Owned = Vec<T>>,
Message: 'static,
Renderer: text::Renderer + 'a,
2020-04-18 14:42:48 +02:00
{
fn width(&self) -> Length {
self.width
2020-04-18 14:42:48 +02:00
}
fn height(&self) -> Length {
Length::Shrink
}
fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
use std::f32;
let limits = limits
.width(self.width)
.height(Length::Shrink)
2020-11-23 17:19:21 +00:00
.pad(self.padding);
2020-04-18 14:42:48 +02:00
let text_size = self.text_size.unwrap_or(renderer.default_size());
let font = self.font;
2020-04-18 14:42:48 +02:00
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
};
2020-04-18 14:42:48 +02:00
let labels = self.options.iter().map(ToString::to_string);
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)
2020-04-18 14:42:48 +02:00
}
_ => 0,
};
let size = {
let intrinsic = Size::new(
2020-04-18 19:53:27 +02:00
max_width as f32
+ f32::from(text_size)
2020-11-23 17:19:21 +00:00
+ f32::from(self.padding.left),
2020-04-18 14:42:48 +02:00
f32::from(text_size),
);
2020-11-23 17:19:21 +00:00
limits.resolve(intrinsic).pad(self.padding)
2020-04-18 14:42:48 +02:00
};
layout::Node::new(size)
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash as _;
match self.width {
Length::Shrink => {
self.placeholder.hash(state);
2020-04-18 14:42:48 +02:00
self.options
.iter()
.map(ToString::to_string)
.for_each(|label| label.hash(state));
}
_ => {
self.width.hash(state);
}
}
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
_renderer: &Renderer,
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let event_status = if *self.is_open {
// TODO: Encode cursor availability in the type system
*self.is_open =
cursor_position.x < 0.0 || cursor_position.y < 0.0;
event::Status::Captured
} else if layout.bounds().contains(cursor_position) {
let selected = self.selected.as_ref();
*self.is_open = true;
*self.hovered_option = self
.options
.iter()
.position(|option| Some(option) == selected);
event::Status::Captured
} else {
event::Status::Ignored
};
if let Some(last_selection) = self.last_selection.take() {
messages.push((self.on_selected)(last_selection));
*self.is_open = false;
event::Status::Captured
} else {
event_status
2020-04-18 14:42:48 +02:00
}
}
Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { y, .. },
}) if self.keyboard_modifiers.command()
&& layout.bounds().contains(cursor_position)
&& !*self.is_open =>
2021-05-10 14:29:35 -06:00
{
fn find_next<'a, T: PartialEq>(
selected: &'a T,
mut options: impl Iterator<Item = &'a T>,
) -> Option<&'a T> {
let _ = options.find(|&option| option == selected);
options.next()
}
let next_option = if y < 0.0 {
2021-05-10 14:29:35 -06:00
if let Some(selected) = self.selected.as_ref() {
find_next(selected, self.options.iter())
2021-05-10 14:29:35 -06:00
} else {
self.options.first()
2021-05-10 14:29:35 -06:00
}
} else if y > 0.0 {
2021-05-10 14:29:35 -06:00
if let Some(selected) = self.selected.as_ref() {
find_next(selected, self.options.iter().rev())
2021-05-10 14:29:35 -06:00
} else {
self.options.last()
2021-05-10 14:29:35 -06:00
}
} else {
None
};
if let Some(next_option) = next_option {
messages.push((self.on_selected)(next_option.clone()));
2021-05-10 14:29:35 -06:00
}
event::Status::Captured
2021-05-10 14:29:35 -06:00
}
Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
*self.keyboard_modifiers = modifiers;
2021-05-10 14:29:35 -06:00
event::Status::Ignored
}
_ => event::Status::Ignored,
2020-04-18 14:42:48 +02:00
}
}
fn mouse_interaction(
&self,
layout: Layout<'_>,
_viewport: &Rectangle,
cursor_position: Point,
) -> mouse::Interaction {
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
if is_mouse_over {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
}
}
2020-04-18 14:42:48 +02:00
fn draw(
&self,
renderer: &mut Renderer,
_style: &renderer::Style,
2020-04-18 14:42:48 +02:00
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) {
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
let is_selected = self.selected.is_some();
let style = if is_mouse_over {
self.style_sheet.hovered()
} else {
self.style_sheet.active()
};
renderer.fill_rectangle(renderer::Quad {
bounds,
background: style.background,
border_color: style.border_color,
border_width: style.border_width,
border_radius: style.border_radius,
});
renderer.fill_text(Text {
content: &Renderer::ARROW_DOWN_ICON.to_string(),
font: Renderer::ICON_FONT,
size: bounds.height * style.icon_size,
bounds: Rectangle {
x: bounds.x + bounds.width
- f32::from(self.padding.horizontal()),
y: bounds.center_y(),
..bounds
},
color: style.text_color,
horizontal_alignment: alignment::Horizontal::Right,
vertical_alignment: alignment::Vertical::Center,
});
if let Some(label) = self
.selected
.as_ref()
.map(ToString::to_string)
.as_ref()
.or_else(|| self.placeholder.as_ref())
{
renderer.fill_text(Text {
content: label,
size: f32::from(
self.text_size.unwrap_or(renderer.default_size()),
),
font: self.font,
color: is_selected
.then(|| style.text_color)
.unwrap_or(style.placeholder_color),
bounds: Rectangle {
x: bounds.x + f32::from(self.padding.left),
y: bounds.center_y(),
..bounds
},
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
})
}
2020-04-18 14:42:48 +02:00
}
fn overlay(
&mut self,
layout: Layout<'_>,
) -> Option<overlay::Element<'_, Message, Renderer>> {
if *self.is_open {
2020-07-05 05:44:10 +02:00
let bounds = layout.bounds();
let mut menu = Menu::new(
&mut self.menu,
&self.options,
&mut self.hovered_option,
&mut self.last_selection,
)
.width(bounds.width.round() as u16)
.padding(self.padding)
.font(self.font)
.style(self.style_sheet.menu());
if let Some(text_size) = self.text_size {
menu = menu.text_size(text_size);
}
Some(menu.overlay(layout.position(), bounds.height))
2020-04-18 14:42:48 +02:00
} else {
None
}
}
}
impl<'a, T: 'a, Message, Renderer> Into<Element<'a, Message, Renderer>>
2020-07-10 02:50:47 +02:00
for PickList<'a, T, Message, Renderer>
2020-04-18 14:42:48 +02:00
where
T: Clone + ToString + Eq,
[T]: ToOwned<Owned = Vec<T>>,
Renderer: text::Renderer + 'a,
2020-04-18 14:42:48 +02:00
Message: 'static,
{
fn into(self) -> Element<'a, Message, Renderer> {
Element::new(self)
}
}