Skip to main content

sonos_cli/tui/
app.rs

1//! TUI application state and navigation types.
2
3use std::cell::RefCell;
4use std::time::{Duration, Instant};
5
6use ratatui_image::picker::Picker;
7
8use crate::config::Config;
9use crate::tui::image_loader::ImageLoader;
10use crate::tui::theme::Theme;
11use crate::tui::types::PickUpState;
12use sonos_sdk::SonosSystem;
13
14const TOAST_DURATION: Duration = Duration::from_secs(3);
15
16/// A temporary notification that auto-dismisses after `TOAST_DURATION`.
17pub struct Toast {
18    pub message: String,
19    pub is_error: bool,
20    created_at: Instant,
21}
22
23impl Toast {
24    pub fn info(message: String) -> Self {
25        Self {
26            message,
27            is_error: false,
28            created_at: Instant::now(),
29        }
30    }
31
32    pub fn error(message: String) -> Self {
33        Self {
34            message,
35            is_error: true,
36            created_at: Instant::now(),
37        }
38    }
39
40    pub fn is_expired(&self) -> bool {
41        self.created_at.elapsed() >= TOAST_DURATION
42    }
43}
44
45/// Top-level TUI state. Owns the SDK handle and all UI state.
46///
47/// Screens read from `&App`; event handlers write to `&mut App`.
48pub struct App {
49    pub system: SonosSystem,
50    pub navigation: Navigation,
51    pub should_quit: bool,
52    pub dirty: bool,
53    #[allow(dead_code)]
54    pub config: Config,
55    pub theme: Theme,
56    /// Toast notification shown in the header. Auto-expires after 3 seconds.
57    pub toast: Option<Toast>,
58    /// Terminal image protocol picker, detected before entering raw mode.
59    /// `None` when album art is disabled or terminal detection failed.
60    /// `RefCell` because `new_resize_protocol()` requires `&mut Picker`.
61    pub picker: RefCell<Option<Picker>>,
62    /// Background image fetcher and cache for album art.
63    pub image_loader: ImageLoader,
64}
65
66impl App {
67    pub fn new(config: Config, theme: Theme, picker: Option<Picker>) -> anyhow::Result<Self> {
68        let system = SonosSystem::new()?;
69        Ok(Self {
70            system,
71            navigation: Navigation::new(),
72            should_quit: false,
73            dirty: true,
74            config,
75            theme,
76            toast: None,
77            picker: RefCell::new(picker),
78            image_loader: ImageLoader::new(),
79        })
80    }
81}
82
83/// Simple two-tab navigation. No stack, no drill-in.
84#[derive(Default)]
85pub struct Navigation {
86    pub tab: Tab,
87    pub tab_focused: bool,
88    pub speakers_state: SpeakerListScreenState,
89    pub settings_state: SettingsScreenState,
90}
91
92/// UI state for the Settings screen dropdown form.
93#[derive(Clone, Debug, Default)]
94pub struct SettingsScreenState {
95    /// Which row (0 = theme, 1 = default group, 2 = album art) is selected.
96    pub selected_row: usize,
97    /// Whether the dropdown for the selected row is open.
98    pub dropdown_open: bool,
99    /// Index within the open dropdown's option list.
100    pub dropdown_index: usize,
101}
102
103impl Navigation {
104    pub fn new() -> Self {
105        Self::default()
106    }
107}
108
109/// Shared UI state for the speaker list.
110#[derive(Clone, Debug, Default)]
111pub struct SpeakerListScreenState {
112    pub selected_index: usize,
113    pub pick_up: Option<PickUpState>,
114}
115
116#[derive(Clone, Debug, Default, PartialEq, Eq)]
117pub enum Tab {
118    #[default]
119    Speakers,
120    Settings,
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn navigation_starts_on_speakers() {
129        let nav = Navigation::new();
130        assert_eq!(nav.tab, Tab::Speakers);
131        assert!(!nav.tab_focused);
132    }
133
134    #[test]
135    fn tab_toggles() {
136        let mut nav = Navigation::new();
137        nav.tab = Tab::Settings;
138        assert_eq!(nav.tab, Tab::Settings);
139        nav.tab = Tab::Speakers;
140        assert_eq!(nav.tab, Tab::Speakers);
141    }
142}