-
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
// 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.Button play;
[GtkChild]
private unowned Gtk.Button pause;
[GtkChild]
private unowned Gtk.Button prev;
[GtkChild]
private unowned Gtk.Button next;
[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;
private Gee.HashMap<string, ZoneSelectorRow>zone_rows = new Gee.HashMap<string, ZoneSelectorRow>();
private string? zone_id = null;
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);
}
}
public PlaybackToolbar() {
Object();
}
construct {
zone_list.row_activated.connect((row) => {
this.zone = ((ZoneSelectorRow) row).zone;
zone_list_popover.popdown();
this.render();
});
this.render();
}
public void listen(Plac.AsyncConnection conn) {
play.clicked.connect(() => {
var row = zone_rows[zone_id];
if (row == null) {
return;
}
conn.control(row.zone, Plac.Transport.ACTION_PLAY);
});
pause.clicked.connect(() => {
var row = zone_rows[zone_id];
if (row == null) {
return;
}
conn.control(row.zone, Plac.Transport.ACTION_PAUSE);
});
prev.clicked.connect(() => {
var row = zone_rows[zone_id];
if (row == null) {
return;
}
conn.control(row.zone, Plac.Transport.ACTION_PREV);
});
next.clicked.connect(() => {
var row = zone_rows[zone_id];
if (row == null) {
return;
}
conn.control(row.zone, Plac.Transport.ACTION_NEXT);
});
conn.zones_changed.connect((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) {
GLib.log("Plac", LEVEL_DEBUG, "Zone (%s) id=%s added", zone.name, zone.id);
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) {
GLib.log("Plac", LEVEL_DEBUG, "Zone (%s) id=%s changed", zone.name, zone.id);
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();
});
}
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
: "";
} else {
playing_line1.label = "";
playing_line2.label = "";
}
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);
} else {
seek.sensitive = false;
seek.set_range(0, 1);
seek.set_value(0);
}
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;
}
}
}
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;
}
}
}