diff options
| author | 2024-09-19 05:14:17 +0200 | |
|---|---|---|
| committer | 2024-09-19 05:14:17 +0200 | |
| commit | b778d5cd567679619809b05e672398d3141558fa (patch) | |
| tree | f70ebe5bd09f069fb9191ee5e022f0fb2dde4c6a /widget/src | |
| parent | 1595e78b1ad58e09dfa049546f6627dd5f80075b (diff) | |
| download | iced-b778d5cd567679619809b05e672398d3141558fa.tar.gz iced-b778d5cd567679619809b05e672398d3141558fa.tar.bz2 iced-b778d5cd567679619809b05e672398d3141558fa.zip  | |
Show `radio` doc example in multiple places
Diffstat (limited to '')
| -rw-r--r-- | widget/src/helpers.rs | 59 | ||||
| -rw-r--r-- | widget/src/radio.rs | 136 | 
2 files changed, 157 insertions, 38 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 4be5045a..1e9bafa7 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -832,7 +832,64 @@ where  /// Creates a new [`Radio`].  /// -/// [`Radio`]: crate::Radio +/// Radio buttons let users choose a single option from a bunch 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::{column, radio}; +/// +/// struct State { +///    selection: Option<Choice>, +/// } +/// +/// #[derive(Debug, Clone, Copy)] +/// enum Message { +///     RadioSelected(Choice), +/// } +/// +/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// enum Choice { +///     A, +///     B, +///     C, +///     All, +/// } +/// +/// fn view(state: &State) -> Element<'_, Message> { +///     let a = radio( +///         "A", +///         Choice::A, +///         state.selection, +///         Message::RadioSelected, +///     ); +/// +///     let b = radio( +///         "B", +///         Choice::B, +///         state.selection, +///         Message::RadioSelected, +///     ); +/// +///     let c = radio( +///         "C", +///         Choice::C, +///         state.selection, +///         Message::RadioSelected, +///     ); +/// +///     let all = radio( +///         "All of the above", +///         Choice::All, +///         state.selection, +///         Message::RadioSelected +///     ); +/// +///     column![a, b, c, all].into() +/// } +/// ```  pub fn radio<'a, Message, Theme, Renderer, V>(      label: impl Into<String>,      value: V, diff --git a/widget/src/radio.rs b/widget/src/radio.rs index cfa961f3..300318fd 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -1,4 +1,61 @@ -//! Create choices using radio buttons. +//! Radio buttons let users choose a single option from a bunch 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::{column, radio}; +//! +//! struct State { +//!    selection: Option<Choice>, +//! } +//! +//! #[derive(Debug, Clone, Copy)] +//! enum Message { +//!     RadioSelected(Choice), +//! } +//! +//! #[derive(Debug, Clone, Copy, PartialEq, Eq)] +//! enum Choice { +//!     A, +//!     B, +//!     C, +//!     All, +//! } +//! +//! fn view(state: &State) -> Element<'_, Message> { +//!     let a = radio( +//!         "A", +//!         Choice::A, +//!         state.selection, +//!         Message::RadioSelected, +//!     ); +//! +//!     let b = radio( +//!         "B", +//!         Choice::B, +//!         state.selection, +//!         Message::RadioSelected, +//!     ); +//! +//!     let c = radio( +//!         "C", +//!         Choice::C, +//!         state.selection, +//!         Message::RadioSelected, +//!     ); +//! +//!     let all = radio( +//!         "All of the above", +//!         Choice::All, +//!         state.selection, +//!         Message::RadioSelected +//!     ); +//! +//!     column![a, b, c, all].into() +//! } +//! ```  use crate::core::alignment;  use crate::core::border::{self, Border};  use crate::core::event::{self, Event}; @@ -18,54 +75,59 @@ use crate::core::{  ///  /// # Example  /// ```no_run -/// # type Radio<'a, Message> = -/// #     iced_widget::Radio<'a, Message, iced_widget::Theme, iced_widget::renderer::Renderer>; +/// # 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::column; -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// pub enum Choice { -///     A, -///     B, -///     C, -///     All, +/// use iced::widget::{column, radio}; +/// +/// struct State { +///    selection: Option<Choice>,  /// }  ///  /// #[derive(Debug, Clone, Copy)] -/// pub enum Message { +/// enum Message {  ///     RadioSelected(Choice),  /// }  /// -/// let selected_choice = Some(Choice::A); +/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// enum Choice { +///     A, +///     B, +///     C, +///     All, +/// }  /// -/// let a = Radio::new( -///     "A", -///     Choice::A, -///     selected_choice, -///     Message::RadioSelected, -/// ); +/// fn view(state: &State) -> Element<'_, Message> { +///     let a = radio( +///         "A", +///         Choice::A, +///         state.selection, +///         Message::RadioSelected, +///     );  /// -/// let b = Radio::new( -///     "B", -///     Choice::B, -///     selected_choice, -///     Message::RadioSelected, -/// ); +///     let b = radio( +///         "B", +///         Choice::B, +///         state.selection, +///         Message::RadioSelected, +///     );  /// -/// let c = Radio::new( -///     "C", -///     Choice::C, -///     selected_choice, -///     Message::RadioSelected, -/// ); +///     let c = radio( +///         "C", +///         Choice::C, +///         state.selection, +///         Message::RadioSelected, +///     );  /// -/// let all = Radio::new( -///     "All of the above", -///     Choice::All, -///     selected_choice, -///     Message::RadioSelected -/// ); +///     let all = radio( +///         "All of the above", +///         Choice::All, +///         state.selection, +///         Message::RadioSelected +///     );  /// -/// let content = column![a, b, c, all]; +///     column![a, b, c, all].into() +/// }  /// ```  #[allow(missing_debug_implementations)]  pub struct Radio<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>  | 
