Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and restore active-project-path explicitly #1472

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions data/io.elementary.code.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
<summary>Remember the last focused document.</summary>
<description>Restore the focused document from a previous session when opening Code.</description>
</key>
<key name="active-project-path" type="s">
<default>''</default>
<summary>The active project path.</summary>
<description>The path to the folder containing the active project.</description>
</key>
<key name="default-build-directory" type="s">
<default>''</default>
<summary>The default build directory's relative path.</summary>
Expand Down
21 changes: 7 additions & 14 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
public ActionGroup toplevel_action_group { get; private set; }
public string icon_name { get; set; }
public string title { get; set; }
public string active_project_path {
get {
return git_manager.active_project_path;
}
}

public FileView (Scratch.Services.PluginsManager plugins_manager) {
plugins = plugins_manager;
Expand Down Expand Up @@ -119,7 +114,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
if (project_folder_item != folder_root) {
toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (project_folder_item.path));
root.remove (project_folder_item);
Scratch.Services.GitManager.get_instance ().remove_project (project_folder_item);
git_manager.remove_project (project_folder_item);
}
}

Expand Down Expand Up @@ -176,14 +171,9 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
selected = null;
}

public void collapse_other_projects (string? keep_open_path = null) {
public void collapse_other_projects () {
unowned string path;
if (keep_open_path == null) {
path = git_manager.active_project_path;
} else {
path = keep_open_path;
git_manager.active_project_path = path;
}
path = git_manager.active_project_path;

foreach (var child in root.children) {
var project_folder = ((ProjectFolderItem) child);
Expand Down Expand Up @@ -528,10 +518,13 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
rename_items_with_same_name (child_folder);
}
}
Scratch.Services.GitManager.get_instance ().remove_project (folder_root);

git_manager.remove_project (folder_root);
write_settings ();
});

// Do not assume this is the active folder

write_settings ();
}

