Skip to content

Commit

Permalink
Merge branch 'master' into fix-rename-in-sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
colinkiama authored Jan 24, 2024
2 parents 520717d + 753a3c5 commit df2a52c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
8 changes: 5 additions & 3 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -602,7 +604,7 @@ namespace Scratch {
title = _("%s - %s").printf (doc.get_basename (), base_title);

toolbar.set_document_focus (doc);
sidebar.choose_project_button.set_document (doc);
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
Expand Down Expand Up @@ -1178,7 +1180,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;
});
Expand Down Expand Up @@ -1346,7 +1348,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) {
Expand Down
92 changes: 52 additions & 40 deletions src/Widgets/ChooseProjectButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -86,22 +96,27 @@ 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);
row.destroy ();
}

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) => {
Expand All @@ -116,56 +131,55 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {

project_listbox.row_activated.connect ((row) => {
var project_entry = ((ProjectRow) row);
select_project (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_entry.active = true;
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) => {
var label = srcval.get_string ();
targetval.set_string (label);
if (project_row.active) {
label_widget.label = label;
}

return true;
}
);

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) {
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;
}

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 {
Expand All @@ -175,9 +189,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))
);
}

Expand All @@ -186,16 +201,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 ();
}
}
}

0 comments on commit df2a52c

Please sign in to comment.