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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Message system for the Vortix application.
//!
//! All state mutations flow through this centralized Message enum,
//! following the Elm Architecture (TEA) pattern. This enables:
//! - Easy debugging (log all messages)
//! - Predictable state changes
//! - Testable update logic
use crate::core::scanner::ActiveSession;
use crate::core::telemetry::TelemetryUpdate;
use crate::state::{FocusedPanel, ToastType};
/// All messages that can modify application state.
///
/// Messages are the single source of truth for state mutations.
/// They can originate from user input or programmatically.
/// Direction for list selection movement
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionMove {
Next,
Prev,
First,
Last,
}
/// Direction for scrolling movement
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollMove {
Up,
Down,
Top,
Bottom,
}
/// All messages that can modify application state.
///
/// Messages are the single source of truth for state mutations.
/// They can originate from user input or programmatically.
#[derive(Debug, Clone)]
#[allow(dead_code)] // Some variants are handled in match but not constructed externally
pub enum Message {
// === Navigation ===
/// Focus next panel
NextPanel,
/// Focus previous panel
PreviousPanel,
/// Focus a specific panel
FocusPanel(FocusedPanel),
/// Toggle zoom on current panel
ToggleZoom,
/// Toggle flip (front/back view) on current panel
ToggleFlip,
// === Profile Management ===
/// Move selection in profile list
ProfileMove(SelectionMove),
// === Connection ===
/// Toggle connection for profile at index (None = selected)
ToggleConnect(Option<usize>),
/// Disconnect from current VPN (press again while disconnecting to force-kill)
Disconnect,
/// Reconnect to last profile
Reconnect,
/// Connect the currently selected sidebar profile
ConnectSelected,
/// Connect to quick slot (0-8)
QuickConnect(usize),
// === UI Overlays ===
/// Close current overlay (Action menu, Help, Config, etc.)
CloseOverlay,
/// Show toast notification
Toast(String, ToastType),
/// View config for selected profile
OpenConfig,
/// Request delete for profile at index
OpenDelete(Option<usize>),
/// Confirm deletion
ConfirmDelete,
/// Confirm VPN profile switch
ConfirmSwitch { idx: usize },
// === Action Menu ===
/// Open the action menu (Single actions)
OpenActionMenu,
/// Open the bulk action menu
OpenBulkMenu,
// === Scrolling ===
/// Scroll current context
Scroll(ScrollMove),
// === Import ===
/// Open import dialog
OpenImport,
// === System ===
/// Log a message
Log(String),
/// Copy IP to clipboard
CopyIp,
/// Clear activity logs
ClearLogs,
/// Quit the application
Quit,
/// Background telemetry update
Telemetry(TelemetryUpdate),
/// Periodic system state synchronization (active profiles)
SyncSystemState(Vec<ActiveSession>),
/// Periodic heartbeat tick
Tick,
/// Connection timeout detected
ConnectionTimeout(String),
/// Default gateway changed (network switch, sleep/wake)
NetworkChanged,
/// Retry a failed connection after exponential backoff delay
RetryConnect {
/// Profile index to retry
idx: usize,
/// Which attempt this is (1-based)
attempt: u32,
},
/// Result from the background connect thread
ConnectResult {
/// Profile name that was being connected
profile: String,
/// Whether the connect command succeeded
success: bool,
/// Error message if the command failed
error: Option<String>,
},
/// Result from the background disconnect thread
DisconnectResult {
/// Profile name that was being disconnected
profile: String,
/// Whether the disconnect command succeeded
success: bool,
/// Error message if the command failed
error: Option<String>,
},
/// Terminal resize event
Resize(u16, u16),
/// Import profile from path
Import(String),
// === Authentication ===
/// Submit credentials from the auth prompt overlay
AuthSubmit {
/// Profile index to connect after saving credentials
idx: usize,
/// Username entered by the user
username: String,
/// Password entered by the user
password: String,
/// Whether to persist credentials for future sessions
save: bool,
/// Whether to auto-connect after saving (false = save-only from manage flow)
connect_after: bool,
},
/// Open the auth credentials manager for the selected profile (edit/view/clear)
ManageAuth,
/// Clear saved credentials for the selected profile
ClearAuth,
// === Profile Sorting ===
/// Cycle profile sort order (Name A-Z → Z-A → Last Used → Protocol)
CycleSortOrder,
// === Kill Switch ===
/// Toggle kill switch mode (Off → Auto → `AlwaysOn` → Off)
ToggleKillSwitch,
// === Overlay / Inline-mode Actions (keyboard + action menus) ===
/// Open profile rename overlay for the selected profile
OpenRename,
/// Open profile search/filter overlay
OpenSearch,
/// Open the help overlay
OpenHelp,
/// Cycle the activity-log level filter (All → Errors → Warn → Info → All)
CycleLogFilter,
}
/// An item in the action menu, mapping a key to a message.
#[derive(Debug, Clone)]
pub struct ActionMenuItem {
/// The key that triggers this action
pub key: &'static str,
/// Human-readable label for the action
pub label: &'static str,
/// The message to dispatch
pub message: Message,
}
/// Get specific actions for the focused item/panel (triggered by 'x')
#[must_use]
pub fn get_single_actions(focused_panel: &FocusedPanel) -> Vec<ActionMenuItem> {
let mut actions = Vec::new();
// 1. Panel-Specific Actions
match focused_panel {
FocusedPanel::Sidebar => {
actions.push(ActionMenuItem {
key: "i",
label: "Import Profiles",
message: Message::OpenImport,
});
actions.push(ActionMenuItem {
key: "c",
label: "Connect / Disconnect",
message: Message::ToggleConnect(None),
});
actions.push(ActionMenuItem {
key: "r",
label: "Reconnect",
message: Message::ConnectSelected,
});
actions.push(ActionMenuItem {
key: "v",
label: "View Configuration",
message: Message::OpenConfig,
});
actions.push(ActionMenuItem {
key: "a",
label: "Edit Auth Credentials",
message: Message::ManageAuth,
});
actions.push(ActionMenuItem {
key: "A",
label: "Clear Auth Credentials",
message: Message::ClearAuth,
});
actions.push(ActionMenuItem {
key: "DEL",
label: "Delete Profile",
message: Message::OpenDelete(None),
});
actions.push(ActionMenuItem {
key: "s",
label: "Sort Profiles",
message: Message::CycleSortOrder,
});
actions.push(ActionMenuItem {
key: "R",
label: "Rename Profile",
message: Message::OpenRename,
});
}
FocusedPanel::Logs => {
actions.push(ActionMenuItem {
key: "L",
label: "Clear Activity Logs",
message: Message::ClearLogs,
});
actions.push(ActionMenuItem {
key: "f",
label: "Filter Log Level",
message: Message::CycleLogFilter,
});
}
FocusedPanel::ConnectionDetails => {
actions.push(ActionMenuItem {
key: "y",
label: "Copy Public IP",
message: Message::CopyIp,
});
}
FocusedPanel::Security => {
actions.push(ActionMenuItem {
key: "K",
label: "Toggle Kill Switch",
message: Message::ToggleKillSwitch,
});
}
FocusedPanel::Chart => {}
}
// 2. Universal Contextual Utility
if matches!(
focused_panel,
FocusedPanel::Chart | FocusedPanel::ConnectionDetails | FocusedPanel::Security
) {
actions.push(ActionMenuItem {
key: "f",
label: "Flip Panel (Front/Back)",
message: Message::ToggleFlip,
});
}
actions.push(ActionMenuItem {
key: "z",
label: "Toggle Zoom View",
message: Message::ToggleZoom,
});
actions
}
/// Get bulk/global actions (triggered by 'b')
#[must_use]
pub fn get_bulk_actions() -> Vec<ActionMenuItem> {
vec![
ActionMenuItem {
key: "i",
label: "Import Profiles",
message: Message::OpenImport,
},
ActionMenuItem {
key: "r",
label: "Reconnect Last",
message: Message::Reconnect,
},
ActionMenuItem {
key: "D",
label: "Disconnect All",
message: Message::Disconnect,
},
ActionMenuItem {
key: "y",
label: "Copy Public IP",
message: Message::CopyIp,
},
ActionMenuItem {
key: "K",
label: "Toggle Kill Switch",
message: Message::ToggleKillSwitch,
},
ActionMenuItem {
key: "/",
label: "Search Profiles",
message: Message::OpenSearch,
},
ActionMenuItem {
key: "?",
label: "Help",
message: Message::OpenHelp,
},
ActionMenuItem {
key: "l",
label: "Next Panel",
message: Message::NextPanel,
},
ActionMenuItem {
key: "h",
label: "Previous Panel",
message: Message::PreviousPanel,
},
ActionMenuItem {
key: "q",
label: "Quit Vortix",
message: Message::Quit,
},
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sidebar_actions_include_connect() {
let actions = get_single_actions(&FocusedPanel::Sidebar);
assert!(actions.iter().any(|a| a.key == "c"));
assert!(actions.iter().any(|a| a.key == "i"));
assert!(actions.iter().any(|a| a.key == "v"));
assert!(actions.iter().any(|a| a.key == "a")); // edit auth credentials
assert!(actions.iter().any(|a| a.key == "A")); // clear auth credentials
assert!(actions.iter().any(|a| a.key == "DEL"));
assert!(actions.iter().any(|a| a.key == "s")); // sort
assert!(actions.iter().any(|a| a.key == "R")); // rename
assert!(actions.iter().any(|a| a.key == "z")); // universal zoom
}
#[test]
fn test_logs_actions_include_clear_and_filter() {
let actions = get_single_actions(&FocusedPanel::Logs);
assert!(actions.iter().any(|a| a.key == "L"));
assert!(actions.iter().any(|a| a.key == "f")); // log filter
assert!(actions.iter().any(|a| a.key == "z"));
}
#[test]
fn test_connection_details_actions_include_copy_ip_and_flip() {
let actions = get_single_actions(&FocusedPanel::ConnectionDetails);
assert!(actions.iter().any(|a| a.key == "y")); // copy IP
assert!(actions.iter().any(|a| a.key == "f")); // flip
}
#[test]
fn test_chart_actions_flip_and_zoom() {
let actions = get_single_actions(&FocusedPanel::Chart);
assert_eq!(actions.len(), 2);
assert!(actions.iter().any(|a| a.key == "f")); // flip
assert!(actions.iter().any(|a| a.key == "z")); // zoom
}
#[test]
fn test_security_actions_include_killswitch_and_flip() {
let actions = get_single_actions(&FocusedPanel::Security);
assert!(actions.iter().any(|a| a.key == "K")); // kill switch
assert!(actions.iter().any(|a| a.key == "f")); // flip
assert!(actions.iter().any(|a| a.key == "z")); // zoom
}
#[test]
fn test_bulk_actions_contains_essentials() {
let actions = get_bulk_actions();
assert!(actions.iter().any(|a| a.key == "i")); // import
assert!(actions.iter().any(|a| a.key == "D")); // disconnect all
assert!(actions.iter().any(|a| a.key == "q")); // quit
assert!(actions.iter().any(|a| a.key == "y")); // copy IP
assert!(actions.iter().any(|a| a.key == "K")); // kill switch
assert!(actions.iter().any(|a| a.key == "/")); // search
assert!(actions.iter().any(|a| a.key == "?")); // help
}
#[test]
fn test_bulk_actions_count() {
let actions = get_bulk_actions();
assert_eq!(actions.len(), 10);
}
#[test]
fn test_selection_move_variants() {
assert_eq!(SelectionMove::Next, SelectionMove::Next);
assert_ne!(SelectionMove::Next, SelectionMove::Prev);
assert_ne!(SelectionMove::First, SelectionMove::Last);
}
#[test]
fn test_scroll_move_variants() {
assert_eq!(ScrollMove::Up, ScrollMove::Up);
assert_ne!(ScrollMove::Up, ScrollMove::Down);
assert_ne!(ScrollMove::Top, ScrollMove::Bottom);
}
}