2024-10-13 09:41:01 +00:00
|
|
|
[GtkTemplate (ui = "/eu/callcc/audrey/ui/window.ui")]
|
|
|
|
class Ui.Window : Adw.ApplicationWindow {
|
2024-10-10 09:53:52 +00:00
|
|
|
[GtkChild] private unowned Gtk.ListBox sidebar;
|
|
|
|
[GtkChild] private unowned Gtk.ListBoxRow sidebar_play_queue;
|
|
|
|
[GtkChild] private unowned Gtk.Stack stack;
|
|
|
|
|
2024-10-13 09:41:01 +00:00
|
|
|
[GtkChild] public unowned Ui.PlayQueue play_queue;
|
2024-10-10 10:51:12 +00:00
|
|
|
[GtkChild] public unowned Adw.ButtonRow shuffle_all_tracks;
|
2024-10-06 11:21:53 +00:00
|
|
|
|
2024-10-12 20:52:29 +00:00
|
|
|
private Setup setup;
|
2024-10-12 12:28:05 +00:00
|
|
|
|
|
|
|
public int64 position { get; private set; }
|
2024-10-11 06:57:01 +00:00
|
|
|
|
2024-10-12 16:35:42 +00:00
|
|
|
public double volume {
|
|
|
|
get { return this.playbin.volume; }
|
|
|
|
set { this.playbin.volume = value; }
|
|
|
|
}
|
|
|
|
public bool mute {
|
|
|
|
get { return this.playbin.mute; }
|
|
|
|
set { this.playbin.mute = value; }
|
|
|
|
}
|
2024-10-11 06:57:01 +00:00
|
|
|
|
2024-10-11 07:53:30 +00:00
|
|
|
public Song? song { get; set; default = null; }
|
|
|
|
|
2024-10-11 09:09:47 +00:00
|
|
|
private Cancellable cancel_loading_art;
|
|
|
|
public bool cover_art_loading { get; set; default = false; }
|
|
|
|
public Gdk.Paintable playing_cover_art { get; set; }
|
|
|
|
|
2024-10-12 12:28:05 +00:00
|
|
|
internal Playbin playbin { get; default = new Playbin (); }
|
|
|
|
|
2024-10-06 11:21:53 +00:00
|
|
|
public Window (Gtk.Application app) {
|
|
|
|
Object (application: app);
|
2024-10-12 16:35:42 +00:00
|
|
|
|
2024-10-12 18:41:03 +00:00
|
|
|
var provider = new Gtk.CssProvider ();
|
2024-10-12 20:52:29 +00:00
|
|
|
provider.load_from_resource("/eu/callcc/audrey/audrey.css");
|
2024-10-12 18:41:03 +00:00
|
|
|
|
|
|
|
Gtk.StyleContext.add_provider_for_display (Gdk.Display.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
|
2024-10-12 16:35:42 +00:00
|
|
|
this.close_request.connect (() => {
|
|
|
|
app.quit ();
|
|
|
|
return false;
|
|
|
|
});
|
2024-10-06 11:21:53 +00:00
|
|
|
}
|
2024-10-10 09:53:52 +00:00
|
|
|
|
|
|
|
construct {
|
2024-10-12 16:35:42 +00:00
|
|
|
Bus.own_name (
|
|
|
|
BusType.SESSION,
|
2024-10-12 20:52:29 +00:00
|
|
|
"org.mpris.MediaPlayer2.audrey",
|
2024-10-12 16:35:42 +00:00
|
|
|
BusNameOwnerFlags.NONE,
|
|
|
|
(conn) => {
|
|
|
|
try {
|
|
|
|
// TODO: mpris
|
|
|
|
} catch (IOError e) {
|
|
|
|
error ("could not register dbus service: %s", e.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {},
|
|
|
|
() => { error ("could not acquire dbus name"); });
|
|
|
|
|
2024-10-12 20:52:29 +00:00
|
|
|
this.setup = new Setup ();
|
2024-10-12 19:06:15 +00:00
|
|
|
|
2024-10-12 12:28:05 +00:00
|
|
|
this.setup.connected.connect ((api) => {
|
|
|
|
public_api = api;
|
|
|
|
|
|
|
|
this.shuffle_all_tracks.sensitive = true;
|
|
|
|
this.shuffle_all_tracks.activated.connect (() => {
|
|
|
|
this.shuffle_all_tracks.sensitive = false;
|
|
|
|
this.play_queue.clear ();
|
|
|
|
api.get_random_songs.begin (null, (song) => {
|
|
|
|
this.play_queue.queue (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;
|
2024-10-12 12:57:37 +00:00
|
|
|
|
|
|
|
this.play_queue.restart ();
|
2024-10-12 12:28:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-13 15:21:29 +00:00
|
|
|
this.play_queue.begin_playback.connect ((song) => {
|
|
|
|
var uri = api.stream_uri (song.id);
|
|
|
|
this.playbin.begin_playback (uri);
|
2024-10-12 12:28:05 +00:00
|
|
|
|
|
|
|
this.song = song;
|
2024-10-12 16:40:52 +00:00
|
|
|
api.scrobble.begin (song.id);
|
2024-10-12 12:28:05 +00:00
|
|
|
});
|
|
|
|
|
2024-10-13 15:21:29 +00:00
|
|
|
this.play_queue.prepare_next.connect ((next_song) => {
|
|
|
|
var next_uri = next_song == null ? null : api.stream_uri (next_song.id);
|
|
|
|
this.playbin.prepare_next (next_uri);
|
2024-10-12 12:28:05 +00:00
|
|
|
});
|
2024-10-13 15:21:29 +00:00
|
|
|
|
|
|
|
this.playbin.song_transition.connect ((uri) => {
|
|
|
|
this.play_queue.song_transition ();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.play_queue.playback_continues.connect ((song) => {
|
|
|
|
this.song = song;
|
|
|
|
api.scrobble.begin (song.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.playbin.playback_finished.connect (() => {
|
|
|
|
this.play_queue.playback_finished ();
|
|
|
|
this.song = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.play_queue.stop_playback.connect (() => {
|
|
|
|
this.playbin.stop_playback ();
|
|
|
|
this.song = null;
|
2024-10-12 12:28:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
this.setup.load ();
|
|
|
|
|
2024-10-10 09:53:52 +00:00
|
|
|
this.sidebar.select_row (this.sidebar.get_row_at_index (0));
|
2024-10-11 09:09:47 +00:00
|
|
|
|
|
|
|
this.notify["song"].connect (() => {
|
|
|
|
if (this.cancel_loading_art != null) {
|
|
|
|
this.cancel_loading_art.cancel ();
|
|
|
|
}
|
|
|
|
this.cancel_loading_art = new Cancellable ();
|
|
|
|
|
|
|
|
this.playing_cover_art = Gdk.Paintable.empty (100, 100);
|
|
|
|
if (this.song != null) {
|
|
|
|
this.cover_art_loading = true;
|
|
|
|
|
|
|
|
string song_id = this.song.id;
|
|
|
|
public_api.cover_art.begin (song_id, 100, this.cancel_loading_art, (obj, res) => {
|
|
|
|
try {
|
|
|
|
this.playing_cover_art = Gdk.Texture.for_pixbuf (public_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-10-12 12:28:05 +00:00
|
|
|
this.song = null;
|
|
|
|
|
2024-10-13 09:56:28 +00:00
|
|
|
this.playbin.notify["position"].connect (() => {
|
2024-10-12 12:28:05 +00:00
|
|
|
// only set if we aren't seeking
|
|
|
|
if (this.seek_timeout_id == 0) {
|
2024-10-13 09:56:28 +00:00
|
|
|
this.position = this.playbin.position;
|
2024-10-12 12:28:05 +00:00
|
|
|
}
|
|
|
|
});
|
2024-10-10 09:53:52 +00:00
|
|
|
}
|
2024-10-11 06:57:01 +00:00
|
|
|
|
2024-10-11 08:22:05 +00:00
|
|
|
[GtkCallback] private void on_sidebar_row_activated (Gtk.ListBoxRow row) {
|
2024-10-12 19:06:15 +00:00
|
|
|
if (row == this.sidebar_play_queue) {
|
2024-10-11 08:22:05 +00:00
|
|
|
this.stack.set_visible_child_name("play_queue");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 12:28:05 +00:00
|
|
|
[GtkCallback] private string format_timestamp (int64 ns) {
|
|
|
|
int64 ms = ns / 1000000;
|
|
|
|
int s = (int) (ms / 1000);
|
2024-10-11 08:22:05 +00:00
|
|
|
return "%02d:%02d".printf (s/60, s%60);
|
|
|
|
}
|
2024-10-13 16:22:44 +00:00
|
|
|
|
|
|
|
private void seek_impl (int64 position) {
|
|
|
|
this.position = position;
|
2024-10-12 12:28:05 +00:00
|
|
|
if (this.seek_timeout_id == 0) {
|
|
|
|
this.seek_timeout_id = Timeout.add (500, () => {
|
2024-10-13 16:22:44 +00:00
|
|
|
playbin.seek(this.position);
|
2024-10-13 10:40:29 +00:00
|
|
|
this.seek_timeout_id = 0;
|
2024-10-12 12:28:05 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2024-10-13 16:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// same timeout logic as https://code.videolan.org/videolan/npapi-vlc/blob/6eae0ffb9cbaf8f6e04423de2ff38daabdf7cae3/npapi/vlcplugin_gtk.cpp#L312
|
|
|
|
private uint seek_timeout_id = 0;
|
|
|
|
[GtkCallback] private bool on_play_position_seek (Gtk.Range range, Gtk.ScrollType scroll_type, double value) {
|
|
|
|
this.seek_impl((int64) value);
|
2024-10-12 12:28:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-10-12 13:36:47 +00:00
|
|
|
|
|
|
|
[GtkCallback] private void on_play_pause_clicked () {
|
2024-10-13 15:21:29 +00:00
|
|
|
if (this.playbin.state == PlaybinState.PLAYING) {
|
2024-10-12 13:36:47 +00:00
|
|
|
this.playbin.pause();
|
|
|
|
} else {
|
|
|
|
this.playbin.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-13 15:21:29 +00:00
|
|
|
[GtkCallback] private string play_pause_icon_name (PlaybinState state) {
|
|
|
|
if (state == PlaybinState.PLAYING) {
|
|
|
|
return "media-playback-pause";
|
|
|
|
} else {
|
|
|
|
return "media-playback-start";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[GtkCallback] private bool playbin_active (PlaybinState state) {
|
|
|
|
return state != PlaybinState.STOPPED;
|
2024-10-12 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 16:35:42 +00:00
|
|
|
[GtkCallback] private string mute_button_icon_name (bool mute) {
|
|
|
|
return mute ? "audio-volume-muted" : "audio-volume-high";
|
|
|
|
}
|
|
|
|
|
|
|
|
[GtkCallback] private void on_mute_toggle () {
|
|
|
|
this.mute = !this.mute;
|
|
|
|
}
|
|
|
|
|
2024-10-12 13:36:47 +00:00
|
|
|
[GtkCallback] private void on_skip_forward_clicked () {
|
|
|
|
this.play_queue.skip_forward ();
|
|
|
|
}
|
|
|
|
|
|
|
|
[GtkCallback] private void on_skip_backward_clicked () {
|
|
|
|
this.play_queue.skip_backward ();
|
|
|
|
}
|
2024-10-12 19:06:15 +00:00
|
|
|
|
|
|
|
[GtkCallback] private void show_setup_dialog () {
|
|
|
|
this.setup.present (this);
|
|
|
|
}
|
2024-10-13 08:03:05 +00:00
|
|
|
|
|
|
|
[GtkCallback] private string format_song_below_title (Song? song) {
|
|
|
|
return song == null ? "" : @"$(song.artist) - $(song.album) - $(song.year)";
|
|
|
|
}
|
2024-10-13 08:14:40 +00:00
|
|
|
|
|
|
|
[GtkCallback] private void seek_backward () {
|
|
|
|
// 10 seconds
|
|
|
|
int64 new_position = position - (int64)10 * 1000 * 1000000;
|
|
|
|
if (new_position < 0) new_position = 0;
|
2024-10-13 16:22:44 +00:00
|
|
|
this.seek_impl (new_position);
|
2024-10-13 08:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[GtkCallback] private void seek_forward () {
|
|
|
|
// 10 seconds
|
|
|
|
int64 new_position = position + (int64)10 * 1000 * 1000000;
|
|
|
|
if (new_position > this.playbin.duration) new_position = this.playbin.duration;
|
2024-10-13 16:22:44 +00:00
|
|
|
this.seek_impl (new_position);
|
2024-10-13 08:14:40 +00:00
|
|
|
}
|
2024-10-06 11:21:53 +00:00
|
|
|
}
|