Skip to content

Commit 580aefc

Browse files
danirabbitJeremy Wootten
andauthored
Sidebar Gtk4 Prep (#2287)
Co-authored-by: Jeremy Wootten <[email protected]>
1 parent 2a924ca commit 580aefc

File tree

8 files changed

+123
-132
lines changed

8 files changed

+123
-132
lines changed

libcore/Interfaces/SidebarInterface.vala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ public interface Files.SidebarInterface : Gtk.Widget {
3636
/* Plugin interface */
3737
public abstract uint32 add_plugin_item (Files.SidebarPluginItem item, Files.PlaceType category);
3838
public abstract bool update_plugin_item (Files.SidebarPluginItem item, uint32 item_id);
39-
public abstract bool remove_item_by_id (uint32 item_id); //Returns true if successfully removed
4039
/* Window interface */
41-
public signal void request_update ();
4240
public signal bool request_focus ();
4341
public signal void sync_needed ();
4442
public signal void path_change_request (string uri, Files.OpenFlag flag);

libcore/Interfaces/SidebarItemInterface.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Authors : Jeremy Wootten <[email protected]>
2222
*/
2323

24-
public interface Sidebar.SidebarItemInterface : Gtk.Widget {
24+
public interface Sidebar.SidebarItemInterface : Object {
2525
/* Non constant static members must be initialised in implementing class */
2626
protected static uint32 row_id;
2727
protected static Gee.HashMap<uint32, SidebarItemInterface> item_id_map;
Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,45 @@
11
/*
2-
* SidebarListInterface.vala
3-
*
4-
* Copyright 2020 elementary LLC. <https://elementary.io>
5-
*
6-
* This program is free software; you can redistribute it and/or modify
7-
* it under the terms of the GNU General Public License as published by
8-
* the Free Software Foundation; either version 2 of the License, or
9-
* (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU General Public License
17-
* along with this program; if not, write to the Free Software
18-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19-
* MA 02110-1301, USA.
2+
* SPDX-License-Identifier: GPL-2.0+
3+
* SPDX-FileCopyrightText: 2020-2023 elementary, Inc. (https://elementary.io)
204
*
215
* Authors : Jeremy Wootten <[email protected]>
226
*/
237

24-
public interface Sidebar.SidebarListInterface : Gtk.Container {
8+
public interface Sidebar.SidebarListInterface : Object {
259
public abstract Files.SidebarInterface sidebar { get; construct; }
10+
public abstract Gtk.ListBox list_box { get; internal set; }
2611

27-
public abstract void select_item (SidebarItemInterface? item);
12+
public abstract void select_item (Gtk.ListBoxRow? item);
2813
public abstract void unselect_all_items ();
2914

30-
public virtual void open_item (SidebarItemInterface item, Files.OpenFlag flag = Files.OpenFlag.DEFAULT) {
15+
public virtual void open_item (SidebarItemInterface item, Files.OpenFlag flag = DEFAULT) {
3116
sidebar.path_change_request (item.uri, flag);
3217
}
3318

3419
public abstract void refresh (); //Clear and recreate all rows
3520
public virtual void refresh_info () {} //Update all rows without recreating them
3621

37-
public virtual uint32 add_plugin_item (Files.SidebarPluginItem plugin_item) {return 0;}
22+
public virtual uint32 add_plugin_item (Files.SidebarPluginItem plugin_item) {
23+
return 0;
24+
}
3825

3926
public virtual void clear () {
40-
foreach (Gtk.Widget child in get_children ()) {
41-
remove (child);
27+
foreach (Gtk.Widget child in list_box.get_children ()) {
28+
list_box.remove (child);
4229
if (child is SidebarItemInterface) {
43-
((SidebarItemInterface)child).destroy_bookmark ();
30+
((SidebarItemInterface) child).destroy_bookmark ();
4431
}
4532
}
4633
}
4734

4835
public virtual void rename_bookmark_by_uri (string uri, string new_name) {}
4936

50-
public virtual void remove_bookmark_by_uri (string uri) {
51-
SidebarItemInterface? row = null;
52-
if (has_uri (uri, out row)) {
53-
if (row.permanent) {
54-
return;
55-
}
56-
remove (row);
57-
row.destroy_bookmark ();
58-
}
59-
}
60-
61-
public virtual bool has_uri (string uri, out unowned SidebarItemInterface? row = null) {
37+
public virtual bool has_uri (string uri, out unowned Gtk.ListBoxRow? row = null) {
6238
row = null;
63-
foreach (unowned Gtk.Widget child in get_children ()) {
39+
foreach (unowned var child in list_box.get_children ()) {
6440
if (child is SidebarItemInterface) {
6541
if (((SidebarItemInterface)child).uri == uri) {
66-
row = (SidebarItemInterface)child;
42+
row = (Gtk.ListBoxRow) child;
6743
return true;
6844
}
6945
}
@@ -74,7 +50,7 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {
7450

7551
public virtual bool select_uri (string uri) {
7652
unselect_all_items ();
77-
SidebarItemInterface? row = null;
53+
Gtk.ListBoxRow? row = null;
7854
if (has_uri (uri, out row)) {
7955
select_item (row);
8056
return true;
@@ -84,15 +60,15 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {
8460
}
8561

8662
public virtual bool remove_item_by_id (uint32 id) {
87-
foreach (Gtk.Widget child in get_children ()) {
63+
foreach (unowned var child in list_box.get_children ()) {
8864
if (child is SidebarItemInterface) {
8965
unowned var row = (SidebarItemInterface)child;
9066
if (row.permanent) {
9167
continue;
9268
}
9369

9470
if (row.id == id) {
95-
remove (row);
71+
list_box.remove (child);
9672
row.destroy_bookmark ();
9773
return true;
9874
}
@@ -109,8 +85,11 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {
10985

11086
/* Second parameter is index of target after which the item should be inserted */
11187
public virtual bool move_item_after (SidebarItemInterface item, int target_index) {
112-
return false;
113-
} // By default not-reorderable
88+
return false; // By default not-reorderable
89+
}
11490

115-
public virtual bool is_drop_target () { return false; } // Whether can drop rows or uris onto list itself (as opposed to onto rows in list)
91+
// Whether can drop rows or uris onto list itself (as opposed to onto rows in list)
92+
public virtual bool is_drop_target () {
93+
return false;
94+
}
11695
}

src/View/Sidebar/BookmarkListBox.vala

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,45 @@
2020
* Authors : Jeremy Wootten <[email protected]>
2121
*/
2222

23-
public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface {
23+
public class Sidebar.BookmarkListBox : Gtk.Box, Sidebar.SidebarListInterface {
24+
public Files.SidebarInterface sidebar { get; construct; }
25+
public Gtk.ListBox list_box { get; internal set; }
26+
2427
private Files.BookmarkList bookmark_list;
2528
private unowned Files.TrashMonitor trash_monitor;
2629

27-
public Files.SidebarInterface sidebar {get; construct;}
28-
2930
public BookmarkListBox (Files.SidebarInterface sidebar) {
30-
Object (
31-
sidebar: sidebar
32-
);
31+
Object (sidebar: sidebar);
3332
}
3433

3534
construct {
36-
hexpand = true;
37-
selection_mode = Gtk.SelectionMode.SINGLE;
35+
list_box = new Gtk.ListBox () {
36+
hexpand = true,
37+
selection_mode = Gtk.SelectionMode.SINGLE
38+
};
39+
40+
add (list_box);
41+
3842
trash_monitor = Files.TrashMonitor.get_default ();
3943
bookmark_list = Files.BookmarkList.get_instance ();
4044
bookmark_list.loaded.connect (() => {
4145
refresh ();
4246
});
43-
row_activated.connect ((row) => {
44-
if (row is SidebarItemInterface) {
45-
((SidebarItemInterface) row).activated ();
47+
48+
list_box.row_activated.connect ((row) => {
49+
if (row is BookmarkRow) {
50+
((BookmarkRow) row).activated ();
4651
}
4752
});
48-
row_selected.connect ((row) => {
49-
if (row is SidebarItemInterface) {
50-
select_item ((SidebarItemInterface) row);
53+
54+
list_box.row_selected.connect ((row) => {
55+
if (row is BookmarkRow) {
56+
select_item (row);
5157
}
5258
});
5359
}
5460

55-
public SidebarItemInterface? add_bookmark (string label,
61+
public BookmarkRow? add_bookmark (string label,
5662
string uri,
5763
Icon gicon,
5864
bool pinned = false,
@@ -61,7 +67,7 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
6167
return insert_bookmark (label, uri, gicon, -1, pinned, permanent);
6268
}
6369

64-
private SidebarItemInterface? insert_bookmark (string label,
70+
private BookmarkRow? insert_bookmark (string label,
6571
string uri,
6672
Icon gicon,
6773
int index,
@@ -74,9 +80,9 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
7480

7581
var row = new BookmarkRow (label, uri, gicon, this, pinned, permanent);
7682
if (index >= 0) {
77-
insert (row, index);
83+
list_box.insert (row, index);
7884
} else {
79-
add (row);
85+
list_box.add (row);
8086
}
8187

8288
return row;
@@ -94,22 +100,22 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
94100
}
95101

96102

97-
public void select_item (SidebarItemInterface? item) {
103+
public void select_item (Gtk.ListBoxRow? item) {
98104
if (item != null && item is BookmarkRow) {
99-
select_row ((BookmarkRow)item);
105+
list_box.select_row (item);
100106
} else {
101107
unselect_all_items ();
102108
}
103109
}
104110

105111
public void unselect_all_items () {
106-
unselect_all ();
112+
list_box.unselect_all ();
107113
}
108114

109115
public void refresh () {
110116
clear ();
111117

112-
SidebarItemInterface? row;
118+
BookmarkRow? row;
113119
var home_uri = "";
114120
try {
115121
home_uri = GLib.Filename.to_uri (PF.UserUtils.get_real_user_home (), null);
@@ -189,8 +195,8 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
189195
int pos = 0) {
190196

191197
int pinned = 0; // Assume pinned items only at start and end of list
192-
foreach (unowned Gtk.Widget child in get_children ()) {
193-
if (((SidebarItemInterface)child).pinned) {
198+
foreach (unowned var child in list_box.get_children ()) {
199+
if (((BookmarkRow)child).pinned) {
194200
pinned++;
195201
} else {
196202
break;
@@ -212,11 +218,11 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
212218

213219
public override bool remove_item_by_id (uint32 id) {
214220
bool removed = false;
215-
this.@foreach ((child) => {
216-
if (child is SidebarItemInterface) {
217-
unowned var row = (SidebarItemInterface)child;
221+
list_box.@foreach ((child) => {
222+
if (child is BookmarkRow) {
223+
unowned var row = (BookmarkRow)child;
218224
if (!row.permanent && row.id == id) {
219-
remove (row);
225+
list_box.remove (row);
220226
bookmark_list.delete_items_with_uri (row.uri); //Assumes no duplicates
221227
removed = true;
222228
}
@@ -227,11 +233,11 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
227233
}
228234

229235
public SidebarItemInterface? get_item_at_index (int index) {
230-
if (index < 0 || index > get_children ().length ()) {
236+
if (index < 0 || index > list_box.get_children ().length ()) {
231237
return null;
232238
}
233239

234-
return (SidebarItemInterface?)(get_row_at_index (index));
240+
return (SidebarItemInterface?) list_box.get_row_at_index (index);
235241
}
236242

237243
public override bool move_item_after (SidebarItemInterface item, int target_index) {
@@ -244,12 +250,12 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
244250
return false;
245251
}
246252

247-
remove (item);
253+
list_box.remove ((Gtk.ListBoxRow) item);
248254

249255
if (old_index > target_index) {
250-
insert (item, ++target_index);
256+
list_box.insert ((Gtk.ListBoxRow) item, ++target_index);
251257
} else {
252-
insert (item, target_index);
258+
list_box.insert ((Gtk.ListBoxRow) item, target_index);
253259
}
254260

255261
bookmark_list.move_item_uri (item.uri, target_index - old_index);

src/View/Sidebar/BookmarkRow.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public class Sidebar.BookmarkRow : Gtk.ListBoxRow, SidebarItemInterface {
364364

365365
drag_failed.connect ((ctx, res) => {
366366
if (res == Gtk.DragResult.NO_TARGET) {
367-
Gdk.Window app_window = list.get_window ().get_effective_toplevel ();
367+
Gdk.Window app_window = list.list_box.get_window ().get_effective_toplevel ();
368368
Gdk.Window drag_window = ctx.get_drag_window ();
369369
Gdk.Rectangle app_rect, drag_rect, intersect_rect;
370370

0 commit comments

Comments
 (0)