Skip to main content

quickstart/
app.rs

1use iced::Size;
2use iced::time::Instant;
3use iced::widget::column;
4use material::widget::{button, navigation, page};
5use material_ui_rs as material;
6
7const WINDOW_SIZE: Size = Size::new(1080.0, 980.0);
8const MIN_WINDOW_SIZE: Size = Size::new(420.0, 720.0);
9
10pub fn main() -> iced::Result {
11    material::application(boot, update, view)
12        .title("material-ui-rs quick start")
13        .subscription(subscription)
14        .window(material::window_with_min_size(WINDOW_SIZE, MIN_WINDOW_SIZE))
15        .run()
16}
17
18#[derive(Debug, Clone)]
19enum Message {
20    Open(Page),
21    Increment,
22    Decrement,
23    Menu,
24    Frame(Instant),
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28enum Page {
29    Home,
30    Settings,
31}
32
33const DESTINATIONS: [navigation::Destination<Page>; 2] = [
34    navigation::Destination::new(Page::Home, "home", "Home"),
35    navigation::Destination::new(Page::Settings, "settings", "Settings"),
36];
37
38struct App {
39    navigation: navigation::NavigationState<Page>,
40    count: i32,
41}
42
43fn boot() -> App {
44    App {
45        navigation: navigation::NavigationState::new(Page::Home),
46        count: 0,
47    }
48}
49
50fn update(app: &mut App, message: Message) {
51    match message {
52        Message::Open(page) => app.navigation.select_now_for_size(page, WINDOW_SIZE),
53        Message::Increment => app.count += 1,
54        Message::Decrement => app.count -= 1,
55        Message::Menu => app.navigation.toggle_menu_now(),
56        Message::Frame(now) => app.navigation.advance_frame(now),
57    }
58}
59
60fn subscription(app: &App) -> iced::Subscription<Message> {
61    app.navigation.subscription(Message::Frame)
62}
63
64fn view(app: &App) -> material::Element<'_, Message> {
65    navigation::suite(&DESTINATIONS, &app.navigation)
66        .window_size(WINDOW_SIZE)
67        .with_menu("Quick start", Message::Menu)
68        .view(Message::Open, app.navigation.selected().view(app))
69}
70
71impl Page {
72    fn view(self, app: &App) -> material::Element<'_, Message> {
73        match self {
74            Self::Home => page::surface(
75                page::header("Home", "A small Material app"),
76                column![
77                    material::text::headline_medium(app.count.to_string()),
78                    button::filled("Increment").on_press(Message::Increment),
79                    button::outlined("Decrement").on_press(Message::Decrement),
80                ]
81                .spacing(12),
82            )
83            .into(),
84            Self::Settings => page::surface(
85                page::header("Settings", "Pages are enum variants"),
86                material::text::body_large("Use the menu button in the rail"),
87            )
88            .into(),
89        }
90    }
91}