From 257264de1c41435328ef685b8b66a71c7666764f Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Tue, 14 Mar 2023 14:34:32 -0600 Subject: Working on Radio example --- examples/radio/Cargo.toml | 10 ++++++++++ examples/radio/src/main.rs | 3 +++ 2 files changed, 13 insertions(+) create mode 100644 examples/radio/Cargo.toml create mode 100644 examples/radio/src/main.rs diff --git a/examples/radio/Cargo.toml b/examples/radio/Cargo.toml new file mode 100644 index 00000000..a8c7f351 --- /dev/null +++ b/examples/radio/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "radio" +version = "0.1.0" +authors = ["Aaron Honeycutt "] +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +iced = { path = "../.." } diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs new file mode 100644 index 00000000..e7a11a96 --- /dev/null +++ b/examples/radio/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} -- cgit From 6d9cf0b60198e7d6c9615f539dac4b06eada5851 Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Tue, 14 Mar 2023 14:36:31 -0600 Subject: Cleaning up from the Checkbox example that this is based on --- examples/radio/src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs index e7a11a96..3baccbe2 100644 --- a/examples/radio/src/main.rs +++ b/examples/radio/src/main.rs @@ -1,3 +1,58 @@ -fn main() { - println!("Hello, world!"); +use iced::widget::{column, container, Radio}; +use iced::{Element, Length, Sandbox, Settings}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +#[derive(Default)] +struct Example { + default_checkbox: bool, + custom_checkbox: bool, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + DefaultChecked(bool), + CustomChecked(bool), +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Default::default() + } + + fn title(&self) -> String { + String::from("Checkbox - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::DefaultChecked(value) => self.default_checkbox = value, + Message::CustomChecked(value) => self.custom_checkbox = value, + } + } + + fn view(&self) -> Element { + let default_checkbox = + checkbox("Default", self.default_checkbox, Message::DefaultChecked); + let custom_checkbox = + checkbox("Custom", self.custom_checkbox, Message::CustomChecked) + .icon(checkbox::Icon { + font: ICON_FONT, + code_point: '\u{e901}', + size: None, + }); + + let content = column![default_checkbox, custom_checkbox].spacing(22); + + container(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } } -- cgit From c96ab27b24a5775aeef7d8a0ed31214a5cb3f0a7 Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Tue, 14 Mar 2023 14:39:26 -0600 Subject: Work on example more --- examples/radio/src/main.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs index 3baccbe2..424fdf42 100644 --- a/examples/radio/src/main.rs +++ b/examples/radio/src/main.rs @@ -25,7 +25,7 @@ impl Sandbox for Example { } fn title(&self) -> String { - String::from("Checkbox - Iced") + String::from("Radio - Iced") } fn update(&mut self, message: Message) { @@ -36,17 +36,12 @@ impl Sandbox for Example { } fn view(&self) -> Element { - let default_checkbox = - checkbox("Default", self.default_checkbox, Message::DefaultChecked); - let custom_checkbox = - checkbox("Custom", self.custom_checkbox, Message::CustomChecked) - .icon(checkbox::Icon { - font: ICON_FONT, - code_point: '\u{e901}', - size: None, - }); - - let content = column![default_checkbox, custom_checkbox].spacing(22); + let selected_choice = Some(Choice::A); + + Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); + Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); + + let content = column![selected_choice].spacing(22); container(content) .width(Length::Fill) -- cgit From addc443f8ddb688248cc1d34e55ee7beed42195c Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Tue, 14 Mar 2023 15:46:04 -0600 Subject: Working more on example --- examples/radio/src/main.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs index 424fdf42..8881f059 100644 --- a/examples/radio/src/main.rs +++ b/examples/radio/src/main.rs @@ -7,14 +7,12 @@ pub fn main() -> iced::Result { #[derive(Default)] struct Example { - default_checkbox: bool, - custom_checkbox: bool, + selected_radio: Option, } #[derive(Debug, Clone, Copy)] enum Message { - DefaultChecked(bool), - CustomChecked(bool), + RadioSelected(Choice), } impl Sandbox for Example { @@ -30,18 +28,19 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { - Message::DefaultChecked(value) => self.default_checkbox = value, - Message::CustomChecked(value) => self.custom_checkbox = value, + Message::RadioSelected(value) => { + self.selected_radio = Some(choice); + } } } fn view(&self) -> Element { - let selected_choice = Some(Choice::A); + let selected_radio = Some(Choice::A); - Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); - Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); + Radio::new(Choice::A, "This is A", selected_radio, Message::RadioSelected); + Radio::new(Choice::B, "This is B", selected_radio, Message::RadioSelected); - let content = column![selected_choice].spacing(22); + let content = column![selected_radio].spacing(22); container(content) .width(Length::Fill) @@ -51,3 +50,30 @@ impl Sandbox for Example { .into() } } + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Choice { + #[default] + A, + B, +} + +impl Choice { + const ALL: [Choice; 2] = [ + Choice::A, + Choice::B, + ]; +} + +impl std::fmt::Display for Choice { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Choice::A => "A", + Choice::B => "B", + } + ) + } +} -- cgit From d5f26c3d39079e5a1d8aa4a18b9fb04e8834022f Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Fri, 17 Mar 2023 13:12:33 -0600 Subject: More work on example --- examples/radio/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs index 8881f059..73b03610 100644 --- a/examples/radio/src/main.rs +++ b/examples/radio/src/main.rs @@ -1,4 +1,4 @@ -use iced::widget::{column, container, Radio}; +use iced::widget::{column, container, radio}; use iced::{Element, Length, Sandbox, Settings}; pub fn main() -> iced::Result { @@ -37,10 +37,10 @@ impl Sandbox for Example { fn view(&self) -> Element { let selected_radio = Some(Choice::A); - Radio::new(Choice::A, "This is A", selected_radio, Message::RadioSelected); - Radio::new(Choice::B, "This is B", selected_radio, Message::RadioSelected); - - let content = column![selected_radio].spacing(22); + let content = column![ + Radio::new(Choice::A, "This is A", selected_radio, Message::RadioSelected), + Radio::new(Choice::B, "This is B", selected_radio, Message::RadioSelected), + ]; container(content) .width(Length::Fill) -- cgit From 4fdd76c07c15f85a518c240aca0e55f482b18bc3 Mon Sep 17 00:00:00 2001 From: Aaron Honeycutt Date: Fri, 17 Mar 2023 13:32:11 -0600 Subject: Now is a working example --- examples/radio/src/main.rs | 65 +++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs index 73b03610..3b19924e 100644 --- a/examples/radio/src/main.rs +++ b/examples/radio/src/main.rs @@ -7,7 +7,7 @@ pub fn main() -> iced::Result { #[derive(Default)] struct Example { - selected_radio: Option, + radio: Option, } #[derive(Debug, Clone, Copy)] @@ -29,18 +29,44 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { Message::RadioSelected(value) => { - self.selected_radio = Some(choice); + self.radio = Some(value); } } } fn view(&self) -> Element { - let selected_radio = Some(Choice::A); + let a_checkbox = radio( + "A", + Choice::A, + self.radio, + Message::RadioSelected, + ); + + let b_checkbox = radio( + "B", + Choice::B, + self.radio, + Message::RadioSelected, + ); + + let c_checkbox = radio( + "C", + Choice::C, + self.radio, + Message::RadioSelected, + ); + + let all_checkbox = radio("All of the above", Choice::All, self.radio, Message::RadioSelected); let content = column![ - Radio::new(Choice::A, "This is A", selected_radio, Message::RadioSelected), - Radio::new(Choice::B, "This is B", selected_radio, Message::RadioSelected), - ]; + a_checkbox, + b_checkbox, + c_checkbox, + all_checkbox, + ] + .spacing(20) + .padding(20) + .max_width(600); container(content) .width(Length::Fill) @@ -51,29 +77,10 @@ impl Sandbox for Example { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub enum Choice { - #[default] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Choice { A, B, -} - -impl Choice { - const ALL: [Choice; 2] = [ - Choice::A, - Choice::B, - ]; -} - -impl std::fmt::Display for Choice { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Choice::A => "A", - Choice::B => "B", - } - ) - } + C, + All, } -- cgit From b505b7203563b8a75a65451f47a3386c50864e6d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Apr 2023 06:43:58 +0200 Subject: Move `radio` example to `Radio` docs --- examples/radio/Cargo.toml | 10 ------ examples/radio/src/main.rs | 86 -------------------------------------------- native/src/widget/helpers.rs | 2 +- native/src/widget/radio.rs | 37 ++++++++++++++++--- 4 files changed, 33 insertions(+), 102 deletions(-) delete mode 100644 examples/radio/Cargo.toml delete mode 100644 examples/radio/src/main.rs diff --git a/examples/radio/Cargo.toml b/examples/radio/Cargo.toml deleted file mode 100644 index a8c7f351..00000000 --- a/examples/radio/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "radio" -version = "0.1.0" -authors = ["Aaron Honeycutt "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -iced = { path = "../.." } diff --git a/examples/radio/src/main.rs b/examples/radio/src/main.rs deleted file mode 100644 index 3b19924e..00000000 --- a/examples/radio/src/main.rs +++ /dev/null @@ -1,86 +0,0 @@ -use iced::widget::{column, container, radio}; -use iced::{Element, Length, Sandbox, Settings}; - -pub fn main() -> iced::Result { - Example::run(Settings::default()) -} - -#[derive(Default)] -struct Example { - radio: Option, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - RadioSelected(Choice), -} - -impl Sandbox for Example { - type Message = Message; - - fn new() -> Self { - Default::default() - } - - fn title(&self) -> String { - String::from("Radio - Iced") - } - - fn update(&mut self, message: Message) { - match message { - Message::RadioSelected(value) => { - self.radio = Some(value); - } - } - } - - fn view(&self) -> Element { - let a_checkbox = radio( - "A", - Choice::A, - self.radio, - Message::RadioSelected, - ); - - let b_checkbox = radio( - "B", - Choice::B, - self.radio, - Message::RadioSelected, - ); - - let c_checkbox = radio( - "C", - Choice::C, - self.radio, - Message::RadioSelected, - ); - - let all_checkbox = radio("All of the above", Choice::All, self.radio, Message::RadioSelected); - - let content = column![ - a_checkbox, - b_checkbox, - c_checkbox, - all_checkbox, - ] - .spacing(20) - .padding(20) - .max_width(600); - - container(content) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() - .into() - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Choice { - A, - B, - C, - All, -} diff --git a/native/src/widget/helpers.rs b/native/src/widget/helpers.rs index d13eca75..24853eaa 100644 --- a/native/src/widget/helpers.rs +++ b/native/src/widget/helpers.rs @@ -147,7 +147,7 @@ where Renderer::Theme: widget::radio::StyleSheet, V: Copy + Eq, { - widget::Radio::new(value, label, selected, on_click) + widget::Radio::new(label, value, selected, on_click) } /// Creates a new [`Toggler`]. diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 9daddfbc..3ca041bf 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -21,10 +21,13 @@ pub use iced_style::radio::{Appearance, StyleSheet}; /// # type Radio = /// # iced_native::widget::Radio; /// # +/// # use iced_native::column; /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// pub enum Choice { /// A, /// B, +/// C, +/// All, /// } /// /// #[derive(Debug, Clone, Copy)] @@ -34,12 +37,36 @@ pub use iced_style::radio::{Appearance, StyleSheet}; /// /// let selected_choice = Some(Choice::A); /// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); +/// let a = Radio::new( +/// "A", +/// Choice::A, +/// selected_choice, +/// Message::RadioSelected, +/// ); /// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); -/// ``` +/// let b = Radio::new( +/// "B", +/// Choice::B, +/// selected_choice, +/// Message::RadioSelected, +/// ); +/// +/// let c = Radio::new( +/// "C", +/// Choice::C, +/// selected_choice, +/// Message::RadioSelected, +/// ); +/// +/// let all = Radio::new( +/// "All of the above", +/// Choice::All, +/// selected_choice, +/// Message::RadioSelected +/// ); /// -/// ![Radio buttons drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/radio.png?raw=true) +/// let content = column![a, b, c, all]; +/// ``` #[allow(missing_debug_implementations)] pub struct Radio where @@ -78,8 +105,8 @@ where /// * a function that will be called when the [`Radio`] is selected. It /// receives the value of the radio and must produce a `Message`. pub fn new( - value: V, label: impl Into, + value: V, selected: Option, f: F, ) -> Self -- cgit