Show pick_list doc example in multiple places
This commit is contained in:
parent
b78243d86f
commit
7b22b7e876
2 changed files with 185 additions and 2 deletions
|
|
@ -924,7 +924,68 @@ where
|
|||
|
||||
/// Creates a new [`PickList`].
|
||||
///
|
||||
/// [`PickList`]: crate::PickList
|
||||
/// Pick lists display a dropdown list of selectable options.
|
||||
///
|
||||
/// # Example
|
||||
/// ```no_run
|
||||
/// # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||
/// #
|
||||
/// use iced::widget::pick_list;
|
||||
///
|
||||
/// struct State {
|
||||
/// favorite: Option<Fruit>,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
/// enum Fruit {
|
||||
/// Apple,
|
||||
/// Orange,
|
||||
/// Strawberry,
|
||||
/// Tomato,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, Clone)]
|
||||
/// enum Message {
|
||||
/// FruitSelected(Fruit),
|
||||
/// }
|
||||
///
|
||||
/// fn view(state: &State) -> Element<'_, Message> {
|
||||
/// let fruits = [
|
||||
/// Fruit::Apple,
|
||||
/// Fruit::Orange,
|
||||
/// Fruit::Strawberry,
|
||||
/// Fruit::Tomato,
|
||||
/// ];
|
||||
///
|
||||
/// pick_list(
|
||||
/// fruits,
|
||||
/// state.favorite,
|
||||
/// Message::FruitSelected,
|
||||
/// )
|
||||
/// .placeholder("Select your favorite fruit...")
|
||||
/// .into()
|
||||
/// }
|
||||
///
|
||||
/// fn update(state: &mut State, message: Message) {
|
||||
/// match message {
|
||||
/// Message::FruitSelected(fruit) => {
|
||||
/// state.favorite = Some(fruit);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl std::fmt::Display for Fruit {
|
||||
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
/// f.write_str(match self {
|
||||
/// Self::Apple => "Apple",
|
||||
/// Self::Orange => "Orange",
|
||||
/// Self::Strawberry => "Strawberry",
|
||||
/// Self::Tomato => "Tomato",
|
||||
/// })
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn pick_list<'a, T, L, V, Message, Theme, Renderer>(
|
||||
options: L,
|
||||
selected: Option<V>,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,65 @@
|
|||
//! Display a dropdown list of selectable values.
|
||||
//! Pick lists display a dropdown list of selectable options.
|
||||
//!
|
||||
//! # Example
|
||||
//! ```no_run
|
||||
//! # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||
//! # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||
//! #
|
||||
//! use iced::widget::pick_list;
|
||||
//!
|
||||
//! struct State {
|
||||
//! favorite: Option<Fruit>,
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
//! enum Fruit {
|
||||
//! Apple,
|
||||
//! Orange,
|
||||
//! Strawberry,
|
||||
//! Tomato,
|
||||
//! }
|
||||
//!
|
||||
//! #[derive(Debug, Clone)]
|
||||
//! enum Message {
|
||||
//! FruitSelected(Fruit),
|
||||
//! }
|
||||
//!
|
||||
//! fn view(state: &State) -> Element<'_, Message> {
|
||||
//! let fruits = [
|
||||
//! Fruit::Apple,
|
||||
//! Fruit::Orange,
|
||||
//! Fruit::Strawberry,
|
||||
//! Fruit::Tomato,
|
||||
//! ];
|
||||
//!
|
||||
//! pick_list(
|
||||
//! fruits,
|
||||
//! state.favorite,
|
||||
//! Message::FruitSelected,
|
||||
//! )
|
||||
//! .placeholder("Select your favorite fruit...")
|
||||
//! .into()
|
||||
//! }
|
||||
//!
|
||||
//! fn update(state: &mut State, message: Message) {
|
||||
//! match message {
|
||||
//! Message::FruitSelected(fruit) => {
|
||||
//! state.favorite = Some(fruit);
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! impl std::fmt::Display for Fruit {
|
||||
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
//! f.write_str(match self {
|
||||
//! Self::Apple => "Apple",
|
||||
//! Self::Orange => "Orange",
|
||||
//! Self::Strawberry => "Strawberry",
|
||||
//! Self::Tomato => "Tomato",
|
||||
//! })
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
use crate::core::alignment;
|
||||
use crate::core::event::{self, Event};
|
||||
use crate::core::keyboard;
|
||||
|
|
@ -20,6 +81,67 @@ use std::borrow::Borrow;
|
|||
use std::f32;
|
||||
|
||||
/// A widget for selecting a single value from a list of options.
|
||||
///
|
||||
/// # Example
|
||||
/// ```no_run
|
||||
/// # mod iced { pub mod widget { pub use iced_widget::*; } pub use iced_widget::Renderer; pub use iced_widget::core::*; }
|
||||
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
|
||||
/// #
|
||||
/// use iced::widget::pick_list;
|
||||
///
|
||||
/// struct State {
|
||||
/// favorite: Option<Fruit>,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
/// enum Fruit {
|
||||
/// Apple,
|
||||
/// Orange,
|
||||
/// Strawberry,
|
||||
/// Tomato,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Debug, Clone)]
|
||||
/// enum Message {
|
||||
/// FruitSelected(Fruit),
|
||||
/// }
|
||||
///
|
||||
/// fn view(state: &State) -> Element<'_, Message> {
|
||||
/// let fruits = [
|
||||
/// Fruit::Apple,
|
||||
/// Fruit::Orange,
|
||||
/// Fruit::Strawberry,
|
||||
/// Fruit::Tomato,
|
||||
/// ];
|
||||
///
|
||||
/// pick_list(
|
||||
/// fruits,
|
||||
/// state.favorite,
|
||||
/// Message::FruitSelected,
|
||||
/// )
|
||||
/// .placeholder("Select your favorite fruit...")
|
||||
/// .into()
|
||||
/// }
|
||||
///
|
||||
/// fn update(state: &mut State, message: Message) {
|
||||
/// match message {
|
||||
/// Message::FruitSelected(fruit) => {
|
||||
/// state.favorite = Some(fruit);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// impl std::fmt::Display for Fruit {
|
||||
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
/// f.write_str(match self {
|
||||
/// Self::Apple => "Apple",
|
||||
/// Self::Orange => "Orange",
|
||||
/// Self::Strawberry => "Strawberry",
|
||||
/// Self::Tomato => "Tomato",
|
||||
/// })
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct PickList<
|
||||
'a,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue