Skip to content

Commit 6c57279

Browse files
committed
MainWindow: replace ListBox with ListView
Don't subclass Gtk.ListBoxRow unbind properly
1 parent d54129a commit 6c57279

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ config_file = configure_file(
2020
)
2121

2222
adw_dep = dependency('libadwaita-1', version: '>=1.4.0')
23-
granite_dep = dependency('granite-7')
23+
granite_dep = dependency('granite-7', version: '>=7.6.0')
2424
gstreamer_dep = dependency('gstreamer-1.0')
2525
gstreamer_pbutils_dep = dependency('gstreamer-pbutils-1.0')
2626
gstreamer_tag_dep = dependency('gstreamer-tag-1.0')

src/MainWindow.vala

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
*/
55

66
public class Music.MainWindow : Gtk.ApplicationWindow {
7+
private Granite.Placeholder queue_placeholder;
78
private Gtk.Button repeat_button;
89
private Gtk.Button shuffle_button;
9-
private Settings settings;
10-
private Gtk.SearchEntry search_entry;
10+
private Gtk.ListView queue_listview;
1111
private Gtk.Revealer search_revealer;
12+
private Gtk.ScrolledWindow scrolled;
13+
private Gtk.SearchEntry search_entry;
14+
private Gtk.SingleSelection selection_model;
15+
private Gtk.Stack queue_stack;
16+
private Settings settings;
1217

1318
construct {
1419
var playback_manager = PlaybackManager.get_default ();
@@ -42,22 +47,29 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
4247
queue_header.pack_end (shuffle_button);
4348
queue_header.pack_end (repeat_button);
4449

45-
var queue_placeholder = new Granite.Placeholder (_("Queue is Empty")) {
50+
queue_placeholder = new Granite.Placeholder (_("Queue is Empty")) {
4651
description = _("Audio files opened from Files will appear here"),
4752
icon = new ThemedIcon ("playlist-queue")
4853
};
4954

50-
var queue_listbox = new Gtk.ListBox () {
55+
selection_model = new Gtk.SingleSelection (playback_manager.queue_liststore);
56+
57+
var factory = new Gtk.SignalListItemFactory ();
58+
59+
queue_listview = new Gtk.ListView (selection_model, factory) {
60+
single_click_activate = true,
5161
hexpand = true,
5262
vexpand = true
5363
};
54-
queue_listbox.bind_model (playback_manager.queue_liststore, create_queue_row);
55-
queue_listbox.set_placeholder (queue_placeholder);
5664

57-
var scrolled = new Gtk.ScrolledWindow () {
58-
child = queue_listbox
65+
scrolled = new Gtk.ScrolledWindow () {
66+
child = queue_listview
5967
};
6068

69+
queue_stack = new Gtk.Stack ();
70+
queue_stack.add_child (queue_placeholder);
71+
queue_stack.add_child (scrolled);
72+
6173
var drop_target = new Gtk.DropTarget (typeof (Gdk.FileList), Gdk.DragAction.COPY);
6274

6375
var add_button_label = new Gtk.Label (_("Open Files…"));
@@ -98,7 +110,7 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
98110

99111
var queue = new Adw.ToolbarView () {
100112
bottom_bar_style = RAISED,
101-
content = scrolled
113+
content = queue_stack
102114
};
103115
queue.add_controller (drop_target);
104116
queue.add_css_class (Granite.STYLE_CLASS_VIEW);
@@ -201,25 +213,39 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
201213
}
202214
});
203215

204-
queue_listbox.row_activated.connect ((row) => {
205-
playback_manager.current_audio = ((TrackRow) row).audio_object;
216+
factory.setup.connect ((obj) => {
217+
var list_item = (Gtk.ListItem) obj;
218+
list_item.child = new TrackRow ();
219+
});
220+
221+
factory.bind.connect ((obj) => {
222+
var list_item = (Gtk.ListItem) obj;
223+
((TrackRow) list_item.child).audio_object = (AudioObject) list_item.item;
224+
});
225+
226+
factory.unbind.connect ((obj) => {
227+
var list_item = (Gtk.ListItem) obj;
228+
((TrackRow) list_item.child).audio_object = null;
229+
});
230+
231+
queue_listview.activate.connect ((index) => {
232+
playback_manager.current_audio = (AudioObject) selection_model.get_item (index);
206233
});
207234

235+
selection_model.items_changed.connect (on_items_changed);
236+
208237
search_entry.search_changed.connect (() => {
209238
int pos = playback_manager.find_title (search_entry.text);
210239
if (pos >= 0) {
211-
queue_listbox.select_row (queue_listbox.get_row_at_index (pos));
212-
var adj = scrolled.vadjustment;
213-
// Search entry is hidden if n_items is zero so no need to check
214-
var ratio = (double)pos / (double)playback_manager.n_items;
215-
adj.@value = adj.upper * ratio;
240+
queue_listview.scroll_to (pos, SELECT, null);
216241
}
217242
});
218243

219244
search_entry.activate.connect (() => {
220-
var selected = queue_listbox.get_selected_row ();
221-
if (selected != null) {
222-
selected.activate ();
245+
var selected = selection_model.get_selected ();
246+
if (selected != -1) {
247+
var selected_audio = (AudioObject) selection_model.get_item (selected);
248+
playback_manager.current_audio = selected_audio;
223249
}
224250
});
225251
}
@@ -301,10 +327,11 @@ public class Music.MainWindow : Gtk.ApplicationWindow {
301327
}
302328
}
303329

304-
private Gtk.Widget create_queue_row (GLib.Object object) {
305-
unowned var audio_object = (AudioObject) object;
306-
return new TrackRow () {
307-
audio_object = audio_object
308-
};
330+
private void on_items_changed () {
331+
if (selection_model.n_items > 0) {
332+
queue_stack.visible_child = scrolled;
333+
} else {
334+
queue_stack.visible_child = queue_placeholder;
335+
}
309336
}
310337
}

src/Widgets/TrackRow.vala

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,42 @@
33
* SPDX-FileCopyrightText: 2021 elementary, Inc. (https://elementary.io)
44
*/
55

6-
public class Music.TrackRow : Gtk.ListBoxRow {
7-
public AudioObject audio_object { get; set ; }
6+
public class Music.TrackRow : Granite.Bin {
7+
private AudioObject _audio_object = null;
8+
public AudioObject audio_object {
9+
get {
10+
return _audio_object;
11+
}
12+
13+
set {
14+
if (_audio_object != null) {
15+
artist_binding.unbind ();
16+
texture_binding.unbind ();
17+
title_binding.unbind ();
18+
}
19+
20+
_audio_object = value;
21+
22+
if (_audio_object == null) {
23+
return;
24+
}
25+
26+
artist_binding = _audio_object.bind_property ("artist", artist_label, "label", SYNC_CREATE);
27+
title_binding = _audio_object.bind_property ("title", title_label, "label", SYNC_CREATE);
28+
texture_binding = _audio_object.bind_property ("texture", album_image.image, "paintable", SYNC_CREATE);
29+
}
30+
}
831

932
private static PlaybackManager playback_manager;
1033

34+
private Binding artist_binding;
35+
private Binding texture_binding;
36+
private Binding title_binding;
37+
38+
private Gtk.Label artist_label;
39+
private Gtk.Label title_label;
1140
private Gtk.Spinner play_icon;
41+
private Music.AlbumImage album_image;
1242

1343
static construct {
1444
playback_manager = PlaybackManager.get_default ();
@@ -18,17 +48,17 @@ public class Music.TrackRow : Gtk.ListBoxRow {
1848
play_icon = new Gtk.Spinner ();
1949
play_icon.add_css_class ("play-indicator");
2050

21-
var album_image = new Music.AlbumImage ();
51+
album_image = new Music.AlbumImage ();
2252
album_image.image.height_request = 32;
2353
album_image.image.width_request = 32;
2454

25-
var title_label = new Gtk.Label (null) {
55+
title_label = new Gtk.Label (null) {
2656
ellipsize = Pango.EllipsizeMode.MIDDLE,
2757
hexpand = true,
2858
xalign = 0
2959
};
3060

31-
var artist_label = new Gtk.Label (null) {
61+
artist_label = new Gtk.Label (null) {
3262
ellipsize = Pango.EllipsizeMode.MIDDLE,
3363
hexpand = true,
3464
xalign = 0
@@ -64,10 +94,6 @@ public class Music.TrackRow : Gtk.ListBoxRow {
6494
});
6595

6696
notify["audio-object"].connect (() => {
67-
audio_object.bind_property ("artist", artist_label, "label", SYNC_CREATE);
68-
audio_object.bind_property ("title", title_label, "label", SYNC_CREATE);
69-
audio_object.bind_property ("texture", album_image.image, "paintable", SYNC_CREATE);
70-
7197
play_icon.spinning = playback_manager.current_audio == audio_object;
7298
});
7399
}

0 commit comments

Comments
 (0)