-
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
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
namespace PlacGtkAdwaita {
[GtkTemplate(ui = "/jp/pocka/plac/gtk-adwaita/ui/playback-toolbar.ui")]
class PlaybackToolbar : Gtk.Box {
public signal void zone_selected(Plac.Transport.Zone? zone);
[GtkChild]
private unowned Gtk.Box controls;
[GtkChild]
private unowned Gtk.Button play;
[GtkChild]
private unowned Gtk.Button pause;
[GtkChild]
private unowned Gtk.Button prev;
[GtkChild]
private unowned Gtk.Button next;
[GtkChild]
private unowned Gtk.Button seek_backwards_10;
[GtkChild]
private unowned Gtk.Button seek_forward_10;
[GtkChild]
private unowned Gtk.Scale seek;
[GtkChild]
private unowned Gtk.Label playing_line1;
[GtkChild]
private unowned Gtk.Label playing_line2;
[GtkChild]
private unowned Gtk.Popover zone_list_popover;
[GtkChild]
private unowned Gtk.DropDown zone_list;
[GtkChild]
private unowned Gtk.ListBox zone_outputs;
[GtkChild]
private unowned Artwork artwork;
private GLib.ListStore zones = new GLib.ListStore(typeof (ZoneWrapper));
private bool is_seeking = false;
private int64? next_seek = null;
private ZoneWrapper? _selected = null;
private ZoneWrapper? selected {
get { return _selected; }
set {
_selected = value;
zone_selected(value.zone);
next_seek = null;
}
}
private Settings settings = new Settings();
private Plac.Transport.Zone? zone {
get {
if (selected != null) {
return selected.zone;
}
return null;
}
}
private string? zone_id {
get {
if (zone != null) {
return zone.id;
}
return null;
}
}
public Plac.AsyncConnection? conn {
get {
return artwork.conn;
}
set {
if (artwork.conn != null) {
artwork.conn.zones_changed.disconnect(on_zone_change);
}
artwork.conn = value;
value.zones_changed.connect(on_zone_change);
}
}
private void on_zone_change(Plac.Transport.ZoneListEvent event) {
bool should_set_zone = this.selected == null;
foreach (string id in event.removed) {
for (int i = 0; i < zones.n_items; i++) {
var item = (ZoneWrapper) zones.get_item(i);
if (item != null && item.zone.id == id) {
zones.remove(i);
if (zone_id == id) {
should_set_zone = true;
}
break;
}
}
GLib.log("Plac", LEVEL_DEBUG, "Zone id=%s removed", id);
}
foreach (Plac.Transport.Zone zone in event.added) {
upsert_zone(zone);
}
foreach (Plac.Transport.Zone zone in event.changed) {
upsert_zone(zone);
}
foreach (Plac.Transport.SeekChange change in event.seek_changed) {
for (int i = 0; i < zones.n_items; i++) {
var item = (ZoneWrapper?) zones.get_item(i);
if (item != null && item.zone.id == change.zone_id) {
var zone = item.zone;
if (zone.now_playing != null) {
zone.now_playing.seek_position = change.seek_position;
zone.now_playing.has_seek_position = true;
}
}
}
}
if (should_set_zone) {
var first = zones.get_item(0);
if (first != null) {
this.selected = (ZoneWrapper) first;
} else {
this.selected = null;
}
}
this.render();
}
private void upsert_zone(Plac.Transport.Zone zone) {
var payload = new ZoneWrapper(zone);
GLib.EqualFunc<ZoneWrapper>eq = ZoneWrapper.is_equal;
uint found_position = 0;
var found = zones.find_with_equal_func(payload, eq, out found_position);
if (!found) {
zones.append(payload);
return;
}
var item = (ZoneWrapper) zones.get_item(found_position);
item.zone = zone;
}
public PlaybackToolbar() {
Object();
}
construct {
var zone_list_factory = new Gtk.SignalListItemFactory();
zone_list_factory.setup.connect((item) => {
var label = new Gtk.Label("");
((Gtk.ListItem) item).child = label;
});
zone_list_factory.bind.connect((item) => {
var list_item = (Gtk.ListItem) item;
var label = (Gtk.Label) list_item.child;
var wrapper = (ZoneWrapper) list_item.item;
label.label = wrapper.zone.name;
});
zone_list.model = zones;
zone_list.factory = zone_list_factory;
zone_list.notify["selected"].connect(() => {
this.selected = (ZoneWrapper) zones.get_item(zone_list.selected);
this.render();
});
play.clicked.connect(() => {
if (conn == null || zone == null) {
return;
}
this.disable_actions();
conn.control.begin(zone, Plac.Transport.ACTION_PLAY, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
pause.clicked.connect(() => {
if (conn == null || zone == null) {
return;
}
this.disable_actions();
conn.control.begin(zone, Plac.Transport.ACTION_PAUSE, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
prev.clicked.connect(() => {
if (conn == null || zone == null) {
return;
}
this.disable_actions();
conn.control.begin(zone, Plac.Transport.ACTION_PREV, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
next.clicked.connect(() => {
if (conn == null || zone == null) {
return;
}
this.disable_actions();
conn.control.begin(zone, Plac.Transport.ACTION_NEXT, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
seek.change_value.connect((scroll, value) => {
schedule_seek((int64) value);
return false;
});
seek_backwards_10.clicked.connect(() => {
var next_value = seek.get_value() - 10;
seek.set_value(next_value);
schedule_seek((int64) next_value);
});
seek_forward_10.clicked.connect(() => {
var next_value = seek.get_value() + 10;
seek.set_value(next_value);
schedule_seek((int64) next_value);
});
seek.set_increments(1, 10);
settings.settings.bind(Settings.SHOW_SEEK_BY_10SECS, seek_backwards_10, "visible", GET);
settings.settings.bind(Settings.SHOW_SEEK_BY_10SECS, seek_forward_10, "visible", GET);
this.render();
}
private void dequeue_seek() {
if (conn == null || next_seek == null || zone == null) {
is_seeking = false;
return;
}
var position = next_seek;
next_seek = null;
conn.seek.begin(zone, position, (obj, res) => {
var result = conn.seek.end(res);
if (result != OK) {
GLib.log("Plac", LEVEL_WARNING, "Failed to seek: %s", result.to_string());
}
dequeue_seek();
});
}
private void schedule_seek(int64 position) {
next_seek = position;
if (!is_seeking) {
is_seeking = true;
dequeue_seek();
}
}
private string format_seconds(uint64 secs) {
var s = secs % 60;
var m = (secs / 60) % 60;
var h = secs / 360;
if (h == 0) {
return "%02u:%02u".printf((uint) m, (uint) s);
}
// I don't care 32 bit platforms. Also I doubt Roon can handle a song
// of a duration longer than max(uint32)
return "%02u:%02u:%02u".printf((uint) h, (uint) m, (uint) s);
}
private void render() {
var zone = this.zone;
if (zone == null) {
play.visible = true;
pause.visible = false;
return;
}
play.sensitive = (zone.allowed_action & Plac.Transport.ACTION_PLAY) != 0;
pause.sensitive = (zone.allowed_action & Plac.Transport.ACTION_PAUSE) != 0;
prev.sensitive = (zone.allowed_action & Plac.Transport.ACTION_PREV) != 0;
next.sensitive = (zone.allowed_action & Plac.Transport.ACTION_NEXT) != 0;
if (zone.now_playing != null) {
playing_line1.label = zone.now_playing.two_line_line1;
playing_line2.label = zone.now_playing.two_line_line2 != null
? zone.now_playing.two_line_line2
: "";
artwork.image_key = zone.now_playing.image_key;
} else {
playing_line1.label = "";
playing_line2.label = "";
artwork.image_key = null;
}
if (
zone.now_playing != null &&
zone.now_playing.has_length &&
zone.now_playing.has_seek_position
) {
seek.sensitive = true;
seek.set_range(0, zone.now_playing.length);
seek.set_value(zone.now_playing.seek_position);
seek.tooltip_text = format_seconds(zone.now_playing.seek_position);
} else {
seek.sensitive = false;
seek.set_range(0, 1);
seek.set_value(0);
seek.tooltip_text = "Not playing";
}
switch (zone.playback) {
case LOADING:
case STOPPED:
case PAUSED:
play.visible = true;
pause.visible = false;
break;
case PLAYING:
play.visible = false;
pause.visible = true;
break;
}
stale_outputs();
foreach (var output in zone.outputs) {
update_output(output);
}
remove_stale_outputs();
}
private void stale_outputs() {
int i = 0;
while (true) {
var row = (ZoneOutputRow?) zone_outputs.get_row_at_index(i++);
if (row == null) {
return;
}
row.stale = true;
}
}
private void remove_stale_outputs() {
var rows_to_remove = new Gee.ArrayList<ZoneOutputRow>();
int i = 0;
while (true) {
var row = (ZoneOutputRow?) zone_outputs.get_row_at_index(i++);
if (row == null) {
break;
}
if (row.stale) {
rows_to_remove.add(row);
}
}
foreach (var row in rows_to_remove) {
zone_outputs.remove(row);
}
}
private void update_output(Plac.Transport.Output output) {
int i = 0;
while (true) {
var row = (ZoneOutputRow?) zone_outputs.get_row_at_index(i++);
if (row == null) {
zone_outputs.append(new ZoneOutputRow(output));
return;
}
if (row.output_id != output.id) {
continue;
}
row.output = output;
row.stale = false;
return;
}
}
private void disable_actions() {
zone_list_popover.sensitive = false;
controls.sensitive = false;
seek.sensitive = false;
}
private void enable_actions() {
zone_list_popover.sensitive = true;
controls.sensitive = true;
seek.sensitive = true;
}
}
// GLib and its ecosystem heavily relies on GObject.
// Wrapping a class to avoid GLib from infecting to other parts.
class ZoneWrapper : Object {
public Plac.Transport.Zone zone { get; set construct; }
public ZoneWrapper(Plac.Transport.Zone zone) {
Object(zone: zone);
}
public static bool is_equal(ZoneWrapper a, ZoneWrapper b) {
return a.zone.id == b.zone.id;
}
}
}