From 9ed4a76f91875b1a8053013c3fdd43266c3414d9 Mon Sep 17 00:00:00 2001 From: Jeremy Paul Wootten Date: Thu, 17 Aug 2023 20:25:55 +0100 Subject: [PATCH] Add/remove project_liststore items properly --- src/Widgets/ChooseProjectButton.vala | 40 +++++++++++----------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index 2c78c5476f..216a03b89c 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -21,7 +21,6 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { private Gtk.Label label_widget; private Gtk.ListBox project_listbox; private ProjectRow? last_entry = null; - public signal void project_chosen (); construct { @@ -90,18 +89,22 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { var model = Scratch.Services.GitManager.get_instance ().project_liststore; model.items_changed.connect ((pos, n_removed, n_added) => { - // This model is put in sort order by the GitManager so model pos the same as listbox index - var project_folder = (Scratch.FolderManager.ProjectFolderItem)(model.get_item (pos)); - if (n_added > 0) { - var row = create_project_row (project_folder); - project_listbox.insert (row, (int)pos); - } else { - // Double check we are removing correct row (do not rely on pos) - var row = find_row_for_path (project_folder.file.file.get_path ()); - if (row != null) { - project_listbox.remove (row); - } + var rows = project_listbox.get_children (); + for (int index = (int)pos; index < pos + n_removed; index++) { + var row = rows.nth_data (index); + row.destroy (); } + + Idle.add (() => { + for (int index = (int)pos; index < pos + n_added; index++) { + var item = model.get_object (index); + if (item is Scratch.FolderManager.ProjectFolderItem) { + var row = create_project_row ((Scratch.FolderManager.ProjectFolderItem)item); + project_listbox.insert (row, index); + } + } + return Source.REMOVE; + }); }); project_listbox.remove.connect ((row) => { @@ -142,19 +145,6 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { return project_row; } - private ProjectRow? find_row_for_path (string project_path) { - foreach (var child in project_listbox.get_children ()) { - if (child is ProjectRow) { - var row = (ProjectRow)child; - if (row.project_path == project_path) { - return row; - } - } - } - - return null; - } - private void select_project (ProjectRow project_entry) { project_listbox.select_row (project_entry); label_widget.label = project_entry.project_name;