1#![windows_subsystem = "windows"]
7use taut::{Align, Color, Font, Length, Theme, Ui, Window};
8
9const SECTIONS: [&str; 4] = ["Appearance", "Notifications", "Account", "About"];
10const LOOKS: [&str; 3] = ["As the system", "Light", "Dark"];
12const ACCENTS: [(&str, u32); 4] = [("Violet", 0x7b83f0), ("Teal", 0x1f9e8c), ("Amber", 0xd5832f), ("Rose", 0xd8587a)];
14const GATHER: [&str; 3] = ["As they come", "Every hour", "Once a day"];
15
16pub struct Settings {
18 section: usize,
19 look: usize,
20 accent: Color,
21 letters: f32,
22 close: bool,
23 notify: bool,
24 sound: bool,
25 gather: usize,
26 quiet: (f32, f32),
28 name: String,
29 email: String,
30 days: i32,
32 reports: bool,
33}
34
35impl Default for Settings {
36 fn default() -> Self {
37 Settings {
38 section: 0,
39 look: 0,
40 accent: Color::rgb(ACCENTS[0].1),
41 letters: 13.0,
42 close: false,
43 notify: true,
44 sound: false,
45 gather: 0,
46 quiet: (22.0, 7.0),
47 name: String::new(),
48 email: String::new(),
49 days: 30,
50 reports: true,
51 }
52 }
53}
54
55fn theme(app: &Settings, system_dark: bool) -> Theme {
57 let dark = [system_dark, false, true][app.look];
58 let base = if dark { Theme::dark() } else { Theme::light() };
59 let (control, gap) = if app.close { (24.0, 5.0) } else { (base.control_height, base.gap) };
60 let letters = app.letters.round();
61 Theme { accent: app.accent, font: Font::new(letters), small_font: Font::new(letters - 1.5), control_height: control, gap, ..base }
62}
63
64pub fn describe(app: &mut Settings, ui: &mut Ui) {
65 ui.theme = theme(app, ui.system_dark());
66 let t = ui.theme;
67 ui.row().grow().align(Align::Start, Align::Start).open();
68
69 ui.column().w(Length::Px(180.0)).h(Length::Grow(1.0)).pad(8.0).gap(2.0).fill(t.panel).open();
71 for (number, name) in SECTIONS.iter().enumerate() {
72 if ui.selectable(app.section == number, name).clicked {
73 app.section = number;
74 }
75 }
76 ui.close();
77
78 ui.column().grow().scroll().pad((24.0, 16.0)).gap(t.gap * 2.0).open();
81 ui.text(SECTIONS[app.section], t.font.bold(), t.text);
82 match app.section {
83 0 => appearance(app, ui),
84 1 => notifications(app, ui),
85 2 => account(app, ui),
86 _ => about(ui),
87 }
88 ui.close();
89 ui.close();
90}
91
92fn setting(ui: &mut Ui, name: &str) {
95 let t = ui.theme;
96 ui.column().gap(4.0).open();
97 ui.text(name, t.small_font, t.text_dim);
98}
99
100fn appearance(app: &mut Settings, ui: &mut Ui) {
101 setting(ui, "Colours");
102 ui.segmented(&mut app.look, &LOOKS);
103 ui.close();
104 setting(ui, "Colour that stands out");
105 ui.wrapping_row().gap(8.0).open();
106 let mut named = ACCENTS.iter().position(|(_, rgb)| Color::rgb(*rgb) == app.accent).unwrap_or(usize::MAX);
107 if ui.segmented(&mut named, &ACCENTS.map(|(name, _)| name)).changed {
108 app.accent = Color::rgb(ACCENTS[named].1);
109 }
110 ui.color_picker(&mut app.accent);
111 ui.close();
112 ui.close();
113 setting(ui, &format!("Size of letters: {}", app.letters.round()));
114 ui.row().max(320.0, f32::INFINITY).open();
115 ui.slider(&mut app.letters, 11.0..=18.0);
116 ui.close();
117 ui.close();
118 setting(ui, "Spacing");
119 ui.switch(&mut app.close, "Closer together");
120 ui.close();
121}
122
123fn notifications(app: &mut Settings, ui: &mut Ui) {
124 setting(ui, "Notifications");
125 ui.switch(&mut app.notify, "Show them");
126 ui.close();
127 ui.set_enabled(app.notify);
129 setting(ui, "Sound");
130 ui.checkbox(&mut app.sound, "With a sound");
131 ui.close();
132 setting(ui, "Gather them");
133 ui.select(&mut app.gather, &GATHER);
134 ui.close();
135 setting(ui, &format!("Quiet from {:02}:00 to {:02}:00", app.quiet.0, app.quiet.1));
136 ui.row().max(320.0, f32::INFINITY).open();
137 ui.range(&mut app.quiet.0, &mut app.quiet.1, 0.0..=24.0);
138 (app.quiet.0, app.quiet.1) = (app.quiet.0.round(), app.quiet.1.round());
139 ui.close();
140 ui.close();
141 if ui.button("Send one to try").clicked {
142 ui.notice("This is how a notification looks.");
143 }
144 ui.set_enabled(true);
145}
146
147fn account(app: &mut Settings, ui: &mut Ui) {
148 setting(ui, "Name");
149 ui.text_box(&mut app.name, "Your name");
150 ui.close();
151 setting(ui, "Email");
152 ui.text_box(&mut app.email, "name@example.com");
153 ui.close();
154 setting(ui, "Days of history to keep");
155 ui.number(&mut app.days, 1);
156 app.days = app.days.clamp(1, 365);
157 ui.close();
158 setting(ui, "When something fails");
159 ui.checkbox(&mut app.reports, "Send a report");
160 ui.close();
161}
162
163fn about(ui: &mut Ui) {
164 let t = ui.theme;
165 setting(ui, "Made with");
166 ui.text("taut", t.font.bold(), t.accent);
167 ui.close();
168 setting(ui, "The system asks for");
169 ui.label(if ui.system_dark() { "Dark windows" } else { "Light windows" });
170 ui.close();
171}
172
173fn main() {
174 let mut app = Settings::default();
175 let window = Window { title: "Settings", width: 720.0, height: 460.0, min_width: 480.0, min_height: 300.0, ..Default::default() };
176 taut::run(window, move |ui| describe(&mut app, ui));
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182 use std::time::Duration;
183 use taut::Role;
184 use taut::testing::Trial;
185
186 #[test]
187 fn sections_are_picked_and_the_window_takes_what_is_set() {
188 let mut trial = Trial::new(Settings::default(), describe);
189 trial.resize(720.0, 400.0, 1.0);
190 assert_eq!(trial.ui.theme.bg, Theme::light().bg);
192 trial.ui.set_preferences(taut::Preferences { dark: true, ..Default::default() });
193 trial.run();
194 assert_eq!(trial.ui.theme.bg, Theme::dark().bg);
195 trial.looks_like("tests/looks/settings-dark.png");
196 trial.press(Role::Tab, "Light");
198 trial.press(Role::Tab, "Teal");
199 assert_eq!((trial.ui.theme.bg, trial.ui.theme.accent), (Theme::light().bg, Color::rgb(0x1f9e8c)));
200 trial.press(Role::Slider, "Size of letters: 13");
201 (0..10).for_each(|_| trial.key("Right"));
202 assert_eq!((trial.app.letters, trial.ui.theme.font.size), (18.0, 18.0));
203 trial.press(Role::Switch, "Closer together");
204 assert_eq!(trial.ui.theme.control_height, 24.0);
205 trial.looks_like("tests/looks/settings-light.png");
206
207 trial.press(Role::Row, "Notifications");
209 assert!(trial.find(Role::CheckBox, "With a sound").enabled);
210 trial.press(Role::Switch, "Show them");
211 assert!(!trial.find(Role::CheckBox, "With a sound").enabled);
212 trial.press(Role::Switch, "Show them");
213 trial.press(Role::ComboBox, "Gather them");
214 trial.press(Role::MenuItem, "Once a day");
215 assert_eq!(trial.app.gather, 2);
216 trial.press(Role::Button, "Send one to try");
217 assert!(trial.has(Role::Text, "This is how a notification looks."));
218 trial.wait(Duration::from_secs(5));
219 assert!(!trial.has(Role::Text, "This is how a notification looks."));
220
221 trial.press(Role::Row, "Account");
223 trial.fill("Your name", "Ada Lovelace");
224 trial.press(Role::SpinButton, "Days of history to keep");
226 trial.key("Ctrl+A");
227 trial.type_text("400");
228 trial.key("Enter");
229 assert_eq!((trial.app.name.as_str(), trial.app.days), ("Ada Lovelace", 365));
230
231 trial.press(Role::Row, "About");
233 assert!(trial.has(Role::Text, "Dark windows"));
234 }
235}