Introduce support for disabling a checkbox

This commit is contained in:
Alexander van Saase 2023-10-02 20:18:15 +02:00 committed by Héctor Ramón Jiménez
parent b95f3ab12f
commit b5f1ca1695
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
14 changed files with 166 additions and 85 deletions

View file

@ -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)