| 1 |
/* |
| 2 |
* This file is part of budgie-desktop |
| 3 |
* |
| 4 |
* Copyright Budgie Desktop Developers |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
const int BACKGROUND_SIZE = 250; |
| 13 |
|
| 14 |
/** |
| 15 |
* A ClientWidget is simply used to control and display information in a two-way |
| 16 |
* fashion with an underlying MPRIS provider (MediaPlayer2) |
| 17 |
* It is "designed" to be self contained and added to a large UI, enabling multiple |
| 18 |
* MPRIS clients to be controlled with multiple widgets |
| 19 |
*/ |
| 20 |
public class MprisClientWidget : Gtk.Box { |
| 21 |
private Gtk.Box? header = null; |
| 22 |
private Gtk.Image? header_icon = null; |
| 23 |
private Gtk.Label? header_label = null; |
| 24 |
private Gtk.Button? header_reveal_button = null; |
| 25 |
private Gtk.Button? header_close_button = null; |
| 26 |
private Gtk.Revealer? content_revealer = null; |
| 27 |
|
| 28 |
Gtk.Image background; |
| 29 |
Gtk.EventBox background_wrap; |
| 30 |
MprisClient client; |
| 31 |
Gtk.Label title_label; |
| 32 |
Gtk.Label artist_label; |
| 33 |
Gtk.Label album_label; |
| 34 |
Gtk.Button prev_btn; |
| 35 |
Gtk.Button play_btn; |
| 36 |
Gtk.Button next_btn; |
| 37 |
string filename = ""; |
| 38 |
Cancellable? cancel; |
| 39 |
|
| 40 |
int our_width = BACKGROUND_SIZE; |
| 41 |
|
| 42 |
/** |
| 43 |
* Create a new ClientWidget |
| 44 |
* |
| 45 |
* @param client The underlying MprisClient instance to use |
| 46 |
*/ |
| 47 |
public MprisClientWidget(MprisClient client, int width) { |
| 48 |
Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0); |
| 49 |
|
| 50 |
header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0); |
| 51 |
header.get_style_context().add_class("raven-header"); |
| 52 |
add(header); |
| 53 |
|
| 54 |
header_icon = new Gtk.Image.from_icon_name("emblem-music-symbolic", Gtk.IconSize.MENU); |
| 55 |
header_icon.margin = 4; |
| 56 |
header_icon.margin_start = 12; |
| 57 |
header_icon.margin_end = 10; |
| 58 |
header.add(header_icon); |
| 59 |
|
| 60 |
header_label = new Gtk.Label(client.player.identity) { |
| 61 |
xalign = 0.0f, |
| 62 |
max_width_chars = 1, |
| 63 |
ellipsize = Pango.EllipsizeMode.END, |
| 64 |
hexpand = true, |
| 65 |
}; |
| 66 |
header_label.set_tooltip_text(client.player.identity); |
| 67 |
header.add(header_label); |
| 68 |
|
| 69 |
Gtk.Widget? row = null; |
| 70 |
cancel = new Cancellable(); |
| 71 |
|
| 72 |
our_width = width; |
| 73 |
|
| 74 |
this.client = client; |
| 75 |
|
| 76 |
var player_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 77 |
|
| 78 |
background = new Gtk.Image.from_icon_name("emblem-music-symbolic", Gtk.IconSize.DIALOG); |
| 79 |
background.set_size_request(80, 80); |
| 80 |
background.pixel_size = 64; |
| 81 |
background.valign = Gtk.Align.START; |
| 82 |
background.get_style_context().add_class("raven-mpris"); |
| 83 |
|
| 84 |
background_wrap = new Gtk.EventBox(); |
| 85 |
background_wrap.add(background); |
| 86 |
background_wrap.button_release_event.connect(this.on_raise_player); |
| 87 |
|
| 88 |
var layout = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0); |
| 89 |
layout.margin_top = 12; |
| 90 |
layout.margin_start = 12; |
| 91 |
layout.margin_end = 12; |
| 92 |
player_box.pack_start(layout, true, true, 0); |
| 93 |
|
| 94 |
layout.add(background_wrap); |
| 95 |
|
| 96 |
/* normal info */ |
| 97 |
var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 3); |
| 98 |
box.margin_start = 12; |
| 99 |
box.margin_end = 12; |
| 100 |
box.valign = Gtk.Align.CENTER; |
| 101 |
|
| 102 |
var controls = new Gtk.Grid(); |
| 103 |
controls.get_style_context().add_class("raven-mpris-controls"); |
| 104 |
controls.set_column_spacing(6); |
| 105 |
controls.set_column_homogeneous(true); |
| 106 |
|
| 107 |
row = create_row(_("Unknown Title"), "emblem-music-symbolic"); |
| 108 |
title_label = row.get_data("label_item"); |
| 109 |
box.pack_start(row, false, false, 0); |
| 110 |
row = create_row(_("Unknown Artist"), "user-info-symbolic"); |
| 111 |
artist_label = row.get_data("label_item"); |
| 112 |
box.pack_start(row, false, false, 0); |
| 113 |
row = create_row(_("Unknown Album"), "media-optical-symbolic"); |
| 114 |
album_label = row.get_data("label_item"); |
| 115 |
box.pack_start(row, false, false, 0); |
| 116 |
|
| 117 |
player_box.pack_start(controls, true, false, 6); |
| 118 |
|
| 119 |
var btn = new Gtk.Button.from_icon_name("media-skip-backward-symbolic", Gtk.IconSize.LARGE_TOOLBAR); |
| 120 |
btn.set_size_request(Gtk.IconSize.DND, Gtk.IconSize.DND); |
| 121 |
btn.set_sensitive(false); |
| 122 |
btn.set_can_focus(false); |
| 123 |
|
| 124 |
prev_btn = btn; |
| 125 |
btn.clicked.connect(() => { |
| 126 |
if (client.player.can_go_previous) { |
| 127 |
client.player.previous.begin((obj, res) => { |
| 128 |
try { |
| 129 |
try { |
| 130 |
client.player.previous.end(res); |
| 131 |
} catch (IOError e) { |
| 132 |
warning("Error going to the previous track %s: %s", client.player.identity, e.message); |
| 133 |
} |
| 134 |
} catch (DBusError e) { |
| 135 |
warning("Error going to the previous track %s: %s", client.player.identity, e.message); |
| 136 |
} |
| 137 |
}); |
| 138 |
} |
| 139 |
}); |
| 140 |
btn.get_style_context().add_class("flat"); |
| 141 |
controls.attach(btn, 0, 0); |
| 142 |
|
| 143 |
btn = new Gtk.Button.from_icon_name("media-playback-start-symbolic", Gtk.IconSize.LARGE_TOOLBAR); |
| 144 |
play_btn = btn; |
| 145 |
btn.set_can_focus(false); |
| 146 |
btn.clicked.connect(() => { |
| 147 |
client.player.play_pause.begin((obj, res) => { |
| 148 |
try { |
| 149 |
try { |
| 150 |
client.player.play_pause.end(res); |
| 151 |
} catch (IOError e) { |
| 152 |
warning("Error toggling play state %s: %s", client.player.identity, e.message); |
| 153 |
} |
| 154 |
} catch (DBusError e) { |
| 155 |
warning("Error toggling the play state %s: %s", client.player.identity, e.message); |
| 156 |
} |
| 157 |
}); |
| 158 |
}); |
| 159 |
btn.get_style_context().add_class("flat"); |
| 160 |
controls.attach_next_to(btn, prev_btn, Gtk.PositionType.RIGHT); |
| 161 |
|
| 162 |
btn = new Gtk.Button.from_icon_name("media-skip-forward-symbolic", Gtk.IconSize.LARGE_TOOLBAR); |
| 163 |
btn.set_sensitive(false); |
| 164 |
btn.set_can_focus(false); |
| 165 |
next_btn = btn; |
| 166 |
btn.clicked.connect(() => { |
| 167 |
if (client.player.can_go_next) { |
| 168 |
client.player.next.begin((obj, res) => { |
| 169 |
try { |
| 170 |
try { |
| 171 |
client.player.next.end(res); |
| 172 |
} catch (IOError e) { |
| 173 |
warning("Error going to the next track %s: %s", client.player.identity, e.message); |
| 174 |
} |
| 175 |
} catch (DBusError e) { |
| 176 |
warning("Error going to the next track %s: %s", client.player.identity, e.message); |
| 177 |
} |
| 178 |
}); |
| 179 |
} |
| 180 |
}); |
| 181 |
btn.get_style_context().add_class("flat"); |
| 182 |
controls.attach_next_to(btn, play_btn, Gtk.PositionType.RIGHT); |
| 183 |
|
| 184 |
controls.set_halign(Gtk.Align.CENTER); |
| 185 |
controls.margin_bottom = 6; |
| 186 |
layout.add(box); |
| 187 |
|
| 188 |
update_from_meta(); |
| 189 |
update_play_status(); |
| 190 |
update_controls(); |
| 191 |
|
| 192 |
client.prop.properties_changed.connect((i, p, inv) => { |
| 193 |
if (i == "org.mpris.MediaPlayer2.Player") { |
| 194 |
/* Handle mediaplayer2 iface */ |
| 195 |
p.foreach((k, v) => { |
| 196 |
if (k == "Metadata") { |
| 197 |
update_from_meta(); |
| 198 |
} else if (k == "PlaybackStatus") { |
| 199 |
update_play_status(); |
| 200 |
} else if (k == "CanGoNext" || k == "CanGoPrevious") { |
| 201 |
update_controls(); |
| 202 |
} |
| 203 |
}); |
| 204 |
} |
| 205 |
}); |
| 206 |
|
| 207 |
player_box.get_style_context().add_class("raven-background"); |
| 208 |
|
| 209 |
/** |
| 210 |
* Custom Player Styling |
| 211 |
* We do this against the parent box itself so styling includes the header |
| 212 |
*/ |
| 213 |
if ((client.player.desktop_entry != null) && (client.player.desktop_entry != "")) { // If a desktop entry is set |
| 214 |
get_style_context().add_class(client.player.desktop_entry); // Add our desktop entry, such as "spotify" to player_box |
| 215 |
} else if (client.player.identity != null) { // If no desktop entry is set, use identity |
| 216 |
get_style_context().add_class(client.player.identity.down()); // Lowercase identity |
| 217 |
} |
| 218 |
|
| 219 |
get_style_context().add_class("mpris-widget"); |
| 220 |
|
| 221 |
content_revealer = new Gtk.Revealer(); |
| 222 |
content_revealer.add(player_box); |
| 223 |
content_revealer.reveal_child = true; |
| 224 |
add(content_revealer); |
| 225 |
|
| 226 |
header_reveal_button = new Gtk.Button.from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU); |
| 227 |
header_reveal_button.get_style_context().add_class("flat"); |
| 228 |
header_reveal_button.get_style_context().add_class("expander-button"); |
| 229 |
header_reveal_button.margin = 4; |
| 230 |
header_reveal_button.valign = Gtk.Align.CENTER; |
| 231 |
header_reveal_button.clicked.connect(() => { |
| 232 |
content_revealer.reveal_child = !content_revealer.child_revealed; |
| 233 |
var image = (Gtk.Image?) header_reveal_button.get_image(); |
| 234 |
if (content_revealer.reveal_child) { |
| 235 |
image.set_from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU); |
| 236 |
} else { |
| 237 |
image.set_from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU); |
| 238 |
} |
| 239 |
}); |
| 240 |
header.pack_end(header_reveal_button, false, false, 0); |
| 241 |
|
| 242 |
if (client.player.can_quit) { |
| 243 |
header_close_button = new Gtk.Button.from_icon_name("window-close-symbolic", Gtk.IconSize.MENU); |
| 244 |
header_close_button.get_style_context().add_class("flat"); |
| 245 |
header_close_button.get_style_context().add_class("primary-control"); |
| 246 |
header_close_button.valign = Gtk.Align.CENTER; |
| 247 |
header_close_button.clicked.connect(() => { |
| 248 |
if (client.player.can_quit) { |
| 249 |
client.player.quit.begin((obj, res) => { |
| 250 |
try { |
| 251 |
try { |
| 252 |
client.player.quit.end(res); |
| 253 |
} catch (IOError e) { |
| 254 |
warning("Error closing %s: %s", client.player.identity, e.message); |
| 255 |
} |
| 256 |
} catch (DBusError e) { |
| 257 |
warning("Error closing %s: %s", client.player.identity, e.message); |
| 258 |
} |
| 259 |
}); |
| 260 |
} |
| 261 |
}); |
| 262 |
header.pack_end(header_close_button, false, false, 0); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
public void update_width(int new_width) { |
| 267 |
this.our_width = new_width; |
| 268 |
// force the reload of the current art |
| 269 |
update_art(filename, true); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* You raise me up ... |
| 274 |
*/ |
| 275 |
private bool on_raise_player() { |
| 276 |
if (client == null || !client.player.can_raise) { |
| 277 |
return Gdk.EVENT_PROPAGATE; |
| 278 |
} |
| 279 |
|
| 280 |
client.player.raise.begin((obj, res) => { |
| 281 |
try { |
| 282 |
try { |
| 283 |
client.player.raise.end(res); |
| 284 |
} catch (IOError e) { |
| 285 |
warning("Error raising the client for %s: %s", client.player.identity, e.message); |
| 286 |
} |
| 287 |
} catch (DBusError e) { |
| 288 |
warning("Error raising the client for %s: %s", client.player.identity, e.message); |
| 289 |
} |
| 290 |
}); |
| 291 |
|
| 292 |
return Gdk.EVENT_STOP; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Update play status based on player requirements |
| 297 |
*/ |
| 298 |
void update_play_status() { |
| 299 |
switch (client.player.playback_status) { |
| 300 |
case "Playing": |
| 301 |
header_icon.set_from_icon_name("media-playback-start-symbolic", Gtk.IconSize.MENU); |
| 302 |
var text = _("Playing - %s").printf(client.player.identity); |
| 303 |
header_label.set_text(text); |
| 304 |
header_label.set_tooltip_text(text); |
| 305 |
((Gtk.Image) play_btn.get_image()).set_from_icon_name("media-playback-pause-symbolic", Gtk.IconSize.SMALL_TOOLBAR); |
| 306 |
break; |
| 307 |
case "Paused": |
| 308 |
header_icon.set_from_icon_name("media-playback-pause-symbolic", Gtk.IconSize.MENU); |
| 309 |
var text = _("Paused - %s").printf(client.player.identity); |
| 310 |
header_label.set_text(text); |
| 311 |
header_label.set_tooltip_text(text); |
| 312 |
((Gtk.Image) play_btn.get_image()).set_from_icon_name("media-playback-start-symbolic", Gtk.IconSize.SMALL_TOOLBAR); |
| 313 |
break; |
| 314 |
default: |
| 315 |
header_icon.set_from_icon_name("media-playback-stop-symbolic", Gtk.IconSize.MENU); |
| 316 |
header_label.set_text(client.player.identity); |
| 317 |
header_label.set_tooltip_text(client.player.identity); |
| 318 |
((Gtk.Image) play_btn.get_image()).set_from_icon_name("media-playback-start-symbolic", Gtk.IconSize.SMALL_TOOLBAR); |
| 319 |
break; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Update prev/next sensitivity based on player requirements |
| 325 |
*/ |
| 326 |
void update_controls() { |
| 327 |
prev_btn.set_sensitive(client.player.can_go_previous); |
| 328 |
next_btn.set_sensitive(client.player.can_go_next); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Utility, handle updating the album art |
| 333 |
*/ |
| 334 |
void update_art(string uri, bool force_reload = false) { |
| 335 |
// Only load the same art again if a force reload was requested |
| 336 |
if (uri == this.filename && !force_reload) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
if (uri.has_prefix("http")) { |
| 341 |
// Cancel the previous fetch if necessary |
| 342 |
if (!this.cancel.is_cancelled()) { |
| 343 |
this.cancel.cancel(); |
| 344 |
} |
| 345 |
this.cancel.reset(); |
| 346 |
|
| 347 |
download_art.begin(uri); |
| 348 |
} else if (uri.has_prefix("file://")) { |
| 349 |
// local |
| 350 |
string fname = uri.split("file://")[1]; |
| 351 |
try { |
| 352 |
var pbuf = new Gdk.Pixbuf.from_file_at_size(fname, 80, 80); |
| 353 |
background.set_from_pixbuf(pbuf); |
| 354 |
get_style_context().remove_class("no-album-art"); |
| 355 |
} catch (Error e) { |
| 356 |
update_art_fallback(); |
| 357 |
} |
| 358 |
} else { |
| 359 |
update_art_fallback(); |
| 360 |
} |
| 361 |
|
| 362 |
// record the current uri |
| 363 |
this.filename = uri; |
| 364 |
} |
| 365 |
|
| 366 |
void update_art_fallback() { |
| 367 |
get_style_context().add_class("no-album-art"); |
| 368 |
background.set_from_icon_name("emblem-music-symbolic", Gtk.IconSize.INVALID); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Fetch the cover art asynchronously and set it as the background image |
| 373 |
*/ |
| 374 |
async void download_art(string uri) { |
| 375 |
// Spotify broke album artwork for open.spotify.com around time of this commit |
| 376 |
var proper_uri = uri.replace("https://open.spotify.com/image/", "https://i.scdn.co/image/"); |
| 377 |
|
| 378 |
try { |
| 379 |
// open the stream |
| 380 |
var art_file = File.new_for_uri(proper_uri); |
| 381 |
// download the art |
| 382 |
var ins = yield art_file.read_async(Priority.DEFAULT, cancel); |
| 383 |
Gdk.Pixbuf? pbuf = yield new Gdk.Pixbuf.from_stream_at_scale_async(ins, 80, 80, true, cancel); |
| 384 |
background.set_from_pixbuf(pbuf); |
| 385 |
get_style_context().remove_class("no-album-art"); |
| 386 |
} catch (Error e) { |
| 387 |
update_art_fallback(); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/* Work around Spotify, etc */ |
| 392 |
string? get_meta_string(string key, string fallback) { |
| 393 |
if (key in client.player.metadata) { |
| 394 |
var label = client.player.metadata[key]; |
| 395 |
string? lab = null; |
| 396 |
unowned VariantType type = label.get_type(); |
| 397 |
|
| 398 |
/* Simple string */ |
| 399 |
if (type.is_subtype_of(VariantType.STRING)) { |
| 400 |
lab = label.get_string(); |
| 401 |
/* string[] */ |
| 402 |
} else if (type.is_subtype_of(VariantType.STRING_ARRAY)) { |
| 403 |
string[] vals = label.dup_strv(); |
| 404 |
lab = string.joinv(", ", vals); |
| 405 |
} |
| 406 |
/* Return if set */ |
| 407 |
if (lab != null && lab != "") { |
| 408 |
return lab; |
| 409 |
} |
| 410 |
} |
| 411 |
/* Fallback to sanity */ |
| 412 |
return fallback; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Update display info such as artist, the background image, etc. |
| 417 |
*/ |
| 418 |
protected void update_from_meta() { |
| 419 |
if (client.player.metadata == null) { // Gnome MPV metadata are null when opened |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
if ("mpris:artUrl" in client.player.metadata) { |
| 424 |
var url = client.player.metadata["mpris:artUrl"].get_string(); |
| 425 |
update_art(url); |
| 426 |
} else { |
| 427 |
update_art_fallback(); |
| 428 |
} |
| 429 |
|
| 430 |
var title = get_meta_string("xesam:title", _("Unknown Title")); |
| 431 |
title_label.set_text(title); |
| 432 |
title_label.set_tooltip_text(title); |
| 433 |
|
| 434 |
var artist = get_meta_string("xesam:artist", _("Unknown Artist")); |
| 435 |
artist_label.set_markup("%s".printf(Markup.escape_text(artist))); |
| 436 |
artist_label.set_tooltip_text(artist); |
| 437 |
|
| 438 |
var album = get_meta_string("xesam:album", _("Unknown Album")); |
| 439 |
album_label.set_markup("%s".printf(Markup.escape_text(album))); |
| 440 |
album_label.set_tooltip_text(album); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Boring utility function, create an image/label row |
| 446 |
* |
| 447 |
* @param name Label to appear on row |
| 448 |
* @param icon Icon name to use, or NULL if using gicon |
| 449 |
* @param gicon A gicon to use, if not using icon |
| 450 |
* |
| 451 |
* @return A Gtk.Box with the boilerplate cruft out of the way |
| 452 |
*/ |
| 453 |
public static Gtk.Widget create_row(string name, string? icon, Icon? gicon = null) { |
| 454 |
var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6); |
| 455 |
Gtk.Image img; |
| 456 |
|
| 457 |
if (icon == null && gicon != null) { |
| 458 |
img = new Gtk.Image.from_gicon(gicon, Gtk.IconSize.MENU); |
| 459 |
} else { |
| 460 |
img = new Gtk.Image.from_icon_name(icon, Gtk.IconSize.MENU); |
| 461 |
} |
| 462 |
|
| 463 |
img.pixel_size = 12; |
| 464 |
box.pack_start(img, false, false, 0); |
| 465 |
|
| 466 |
var label = new Gtk.Label(name) { |
| 467 |
valign = Gtk.Align.START, |
| 468 |
xalign = 0.0f, |
| 469 |
max_width_chars = 1, |
| 470 |
ellipsize = Pango.EllipsizeMode.END, |
| 471 |
hexpand = true, |
| 472 |
}; |
| 473 |
|
| 474 |
// I don't know why, but if this is omitted then the widget explodes in size when |
| 475 |
// the placeholder is added |
| 476 |
label.set_line_wrap(true); |
| 477 |
|
| 478 |
box.pack_start(label, true, true, 0); |
| 479 |
|
| 480 |
box.set_data("label_item", label); |
| 481 |
box.set_data("image_item", img); |
| 482 |
|
| 483 |
return box; |
| 484 |
} |