Skip to main content

sonos_cli/tui/
app.rs

1//! TUI application state and navigation types.
2
3use std::cell::RefCell;
4
5use ratatui_image::picker::Picker;
6
7use crate::config::Config;
8use crate::tui::image_loader::ImageLoader;
9use crate::tui::theme::Theme;
10use crate::tui::types::PickUpState;
11use sonos_sdk::SonosSystem;
12
13/// Top-level TUI state. Owns the SDK handle and all UI state.
14///
15/// Screens read from `&App`; event handlers write to `&mut App`.
16pub struct App {
17    pub system: SonosSystem,
18    pub navigation: Navigation,
19    pub should_quit: bool,
20    pub dirty: bool,
21    #[allow(dead_code)]
22    pub config: Config,
23    pub theme: Theme,
24    /// Inline status message (e.g. errors from speaker actions). Cleared on next key press.
25    pub status_message: Option<String>,
26    /// Terminal image protocol picker, detected before entering raw mode.
27    /// `None` when album art is disabled or terminal detection failed.
28    /// `RefCell` because `new_resize_protocol()` requires `&mut Picker`.
29    pub picker: RefCell<Option<Picker>>,
30    /// Background image fetcher and cache for album art.
31    pub image_loader: ImageLoader,
32}
33
34impl App {
35    pub fn new(config: Config, theme: Theme, picker: Option<Picker>) -> anyhow::Result<Self> {
36        let system = SonosSystem::new()?;
37        Ok(Self {
38            system,
39            navigation: Navigation::new(),
40            should_quit: false,
41            dirty: true,
42            config,
43            theme,
44            status_message: None,
45            picker: RefCell::new(picker),
46            image_loader: ImageLoader::new(),
47        })
48    }
49}
50
51/// Simple two-tab navigation. No stack, no drill-in.
52#[derive(Default)]
53pub struct Navigation {
54    pub tab: Tab,
55    pub tab_focused: bool,
56    pub speakers_state: SpeakerListScreenState,
57    pub settings_state: SettingsScreenState,
58}
59
60/// UI state for the Settings screen dropdown form.
61#[derive(Clone, Debug, Default)]
62pub struct SettingsScreenState {
63    /// Which row (0 = theme, 1 = default group, 2 = album art) is selected.
64    pub selected_row: usize,
65    /// Whether the dropdown for the selected row is open.
66    pub dropdown_open: bool,
67    /// Index within the open dropdown's option list.
68    pub dropdown_index: usize,
69}
70
71impl Navigation {
72    pub fn new() -> Self {
73        Self::default()
74    }
75}
76
77/// Shared UI state for the speaker list.
78#[derive(Clone, Debug, Default)]
79pub struct SpeakerListScreenState {
80    pub selected_index: usize,
81    pub pick_up: Option<PickUpState>,
82}
83
84#[derive(Clone, Debug, Default, PartialEq, Eq)]
85pub enum Tab {
86    #[default]
87    Speakers,
88    Settings,
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn navigation_starts_on_speakers() {
97        let nav = Navigation::new();
98        assert_eq!(nav.tab, Tab::Speakers);
99        assert!(!nav.tab_focused);
100    }
101
102    #[test]
103    fn tab_toggles() {
104        let mut nav = Navigation::new();
105        nav.tab = Tab::Settings;
106        assert_eq!(nav.tab, Tab::Settings);
107        nav.tab = Tab::Speakers;
108        assert_eq!(nav.tab, Tab::Speakers);
109    }
110}