Expand Down
34 changes: 29 additions & 5 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ namespace Scratch {
public Scratch.Application app { get; private set; }
public bool restore_docs { get; construct; }
public RestoreOverride restore_override { get; construct set; }
public string default_globalsearch_path {
owned get {
if (document_view.current_document != null) {
if (document_view.current_document.project_path != "") {
return document_view.current_document.project_path;
}
}

return git_manager.active_project_path;
}
}

public Scratch.Widgets.DocumentView document_view;

Expand Down Expand Up @@ -618,7 +629,6 @@ namespace Scratch {
title = _("%s - %s").printf (doc.get_basename (), base_title);

toolbar.set_document_focus (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 @@ -1200,8 +1210,10 @@ namespace Scratch {
}

private void action_find_global (SimpleAction action, Variant? param) {
var selected_text = "";
var search_path = "";

var current_doc = get_current_document ();
string selected_text = "";
if (current_doc != null) {
selected_text = current_doc.get_selected_text (false);
}
Expand All @@ -1221,7 +1233,19 @@ namespace Scratch {
term = search_bar.search_entry.text;
}

folder_manager_view.search_global (get_target_path_for_actions (param), term);
if (param != null && param.get_string () != "") {
search_path = param.get_string ();
} else {
search_path = default_globalsearch_path;
}

if (search_path != "") {
folder_manager_view.search_global (search_path, term);
} else {
// Fallback to standard search
warning ("Unable to perform global search - search document instead");
action_fetch (action, param);
}
}

private void update_find_actions () {
Expand All @@ -1232,9 +1256,9 @@ namespace Scratch {
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (is_current_doc);
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 can_global_search = is_current_doc || git_manager.active_project_path != null;
Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (can_global_search);

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
10 changes: 10 additions & 0 deletions src/Services/Document.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ namespace Scratch.Services {
}
}

public string project_path {
owned get {
if (source_view.project != null) {
return source_view.project.path;
} else {
return "";
}
}
}

private string _title = "";
public string title {
get { return _title; }
Expand Down
6 changes: 3 additions & 3 deletions src/Services/GitManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ namespace Scratch.Services {
return instance;
}

private GitManager () {
construct {
// Used to populate the ChooseProject popover in sorted order
project_liststore = new ListStore (typeof (FolderManager.ProjectFolderItem));
settings.bind ("active-project-path", this, "active-project-path", DEFAULT);
}

public MonitoredRepository? add_project (FolderManager.ProjectFolderItem root_folder) {
Expand All @@ -72,8 +73,7 @@ namespace Scratch.Services {
);
}

//Ensure active_project_path always set
active_project_path = root_path;
// No longer need to set default project (restored from settings or left unset)
return project_gitrepo_map.@get (root_path);
}

Expand Down
88 changes: 34 additions & 54 deletions src/Widgets/ChooseProjectButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@

public class Code.ChooseProjectButton : Gtk.MenuButton {
private const string NO_PROJECT_SELECTED = N_("No Project Selected");
private const string PROJECT_TOOLTIP = N_("Active Git Project: %s");
private Gtk.Label label_widget;
private Gtk.ListBox project_listbox;
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 ();

Expand All @@ -44,8 +35,6 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
xalign = 0.0f
};

tooltip_text = _("Active Git project: %s").printf (_(NO_PROJECT_SELECTED));

var grid = new Gtk.Grid () {
halign = Gtk.Align.START
};
Expand Down Expand Up @@ -97,6 +86,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
popover = project_popover;

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++) {
Expand All @@ -111,89 +101,78 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
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) => {
var project_row = row as ProjectRow;
var current_project = Scratch.Services.GitManager.get_instance ().active_project_path;
if (project_row.project_path == current_project) {
label_widget.label = _(NO_PROJECT_SELECTED);
label_widget.tooltip_text = _("Active Git project: %s").printf (_(NO_PROJECT_SELECTED));
Scratch.Services.GitManager.get_instance ().active_project_path = "";
// Label and active_path will be updated automatically
}
});

project_listbox.row_activated.connect ((row) => {
var project_entry = ((ProjectRow) row);
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 ();
});

realize.connect (() => {
set_active_path (git_manager.active_project_path);
git_manager.notify["active-project-path"].connect (() => {
set_active_path (git_manager.active_project_path);
});
});
}

// Set appearance (only) of project chooser button and list according to active path
private void set_active_path (string active_path) {
foreach (var child in project_listbox.get_children ()) {
var project_entry = ((ProjectRow) child);
if (active_path.has_prefix (project_entry.project_path + Path.DIR_SEPARATOR_S)) {
project_listbox.row_activated (project_entry);
break;
}
var project_row = ((ProjectRow) child);
// All paths must not end in directory separator so can be compared directly
project_row.active = active_path == project_row.project_path;
}

if (active_path != "") {
label_widget.label = Path.get_basename (active_path);
tooltip_text = _(PROJECT_TOOLTIP).printf (Scratch.Utils.replace_home_with_tilde (active_path));
} else {
label_widget.label = Path.get_basename (_(NO_PROJECT_SELECTED));
tooltip_text = _(PROJECT_TOOLTIP).printf (_(NO_PROJECT_SELECTED));
}
}

private Gtk.Widget create_project_row (Scratch.FolderManager.ProjectFolderItem project_folder) {
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;
}
);
var project_path = project_folder.file.file.get_path ();
var project_row = new ProjectRow (project_path);
// Project folder items cannot be renamed in UI, no need to handle

return project_row;
}

public class ProjectRow : Gtk.ListBoxRow {
private Gtk.CheckButton check_button;
public bool active {
get {
return project_radio.active;
return check_button.active;
}

set {
if (value) {
project_radio.active = true;
}
check_button.active = value;
}
}

public string project_path { get; construct; }
public string project_name {
get {
return project_radio.label;
return check_button.label;
}
}

public Gtk.RadioButton project_radio { get; construct; }

public ProjectRow (string project_path, Gtk.RadioButton? group_source ) {
public ProjectRow (string project_path) {
Object (
project_path: project_path,
project_radio: new Gtk.RadioButton.with_label_from_widget (group_source, Path.get_basename (project_path))
project_path: project_path
);
}

Expand All @@ -202,8 +181,9 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
}

construct {
add (project_radio);
project_radio.button_release_event.connect (() => {
check_button = new Gtk.CheckButton.with_label (Path.get_basename (project_path));
add (check_button);
check_button.button_release_event.connect (() => {
activate ();
return Gdk.EVENT_PROPAGATE;
});
Expand Down