iced-yoda/examples/checkbox/src/main.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2023-02-24 13:56:37 +01:00
use iced::executor;
use iced::font::{self, Font};
2024-02-01 13:09:14 +01:00
use iced::theme;
use iced::widget::{checkbox, column, container, row, text};
2023-02-24 13:56:37 +01:00
use iced::{Application, Command, Element, Length, Settings, Theme};
const ICON_FONT: Font = Font::with_name("icons");
pub fn main() -> iced::Result {
Example::run(Settings::default())
}
#[derive(Default)]
struct Example {
2024-02-01 13:09:14 +01:00
default: bool,
styled: bool,
custom: bool,
}
#[derive(Debug, Clone, Copy)]
enum Message {
2024-02-01 13:09:14 +01:00
DefaultToggled(bool),
CustomToggled(bool),
StyledToggled(bool),
2023-02-24 13:56:37 +01:00
FontLoaded(Result<(), font::Error>),
}
2023-02-24 13:56:37 +01:00
impl Application for Example {
type Message = Message;
2023-02-24 13:56:37 +01:00
type Flags = ();
type Executor = executor::Default;
type Theme = Theme;
2023-02-24 13:56:37 +01:00
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
Self::default(),
font::load(include_bytes!("../fonts/icons.ttf").as_slice())
2023-02-24 13:56:37 +01:00
.map(Message::FontLoaded),
)
}
fn title(&self) -> String {
String::from("Checkbox - Iced")
}
2023-02-24 13:56:37 +01:00
fn update(&mut self, message: Message) -> Command<Message> {
match message {
2024-02-01 13:09:14 +01:00
Message::DefaultToggled(default) => {
self.default = default;
}
2024-02-01 13:09:14 +01:00
Message::StyledToggled(styled) => {
self.styled = styled;
}
Message::CustomToggled(custom) => {
self.custom = custom;
}
2023-02-24 13:56:37 +01:00
Message::FontLoaded(_) => (),
}
2023-02-24 13:56:37 +01:00
Command::none()
}
fn view(&self) -> Element<Message> {
2024-02-01 13:09:14 +01:00
let default_checkbox = checkbox("Default", self.default)
.on_toggle(Message::DefaultToggled);
2024-02-01 13:09:14 +01:00
let styled_checkbox = |label, style| {
checkbox(label, self.styled)
.on_toggle_maybe(self.default.then_some(Message::StyledToggled))
2024-02-01 13:09:14 +01:00
.style(style)
};
2024-02-01 13:09:14 +01:00
let checkboxes = row![
styled_checkbox("Primary", theme::Checkbox::Primary),
styled_checkbox("Secondary", theme::Checkbox::Secondary),
styled_checkbox("Success", theme::Checkbox::Success),
styled_checkbox("Danger", theme::Checkbox::Danger),
]
2024-02-01 13:16:12 +01:00
.spacing(20);
2024-02-01 13:09:14 +01:00
let custom_checkbox = checkbox("Custom", self.custom)
.on_toggle(Message::CustomToggled)
.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 =
2024-02-01 13:16:12 +01:00
column![default_checkbox, checkboxes, custom_checkbox].spacing(20);
container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}