forked from aome510/spotify-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.rs
More file actions
167 lines (154 loc) · 4.96 KB
/
Copy pathpopup.rs
File metadata and controls
167 lines (154 loc) · 4.96 KB
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
use crate::{
command,
state::{
model::{Album, Artist, Episode, EpisodeId, Playlist, Show, Track, TrackId},
ItemId,
},
ui::single_line_input::LineInput,
};
use ratatui::widgets::ListState;
use rspotify::model::PlaylistId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlaylistCreateCurrentField {
Name,
Desc,
}
#[derive(Debug)]
pub enum PopupState {
Search {
query: String,
},
UserPlaylistList(PlaylistPopupAction, ListState),
UserFollowedArtistList(ListState),
UserSavedAlbumList(ListState),
DeviceList(ListState),
ArtistList(ArtistPopupAction, Vec<Artist>, ListState),
ThemeList(Vec<crate::config::Theme>, ListState),
ActionList(Box<ActionListItem>, ListState),
PlaylistCreate {
name: LineInput,
desc: LineInput,
current_field: PlaylistCreateCurrentField,
},
ConfirmAction {
message: String,
action: ConfirmableAction,
},
}
#[derive(Debug, Clone)]
pub enum ConfirmableAction {
DeleteTrackFromPlaylist {
playlist_id: PlaylistId<'static>,
track_id: TrackId<'static>,
},
DeleteFromLibrary(ItemId),
}
#[derive(Debug, Clone)]
pub enum ActionListItem {
Track(Track, Vec<command::Action>),
Artist(Artist, Vec<command::Action>),
Album(Album, Vec<command::Action>),
Playlist(Playlist, Vec<command::Action>),
Show(Show, Vec<command::Action>),
Episode(Episode, Vec<command::Action>),
}
/// An action on an item in a playlist popup list
#[derive(Debug)]
pub enum PlaylistPopupAction {
Browse {
folder_id: usize,
search_query: String,
},
AddTrack {
folder_id: usize,
track_id: TrackId<'static>,
search_query: String,
},
AddEpisode {
folder_id: usize,
episode_id: EpisodeId<'static>,
search_query: String,
},
}
/// An action on an item in an artist popup list
#[derive(Copy, Clone, Debug)]
pub enum ArtistPopupAction {
Browse,
ShowActions,
}
impl PopupState {
/// gets the (immutable) list state of a (list) popup
pub fn list_state(&self) -> Option<&ListState> {
match self {
Self::DeviceList(list_state)
| Self::UserPlaylistList(.., list_state)
| Self::UserFollowedArtistList(list_state)
| Self::UserSavedAlbumList(list_state)
| Self::ArtistList(.., list_state)
| Self::ThemeList(.., list_state)
| Self::ActionList(.., list_state) => Some(list_state),
Self::Search { .. } | Self::PlaylistCreate { .. } | Self::ConfirmAction { .. } => None,
}
}
/// gets the (mutable) list state of a (list) popup
pub fn list_state_mut(&mut self) -> Option<&mut ListState> {
match self {
Self::DeviceList(list_state)
| Self::UserPlaylistList(.., list_state)
| Self::UserFollowedArtistList(list_state)
| Self::UserSavedAlbumList(list_state)
| Self::ArtistList(.., list_state)
| Self::ThemeList(.., list_state)
| Self::ActionList(.., list_state) => Some(list_state),
Self::Search { .. } | Self::PlaylistCreate { .. } | Self::ConfirmAction { .. } => None,
}
}
/// gets the selected position of a (list) popup
pub fn list_selected(&self) -> Option<usize> {
match self.list_state() {
None => None,
Some(state) => state.selected(),
}
}
/// selects a position in a (list) popup
pub fn list_select(&mut self, id: Option<usize>) {
match self.list_state_mut() {
None => {}
Some(state) => state.select(id),
}
}
}
impl ActionListItem {
pub fn n_actions(&self) -> usize {
match self {
ActionListItem::Track(.., actions)
| ActionListItem::Artist(.., actions)
| ActionListItem::Album(.., actions)
| ActionListItem::Playlist(.., actions)
| ActionListItem::Show(.., actions)
| ActionListItem::Episode(.., actions) => actions.len(),
}
}
pub fn name(&self) -> &str {
match self {
ActionListItem::Track(track, ..) => &track.name,
ActionListItem::Artist(artist, ..) => &artist.name,
ActionListItem::Album(album, ..) => &album.name,
ActionListItem::Playlist(playlist, ..) => &playlist.name,
ActionListItem::Show(show, ..) => &show.name,
ActionListItem::Episode(episode, ..) => &episode.name,
}
}
pub fn actions_desc(&self) -> Vec<String> {
match self {
ActionListItem::Track(.., actions)
| ActionListItem::Artist(.., actions)
| ActionListItem::Album(.., actions)
| ActionListItem::Playlist(.., actions)
| ActionListItem::Show(.., actions)
| ActionListItem::Episode(.., actions) => {
actions.iter().map(|a| format!("{a:?}")).collect::<Vec<_>>()
}
}
}
}