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}
12
13/// State for a speaker being moved between groups.
14#[derive(Clone, Debug)]
15pub struct PickUpState {
16    pub speaker_id: SpeakerId,
17    pub speaker_name: String,
18    pub original_group_id: Option<GroupId>,
19    pub active_zone_index: usize,
20}
21
22/// The kind of drop zone target.
23#[derive(Clone, Debug, PartialEq)]
24pub enum DropZoneKind {
25    /// An existing group to drop into.
26    ExistingGroup(GroupId),
27    /// Create a new standalone group (leave current group).
28    NewGroup,
29}
30
31/// A single drop zone in the pick-up view.
32#[derive(Clone, Debug)]
33#[allow(dead_code)]
34pub struct DropZone {
35    pub kind: DropZoneKind,
36    /// Group name (coordinator name) for display. "Add new group" for NewGroup.
37    pub group_name: String,
38    /// Names of remaining members (excluding the picked-up speaker).
39    pub remaining_members: Vec<String>,
40    /// Inner height of the zone (equals original member count).
41    pub inner_height: usize,
42}
43
44/// Pre-computed render data for drop zone mode.
45pub struct DropZoneData {
46    pub zones: Vec<DropZone>,
47    pub active_zone_index: usize,
48    pub speaker_name: String,
49    pub status_message: Option<String>,
50}
51
52/// Screen data enum — normal speaker list or pick-up drop zone view.
53pub enum SpeakerScreenData {
54    Normal(SpeakerListData),
55    PickUp(DropZoneData),
56}
57
58/// Action returned from speaker list key handling so callers can respond.
59pub enum SpeakerListAction {
60    Handled,
61    FocusTabBar,
62}
63
64/// Build the flat list of entries from the current system state.
65///
66/// All groups get headers — including standalone speakers (Sonos treats every
67/// speaker as belonging to a group). Multi-member groups are listed first,
68/// then standalone groups.
69pub fn build_list_entries(system: &SonosSystem) -> Vec<ListEntry> {
70    let groups = system.groups();
71    let mut entries = Vec::new();
72
73    // Multi-member groups first
74    for group in &groups {
75        if group.is_standalone() {
76            continue;
77        }
78        entries.push(ListEntry::GroupHeader(group.id.clone()));
79        for member in group.members() {
80            entries.push(ListEntry::SpeakerRow(member.id.clone()));
81        }
82    }
83
84    // Standalone groups — each gets a GroupHeader + single SpeakerRow
85    for group in &groups {
86        if !group.is_standalone() {
87            continue;
88        }
89        entries.push(ListEntry::GroupHeader(group.id.clone()));
90        if let Some(coord) = group.coordinator() {
91            entries.push(ListEntry::SpeakerRow(coord.id.clone()));
92        }
93    }
94
95    entries
96}
97
98/// Determine which group a list entry at `index` belongs to.
99pub fn group_for_entry(entries: &[ListEntry], index: usize) -> Option<GroupId> {
100    if index >= entries.len() {
101        return None;
102    }
103    for i in (0..=index).rev() {
104        if let ListEntry::GroupHeader(gid) = &entries[i] {
105            return Some(gid.clone());
106        }
107    }
108    None
109}
110
111/// Pre-computed render data for the bottom player bar widget.
112pub struct BottomBarData {
113    pub group_name: String,
114    pub track_title: Option<String>,
115    pub track_artist: Option<String>,
116    pub album_art_protocol: Option<StatefulProtocol>,
117    pub playback_state: Option<PlaybackState>,
118    pub progress: f64,
119    pub position_ms: u64,
120    pub duration_ms: u64,
121    pub volume: u16,
122    pub is_wide: bool,
123}
124
125/// Pre-computed render data for the speaker list widget (normal mode).
126pub struct SpeakerListData {
127    pub entries: Vec<ListEntry>,
128    pub entry_data: Vec<EntryRenderData>,
129    pub selected_index: usize,
130    pub status_message: Option<String>,
131}
132
133/// Per-entry display data, pre-resolved by the screen layer.
134pub struct EntryRenderData {
135    pub name: String,
136    pub model_name: Option<String>,
137    pub speaker_volume: Option<u16>,
138    pub group_volume: Option<u16>,
139    pub playback_state: Option<PlaybackState>,
140    pub track_info: Option<String>,
141    /// True when this speaker row is the last member of its group (renders `└` connector).
142    pub is_last_in_group: bool,
143}
144
145/// Action returned from settings key handling so callers can respond.
146pub enum SettingsAction {
147    Handled,
148    FocusTabBar,
149}
150
151/// A single row in the settings form.
152pub struct SettingsItem {
153    pub label: &'static str,
154    pub value: String,
155    pub options: Vec<String>,
156}
157
158/// Pre-computed render data for the settings widget.
159pub struct SettingsData {
160    pub items: Vec<SettingsItem>,
161    pub selected_row: usize,
162    pub dropdown_open: bool,
163    pub dropdown_index: usize,
164    pub status_message: Option<String>,
165}