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
|
use futures::{channel::mpsc, StreamExt};
use gtk::prelude::*;
use libhandy::prelude::*;
use libhandy::{ApplicationWindow, HeaderBar};
use mpd::idle::Idle;
use mpd::Client;
struct TunesUI {
header_bar: HeaderBar,
// album_art: Image,
// queue_switcher: Notebook,
}
fn header_title(conn: &mut mpd::client::Client) -> mpd::error::Result<String> {
let status = conn.status();
let state_descriptor = match status?.state {
mpd::status::State::Stop => "[STOPPED]",
mpd::status::State::Pause => "[PAUSED]",
mpd::status::State::Play => "[PLAYING]",
};
if let Some(song) = conn.currentsong()? {
Ok(format!(
"{} {} - {}",
state_descriptor,
song.title.unwrap_or_else(|| "Untitled".into()),
song.artist.unwrap_or_else(|| "Untitled".into()),
))
} else {
Ok("Tunes".into())
}
}
struct SongInfo {
album_art: gtk::Image,
song_text: gtk::Label,
}
fn song_info() -> mpd::error::Result<(gtk::Widget, SongInfo)> {
let container = gtk::Box::new(gtk::Orientation::Vertical, 16);
let album_art = gtk::Image::new();
let song_text = gtk::Label::new(None);
song_text.set_justify(gtk::Justification::Center);
container.add(&album_art);
container.add(&song_text);
let res = SongInfo {
album_art,
song_text,
};
update_song_info(&res)?;
Ok((container.upcast(), res))
}
fn update_song_info(song_info: &SongInfo) -> mpd::error::Result<()> {
let mut conn = Client::connect("127.0.0.1:6600").unwrap();
if let Some(song) = conn.currentsong()? {
let image_data = conn.albumart(&song).unwrap();
let image_pixbuf = gtk::gdk_pixbuf::Pixbuf::from_stream(
>k::gio::MemoryInputStream::from_bytes(>k::glib::Bytes::from(&image_data)),
gtk::gio::Cancellable::NONE,
)
.ok()
.and_then(|x| x.scale_simple(128, 128, gtk::gdk_pixbuf::InterpType::Hyper));
song_info.album_art.set_pixbuf(image_pixbuf.as_ref());
let ssdf = "[Unknown]".into();
let title = song.title.unwrap_or_else(|| "[Unknown]".into());
let album = song.tags.get("Album").unwrap_or(&ssdf);
let artist = song.artist.unwrap_or_else(|| "[Unknown]".into());
let string = format!("{}\n{} - {}", title, artist, album);
song_info.song_text.set_text(&string);
let attr_list = gtk::pango::AttrList::new();
let mut attr = gtk::pango::AttrFloat::new_scale(2.0);
attr.set_start_index(0);
attr.set_end_index(title.len() as u32);
attr_list.insert(attr);
let mut attr = gtk::pango::AttrFloat::new_scale(1.5);
attr.set_start_index(title.len() as u32 + 1);
// attr.set_end_index(title.len() as u32 + 1 + album.len() as u32);
attr_list.insert(attr);
song_info.song_text.set_attributes(Some(&attr_list));
}
Ok(())
}
fn main() {
let application = gtk::Application::builder()
.application_id("space.jakob.Tunes")
.build();
application.connect_activate(|app| {
libhandy::init();
let mut conn = Client::connect("127.0.0.1:6600").unwrap();
// conn.volume(100).unwrap();
// conn.load("My Lounge Playlist", ..).unwrap();
// conn.play().unwrap();
let stack = gtk::Stack::new();
let (song_info_view, song_info_container) = song_info().unwrap();
stack.add_named(&song_info_view, "Currently Playing");
let header_bar = HeaderBar::builder()
.show_close_button(true)
.title(&header_title(&mut conn).unwrap())
.build();
let asdf = libhandy::ViewSwitcherTitle::builder()
.title("Tunes")
.stack(&stack)
.build();
header_bar.add(&asdf);
let ui = TunesUI { header_bar };
// Combine the content in a box
let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
// Handy's ApplicationWindow does not include a HeaderBar
content.add(&ui.header_bar);
content.add(&stack);
let window = ApplicationWindow::builder()
.default_width(350)
.default_height(70)
// add content to window
.child(&content)
.build();
window.set_application(Some(app));
window.show_all();
let (mut sender, mut receiver) = mpsc::channel(1000);
std::thread::spawn(move || loop {
let mut conn = Client::connect("127.0.0.1:6600").unwrap();
if let Ok(_subsystems) = conn.wait(&[mpd::idle::Subsystem::Player]) {
sender.try_send(true).expect("Couldn't notify thread");
} else {
sender.try_send(false).expect("Couldn't notify thread");
break;
}
});
let main_context = gtk::glib::MainContext::default();
main_context.spawn_local(async move {
let mut conn = Client::connect("127.0.0.1:6600").unwrap();
while let Some(_item) = receiver.next().await {
if let Ok(title) = header_title(&mut conn) {
ui.header_bar.set_title(Some(&title));
update_song_info(&song_info_container).expect("Couldn't update song info");
}
}
});
});
application.run();
}
|