Skip to content

Commit

Permalink
Sidebar Gtk4 Prep (#2287)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremy Wootten <jeremy@elementaryos.org>
  • Loading branch information
danirabbit and Jeremy Wootten authored Aug 23, 2023
1 parent 2a924ca commit 580aefc
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 132 deletions.
2 changes: 0 additions & 2 deletions libcore/Interfaces/SidebarInterface.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public interface Files.SidebarInterface : Gtk.Widget {
/* Plugin interface */
public abstract uint32 add_plugin_item (Files.SidebarPluginItem item, Files.PlaceType category);
public abstract bool update_plugin_item (Files.SidebarPluginItem item, uint32 item_id);
public abstract bool remove_item_by_id (uint32 item_id); //Returns true if successfully removed
/* Window interface */
public signal void request_update ();
public signal bool request_focus ();
public signal void sync_needed ();
public signal void path_change_request (string uri, Files.OpenFlag flag);
Expand Down
2 changes: 1 addition & 1 deletion libcore/Interfaces/SidebarItemInterface.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Authors : Jeremy Wootten <jeremy@elementaryos.org>
*/

public interface Sidebar.SidebarItemInterface : Gtk.Widget {
public interface Sidebar.SidebarItemInterface : Object {
/* Non constant static members must be initialised in implementing class */
protected static uint32 row_id;
protected static Gee.HashMap<uint32, SidebarItemInterface> item_id_map;
Expand Down
69 changes: 24 additions & 45 deletions libcore/Interfaces/SidebarListInterface.vala
Original file line number Diff line number Diff line change
@@ -1,69 +1,45 @@
/*
* SidebarListInterface.vala
*
* Copyright 2020 elementary LLC. <https://elementary.io>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* SPDX-License-Identifier: GPL-2.0+
* SPDX-FileCopyrightText: 2020-2023 elementary, Inc. (https://elementary.io)
*
* Authors : Jeremy Wootten <jeremy@elementaryos.org>
*/

public interface Sidebar.SidebarListInterface : Gtk.Container {
public interface Sidebar.SidebarListInterface : Object {
public abstract Files.SidebarInterface sidebar { get; construct; }
public abstract Gtk.ListBox list_box { get; internal set; }

public abstract void select_item (SidebarItemInterface? item);
public abstract void select_item (Gtk.ListBoxRow? item);
public abstract void unselect_all_items ();

public virtual void open_item (SidebarItemInterface item, Files.OpenFlag flag = Files.OpenFlag.DEFAULT) {
public virtual void open_item (SidebarItemInterface item, Files.OpenFlag flag = DEFAULT) {
sidebar.path_change_request (item.uri, flag);
}

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

public virtual uint32 add_plugin_item (Files.SidebarPluginItem plugin_item) {return 0;}
public virtual uint32 add_plugin_item (Files.SidebarPluginItem plugin_item) {
return 0;
}

public virtual void clear () {
foreach (Gtk.Widget child in get_children ()) {
remove (child);
foreach (Gtk.Widget child in list_box.get_children ()) {
list_box.remove (child);
if (child is SidebarItemInterface) {
((SidebarItemInterface)child).destroy_bookmark ();
((SidebarItemInterface) child).destroy_bookmark ();
}
}
}

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

public virtual void remove_bookmark_by_uri (string uri) {
SidebarItemInterface? row = null;
if (has_uri (uri, out row)) {
if (row.permanent) {
return;
}
remove (row);
row.destroy_bookmark ();
}
}

public virtual bool has_uri (string uri, out unowned SidebarItemInterface? row = null) {
public virtual bool has_uri (string uri, out unowned Gtk.ListBoxRow? row = null) {
row = null;
foreach (unowned Gtk.Widget child in get_children ()) {
foreach (unowned var child in list_box.get_children ()) {
if (child is SidebarItemInterface) {
if (((SidebarItemInterface)child).uri == uri) {
row = (SidebarItemInterface)child;
row = (Gtk.ListBoxRow) child;
return true;
}
}
Expand All @@ -74,7 +50,7 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {

public virtual bool select_uri (string uri) {
unselect_all_items ();
SidebarItemInterface? row = null;
Gtk.ListBoxRow? row = null;
if (has_uri (uri, out row)) {
select_item (row);
return true;
Expand All @@ -84,15 +60,15 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {
}

public virtual bool remove_item_by_id (uint32 id) {
foreach (Gtk.Widget child in get_children ()) {
foreach (unowned var child in list_box.get_children ()) {
if (child is SidebarItemInterface) {
unowned var row = (SidebarItemInterface)child;
if (row.permanent) {
continue;
}

if (row.id == id) {
remove (row);
list_box.remove (child);
row.destroy_bookmark ();
return true;
}
Expand All @@ -109,8 +85,11 @@ public interface Sidebar.SidebarListInterface : Gtk.Container {

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

public virtual bool is_drop_target () { return false; } // Whether can drop rows or uris onto list itself (as opposed to onto rows in list)
// Whether can drop rows or uris onto list itself (as opposed to onto rows in list)
public virtual bool is_drop_target () {
return false;
}
}
72 changes: 39 additions & 33 deletions src/View/Sidebar/BookmarkListBox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,45 @@
* Authors : Jeremy Wootten <jeremy@elementaryos.org>
*/

public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface {
public class Sidebar.BookmarkListBox : Gtk.Box, Sidebar.SidebarListInterface {
public Files.SidebarInterface sidebar { get; construct; }
public Gtk.ListBox list_box { get; internal set; }

private Files.BookmarkList bookmark_list;
private unowned Files.TrashMonitor trash_monitor;

public Files.SidebarInterface sidebar {get; construct;}

public BookmarkListBox (Files.SidebarInterface sidebar) {
Object (
sidebar: sidebar
);
Object (sidebar: sidebar);
}

construct {
hexpand = true;
selection_mode = Gtk.SelectionMode.SINGLE;
list_box = new Gtk.ListBox () {
hexpand = true,
selection_mode = Gtk.SelectionMode.SINGLE
};

add (list_box);

trash_monitor = Files.TrashMonitor.get_default ();
bookmark_list = Files.BookmarkList.get_instance ();
bookmark_list.loaded.connect (() => {
refresh ();
});
row_activated.connect ((row) => {
if (row is SidebarItemInterface) {
((SidebarItemInterface) row).activated ();

list_box.row_activated.connect ((row) => {
if (row is BookmarkRow) {
((BookmarkRow) row).activated ();
}
});
row_selected.connect ((row) => {
if (row is SidebarItemInterface) {
select_item ((SidebarItemInterface) row);

list_box.row_selected.connect ((row) => {
if (row is BookmarkRow) {
select_item (row);
}
});
}

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

private SidebarItemInterface? insert_bookmark (string label,
private BookmarkRow? insert_bookmark (string label,
string uri,
Icon gicon,
int index,
Expand All @@ -74,9 +80,9 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface

var row = new BookmarkRow (label, uri, gicon, this, pinned, permanent);
if (index >= 0) {
insert (row, index);
list_box.insert (row, index);
} else {
add (row);
list_box.add (row);
}

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


public void select_item (SidebarItemInterface? item) {
public void select_item (Gtk.ListBoxRow? item) {
if (item != null && item is BookmarkRow) {
select_row ((BookmarkRow)item);
list_box.select_row (item);
} else {
unselect_all_items ();
}
}

public void unselect_all_items () {
unselect_all ();
list_box.unselect_all ();
}

public void refresh () {
clear ();

SidebarItemInterface? row;
BookmarkRow? row;
var home_uri = "";
try {
home_uri = GLib.Filename.to_uri (PF.UserUtils.get_real_user_home (), null);
Expand Down Expand Up @@ -189,8 +195,8 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
int pos = 0) {

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

public override bool remove_item_by_id (uint32 id) {
bool removed = false;
this.@foreach ((child) => {
if (child is SidebarItemInterface) {
unowned var row = (SidebarItemInterface)child;
list_box.@foreach ((child) => {
if (child is BookmarkRow) {
unowned var row = (BookmarkRow)child;
if (!row.permanent && row.id == id) {
remove (row);
list_box.remove (row);
bookmark_list.delete_items_with_uri (row.uri); //Assumes no duplicates
removed = true;
}
Expand All @@ -227,11 +233,11 @@ public class Sidebar.BookmarkListBox : Gtk.ListBox, Sidebar.SidebarListInterface
}

public SidebarItemInterface? get_item_at_index (int index) {
if (index < 0 || index > get_children ().length ()) {
if (index < 0 || index > list_box.get_children ().length ()) {
return null;
}

return (SidebarItemInterface?)(get_row_at_index (index));
return (SidebarItemInterface?) list_box.get_row_at_index (index);
}

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

remove (item);
list_box.remove ((Gtk.ListBoxRow) item);

if (old_index > target_index) {
insert (item, ++target_index);
list_box.insert ((Gtk.ListBoxRow) item, ++target_index);
} else {
insert (item, target_index);
list_box.insert ((Gtk.ListBoxRow) item, target_index);
}

bookmark_list.move_item_uri (item.uri, target_index - old_index);
Expand Down
2 changes: 1 addition & 1 deletion src/View/Sidebar/BookmarkRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public class Sidebar.BookmarkRow : Gtk.ListBoxRow, SidebarItemInterface {

drag_failed.connect ((ctx, res) => {
if (res == Gtk.DragResult.NO_TARGET) {
Gdk.Window app_window = list.get_window ().get_effective_toplevel ();
Gdk.Window app_window = list.list_box.get_window ().get_effective_toplevel ();
Gdk.Window drag_window = ctx.get_drag_window ();
Gdk.Rectangle app_rect, drag_rect, intersect_rect;

Expand Down
Loading

0 comments on commit 580aefc

Please sign in to comment.