-
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
// 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.ListBox zone_list;
[GtkChild]
private unowned Artwork artwork;
private Gee.HashMap<string, ZoneSelectorRow>zone_rows = new Gee.HashMap<string, ZoneSelectorRow>();
private bool is_seeking = false;
private int64? next_seek = null;
private string? zone_id = null;
private Settings settings = new Settings();
private Plac.Transport.Zone? zone {
get {
var row = zone_rows[zone_id];
if (row == null) {
return null;
}
return row.zone;
}
set {
if (value == null) {
zone_id = null;
} else {
zone_id = value.id;
}
zone_selected(value);
next_seek = 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) {
foreach (string id in event.removed) {
GLib.log("Plac", LEVEL_DEBUG, "Zone id=%s removed", id);
if (zone_rows.has_key(id)) {
var existing = zone_rows[id];
zone_rows.unset(id);
zone_list.remove(existing);
}
}
foreach (Plac.Transport.Zone zone in event.added) {
if (zone_rows.has_key(zone.id)) {
var existing = zone_rows[zone.id];
existing.zone = zone;
existing.render();
} else {
var row = new ZoneSelectorRow(zone);
zone_rows[zone.id] = row;
zone_list.append(row);
if (zone_id == null) {
this.zone = zone;
}
}
}
foreach (Plac.Transport.Zone zone in event.changed) {
if (zone_rows.has_key(zone.id)) {
var existing = zone_rows[zone.id];
existing.zone = zone;
existing.render();
} else {
var row = new ZoneSelectorRow(zone);
zone_rows[zone.id] = row;
zone_list.append(row);
if (zone_id == null) {
this.zone = zone;
}
}
}
foreach (Plac.Transport.SeekChange change in event.seek_changed) {
if (zone_rows.has_key(change.zone_id)) {
var zone = zone_rows[change.zone_id].zone;
if (zone.now_playing != null) {
zone.now_playing.seek_position = change.seek_position;
zone.now_playing.has_seek_position = true;
}
}
}
// Selected zone has been removed.
if (zone_id != null && !zone_rows.has_key(zone_id)) {
this.zone = null;
foreach (var entry in zone_rows) {
this.zone = entry.value.zone;
break;
}
}
this.render();
}
public PlaybackToolbar() {
Object();
}
construct {
zone_list.row_activated.connect((row) => {
this.zone = ((ZoneSelectorRow) row).zone;
zone_list_popover.popdown();
this.render();
});
play.clicked.connect(() => {
var row = zone_rows[zone_id];
if (conn == null || row == null) {
return;
}
this.disable_actions();
conn.control.begin(row.zone, Plac.Transport.ACTION_PLAY, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
pause.clicked.connect(() => {
var row = zone_rows[zone_id];
if (conn == null || row == null) {
return;
}
this.disable_actions();
conn.control.begin(row.zone, Plac.Transport.ACTION_PAUSE, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
prev.clicked.connect(() => {
var row = zone_rows[zone_id];
if (conn == null || row == null) {
return;
}
this.disable_actions();
conn.control.begin(row.zone, Plac.Transport.ACTION_PREV, (obj, res) => {
conn.control.end(res);
this.enable_actions();
});
});
next.clicked.connect(() => {
var row = zone_rows[zone_id];
if (conn == null || row == null) {
return;
}
this.disable_actions();
conn.control.begin(row.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;
}
}
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;
}
}
class ZoneSelectorRow : Gtk.ListBoxRow {
private Gtk.Label label;
public Plac.Transport.Zone zone { get; construct set; }
public ZoneSelectorRow(Plac.Transport.Zone zone) {
Object(zone: zone);
}
construct {
label = new Gtk.Label(zone.name);
this.child = label;
this.activatable = true;
}
public void render() {
this.label.label = zone.name;
}
}
}