From b83d86fe91e68f47e0eaefb8b9712ab8e4c7d187 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 23 Jan 2024 11:31:32 +0000 Subject: [PATCH 1/6] Deactivate all before select new active project. --- src/Widgets/ChooseProjectButton.vala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index 601ce2746..77e0c1099 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -116,6 +116,13 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { project_listbox.row_activated.connect ((row) => { var project_entry = ((ProjectRow) row); + int index = 0; + ProjectRow project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); + while (project_row != null) { + project_row.active = false; + project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); + } + select_project (project_entry); project_chosen (); }); From 036f5a393a24c1dfed0296655a7afbc127f7b266 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 23 Jan 2024 11:36:17 +0000 Subject: [PATCH 2/6] Use var --- src/Widgets/ChooseProjectButton.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index 77e0c1099..442a9df28 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -117,7 +117,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { project_listbox.row_activated.connect ((row) => { var project_entry = ((ProjectRow) row); int index = 0; - ProjectRow project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); + var project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); while (project_row != null) { project_row.active = false; project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); From d70d40e96379a394ecb517da0ec328dd4017326a Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 24 Jan 2024 11:52:47 +0000 Subject: [PATCH 3/6] Use row_selected, untangle,comment --- src/Widgets/ChooseProjectButton.vala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index 442a9df28..b199c8835 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -114,7 +114,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } }); - project_listbox.row_activated.connect ((row) => { + project_listbox.row_selected.connect ((row) => { var project_entry = ((ProjectRow) row); int index = 0; var project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); @@ -123,6 +123,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); } + project_entry.active = true; select_project (project_entry); project_chosen (); }); @@ -130,6 +131,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { private Gtk.Widget create_project_row (Scratch.FolderManager.ProjectFolderItem project_folder) { var project_row = new ProjectRow (project_folder.file.file.get_path ()); + // Handle renaming of project; project_folder.bind_property ("name", project_row.project_radio, "label", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE, (binding, srcval, ref targetval) => { var label = srcval.get_string (); @@ -137,6 +139,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { if (project_row.active) { label_widget.label = label; } + return true; } ); @@ -150,12 +153,11 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } private void select_project (ProjectRow project_entry) { - project_listbox.select_row (project_entry); label_widget.label = project_entry.project_name; var tooltip_text = Scratch.Utils.replace_home_with_tilde (project_entry.project_path); label_widget.tooltip_text = _("Active Git project: %s").printf (tooltip_text); - project_entry.active = true; Scratch.Services.GitManager.get_instance ().active_project_path = project_entry.project_path; + project_listbox.select_row (project_entry); } public void set_document (Scratch.Services.Document doc) { From 829f127433e9a41704a5f7359f0e622321edc9ce Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 24 Jan 2024 14:59:13 +0000 Subject: [PATCH 4/6] Simplify, clarify, fix grouping --- src/MainWindow.vala | 10 ++- src/Widgets/ChooseProjectButton.vala | 94 +++++++++++++++------------- 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index fd7ac259f..ed785fe9a 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -107,6 +107,7 @@ namespace Scratch { private ulong color_scheme_listener_handler_id = 0; + private Services.GitManager git_manager; private const ActionEntry[] ACTION_ENTRIES = { { ACTION_FIND, action_fetch, "s" }, @@ -236,6 +237,7 @@ namespace Scratch { default_theme.add_resource_path ("/io/elementary/code"); document_manager = Scratch.Services.DocumentManager.get_instance (); + git_manager = Services.GitManager.get_instance (); actions = new SimpleActionGroup (); actions.add_action_entries (ACTION_ENTRIES, this); @@ -586,7 +588,9 @@ namespace Scratch { title = _("%s - %s").printf (doc.get_basename (), base_title); toolbar.set_document_focus (doc); - sidebar.choose_project_button.set_document (doc); + // sidebar.choose_project_button.set_document (doc); + warning ("WINDOW: set active project _path %s", doc.source_view.project.path); + git_manager.active_project_path = doc.source_view.project.path; folder_manager_view.select_path (doc.file.get_path ()); // Must follow setting focus document for editorconfig plug @@ -1162,7 +1166,7 @@ namespace Scratch { Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc); Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc); - var is_active_project = Services.GitManager.get_instance ().active_project_path != ""; + var is_active_project = git_manager.active_project_path != ""; Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (is_active_project); return Source.REMOVE; }); @@ -1330,7 +1334,7 @@ namespace Scratch { } if (path == "") { // Happens when keyboard accelerator is used - path = Services.GitManager.get_instance ().active_project_path; + path = git_manager.active_project_path; if (path == null) { var current_doc = get_current_document (); if (current_doc != null) { diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index b199c8835..301f25a97 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -20,7 +20,17 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { private const string NO_PROJECT_SELECTED = N_("No Project Selected"); private Gtk.Label label_widget; private Gtk.ListBox project_listbox; - private ProjectRow? last_entry = null; + public unowned Gtk.RadioButton? group_source { + get { + var first_row = project_listbox.get_row_at_index (0); + if (first_row != null) { + return ((ProjectRow)first_row).project_radio; + } else { + return null; + } + } + } + public signal void project_chosen (); construct { @@ -86,9 +96,8 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { popover = project_popover; - var model = Scratch.Services.GitManager.get_instance ().project_liststore; - - model.items_changed.connect ((pos, n_removed, n_added) => { + var git_manager = Scratch.Services.GitManager.get_instance (); + git_manager.project_liststore.items_changed.connect ((src, pos, n_removed, n_added) => { var rows = project_listbox.get_children (); for (int index = (int)pos; index < pos + n_removed; index++) { var row = rows.nth_data (index); @@ -96,12 +105,18 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } for (int index = (int)pos; index < pos + n_added; index++) { - var item = model.get_object (index); + var item = src.get_object (index); if (item is Scratch.FolderManager.ProjectFolderItem) { var row = create_project_row ((Scratch.FolderManager.ProjectFolderItem)item); project_listbox.insert (row, index); } } + + set_active_path (git_manager.active_project_path); + }); + + git_manager.notify["active-project-path"].connect (() => { + set_active_path (git_manager.active_project_path); }); project_listbox.remove.connect ((row) => { @@ -114,23 +129,28 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } }); - project_listbox.row_selected.connect ((row) => { + project_listbox.row_activated.connect ((row) => { var project_entry = ((ProjectRow) row); - int index = 0; - var project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); - while (project_row != null) { - project_row.active = false; - project_row = ((ProjectRow)(project_listbox.get_row_at_index (index++))); - } - + label_widget.label = project_entry.project_name; + var tooltip_text = Scratch.Utils.replace_home_with_tilde (project_entry.project_path); + label_widget.tooltip_text = _("Active Git project: %s").printf (tooltip_text); + Scratch.Services.GitManager.get_instance ().active_project_path = project_entry.project_path; project_entry.active = true; - select_project (project_entry); project_chosen (); }); } + private void set_active_path (string active_path) { + project_listbox.get_children ().foreach ((child) => { + var project_entry = ((ProjectRow) child); + if (active_path.has_prefix (project_entry.project_path)) { + project_listbox.row_activated (project_entry); + } + }); + } + private Gtk.Widget create_project_row (Scratch.FolderManager.ProjectFolderItem project_folder) { - var project_row = new ProjectRow (project_folder.file.file.get_path ()); + var project_row = new ProjectRow (project_folder.file.file.get_path (), group_source); // Handle renaming of project; project_folder.bind_property ("name", project_row.project_radio, "label", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE, (binding, srcval, ref targetval) => { @@ -144,37 +164,25 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } ); - if (last_entry != null) { - project_row.project_radio.join_group (last_entry.project_radio); - } - last_entry = project_row; - return project_row; } private void select_project (ProjectRow project_entry) { - label_widget.label = project_entry.project_name; - var tooltip_text = Scratch.Utils.replace_home_with_tilde (project_entry.project_path); - label_widget.tooltip_text = _("Active Git project: %s").printf (tooltip_text); - Scratch.Services.GitManager.get_instance ().active_project_path = project_entry.project_path; - project_listbox.select_row (project_entry); } - public void set_document (Scratch.Services.Document doc) { - set_active_path (doc.file.get_path ()); - } + public class ProjectRow : Gtk.ListBoxRow { + public bool active { + get { + return project_radio.active; + } - public void set_active_path (string active_path) { - project_listbox.get_children ().foreach ((child) => { - var project_entry = ((ProjectRow) child); - if (active_path.has_prefix (project_entry.project_path)) { - select_project (project_entry); + set { + if (value) { + project_radio.active = true; + } } - }); - } + } - public class ProjectRow : Gtk.ListBoxRow { - public bool active { get; set; } public string project_path { get; construct; } public string project_name { get { @@ -184,9 +192,10 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { public Gtk.RadioButton project_radio { get; construct; } - public ProjectRow (string project_path) { + public ProjectRow (string project_path, Gtk.RadioButton? group_source ) { Object ( - project_path: project_path + project_path: project_path, + project_radio: new Gtk.RadioButton.with_label_from_widget (group_source, Path.get_basename (project_path)) ); } @@ -195,16 +204,13 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { } construct { - project_radio = new Gtk.RadioButton.with_label (null, Path.get_basename (project_path)); add (project_radio); - show_all (); - - bind_property ("active", project_radio, "active", BindingFlags.BIDIRECTIONAL); - project_radio.button_release_event.connect (() => { activate (); return Gdk.EVENT_PROPAGATE; }); + + show_all (); } } } From f500dc67f533b85276fc38b2f2a2cad2203680c5 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 24 Jan 2024 15:09:55 +0000 Subject: [PATCH 5/6] remove extraneous code --- src/MainWindow.vala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index ed785fe9a..9aa943ac4 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -588,8 +588,6 @@ namespace Scratch { title = _("%s - %s").printf (doc.get_basename (), base_title); toolbar.set_document_focus (doc); - // sidebar.choose_project_button.set_document (doc); - warning ("WINDOW: set active project _path %s", doc.source_view.project.path); git_manager.active_project_path = doc.source_view.project.path; folder_manager_view.select_path (doc.file.get_path ()); From 6d26744dfdabe330d524fa8dcf02593c7570bb87 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 24 Jan 2024 15:12:54 +0000 Subject: [PATCH 6/6] Remove more extraneous code --- src/Widgets/ChooseProjectButton.vala | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Widgets/ChooseProjectButton.vala b/src/Widgets/ChooseProjectButton.vala index 301f25a97..5f450d09d 100644 --- a/src/Widgets/ChooseProjectButton.vala +++ b/src/Widgets/ChooseProjectButton.vala @@ -167,9 +167,6 @@ public class Code.ChooseProjectButton : Gtk.MenuButton { return project_row; } - private void select_project (ProjectRow project_entry) { - } - public class ProjectRow : Gtk.ListBoxRow { public bool active { get {