audrey/src/ui/window.vala

124 lines
4 KiB
Vala

[GtkTemplate (ui = "/eu/callcc/audrey/ui/window.ui")]
class Ui.Window : Adw.ApplicationWindow {
[GtkChild] private unowned Gtk.ListBox sidebar;
[GtkChild] private unowned Gtk.ListBoxRow sidebar_play_queue;
[GtkChild] private unowned Gtk.Stack stack;
[GtkChild] public unowned Ui.PlayQueue play_queue;
[GtkChild] public unowned Ui.Playbar playbar;
[GtkChild] public unowned Adw.ButtonRow shuffle_all_tracks;
private Setup setup;
private Subsonic.Client api;
public int volume {
get { return this.playbin.volume; }
set { this.playbin.volume = value; }
}
public bool mute {
get { return this.playbin.mute; }
set { this.playbin.mute = value; }
}
public Subsonic.Song? song { get; private set; }
public Gdk.Paintable? playing_cover_art { get; set; default = null; }
private Cancellable cancel_loading_art;
public bool cover_art_loading { get; set; default = false; }
public Playbin playbin { get; private set; default = new Playbin (); }
public Window (Gtk.Application app) {
Object (application: app);
}
private void now_playing (Subsonic.Song song) {
this.song = song;
// api.scrobble.begin (this.song.id); TODO
if (this.cancel_loading_art != null) {
this.cancel_loading_art.cancel ();
}
this.cancel_loading_art = new GLib.Cancellable ();
this.playing_cover_art = null; // TODO: preload next art somehow
this.cover_art_loading = true;
string song_id = this.song.id;
this.api.cover_art.begin (song_id, this.cancel_loading_art, (obj, res) => {
try {
this.playing_cover_art = Gdk.Texture.for_pixbuf (this.api.cover_art.end (res));
this.cover_art_loading = false;
} catch (Error e) {
if (!(e is IOError.CANCELLED)) {
warning ("could not load cover for %s: %s", song_id, e.message);
this.cover_art_loading = false;
}
}
});
}
construct {
// TODO: mpris
// Bus.own_name (
// BusType.SESSION,
// "org.mpris.MediaPlayer2.audrey",
// BusNameOwnerFlags.NONE,
// (conn) => {
// try {
// } catch (IOError e) {
// error ("could not register dbus service: %s", e.message);
// }
// },
// () => {},
// () => { error ("could not acquire dbus name"); });
this.setup = new Setup ();
this.setup.connected.connect ((api) => {
this.api = api;
this.playbin.api = api;
});
this.setup.load ();
this.sidebar.select_row (this.sidebar.get_row_at_index (0));
this.playbin.new_track.connect (() => {
this.now_playing (this.playbin.play_queue.get_item (this.playbin.play_queue_position) as Subsonic.Song);
});
this.playbin.stopped.connect (() => {
this.playing_cover_art = null;
this.song = null;
});
this.shuffle_all_tracks.sensitive = true;
this.shuffle_all_tracks.activated.connect (() => {
this.shuffle_all_tracks.sensitive = false;
this.playbin.clear ();
api.get_random_songs.begin (null, (song) => {
this.playbin.append_track (song);
}, (obj, res) => {
try {
api.get_random_songs.end (res);
} catch (Error e) {
error ("could not get random songs: %s", e.message);
}
this.shuffle_all_tracks.sensitive = true;
this.playbin.select_track (0);
});
});
}
[GtkCallback] private void on_sidebar_row_activated (Gtk.ListBoxRow row) {
if (row == this.sidebar_play_queue) {
this.stack.set_visible_child_name("play_queue");
}
}
[GtkCallback] private void show_setup_dialog () {
this.setup.present (this);
}
}