/builds/framezero/productivity/src/core/settings.rs
Line | Count | Source |
1 | | use anyhow::{Context, Result, bail}; |
2 | | |
3 | | use super::task::TaskStatus; |
4 | | |
5 | | pub const DEFAULT_WORKDAY_START: u16 = 6 * 60; |
6 | | pub const DEFAULT_WORKDAY_END: u16 = 22 * 60; |
7 | | /// Longest font family name accepted as the UI font preference. |
8 | | pub const MAX_UI_FONT_LEN: usize = 200; |
9 | | |
10 | | /// Checks a UI font family name; `None` means the system default font. |
11 | 250 | pub fn validate_ui_font(family: Option<&str>) -> Result<()> { |
12 | 250 | if let Some(family) = family |
13 | 8 | && (family.trim() != family |
14 | 7 | || family.is_empty() |
15 | 6 | || family.chars().count() > MAX_UI_FONT_LEN |
16 | 5 | || family.chars().any(char::is_control)) |
17 | | { |
18 | 4 | bail!("Choose an installed font, or the system default."); |
19 | 246 | } |
20 | 246 | Ok(()) |
21 | 250 | } |
22 | | |
23 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
24 | | #[repr(usize)] |
25 | | pub enum TaskView { |
26 | | Today, |
27 | | List, |
28 | | Matrix, |
29 | | Kanban, |
30 | | Projects, |
31 | | Calendar, |
32 | | } |
33 | | |
34 | | impl TaskView { |
35 | | pub const ALL: [Self; 6] = [ |
36 | | Self::Today, |
37 | | Self::List, |
38 | | Self::Matrix, |
39 | | Self::Kanban, |
40 | | Self::Projects, |
41 | | Self::Calendar, |
42 | | ]; |
43 | | } |
44 | | |
45 | | /// Light or dark rendering; System follows the operating system's appearance. |
46 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
47 | | pub enum Appearance { |
48 | | #[default] |
49 | | System, |
50 | | Light, |
51 | | Dark, |
52 | | } |
53 | | |
54 | | /// The app's accent palette, independent of light or dark appearance. |
55 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
56 | | pub enum ColorScheme { |
57 | | #[default] |
58 | | Forest, |
59 | | Ocean, |
60 | | } |
61 | | |
62 | | macro_rules! text_enum { |
63 | | ($type:ident, $error:literal, $($variant:ident => $value:literal, $title:literal),+ $(,)?) => { |
64 | | impl $type { |
65 | | pub const ALL: [Self; [$($value),+].len()] = [$(Self::$variant),+]; |
66 | | |
67 | 462 | pub fn title(self) -> &'static str { |
68 | 462 | match self { |
69 | 462 | $(Self::$variant => $title),+ |
70 | 462 | } |
71 | 462 | } |
72 | | |
73 | 997 | pub fn as_str(self) -> &'static str { |
74 | 997 | match self { |
75 | 997 | $(Self::$variant => $value),+ |
76 | 997 | } |
77 | 997 | } |
78 | | } |
79 | | |
80 | | impl std::str::FromStr for $type { |
81 | | type Err = anyhow::Error; |
82 | | |
83 | 598 | fn from_str(value: &str) -> Result<Self> { |
84 | 598 | match value { |
85 | 31 | $($value => Ok(Self::$variant),)+ |
86 | 1 | _ => bail!(concat!($error, ": {}"), value), |
87 | | } |
88 | 598 | } |
89 | | } |
90 | | }; |
91 | | } |
92 | | |
93 | | text_enum!(Appearance, "Unknown appearance", System => "system", "System", Light => "light", "Day", Dark => "dark", "Night"); |
94 | | text_enum!(ColorScheme, "Unknown color scheme", Forest => "forest", "Forest", Ocean => "ocean", "Ocean"); |
95 | | |
96 | | /// The Kanban board's column order and hidden columns, one column per status. |
97 | | /// At least one column stays shown. Hiding a column only hides it on the board. |
98 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
99 | | pub struct KanbanColumns { |
100 | | order: [TaskStatus; 4], |
101 | | /// Bit `i` hides `TaskStatus::ALL[i]`. |
102 | | hidden: u8, |
103 | | } |
104 | | |
105 | | impl Default for KanbanColumns { |
106 | 269 | fn default() -> Self { |
107 | 269 | Self { |
108 | 269 | order: TaskStatus::ALL, |
109 | 269 | hidden: 0, |
110 | 269 | } |
111 | 269 | } |
112 | | } |
113 | | |
114 | | impl KanbanColumns { |
115 | | /// Every column hidden; never valid. |
116 | | const ALL_HIDDEN: u8 = 0b1111; |
117 | | |
118 | 1.23k | fn bit(status: TaskStatus) -> u8 { |
119 | 1.23k | let index = TaskStatus::ALL |
120 | 1.23k | .iter() |
121 | 3.06k | .position(|candidate| *candidate == status) |
122 | 1.23k | .expect("every status is listed"); |
123 | 1.23k | 1 << index |
124 | 1.23k | } |
125 | | |
126 | | /// Reads the stored order (comma-separated status names) and hidden-column bits. |
127 | 302 | pub fn from_stored(order: &str, hidden: u8) -> Result<Self> { |
128 | 302 | let statuses: Vec<TaskStatus> = order |
129 | 302 | .split(',') |
130 | 1.20k | .map(|name| { |
131 | 1.20k | name.parse() |
132 | 2 | .with_context(|| format!("Unknown Kanban column: {name}")) |
133 | 1.20k | }) |
134 | 2 | .collect::<Result<_>>()?; |
135 | 300 | let order: [TaskStatus; 4] = statuses |
136 | 300 | .try_into() |
137 | 1 | .map_err(|_| anyhow::anyhow!("The Kanban board needs exactly four columns."))?; |
138 | 299 | let columns = Self { order, hidden }; |
139 | 299 | columns.validate()?3 ; |
140 | 296 | Ok(columns) |
141 | 302 | } |
142 | | |
143 | | /// The stored order: comma-separated status names. |
144 | 297 | pub fn stored_order(self) -> String { |
145 | 297 | self.order.map(TaskStatus::as_str).join(",") |
146 | 297 | } |
147 | | |
148 | 297 | pub fn stored_hidden(self) -> u8 { |
149 | 297 | self.hidden |
150 | 297 | } |
151 | | |
152 | 926 | pub fn validate(self) -> Result<()> { |
153 | 926 | if TaskStatus::ALL |
154 | 926 | .iter() |
155 | 3.70k | .any(|status| !self.order.contains(status)) |
156 | | { |
157 | 1 | bail!("Each status needs exactly one Kanban column."); |
158 | 925 | } |
159 | 925 | if self.hidden & !Self::ALL_HIDDEN != 0 { |
160 | 1 | bail!("Unknown hidden Kanban columns."); |
161 | 924 | } |
162 | 924 | if self.hidden == Self::ALL_HIDDEN { |
163 | 2 | bail!("Keep at least one Kanban column shown."); |
164 | 922 | } |
165 | 922 | Ok(()) |
166 | 926 | } |
167 | | |
168 | | /// Every column in board order, shown or not. |
169 | 5 | pub fn order(self) -> [TaskStatus; 4] { |
170 | 5 | self.order |
171 | 5 | } |
172 | | |
173 | 1.22k | pub fn is_hidden(self, status: TaskStatus) -> bool { |
174 | 1.22k | self.hidden & Self::bit(status) != 0 |
175 | 1.22k | } |
176 | | |
177 | | /// Shown columns in board order. |
178 | 150 | pub fn shown(self) -> impl Iterator<Item = TaskStatus> { |
179 | 150 | self.order |
180 | 150 | .into_iter() |
181 | 600 | .filter(move |status| !self.is_hidden(*status)) |
182 | 150 | } |
183 | | |
184 | | /// Hidden columns in board order. |
185 | 160 | pub fn hidden(self) -> impl Iterator<Item = TaskStatus> { |
186 | 160 | self.order |
187 | 160 | .into_iter() |
188 | 620 | .filter(move |status| self.is_hidden(*status)) |
189 | 160 | } |
190 | | |
191 | | /// Shows or hides a column; the last shown column cannot be hidden. |
192 | 12 | pub fn set_hidden(&mut self, status: TaskStatus, hidden: bool) -> Result<()> { |
193 | 12 | let mut candidate = *self; |
194 | 12 | if hidden { |
195 | 8 | candidate.hidden |= Self::bit(status); |
196 | | } else { |
197 | 4 | candidate.hidden &= !Self::bit(status); |
198 | | } |
199 | 12 | candidate.validate()?1 ; |
200 | 11 | *self = candidate; |
201 | 11 | Ok(()) |
202 | 12 | } |
203 | | |
204 | | /// Moves `status` into the place of `target`, shifting the columns in between. |
205 | 5 | pub fn move_to(&mut self, status: TaskStatus, target: TaskStatus) { |
206 | 5 | if status == target { |
207 | 1 | return; |
208 | 4 | } |
209 | 16 | let mut order: Vec<_> = self.order.into_iter()4 .filter(|s| *s != status).collect(); |
210 | 12 | let from = self.order.iter()4 .position(|s| *s == status); |
211 | 8 | let to = self.order.iter()4 .position(|s| *s == target); |
212 | 4 | let (Some(from), Some(to)) = (from, to) else { |
213 | 0 | return; |
214 | | }; |
215 | | // Moving right lands after the target, moving left before it. |
216 | 4 | let index = order |
217 | 4 | .iter() |
218 | 7 | .position(|s| *s == target) |
219 | 4 | .map_or(order.len(), |index| index + usize::from(from < to)); |
220 | 4 | order.insert(index, status); |
221 | 4 | self.order = order.try_into().expect("the order keeps four columns"); |
222 | 5 | } |
223 | | } |
224 | | |
225 | | /// Saved preferences: screen visibility, appearance, calendar, and Today display. |
226 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
227 | | pub struct DisplaySettings { |
228 | | /// Indexed by [`TaskView`]. |
229 | | pub visible_views: [bool; 6], |
230 | | pub notes_visible: bool, |
231 | | /// Insights is not a task view and does not count towards keeping one shown. |
232 | | pub insights_visible: bool, |
233 | | pub compact: bool, |
234 | | pub appearance: Appearance, |
235 | | pub color_scheme: ColorScheme, |
236 | | pub calendar_week_numbers: bool, |
237 | | pub calendar_work_week: bool, |
238 | | /// Monday is bit 0, Sunday bit 6. At least one working day is required. |
239 | | pub calendar_workdays: u8, |
240 | | /// Local minutes after midnight, in 15-minute steps. Start is strictly before end. |
241 | | pub today_workday_start: u16, |
242 | | /// The end may be 24:00 (1440); overnight ranges are not supported. |
243 | | pub today_workday_end: u16, |
244 | | pub kanban_columns: KanbanColumns, |
245 | | } |
246 | | |
247 | | impl DisplaySettings { |
248 | 7.85k | pub fn calendar_day_visible(self, monday_based_index: usize) -> bool { |
249 | 7.85k | monday_based_index < 7 |
250 | 7.85k | && (!self.calendar_work_week || self.calendar_workdays138 & (1 << monday_based_index) != 0) |
251 | 7.85k | } |
252 | | |
253 | 38.0k | pub fn view_visible(self, view: TaskView) -> bool { |
254 | 38.0k | self.visible_views[view as usize] |
255 | 38.0k | } |
256 | | |
257 | 23 | pub fn set_view_visible(&mut self, view: TaskView, visible: bool) -> Result<()> { |
258 | 23 | let mut candidate = *self; |
259 | 23 | candidate.visible_views[view as usize] = visible; |
260 | 23 | candidate.validate()?4 ; |
261 | 19 | *self = candidate; |
262 | 19 | Ok(()) |
263 | 23 | } |
264 | | |
265 | 4 | pub fn set_notes_visible(&mut self, visible: bool) { |
266 | 4 | self.notes_visible = visible; |
267 | 4 | } |
268 | | |
269 | 634 | pub fn validate(self) -> Result<()> { |
270 | 634 | if !self |
271 | 634 | .today_workday_start |
272 | 634 | .is_multiple_of(super::timebox::SLOT_MINUTES) |
273 | 632 | || !self |
274 | 632 | .today_workday_end |
275 | 632 | .is_multiple_of(super::timebox::SLOT_MINUTES) |
276 | 630 | || self.today_workday_start >= self.today_workday_end |
277 | 624 | || self.today_workday_end > super::timebox::DAY_MINUTES |
278 | | { |
279 | 12 | bail!("Choose workday times in 15-minute steps, with Start before End (00:00–24:00)."); |
280 | 622 | } |
281 | 622 | if !(1..=127).contains(&self.calendar_workdays) { |
282 | 2 | bail!("Select at least one working day."); |
283 | 620 | } |
284 | 726 | if !self.visible_views.iter()620 .any(|visible| *visible) { |
285 | 5 | bail!("Keep at least one task view shown."); |
286 | 615 | } |
287 | 615 | self.kanban_columns.validate() |
288 | 634 | } |
289 | | } |
290 | | |
291 | | impl Default for DisplaySettings { |
292 | 267 | fn default() -> Self { |
293 | 267 | Self { |
294 | 267 | visible_views: [true; 6], |
295 | 267 | notes_visible: false, |
296 | 267 | insights_visible: true, |
297 | 267 | compact: false, |
298 | 267 | appearance: Appearance::System, |
299 | 267 | color_scheme: ColorScheme::Forest, |
300 | 267 | calendar_week_numbers: false, |
301 | 267 | calendar_work_week: false, |
302 | 267 | calendar_workdays: 0b001_1111, |
303 | 267 | today_workday_start: DEFAULT_WORKDAY_START, |
304 | 267 | today_workday_end: DEFAULT_WORKDAY_END, |
305 | 267 | kanban_columns: KanbanColumns::default(), |
306 | 267 | } |
307 | 267 | } |
308 | | } |
309 | | |
310 | | #[cfg(test)] |
311 | | mod tests { |
312 | | use super::*; |
313 | | |
314 | | #[test] |
315 | 1 | fn workday_times_are_ordered_quarter_hours_within_one_day() { |
316 | 1 | let defaults = DisplaySettings::default(); |
317 | 1 | assert_eq!( |
318 | 1 | (defaults.today_workday_start, defaults.today_workday_end), |
319 | 1 | (360, 1320) |
320 | | ); |
321 | 1 | for (start, end) in [(0, 1440), (555, 1020), (1425, 1440), (0, 15)] { |
322 | 4 | assert!( |
323 | 4 | DisplaySettings { |
324 | 4 | today_workday_start: start, |
325 | 4 | today_workday_end: end, |
326 | 4 | ..defaults |
327 | 4 | } |
328 | 4 | .validate() |
329 | 4 | .is_ok() |
330 | | ); |
331 | | } |
332 | 1 | for (start, end) in [ |
333 | 1 | (540, 540), |
334 | 1 | (1020, 540), |
335 | 1 | (541, 1020), |
336 | 1 | (540, 1021), |
337 | 1 | (0, 1455), |
338 | 1 | (1440, 1440), |
339 | 1 | ] { |
340 | 6 | assert!( |
341 | 6 | DisplaySettings { |
342 | 6 | today_workday_start: start, |
343 | 6 | today_workday_end: end, |
344 | 6 | ..defaults |
345 | 6 | } |
346 | 6 | .validate() |
347 | 6 | .is_err() |
348 | | ); |
349 | | } |
350 | 1 | } |
351 | | |
352 | | #[test] |
353 | 1 | fn kanban_columns_reorder_hide_and_round_trip_but_keep_one_shown() { |
354 | | use TaskStatus::*; |
355 | 1 | let mut columns = KanbanColumns::default(); |
356 | 1 | assert_eq!(columns.order(), TaskStatus::ALL); |
357 | 1 | assert_eq!(columns.shown().count(), 4); |
358 | 1 | columns.move_to(OnHold, InProgress); |
359 | 1 | assert_eq!(columns.order(), [ToDo, OnHold, InProgress, Done]); |
360 | 1 | columns.move_to(ToDo, Done); |
361 | 1 | assert_eq!(columns.order(), [OnHold, InProgress, Done, ToDo]); |
362 | 1 | columns.move_to(Done, Done); |
363 | 1 | assert_eq!(columns.order(), [OnHold, InProgress, Done, ToDo]); |
364 | 1 | columns.set_hidden(Done, true).unwrap(); |
365 | 1 | columns.set_hidden(OnHold, true).unwrap(); |
366 | 1 | columns.set_hidden(ToDo, true).unwrap(); |
367 | 1 | assert_eq!(columns.shown().collect::<Vec<_>>(), [InProgress]); |
368 | 1 | assert_eq!(columns.hidden().collect::<Vec<_>>(), [OnHold, Done, ToDo]); |
369 | 1 | assert!(columns.set_hidden(InProgress, true).is_err()); |
370 | 1 | assert!(!columns.is_hidden(InProgress)); |
371 | 1 | let stored = KanbanColumns::from_stored(&columns.stored_order(), columns.stored_hidden()); |
372 | 1 | assert_eq!(stored.unwrap(), columns); |
373 | 1 | columns.set_hidden(Done, false).unwrap(); |
374 | 1 | assert!(!columns.is_hidden(Done)); |
375 | 1 | for (order, hidden) in [ |
376 | 1 | ("to_do,in_progress,done", 0), |
377 | 1 | ("to_do,in_progress,done,done", 0), |
378 | 1 | ("to_do,in_progress,done,waiting", 0), |
379 | 1 | ("to_do,in_progress,done,on_hold", 15), |
380 | 1 | ("to_do,in_progress,done,on_hold", 16), |
381 | 1 | ] { |
382 | 5 | assert!( |
383 | 5 | KanbanColumns::from_stored(order, hidden).is_err(), |
384 | | "{order} {hidden}" |
385 | | ); |
386 | | } |
387 | 1 | } |
388 | | |
389 | | #[test] |
390 | 1 | fn workdays_are_configurable_and_cannot_be_empty() { |
391 | 1 | let mut settings = DisplaySettings::default(); |
392 | 7 | assert!1 ((0..71 ).all(|index| settings.calendar_day_visible(index))); |
393 | 1 | settings.calendar_work_week = true; |
394 | 1 | assert!(settings.calendar_day_visible(4)); |
395 | 1 | assert!(!settings.calendar_day_visible(6)); |
396 | 1 | settings.calendar_workdays = 0b100_1111; |
397 | 1 | assert!(!settings.calendar_day_visible(4)); |
398 | 1 | assert!(!settings.calendar_day_visible(5)); |
399 | 1 | assert!(settings.calendar_day_visible(6)); |
400 | 1 | assert!(settings.validate().is_ok()); |
401 | 1 | settings.calendar_workdays = 0; |
402 | 1 | assert!(settings.validate().is_err()); |
403 | 1 | } |
404 | | |
405 | | #[test] |
406 | 1 | fn appearance_and_color_scheme_default_and_round_trip() { |
407 | 1 | let settings = DisplaySettings::default(); |
408 | 1 | assert_eq!(settings.appearance, Appearance::System); |
409 | 1 | assert_eq!(settings.color_scheme, ColorScheme::Forest); |
410 | 1 | for appearance in Appearance::ALL { |
411 | 3 | assert_eq!( |
412 | 3 | appearance.as_str().parse::<Appearance>().unwrap(), |
413 | 3 | appearance |
414 | | ); |
415 | | } |
416 | 1 | for scheme in ColorScheme::ALL { |
417 | 2 | assert_eq!(scheme.as_str().parse::<ColorScheme>().unwrap(), scheme); |
418 | | } |
419 | 1 | assert!("sepia".parse::<ColorScheme>().is_err()); |
420 | 1 | assert_eq!(Appearance::Light.title(), "Day"); |
421 | 1 | assert_eq!(Appearance::Dark.title(), "Night"); |
422 | 1 | } |
423 | | |
424 | | #[test] |
425 | 1 | fn views_default_to_shown_and_last_visible_view_cannot_be_hidden() { |
426 | 1 | let mut settings = DisplaySettings::default(); |
427 | 1 | assert!(!settings.notes_visible); |
428 | 1 | for view in TaskView::ALL { |
429 | 6 | assert!(settings.view_visible(view)); |
430 | | } |
431 | 1 | for view in [ |
432 | 1 | TaskView::Today, |
433 | 1 | TaskView::List, |
434 | 1 | TaskView::Matrix, |
435 | 1 | TaskView::Kanban, |
436 | 1 | TaskView::Projects, |
437 | 1 | ] { |
438 | 5 | settings.set_view_visible(view, false).unwrap(); |
439 | | } |
440 | 1 | settings.set_notes_visible(true); |
441 | 1 | let before = settings; |
442 | 1 | assert!( |
443 | 1 | settings |
444 | 1 | .set_view_visible(TaskView::Calendar, false) |
445 | 1 | .is_err() |
446 | | ); |
447 | 1 | assert_eq!(settings, before); |
448 | 1 | settings.set_notes_visible(false); |
449 | 1 | assert!(!settings.notes_visible); |
450 | 6 | assert!1 (settings.visible_views.iter()1 .any(|visible| *visible)); |
451 | 1 | settings.set_view_visible(TaskView::List, true).unwrap(); |
452 | 1 | settings |
453 | 1 | .set_view_visible(TaskView::Calendar, false) |
454 | 1 | .unwrap(); |
455 | 1 | } |
456 | | } |