libcosmic/examples/cosmic/src/main.rs

209 lines
6.4 KiB
Rust
Raw Normal View History

use cosmic::{
widget::{
button,
list_item,
list_row,
2022-09-30 10:38:31 -06:00
list_section,
2022-09-30 09:57:11 -06:00
list_view,
2022-09-30 09:51:00 -06:00
nav_bar,
2022-09-30 11:14:44 -06:00
nav_button,
toggler,
},
settings,
iced::{theme, Alignment, Color, Element, Length, Sandbox, Theme},
iced::widget::{
2022-09-30 11:14:44 -06:00
checkbox,
container,
horizontal_space,
pick_list,
progress_bar,
radio,
row,
slider,
text,
},
2022-09-30 08:55:37 -06:00
};
pub fn main() -> cosmic::iced::Result {
Window::run(settings())
2022-09-30 08:55:37 -06:00
}
#[derive(Default)]
struct Window {
page: u8,
debug: bool,
theme: Theme,
slider_value: f32,
checkbox_value: bool,
toggler_value: bool,
2022-09-30 11:14:44 -06:00
pick_list_selected: Option<&'static str>,
2022-09-30 08:55:37 -06:00
}
#[derive(Debug, Clone)]
enum Message {
Page(u8),
Debug(bool),
ThemeChanged(Theme),
ButtonPressed,
SliderChanged(f32),
CheckboxToggled(bool),
TogglerToggled(bool),
2022-09-30 11:14:44 -06:00
PickListSelected(&'static str),
2022-09-30 08:55:37 -06:00
}
impl Sandbox for Window {
type Message = Message;
fn new() -> Self {
let mut window = Window::default();
window.slider_value = 50.0;
window
}
fn title(&self) -> String {
String::from("COSMIC Design System - Iced")
}
fn update(&mut self, message: Message) {
match message {
Message::Page(page) => self.page = page,
Message::Debug(debug) => self.debug = debug,
Message::ThemeChanged(theme) => self.theme = theme,
Message::ButtonPressed => {}
Message::SliderChanged(value) => self.slider_value = value,
Message::CheckboxToggled(value) => self.checkbox_value = value,
Message::TogglerToggled(value) => self.toggler_value = value,
2022-09-30 11:14:44 -06:00
Message::PickListSelected(value) => self.pick_list_selected = Some(value),
2022-09-30 08:55:37 -06:00
}
}
fn view(&self) -> Element<Message> {
2022-09-30 09:51:00 -06:00
let sidebar: Element<_> = nav_bar!(
//TODO: Support symbolic icons
2022-09-30 11:14:44 -06:00
nav_button!("network-wireless", "Wi-Fi")
.on_press(Message::Page(0))
.style(if self.page == 0 { theme::Button::Primary } else { theme::Button::Text })
2022-09-30 09:51:00 -06:00
,
2022-09-30 11:14:44 -06:00
nav_button!("preferences-desktop", "Desktop")
.on_press(Message::Page(1))
.style(if self.page == 1 { theme::Button::Primary } else { theme::Button::Text })
2022-09-30 09:51:00 -06:00
,
2022-09-30 11:14:44 -06:00
nav_button!("system-software-update", "OS Upgrade & Recovery")
.on_press(Message::Page(2))
.style(if self.page == 2 { theme::Button::Primary } else { theme::Button::Text })
2022-09-30 08:55:37 -06:00
)
.into();
let choose_theme = [Theme::Light, Theme::Dark].iter().fold(
2022-09-30 10:38:31 -06:00
row![text("Debug theme:")].spacing(10).align_items(Alignment::Center),
2022-09-30 08:55:37 -06:00
|row, theme| {
row.push(radio(
format!("{:?}", theme),
*theme,
Some(self.theme),
Message::ThemeChanged,
))
},
);
2022-09-30 10:38:31 -06:00
let content: Element<_> = list_view!(
list_section!(
"Debug",
choose_theme,
toggler(
String::from("Debug layout"),
self.debug,
Message::Debug,
)
),
list_section!(
"Buttons",
list_row!(
2022-09-30 09:57:11 -06:00
button!("Primary")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Primary)
.on_press(Message::ButtonPressed)
2022-09-30 09:57:11 -06:00
,
button!("Secondary")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Secondary)
.on_press(Message::ButtonPressed)
2022-09-30 09:57:11 -06:00
,
button!("Positive")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Positive)
.on_press(Message::ButtonPressed)
2022-09-30 09:57:11 -06:00
,
button!("Destructive")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Destructive)
.on_press(Message::ButtonPressed)
2022-09-30 09:57:11 -06:00
,
button!("Text")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Text)
.on_press(Message::ButtonPressed)
2022-09-30 09:57:11 -06:00
,
),
list_row!(
2022-09-30 09:57:11 -06:00
button!("Primary")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Primary)
2022-09-30 09:57:11 -06:00
,
button!("Secondary")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Secondary)
2022-09-30 09:57:11 -06:00
,
button!("Positive")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Positive)
2022-09-30 09:57:11 -06:00
,
button!("Destructive")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Destructive)
2022-09-30 09:57:11 -06:00
,
button!("Text")
2022-09-30 10:53:50 -06:00
.style(theme::Button::Text)
2022-09-30 09:57:11 -06:00
,
),
2022-09-30 10:38:31 -06:00
),
list_section!(
"Controls",
list_item!(
2022-09-30 10:53:50 -06:00
"Toggler",
2022-09-30 09:57:11 -06:00
toggler(None, self.toggler_value, Message::TogglerToggled)
),
list_item!(
2022-09-30 11:14:44 -06:00
"Pick List (TODO)",
pick_list(vec![
"Option 1",
"Option 2",
"Option 3",
"Option 4",
], self.pick_list_selected, Message::PickListSelected)
),
list_item!(
2022-09-30 10:53:50 -06:00
"Slider",
2022-09-30 09:57:11 -06:00
slider(0.0..=100.0, self.slider_value, Message::SliderChanged)
2022-09-30 10:53:50 -06:00
.width(Length::Units(250))
),
list_item!(
2022-09-30 10:53:50 -06:00
"Progress",
progress_bar(0.0..=100.0, self.slider_value)
.width(Length::Units(250))
.height(Length::Units(4))
),
2022-09-30 09:57:11 -06:00
checkbox("Checkbox", self.checkbox_value, Message::CheckboxToggled),
2022-09-30 08:55:37 -06:00
)
2022-09-30 10:38:31 -06:00
)
2022-09-30 08:55:37 -06:00
.into();
container(row![
if self.debug { sidebar.explain(Color::WHITE) } else { sidebar },
horizontal_space(Length::Fill),
if self.debug { content.explain(Color::WHITE) } else { content },
horizontal_space(Length::Fill),
])
.padding([16, 8])
.width(Length::Fill)
.height(Length::Fill)
.into()
}
fn theme(&self) -> Theme {
self.theme
}
}