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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! List-view tab definitions and state management.
/// Describes whether a tab is global or tied to the active project.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TabScope {
Global,
Project,
}
/// Available top-level tabs in list mode.
///
/// The derived `Default` selects `Tab::Projects` so newly initialized
/// navigation state starts on the projects tab.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum Tab {
#[default]
Projects,
Sessions,
Review,
Settings,
/// Process-local system event log page.
Logs,
}
impl Tab {
/// Tabs in the order they are rendered.
pub const ALL: [Self; 5] = [
Self::Projects,
Self::Sessions,
Self::Review,
Self::Settings,
Self::Logs,
];
/// Project-scoped tabs in display order.
pub const PROJECT_SCOPED: [Self; 4] =
[Self::Sessions, Self::Review, Self::Settings, Self::Logs];
/// Returns the available top-level tabs.
pub fn available_tabs() -> &'static [Self] {
&Self::ALL
}
/// Returns the project-scoped tabs available for the current project.
pub fn project_scoped_tabs() -> &'static [Self] {
&Self::PROJECT_SCOPED
}
/// Returns the display label used in the tabs header.
pub fn title(self) -> &'static str {
match self {
Tab::Projects => "Projects",
Tab::Sessions => "Sessions",
Tab::Review => "Inbox",
Tab::Settings => "Settings",
Tab::Logs => "Logs",
}
}
/// Returns the stable persisted value used for startup restoration.
pub(crate) fn as_str(self) -> &'static str {
self.title()
}
/// Parses one persisted tab value.
pub(crate) fn from_str(value: &str) -> Option<Self> {
match value {
"Projects" => Some(Self::Projects),
"Sessions" => Some(Self::Sessions),
"Inbox" => Some(Self::Review),
"Settings" => Some(Self::Settings),
"Logs" => Some(Self::Logs),
_ => None,
}
}
/// Returns whether the tab is global or tied to the active project.
#[must_use]
pub fn scope(self) -> TabScope {
match self {
Tab::Projects => TabScope::Global,
Tab::Sessions | Tab::Review | Tab::Settings | Tab::Logs => TabScope::Project,
}
}
/// Cycles to the next tab in display order.
#[must_use]
fn next(self) -> Self {
let tabs = Self::available_tabs();
let tab_index = self.index();
let next_index = (tab_index + 1) % tabs.len();
tabs[next_index]
}
/// Cycles to the previous tab in display order.
#[must_use]
fn previous(self) -> Self {
let tabs = Self::available_tabs();
let tab_index = self.index();
let previous_index = (tab_index + tabs.len() - 1) % tabs.len();
tabs[previous_index]
}
/// Returns the display-order index for the tab.
fn index(self) -> usize {
match Self::available_tabs().iter().position(|tab| *tab == self) {
Some(tab_index) => tab_index,
None => unreachable!("tab must exist in the display order"),
}
}
}
/// Manages selection state for top-level tabs.
///
/// The derived `Default` initializes `current` to `Tab::default()`, which
/// selects `Tab::Projects` on a freshly constructed manager.
#[derive(Default)]
pub struct TabManager {
current: Tab,
}
impl TabManager {
/// Builds a manager with an explicit starting tab.
#[must_use]
pub fn new(current: Tab) -> Self {
Self { current }
}
/// Returns the currently selected tab.
#[must_use]
pub fn current(&self) -> Tab {
self.current
}
/// Cycles selection to the next tab.
pub fn next(&mut self) {
self.current = self.current.next();
}
/// Cycles selection to the previous tab.
pub fn previous(&mut self) {
self.current = self.current.previous();
}
/// Sets the currently selected tab.
pub fn set(&mut self, tab: Tab) {
self.current = tab;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tab_title() {
// Arrange
// Act
let titles = Tab::ALL.map(Tab::title);
// Assert
assert_eq!(
titles,
["Projects", "Sessions", "Inbox", "Settings", "Logs"]
);
}
#[test]
fn test_tab_scope_marks_only_projects_as_global() {
// Arrange
// Act
let scopes = Tab::ALL.map(Tab::scope);
// Assert
assert_eq!(
scopes,
[
TabScope::Global,
TabScope::Project,
TabScope::Project,
TabScope::Project,
TabScope::Project
]
);
}
#[test]
fn test_tab_from_str_parses_persisted_values() {
// Arrange
let values = [
("Projects", Some(Tab::Projects)),
("Sessions", Some(Tab::Sessions)),
("Inbox", Some(Tab::Review)),
("Settings", Some(Tab::Settings)),
("Logs", Some(Tab::Logs)),
("Invalid", None),
];
// Act & Assert
for (value, expected_tab) in values {
assert_eq!(Tab::from_str(value), expected_tab);
}
}
#[test]
fn test_tab_as_str_matches_persisted_values() {
// Arrange
// Act
let values = Tab::ALL.map(Tab::as_str);
// Assert
assert_eq!(
values,
["Projects", "Sessions", "Inbox", "Settings", "Logs"]
);
}
#[test]
fn test_tab_next_cycles_in_display_order() {
// Arrange
// Act
let next_tabs = Tab::ALL.map(Tab::next);
// Assert
assert_eq!(
next_tabs,
[
Tab::Sessions,
Tab::Review,
Tab::Settings,
Tab::Logs,
Tab::Projects
]
);
}
#[test]
fn test_tab_previous_cycles_in_display_order() {
// Arrange
// Act
let previous_tabs = Tab::ALL.map(Tab::previous);
// Assert
assert_eq!(
previous_tabs,
[
Tab::Logs,
Tab::Projects,
Tab::Sessions,
Tab::Review,
Tab::Settings
]
);
}
#[test]
fn test_tab_project_scoped_order_keeps_project_pages_grouped() {
// Arrange
// Act
let project_scoped_tabs = Tab::project_scoped_tabs();
// Assert
assert_eq!(
project_scoped_tabs,
&[Tab::Sessions, Tab::Review, Tab::Settings, Tab::Logs]
);
}
#[test]
fn test_tab_manager_new_defaults_to_projects() {
// Arrange
// Act
let manager = TabManager::default();
// Assert
assert_eq!(manager.current(), Tab::Projects);
}
#[test]
fn test_tab_manager_new_uses_explicit_tab() {
// Arrange
// Act
let manager = TabManager::new(Tab::Sessions);
// Assert
assert_eq!(manager.current(), Tab::Sessions);
}
#[test]
fn test_tab_manager_next_cycles_tabs_with_tasks() {
// Arrange
let mut manager = TabManager::default();
let mut observed_tabs = Vec::new();
// Act
observed_tabs.push(manager.current());
manager.next();
observed_tabs.push(manager.current());
manager.next();
observed_tabs.push(manager.current());
manager.next();
observed_tabs.push(manager.current());
manager.next();
observed_tabs.push(manager.current());
manager.next();
observed_tabs.push(manager.current());
// Assert
assert_eq!(
observed_tabs,
vec![
Tab::Projects,
Tab::Sessions,
Tab::Review,
Tab::Settings,
Tab::Logs,
Tab::Projects
]
);
}
#[test]
fn test_tab_manager_previous_cycles_tabs_without_tasks() {
// Arrange
let mut manager = TabManager::default();
let mut observed_tabs = Vec::new();
// Act
observed_tabs.push(manager.current());
manager.previous();
observed_tabs.push(manager.current());
manager.previous();
observed_tabs.push(manager.current());
manager.previous();
observed_tabs.push(manager.current());
manager.previous();
observed_tabs.push(manager.current());
manager.previous();
observed_tabs.push(manager.current());
// Assert
assert_eq!(
observed_tabs,
vec![
Tab::Projects,
Tab::Logs,
Tab::Settings,
Tab::Review,
Tab::Sessions,
Tab::Projects
]
);
}
#[test]
fn test_tab_manager_set_updates_current_tab() {
// Arrange
let mut manager = TabManager::default();
// Act
manager.set(Tab::Settings);
// Assert
assert_eq!(manager.current(), Tab::Settings);
}
}