expose play queue length as playbin property

This commit is contained in:
Erica Z 2024-10-28 10:26:12 +01:00
parent e925dc33cc
commit 7799566758
3 changed files with 8 additions and 7 deletions

View file

@ -54,8 +54,9 @@ public class Playbin : GLib.Object {
public weak Subsonic.Client api { get; set; default = null; } public weak Subsonic.Client api { get; set; default = null; }
public ListStore _play_queue; private ListStore _play_queue = new ListStore (typeof (Subsonic.Song));
public ListModel play_queue { get { return this._play_queue; } } public ListModel play_queue { get { return this._play_queue; } }
public uint play_queue_length { get; private set; default = 0; }
// try to prevent wait_event to be called twice // try to prevent wait_event to be called twice
private bool is_handling_event = false; private bool is_handling_event = false;
@ -71,8 +72,6 @@ public class Playbin : GLib.Object {
} }
public Playbin () { public Playbin () {
this._play_queue = new ListStore (typeof (Subsonic.Song));
assert (this.mpv.initialize () >= 0); assert (this.mpv.initialize () >= 0);
assert (this.mpv.set_property_string ("audio-client-name", "audrey") >= 0); assert (this.mpv.set_property_string ("audio-client-name", "audrey") >= 0);
assert (this.mpv.set_property_string ("user-agent", Audrey.Const.user_agent) >= 0); assert (this.mpv.set_property_string ("user-agent", Audrey.Const.user_agent) >= 0);
@ -280,6 +279,7 @@ public class Playbin : GLib.Object {
{ {
assert (this.mpv.command({"playlist-remove", position.to_string ()}) >= 0); assert (this.mpv.command({"playlist-remove", position.to_string ()}) >= 0);
this._play_queue.remove (position); this._play_queue.remove (position);
this.play_queue_length -= 1;
if (this.play_queue_position > position) this.play_queue_position -= 1; if (this.play_queue_position > position) this.play_queue_position -= 1;
if (this.play_queue_position == this._play_queue.get_n_items ()) { if (this.play_queue_position == this._play_queue.get_n_items ()) {
// we just killed the last track // we just killed the last track
@ -296,6 +296,7 @@ public class Playbin : GLib.Object {
} }
this.state = PlaybinState.STOPPED; this.state = PlaybinState.STOPPED;
this._play_queue.remove_all (); this._play_queue.remove_all ();
this.play_queue_length = 0;
this.play_queue_position = -1; this.play_queue_position = -1;
this.stopped (); this.stopped ();
@ -308,6 +309,7 @@ public class Playbin : GLib.Object {
"append", "append",
}) >= 0); }) >= 0);
this._play_queue.append (song); this._play_queue.append (song);
this.play_queue_length += 1;
} }
public async void append_track_async (Subsonic.Song song) { public async void append_track_async (Subsonic.Song song) {
@ -319,6 +321,7 @@ public class Playbin : GLib.Object {
assert (err >= 0); assert (err >= 0);
this._play_queue.append (song); this._play_queue.append (song);
this.play_queue_length += 1;
} }
public void move_track (uint from, uint to) public void move_track (uint from, uint to)

View file

@ -1,12 +1,11 @@
using Gtk 4.0; using Gtk 4.0;
using Adw 1; using Adw 1;
using Gio 2.0;
template $UiPlayQueue: Adw.Bin { template $UiPlayQueue: Adw.Bin {
name: "play-queue"; name: "play-queue";
child: Stack { child: Stack {
visible-child-name: bind $visible_child_name (template.playbin as <$Playbin>.play_queue as <Gio.ListStore>.n-items) as <string>; visible-child-name: bind $visible_child_name (template.playbin as <$Playbin>.play-queue-length) as <string>;
StackPage { StackPage {
name: "empty"; name: "empty";

View file

@ -1,6 +1,5 @@
using Gtk 4.0; using Gtk 4.0;
using Adw 1; using Adw 1;
using Gio 2.0;
template $UiPlaybar: Adw.Bin { template $UiPlaybar: Adw.Bin {
child: CenterBox { child: CenterBox {
@ -120,7 +119,7 @@ template $UiPlaybar: Adw.Bin {
Button { Button {
icon-name: bind $play_pause_icon_name (template.playbin as <$Playbin>.state as <$PlaybinState>) as <string>; icon-name: bind $play_pause_icon_name (template.playbin as <$Playbin>.state as <$PlaybinState>) as <string>;
valign: center; valign: center;
sensitive: bind $can_press_play (template.playbin as <$Playbin>.state as <$PlaybinState>, template.playbin as <$Playbin>.play_queue as <Gio.ListStore>.n-items) as <bool>; sensitive: bind $can_press_play (template.playbin as <$Playbin>.state as <$PlaybinState>, template.playbin as <$Playbin>.play-queue-length) as <bool>;
clicked => $on_play_pause_clicked (); clicked => $on_play_pause_clicked ();
} }