2023-02-24 13:56:37 +01:00
|
|
|
use iced::executor;
|
|
|
|
|
use iced::font::{self, Font};
|
2023-10-02 20:18:15 +02:00
|
|
|
use iced::theme::Checkbox;
|
|
|
|
|
use iced::widget::{checkbox, column, container, row, text};
|
2023-02-24 13:56:37 +01:00
|
|
|
use iced::{Application, Command, Element, Length, Settings, Theme};
|
2023-02-16 14:13:04 +01:00
|
|
|
|
2023-03-30 00:56:00 +02:00
|
|
|
const ICON_FONT: Font = Font::with_name("icons");
|
2023-02-16 14:13:04 +01:00
|
|
|
|
|
|
|
|
pub fn main() -> iced::Result {
|
|
|
|
|
Example::run(Settings::default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Example {
|
|
|
|
|
default_checkbox: bool,
|
|
|
|
|
custom_checkbox: bool,
|
2023-10-02 20:18:15 +02:00
|
|
|
styled_checkbox_enabled: bool,
|
|
|
|
|
styled_checkbox_checked: bool,
|
2023-02-16 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
|
enum Message {
|
|
|
|
|
DefaultChecked(bool),
|
|
|
|
|
CustomChecked(bool),
|
2023-10-02 20:18:15 +02:00
|
|
|
StyledChecked(bool),
|
2023-02-24 13:56:37 +01:00
|
|
|
FontLoaded(Result<(), font::Error>),
|
2023-02-16 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-24 13:56:37 +01:00
|
|
|
impl Application for Example {
|
2023-02-16 14:13:04 +01:00
|
|
|
type Message = Message;
|
2023-02-24 13:56:37 +01:00
|
|
|
type Flags = ();
|
|
|
|
|
type Executor = executor::Default;
|
|
|
|
|
type Theme = Theme;
|
2023-02-16 14:13:04 +01:00
|
|
|
|
2023-02-24 13:56:37 +01:00
|
|
|
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
|
|
|
|
|
(
|
|
|
|
|
Self::default(),
|
2023-05-25 23:48:42 +02:00
|
|
|
font::load(include_bytes!("../fonts/icons.ttf").as_slice())
|
2023-02-24 13:56:37 +01:00
|
|
|
.map(Message::FontLoaded),
|
|
|
|
|
)
|
2023-02-16 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
|
|
|
|
String::from("Checkbox - Iced")
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 13:56:37 +01:00
|
|
|
fn update(&mut self, message: Message) -> Command<Message> {
|
2023-02-16 14:13:04 +01:00
|
|
|
match message {
|
2023-10-02 20:18:15 +02:00
|
|
|
Message::DefaultChecked(value) => {
|
|
|
|
|
self.default_checkbox = value;
|
|
|
|
|
self.styled_checkbox_enabled = value;
|
|
|
|
|
}
|
2023-02-16 14:13:04 +01:00
|
|
|
Message::CustomChecked(value) => self.custom_checkbox = value,
|
2023-10-02 20:18:15 +02:00
|
|
|
Message::StyledChecked(value) => {
|
|
|
|
|
self.styled_checkbox_checked = value
|
|
|
|
|
}
|
2023-02-24 13:56:37 +01:00
|
|
|
Message::FontLoaded(_) => (),
|
2023-02-16 14:13:04 +01:00
|
|
|
}
|
2023-02-24 13:56:37 +01:00
|
|
|
|
|
|
|
|
Command::none()
|
2023-02-16 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view(&self) -> Element<Message> {
|
2023-10-02 20:18:15 +02:00
|
|
|
let default_checkbox = checkbox("Default", self.default_checkbox)
|
|
|
|
|
.on_toggle(Message::DefaultChecked);
|
2023-02-16 14:13:04 +01:00
|
|
|
|
2023-10-02 20:18:15 +02:00
|
|
|
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);
|
2023-02-16 14:13:04 +01:00
|
|
|
|
|
|
|
|
container(content)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
|
|
|
|
.center_x()
|
|
|
|
|
.center_y()
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
}
|