Skip to content

Commit

Permalink
Add/remove project_liststore items properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Paul Wootten authored and Jeremy Paul Wootten committed Aug 17, 2023
1 parent e436721 commit 9ed4a76
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/Widgets/ChooseProjectButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9ed4a76

Please sign in to comment.