summaryrefslogtreecommitdiff
path: root/theme_test/src/main.rs
blob: 71927852cd53cf0b46ac8824b065589c9a378124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use iced::Length::Fill;
use iced::widget::{
    button, center, checkbox, column, container, horizontal_rule, pick_list,
    radio, row, slider, text_input,
};
use iced::{Element, Length};
use iced_anim::{Animated, Animation, Event};
use iced_dialog::dialog;
use material_theme::button::{elevated, filled_tonal, outlined, text};
use material_theme::container::{
    error, error_container, inverse_surface, primary, primary_container,
    secondary, secondary_container, surface, surface_container,
    surface_container_high, surface_container_highest, surface_container_low,
    surface_container_lowest, tertiary, tertiary_container,
};
use material_theme::text::surface_variant;
use material_theme::{DARK, LIGHT, Theme};

fn main() -> iced::Result {
    iced::application(State::default, State::update, State::view)
        .theme(|state| state.theme.value().clone())
        .run()
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
enum Message {
    Noop,
    OpenDialog,
    CloseDialog,
    Input(String),
    CheckBox(bool),
    Radio(Choice),
    Slider(f32),
    SwitchTheme(Event<Theme>),
}

#[derive(Debug, Default)]
pub struct State {
    theme: Animated<Theme>,
    show_dialog: bool,
    content: String,
    is_checked: bool,
    selection: Option<Choice>,
    value: f32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Choice {
    A,
    B,
    C,
}

impl State {
    fn update(&mut self, message: Message) {
        match message {
            Message::Noop => {}
            Message::OpenDialog => {
                self.show_dialog = true;
            }
            Message::CloseDialog => {
                self.show_dialog = false;
            }
            Message::Input(content) => self.content = content,
            Message::CheckBox(is_checked) => self.is_checked = is_checked,
            Message::Radio(choice) => self.selection = Some(choice),
            Message::Slider(value) => self.value = value,
            Message::SwitchTheme(event) => {
                self.theme.update(event);
            }
        }
    }
    fn view(&self) -> Element<'_, Message, Theme> {
        let base = container(
            row![
                column![
                    button("Disabled"),
                    button("Filled").on_press(Message::Noop),
                    button("Filled Tonal")
                        .on_press(Message::Noop)
                        .style(filled_tonal),
                    button("Elevated").on_press(Message::Noop).style(elevated),
                    button("Outlined").on_press(Message::Noop).style(outlined),
                    button("Text").on_press(Message::Noop).style(text),
                    button("Text Disabled").style(text),
                ]
                .spacing(10),
                column![
                    container("None").padding(8),
                    container("Primary").padding(8).style(primary),
                    container("Primary Container")
                        .padding(8)
                        .style(primary_container),
                    container("Secondary").padding(8).style(secondary),
                    container("Secondary Container")
                        .padding(8)
                        .style(secondary_container),
                    container("Tertiary").padding(8).style(tertiary),
                    container("Tertiary Container")
                        .padding(8)
                        .style(tertiary_container),
                    container("Error").padding(8).style(error),
                    container("Error Container")
                        .padding(8)
                        .style(error_container),
                    container("Surface").padding(8).style(surface),
                    container(
                        iced::widget::text("Surface Variant")
                            .style(surface_variant)
                    )
                    .padding(8)
                    .style(surface),
                    container("Inverse Surface")
                        .padding(8)
                        .style(inverse_surface),
                    container("Surface Container Lowest")
                        .padding(8)
                        .style(surface_container_lowest),
                    container("Surface Container Low")
                        .padding(8)
                        .style(surface_container_low),
                    container("Surface Container")
                        .padding(8)
                        .style(surface_container),
                    container("Surface Container High")
                        .padding(8)
                        .style(surface_container_high),
                    container("Surface Container Highest")
                        .padding(8)
                        .style(surface_container_highest),
                ]
                .spacing(10),
                column![
                    // Pick List
                    pick_list(
                        [LIGHT.clone(), DARK.clone()],
                        Some(self.theme.target()),
                        |theme| Message::SwitchTheme(theme.into())
                    )
                    .placeholder("Select a theme..."),
                    horizontal_rule(1),
                    // Button
                    button("Open Dialog").on_press(Message::OpenDialog),
                    horizontal_rule(1),
                    // Text Input
                    text_input("Type something here...", &self.content)
                        .on_input(Message::Input),
                    horizontal_rule(1),
                    // Checkbox
                    checkbox("Normal", self.is_checked)
                        .on_toggle(Message::CheckBox),
                    checkbox("Error", self.is_checked)
                        .on_toggle(Message::CheckBox)
                        .style(material_theme::checkbox::error),
                    checkbox("Disabled", self.is_checked),
                    horizontal_rule(1),
                    // Radio
                    radio("A", Choice::A, self.selection, Message::Radio,),
                    radio("B", Choice::B, self.selection, Message::Radio,),
                    radio("C", Choice::C, self.selection, Message::Radio,),
                    horizontal_rule(1),
                    // Slider
                    center(iced::widget::text!("{:.1}", self.value))
                        .width(Length::Fill)
                        .height(Length::Shrink),
                    slider(0.0..=100.0, self.value, Message::Slider).step(0.1)
                ]
                .spacing(10)
            ]
            .spacing(20),
        )
        .width(Fill)
        .height(Fill)
        .padding(12);

        let dialog =
            dialog(self.show_dialog, base, iced::widget::text("Say Hi!"))
                .title("This is a Dialog.")
                .push_button(iced_dialog::button("Hi!", Message::CloseDialog))
                .width(280)
                .height(186);

        Animation::new(&self.theme, dialog)
            .on_update(Message::SwitchTheme)
            .into()
    }
}