From 73b8ae8e5e7f57c608c775272a2980995ab22bb3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 10 Jul 2020 02:50:47 +0200 Subject: Rename `ComboBox` to `PickList` --- native/src/widget.rs | 6 +- native/src/widget/combo_box.rs | 320 ----------------------------------------- native/src/widget/pick_list.rs | 320 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 323 insertions(+), 323 deletions(-) delete mode 100644 native/src/widget/combo_box.rs create mode 100644 native/src/widget/pick_list.rs (limited to 'native/src') diff --git a/native/src/widget.rs b/native/src/widget.rs index 931b4739..8539e519 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -23,10 +23,10 @@ pub mod button; pub mod checkbox; pub mod column; -pub mod combo_box; pub mod container; pub mod image; pub mod pane_grid; +pub mod pick_list; pub mod progress_bar; pub mod radio; pub mod row; @@ -44,14 +44,14 @@ pub use checkbox::Checkbox; #[doc(no_inline)] pub use column::Column; #[doc(no_inline)] -pub use combo_box::ComboBox; -#[doc(no_inline)] pub use container::Container; #[doc(no_inline)] pub use image::Image; #[doc(no_inline)] pub use pane_grid::PaneGrid; #[doc(no_inline)] +pub use pick_list::PickList; +#[doc(no_inline)] pub use progress_bar::ProgressBar; #[doc(no_inline)] pub use radio::Radio; diff --git a/native/src/widget/combo_box.rs b/native/src/widget/combo_box.rs deleted file mode 100644 index fefaf8ff..00000000 --- a/native/src/widget/combo_box.rs +++ /dev/null @@ -1,320 +0,0 @@ -//! Display a dropdown list of selectable values. -use crate::{ - layout, mouse, overlay, - overlay::menu::{self, Menu}, - scrollable, text, Clipboard, Element, Event, Hasher, Layout, Length, Point, - Rectangle, Size, Widget, -}; -use std::borrow::Cow; - -/// A widget for selecting a single value from a list of options. -#[allow(missing_debug_implementations)] -pub struct ComboBox<'a, T, Message, Renderer: self::Renderer> -where - [T]: ToOwned>, -{ - menu: &'a mut menu::State, - on_selected: Box Message>, - options: Cow<'a, [T]>, - selected: Option, - width: Length, - padding: u16, - text_size: Option, - font: Renderer::Font, - style: ::Style, - is_open: bool, -} - -/// The local state of a [`ComboBox`]. -/// -/// [`ComboBox`]: struct.ComboBox.html -#[derive(Debug, Clone, Default)] -pub struct State { - menu: menu::State, -} - -impl<'a, T: 'a, Message, Renderer: self::Renderer> - ComboBox<'a, T, Message, Renderer> -where - T: ToString, - [T]: ToOwned>, -{ - /// Creates a new [`ComboBox`] with the given [`State`], a list of options, - /// the current selected value, and the message to produce when an option is - /// selected. - /// - /// [`ComboBox`]: struct.ComboBox.html - /// [`State`]: struct.State.html - pub fn new( - state: &'a mut State, - options: impl Into>, - selected: Option, - on_selected: impl Fn(T) -> Message + 'static, - ) -> Self { - let is_open = state.menu.is_open(); - - Self { - menu: &mut state.menu, - on_selected: Box::new(on_selected), - options: options.into(), - selected, - width: Length::Shrink, - text_size: None, - padding: Renderer::DEFAULT_PADDING, - font: Default::default(), - style: Default::default(), - is_open, - } - } - - /// Sets the width of the [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the padding of the [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - pub fn padding(mut self, padding: u16) -> Self { - self.padding = padding; - self - } - - /// Sets the text size of the [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - pub fn text_size(mut self, size: u16) -> Self { - self.text_size = Some(size); - self - } - - /// Sets the font of the [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - pub fn font(mut self, font: Renderer::Font) -> Self { - self.font = font; - self - } - - /// Sets the style of the [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - pub fn style( - mut self, - style: impl Into<::Style>, - ) -> Self { - self.style = style.into(); - self - } -} - -impl<'a, T: 'a, Message, Renderer> Widget - for ComboBox<'a, T, Message, Renderer> -where - T: Clone + ToString + Eq, - [T]: ToOwned>, - Message: 'static, - Renderer: self::Renderer + scrollable::Renderer + 'a, -{ - fn width(&self) -> Length { - Length::Shrink - } - - 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) - .pad(f32::from(self.padding)); - - let text_size = self.text_size.unwrap_or(renderer.default_size()); - - let max_width = match self.width { - Length::Shrink => { - let labels = self.options.iter().map(ToString::to_string); - - labels - .map(|label| { - let (width, _) = renderer.measure( - &label, - text_size, - Renderer::Font::default(), - Size::new(f32::INFINITY, f32::INFINITY), - ); - - width.round() as u32 - }) - .max() - .unwrap_or(100) - } - _ => 0, - }; - - let size = { - let intrinsic = Size::new( - max_width as f32 - + f32::from(text_size) - + f32::from(self.padding), - f32::from(text_size), - ); - - limits.resolve(intrinsic).pad(f32::from(self.padding)) - }; - - layout::Node::new(size) - } - - fn hash_layout(&self, state: &mut Hasher) { - use std::hash::Hash as _; - - match self.width { - Length::Shrink => { - 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, - _messages: &mut Vec, - _renderer: &Renderer, - _clipboard: Option<&dyn Clipboard>, - ) { - if !self.is_open { - match event { - Event::Mouse(mouse::Event::ButtonPressed( - mouse::Button::Left, - )) => { - if layout.bounds().contains(cursor_position) { - let selected = self.selected.as_ref(); - - self.menu.open( - self.options - .iter() - .position(|option| Some(option) == selected), - ); - } - } - _ => {} - } - } - } - - fn draw( - &self, - renderer: &mut Renderer, - _defaults: &Renderer::Defaults, - layout: Layout<'_>, - cursor_position: Point, - ) -> Renderer::Output { - self::Renderer::draw( - renderer, - layout.bounds(), - cursor_position, - self.selected.as_ref().map(ToString::to_string), - self.padding, - self.text_size.unwrap_or(renderer.default_size()), - self.font, - &self.style, - ) - } - - fn overlay( - &mut self, - layout: Layout<'_>, - ) -> Option> { - if self.menu.is_open() { - let bounds = layout.bounds(); - - let mut menu = - Menu::new(&mut self.menu, &self.options, &self.on_selected) - .width(bounds.width.round() as u16) - .padding(self.padding) - .font(self.font) - .style(Renderer::menu_style(&self.style)); - - if let Some(text_size) = self.text_size { - menu = menu.text_size(text_size); - } - - Some(menu.overlay(layout.position(), bounds.height)) - } else { - None - } - } -} - -/// The renderer of a [`ComboBox`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`ComboBox`] in your user interface. -/// -/// [`ComboBox`]: struct.ComboBox.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer: text::Renderer + menu::Renderer { - /// The default padding of a [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - const DEFAULT_PADDING: u16; - - /// The [`ComboBox`] style supported by this renderer. - /// - /// [`ComboBox`]: struct.ComboBox.html - type Style: Default; - - /// Returns the style of the [`Menu`] of the [`ComboBox`]. - /// - /// [`Menu`]: ../../overlay/menu/struct.Menu.html - /// [`ComboBox`]: struct.ComboBox.html - fn menu_style( - style: &::Style, - ) -> ::Style; - - /// Draws a [`ComboBox`]. - /// - /// [`ComboBox`]: struct.ComboBox.html - fn draw( - &mut self, - bounds: Rectangle, - cursor_position: Point, - selected: Option, - padding: u16, - text_size: u16, - font: Self::Font, - style: &::Style, - ) -> Self::Output; -} - -impl<'a, T: 'a, Message, Renderer> Into> - for ComboBox<'a, T, Message, Renderer> -where - T: Clone + ToString + Eq, - [T]: ToOwned>, - Renderer: self::Renderer + 'a, - Message: 'static, -{ - fn into(self) -> Element<'a, Message, Renderer> { - Element::new(self) - } -} diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs new file mode 100644 index 00000000..9f62e550 --- /dev/null +++ b/native/src/widget/pick_list.rs @@ -0,0 +1,320 @@ +//! Display a dropdown list of selectable values. +use crate::{ + layout, mouse, overlay, + overlay::menu::{self, Menu}, + scrollable, text, Clipboard, Element, Event, Hasher, Layout, Length, Point, + Rectangle, Size, Widget, +}; +use std::borrow::Cow; + +/// A widget for selecting a single value from a list of options. +#[allow(missing_debug_implementations)] +pub struct PickList<'a, T, Message, Renderer: self::Renderer> +where + [T]: ToOwned>, +{ + menu: &'a mut menu::State, + on_selected: Box Message>, + options: Cow<'a, [T]>, + selected: Option, + width: Length, + padding: u16, + text_size: Option, + font: Renderer::Font, + style: ::Style, + is_open: bool, +} + +/// The local state of a [`PickList`]. +/// +/// [`PickList`]: struct.PickList.html +#[derive(Debug, Clone, Default)] +pub struct State { + menu: menu::State, +} + +impl<'a, T: 'a, Message, Renderer: self::Renderer> + PickList<'a, T, Message, Renderer> +where + T: ToString, + [T]: ToOwned>, +{ + /// 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. + /// + /// [`PickList`]: struct.PickList.html + /// [`State`]: struct.State.html + pub fn new( + state: &'a mut State, + options: impl Into>, + selected: Option, + on_selected: impl Fn(T) -> Message + 'static, + ) -> Self { + let is_open = state.menu.is_open(); + + Self { + menu: &mut state.menu, + on_selected: Box::new(on_selected), + options: options.into(), + selected, + width: Length::Shrink, + text_size: None, + padding: Renderer::DEFAULT_PADDING, + font: Default::default(), + style: Default::default(), + is_open, + } + } + + /// Sets the width of the [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the padding of the [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + pub fn padding(mut self, padding: u16) -> Self { + self.padding = padding; + self + } + + /// Sets the text size of the [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + pub fn text_size(mut self, size: u16) -> Self { + self.text_size = Some(size); + self + } + + /// Sets the font of the [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + pub fn font(mut self, font: Renderer::Font) -> Self { + self.font = font; + self + } + + /// Sets the style of the [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + pub fn style( + mut self, + style: impl Into<::Style>, + ) -> Self { + self.style = style.into(); + self + } +} + +impl<'a, T: 'a, Message, Renderer> Widget + for PickList<'a, T, Message, Renderer> +where + T: Clone + ToString + Eq, + [T]: ToOwned>, + Message: 'static, + Renderer: self::Renderer + scrollable::Renderer + 'a, +{ + fn width(&self) -> Length { + Length::Shrink + } + + 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) + .pad(f32::from(self.padding)); + + let text_size = self.text_size.unwrap_or(renderer.default_size()); + + let max_width = match self.width { + Length::Shrink => { + let labels = self.options.iter().map(ToString::to_string); + + labels + .map(|label| { + let (width, _) = renderer.measure( + &label, + text_size, + Renderer::Font::default(), + Size::new(f32::INFINITY, f32::INFINITY), + ); + + width.round() as u32 + }) + .max() + .unwrap_or(100) + } + _ => 0, + }; + + let size = { + let intrinsic = Size::new( + max_width as f32 + + f32::from(text_size) + + f32::from(self.padding), + f32::from(text_size), + ); + + limits.resolve(intrinsic).pad(f32::from(self.padding)) + }; + + layout::Node::new(size) + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::hash::Hash as _; + + match self.width { + Length::Shrink => { + 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, + _messages: &mut Vec, + _renderer: &Renderer, + _clipboard: Option<&dyn Clipboard>, + ) { + if !self.is_open { + match event { + Event::Mouse(mouse::Event::ButtonPressed( + mouse::Button::Left, + )) => { + if layout.bounds().contains(cursor_position) { + let selected = self.selected.as_ref(); + + self.menu.open( + self.options + .iter() + .position(|option| Some(option) == selected), + ); + } + } + _ => {} + } + } + } + + fn draw( + &self, + renderer: &mut Renderer, + _defaults: &Renderer::Defaults, + layout: Layout<'_>, + cursor_position: Point, + ) -> Renderer::Output { + self::Renderer::draw( + renderer, + layout.bounds(), + cursor_position, + self.selected.as_ref().map(ToString::to_string), + self.padding, + self.text_size.unwrap_or(renderer.default_size()), + self.font, + &self.style, + ) + } + + fn overlay( + &mut self, + layout: Layout<'_>, + ) -> Option> { + if self.menu.is_open() { + let bounds = layout.bounds(); + + let mut menu = + Menu::new(&mut self.menu, &self.options, &self.on_selected) + .width(bounds.width.round() as u16) + .padding(self.padding) + .font(self.font) + .style(Renderer::menu_style(&self.style)); + + if let Some(text_size) = self.text_size { + menu = menu.text_size(text_size); + } + + Some(menu.overlay(layout.position(), bounds.height)) + } else { + None + } + } +} + +/// The renderer of a [`PickList`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`PickList`] in your user interface. +/// +/// [`PickList`]: struct.PickList.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer: text::Renderer + menu::Renderer { + /// The default padding of a [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + const DEFAULT_PADDING: u16; + + /// The [`PickList`] style supported by this renderer. + /// + /// [`PickList`]: struct.PickList.html + type Style: Default; + + /// Returns the style of the [`Menu`] of the [`PickList`]. + /// + /// [`Menu`]: ../../overlay/menu/struct.Menu.html + /// [`PickList`]: struct.PickList.html + fn menu_style( + style: &::Style, + ) -> ::Style; + + /// Draws a [`PickList`]. + /// + /// [`PickList`]: struct.PickList.html + fn draw( + &mut self, + bounds: Rectangle, + cursor_position: Point, + selected: Option, + padding: u16, + text_size: u16, + font: Self::Font, + style: &::Style, + ) -> Self::Output; +} + +impl<'a, T: 'a, Message, Renderer> Into> + for PickList<'a, T, Message, Renderer> +where + T: Clone + ToString + Eq, + [T]: ToOwned>, + Renderer: self::Renderer + 'a, + Message: 'static, +{ + fn into(self) -> Element<'a, Message, Renderer> { + Element::new(self) + } +} -- cgit