diff options
author | 2023-10-02 20:18:15 +0200 | |
---|---|---|
committer | 2024-02-01 13:16:28 +0100 | |
commit | b5f1ca169567f47b3976de5b4c5a2b1ce37656d5 (patch) | |
tree | 326fae951f16d98ba6141e6cd20d6e7d02e8c71d /examples/checkbox | |
parent | b95f3ab12f863f4de668e4e6f8a75b7d3acdf2b7 (diff) | |
download | iced-b5f1ca169567f47b3976de5b4c5a2b1ce37656d5.tar.gz iced-b5f1ca169567f47b3976de5b4c5a2b1ce37656d5.tar.bz2 iced-b5f1ca169567f47b3976de5b4c5a2b1ce37656d5.zip |
Introduce support for disabling a `checkbox`
Diffstat (limited to 'examples/checkbox')
-rw-r--r-- | examples/checkbox/src/main.rs | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/examples/checkbox/src/main.rs b/examples/checkbox/src/main.rs index ef1a054d..314cb3fe 100644 --- a/examples/checkbox/src/main.rs +++ b/examples/checkbox/src/main.rs @@ -1,6 +1,7 @@ use iced::executor; use iced::font::{self, Font}; -use iced::widget::{checkbox, column, container, text}; +use iced::theme::Checkbox; +use iced::widget::{checkbox, column, container, row, text}; use iced::{Application, Command, Element, Length, Settings, Theme}; const ICON_FONT: Font = Font::with_name("icons"); @@ -13,12 +14,15 @@ pub fn main() -> iced::Result { struct Example { default_checkbox: bool, custom_checkbox: bool, + styled_checkbox_enabled: bool, + styled_checkbox_checked: bool, } #[derive(Debug, Clone, Copy)] enum Message { DefaultChecked(bool), CustomChecked(bool), + StyledChecked(bool), FontLoaded(Result<(), font::Error>), } @@ -42,8 +46,14 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command<Message> { match message { - Message::DefaultChecked(value) => self.default_checkbox = value, + Message::DefaultChecked(value) => { + self.default_checkbox = value; + self.styled_checkbox_enabled = value; + } Message::CustomChecked(value) => self.custom_checkbox = value, + Message::StyledChecked(value) => { + self.styled_checkbox_checked = value + } Message::FontLoaded(_) => (), } @@ -51,19 +61,40 @@ impl Application for Example { } fn view(&self) -> Element<Message> { - 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, - line_height: text::LineHeight::Relative(1.0), - shaping: text::Shaping::Basic, - }); + let default_checkbox = checkbox("Default", self.default_checkbox) + .on_toggle(Message::DefaultChecked); - let content = column![default_checkbox, custom_checkbox].spacing(22); + let checkboxes = [ + ("Primary", Checkbox::Primary), + ("Secondary", Checkbox::Secondary), + ("Success", Checkbox::Success), + ("Danger", Checkbox::Danger), + ]; + + let checkboxes = row(checkboxes + .into_iter() + .map(|(label, style)| { + checkbox(label, self.styled_checkbox_checked) + .on_toggle_maybe( + self.default_checkbox.then(|| Message::StyledChecked), + ) + .style(style) + }) + .map(Element::from)) + .spacing(10); + + let custom_checkbox = checkbox("Custom", self.custom_checkbox) + .on_toggle(Message::CustomChecked) + .icon(checkbox::Icon { + font: ICON_FONT, + code_point: '\u{e901}', + size: None, + line_height: text::LineHeight::Relative(1.0), + shaping: text::Shaping::Basic, + }); + + let content = + column![default_checkbox, checkboxes, custom_checkbox,].spacing(22); container(content) .width(Length::Fill) |