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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use mpd;
#[derive(Debug, Clone)]
pub struct ListPage {
pub tag: mpd::Tag,
pub value: String,
pub top_levels: Vec<(String, mpd::Tag)>,
pub sub_pages: Vec<mpd::Tag>,
}
#[derive(Debug, PartialEq)]
pub enum ListPageItem {
ListPageString { string: String },
ListPageTrack { track: mpd::Track },
}
#[derive(Debug)]
pub enum Action {
Init,
Idle,
Quit,
SecondPassed,
GotStatus {
status: mpd::Status,
},
GotQueue {
tracks: Vec<mpd::Track>,
},
GotClient {
client: mpd::Client,
},
GotIdleClient {
idle_client: mpd::Client,
},
SetVolume {
volume: u16,
},
RaiseVolume,
LowerVolume,
SetEndless {
endless: bool,
},
SetRandomize {
randomize: bool,
},
SetSingleMode {
single: bool,
},
SetConsumeMode {
consume: bool,
},
ToggleSingleMode,
ToggleConsumeMode,
// reundant actions for hotkeys
ToggleEndless,
ToggleRandomize,
TogglePause,
//
NextTrack,
PreviousTrack,
Search {
search: String,
tag: mpd::Tag,
},
FindListPage {
page: ListPage,
},
GotListPage {
page: ListPage,
results: Vec<mpd::Track>,
},
List {
tag: mpd::Tag,
},
GotList {
tag: mpd::Tag,
items: Vec<String>,
},
GotSearch {
items: Vec<mpd::Track>,
},
AddFilesToQueue {
files: Vec<String>,
},
QueueNext {
index: usize,
},
Play {
index: usize,
},
RemoveTrack {
index: usize,
},
Seek {
position: f64,
},
SeekRelative {
offset: f64,
},
SetPause {
pause: bool,
},
UpdateStatus,
UpdateQueue,
OpenQueuePage,
OpenFilterPage,
ScrollTracklistTo {
value: f64,
},
ClearQueue,
ClientError {
error: anyhow::Error,
},
OpenConfig,
SaveWipConfig,
// OpenMenuPopup,
SelectServerDelta {
delta: i16,
},
ConfigRemoveServer {
index: usize,
},
ConfigAddServer,
ConfigUpdateServerInput,
ConfigSetActiveServer {
index: usize,
},
InitClient,
InitMpris,
RaiseWindow,
GotCover {
uri: String,
texture: Option<gtk::gdk::Texture>,
},
SetMprisCover,
}
pub fn track_title(t: &mpd::Track) -> String {
match &t.title {
None => std::path::Path::new(&t.file)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_owned(),
Some(title) => title.clone(),
}
}
pub fn track_subtitle(t: &mpd::Track) -> String {
match (&t.artist, &t.album) {
(None, None) => t.file.clone(),
(Some(artist), None) => format!("{} - ", &artist),
(None, Some(album)) => format!(" - {}", &album),
(Some(artist), Some(album)) => format!("{} - {}", &artist, &album),
}
}
pub fn track_triplet_string(t: &mpd::Track) -> String {
format!("{} - {}", track_title(t), track_subtitle(t))
}
pub fn make_time_string(time: u16) -> String {
format!("{:02}:{:02}", time / 60, time % 60)
}
/// Copied from the gtk-macros crate
///
/// Send an event through a glib::Sender
///
/// - Before:
///
/// Example:
///
/// ```no_run
/// sender.send(Action::DoThing).expect("Failed to send DoThing through the glib channel?");
/// ```
///
/// - After:
///
/// Example:
///
/// ```no_run
/// send!(self.sender, Action::DoThing);
/// ```
#[macro_export]
macro_rules! send {
($sender:expr, $action:expr) => {
if let Err(err) = $sender.send($action) {
panic!(
"Failed to send \"{}\" action due to {}",
stringify!($action),
err
);
}
};
}
#[macro_export]
macro_rules! add_action {
($state:expr, $name:expr, $action:expr) => {
let gaction = gtk::gio::SimpleAction::new($name, None);
let sender = &($state).sender;
gaction.connect_activate(clone!(@strong sender => move |_, _| {
send!(sender, $action);
}));
($state).app.add_action(&gaction);
}
}