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

105 lines
3.3 KiB
Rust
Raw Normal View History

//! Display a dropdown list of selectable values.
2020-04-18 14:42:48 +02:00
use crate::backend::{self, Backend};
use crate::{Primitive, Renderer};
use iced_native::{
2020-11-23 17:19:21 +00:00
mouse, Font, HorizontalAlignment, Padding, Point, Rectangle,
VerticalAlignment,
2020-04-18 14:42:48 +02:00
};
use iced_style::menu;
2020-04-18 14:42:48 +02:00
2020-07-10 02:50:47 +02:00
pub use iced_native::pick_list::State;
pub use iced_style::pick_list::{Style, StyleSheet};
/// A widget allowing the selection of a single value from a list of options.
2020-07-10 02:50:47 +02:00
pub type PickList<'a, T, Message, Backend> =
iced_native::PickList<'a, T, Message, Renderer<Backend>>;
2020-04-18 14:42:48 +02:00
2020-07-10 02:50:47 +02:00
impl<B> iced_native::pick_list::Renderer for Renderer<B>
2020-04-18 14:42:48 +02:00
where
B: Backend + backend::Text,
{
type Style = Box<dyn StyleSheet>;
2020-11-23 17:19:21 +00:00
const DEFAULT_PADDING: Padding = Padding::new(5);
2020-04-18 14:42:48 +02:00
fn menu_style(style: &Box<dyn StyleSheet>) -> menu::Style {
style.menu()
}
2020-04-18 14:42:48 +02:00
fn draw(
&mut self,
bounds: Rectangle,
cursor_position: Point,
selected: Option<String>,
placeholder: Option<&str>,
2020-11-23 17:19:21 +00:00
padding: Padding,
2020-07-08 07:04:20 +02:00
text_size: u16,
font: Font,
style: &Box<dyn StyleSheet>,
2020-04-18 14:42:48 +02:00
) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position);
let is_selected = selected.is_some();
2020-04-18 14:42:48 +02:00
let style = if is_mouse_over {
style.hovered()
} else {
style.active()
};
2020-04-18 14:42:48 +02:00
let background = Primitive::Quad {
bounds,
background: style.background,
border_color: style.border_color,
border_width: style.border_width,
border_radius: style.border_radius,
2020-04-18 14:42:48 +02:00
};
2020-04-18 19:53:27 +02:00
let arrow_down = Primitive::Text {
content: B::ARROW_DOWN_ICON.to_string(),
font: B::ICON_FONT,
size: bounds.height * style.icon_size,
2020-04-18 19:53:27 +02:00
bounds: Rectangle {
x: bounds.x + bounds.width - f32::from(padding.horizontal()),
2020-04-18 19:53:27 +02:00
y: bounds.center_y(),
..bounds
},
color: style.text_color,
2020-04-18 19:53:27 +02:00
horizontal_alignment: HorizontalAlignment::Right,
vertical_alignment: VerticalAlignment::Center,
};
2020-04-18 14:42:48 +02:00
(
2020-04-18 19:53:27 +02:00
Primitive::Group {
primitives: if let Some(label) =
selected.or_else(|| placeholder.map(str::to_string))
{
2020-04-18 19:53:27 +02:00
let label = Primitive::Text {
content: label,
size: f32::from(text_size),
2020-07-08 07:04:20 +02:00
font,
color: is_selected
.then(|| style.text_color)
.unwrap_or(style.placeholder_color),
2020-04-18 19:53:27 +02:00
bounds: Rectangle {
2020-11-23 17:19:21 +00:00
x: bounds.x + f32::from(padding.left),
2020-04-18 19:53:27 +02:00
y: bounds.center_y(),
..bounds
},
horizontal_alignment: HorizontalAlignment::Left,
vertical_alignment: VerticalAlignment::Center,
};
2020-04-18 14:42:48 +02:00
2020-04-18 19:53:27 +02:00
vec![background, label, arrow_down]
} else {
vec![background, arrow_down]
},
2020-04-18 14:42:48 +02:00
},
if is_mouse_over {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
},
)
}
}