Skip to main content

sonos_cli/tui/
types.rs

1//! Shared types used across TUI layers (widgets, screens, handlers).
2
3use ratatui_image::protocol::StatefulProtocol;
4use sonos_sdk::{GroupId, PlaybackState, SonosSystem, SpeakerId};
5
6/// A single row in the flat speaker list. Navigation and rendering dispatch on this.
7#[derive(Clone, Debug, PartialEq)]
8pub enum ListEntry {
9    GroupHeader(GroupId),
10    SpeakerRow(SpeakerId),
11    /// Inline drop target shown during pickup mode — "Add to group" or "Already in group".
12    AddToGroupRow(GroupId),
13    /// "Create new group" action row at the bottom during pickup mode.
14    CreateNewGroupRow,
15}
16
17/// State for a speaker being moved between groups.
18#[derive(Clone, Debug)]
19pub struct PickUpState {
20    pub speaker_id: SpeakerId,
21    pub original_group_id: Option<GroupId>,
22}
23
24/// Action returned from speaker list key handling so callers can respond.
25pub enum SpeakerListAction {
26    Handled,
27    FocusTabBar,
28}
29
30/// Build the flat list of entries from the current system state.
31///
32/// All groups get headers — including standalone speakers (Sonos treats every
33/// speaker as belonging to a group). Multi-member groups are listed first,
34/// then standalone groups.
35///
36/// When `pick_up` is `Some`, each group gets an `AddToGroupRow` after its
37/// speakers, and a `CreateNewGroupRow` appears at the end.
38pub fn build_list_entries(system: &SonosSystem, pick_up: Option<&PickUpState>) -> Vec<ListEntry> {
39    let groups = system.groups();
40    let mut entries = Vec::new();
41
42    // Multi-member groups first
43    for group in &groups {
44        if group.is_standalone() {
45            continue;
46        }
47        entries.push(ListEntry::GroupHeader(group.id.clone()));
48        for member in group.members() {
49            entries.push(ListEntry::SpeakerRow(member.id.clone()));
50        }
51        if pick_up.is_some() {
52            entries.push(ListEntry::AddToGroupRow(group.id.clone()));
53        }
54    }
55
56    // Standalone groups — each gets a GroupHeader + single SpeakerRow
57    for group in &groups {
58        if !group.is_standalone() {
59            continue;
60        }
61        entries.push(ListEntry::GroupHeader(group.id.clone()));
62        if let Some(coord) = group.coordinator() {
63            entries.push(ListEntry::SpeakerRow(coord.id.clone()));
64        }
65        if pick_up.is_some() {
66            entries.push(ListEntry::AddToGroupRow(group.id.clone()));
67        }
68    }
69
70    if pick_up.is_some() {
71        entries.push(ListEntry::CreateNewGroupRow);
72    }
73
74    entries
75}
76
77/// Determine which group a list entry at `index` belongs to.
78pub fn group_for_entry(entries: &[ListEntry], index: usize) -> Option<GroupId> {
79    if index >= entries.len() {
80        return None;
81    }
82    if let ListEntry::AddToGroupRow(gid) = &entries[index] {
83        return Some(gid.clone());
84    }
85    if matches!(&entries[index], ListEntry::CreateNewGroupRow) {
86        return None;
87    }
88    for i in (0..=index).rev() {
89        if let ListEntry::GroupHeader(gid) = &entries[i] {
90            return Some(gid.clone());
91        }
92    }
93    None
94}
95
96/// Pre-computed render data for the bottom player bar widget.
97pub struct BottomBarData {
98    pub group_name: String,
99    pub track_title: Option<String>,
100    pub track_artist: Option<String>,
101    pub track_album: Option<String>,
102    pub album_art_protocol: Option<StatefulProtocol>,
103    pub playback_state: Option<PlaybackState>,
104    pub progress: f64,
105    pub position_ms: u64,
106    pub duration_ms: u64,
107    pub volume: u16,
108    pub is_wide: bool,
109    /// True when the selected entry is a group header (volume bar shows accent).
110    /// False for speaker rows (volume bar shows dimmed).
111    pub group_volume_active: bool,
112}
113
114/// Pre-computed render data for the speaker list widget.
115pub struct SpeakerListData {
116    pub entries: Vec<ListEntry>,
117    pub entry_data: Vec<EntryRenderData>,
118    pub selected_index: usize,
119    /// ID of the picked-up speaker (for reverse-highlight matching).
120    pub picked_up_speaker_id: Option<SpeakerId>,
121}
122
123/// Per-entry display data, pre-resolved by the screen layer.
124pub struct EntryRenderData {
125    pub name: String,
126    pub model_name: Option<String>,
127    pub speaker_volume: Option<u16>,
128    pub group_volume: Option<u16>,
129    pub playback_state: Option<PlaybackState>,
130    pub track_info: Option<String>,
131    /// True when this entry is the last member of its group block (renders `└` connector).
132    pub is_last_in_group: bool,
133    /// True for `AddToGroupRow` where the speaker already belongs (dimmed, not selectable).
134    pub is_home_group: bool,
135}
136
137/// Action returned from settings key handling so callers can respond.
138pub enum SettingsAction {
139    Handled,
140    FocusTabBar,
141}
142
143/// A single row in the settings form.
144pub struct SettingsItem {
145    pub label: &'static str,
146    pub value: String,
147    pub options: Vec<String>,
148}
149
150/// Pre-computed render data for the settings widget.
151pub struct SettingsData {
152    pub items: Vec<SettingsItem>,
153    pub selected_row: usize,
154    pub dropdown_open: bool,
155    pub dropdown_index: usize,
156}