Skip to content

Commit

Permalink
Store and restore active-project-path explicitly (#1472)
Browse files Browse the repository at this point in the history
* Add "active-project-path" setting.

* Sync with GitManager and ProjectChooserButton

* Dont change active project just because a document changes

* Remove some unused code relating to active_project

* Do not override active project with default on startup

* Show global search dialog if scope unspecified

* Distinguish global search path from default action target

* Allow global search when no active project

* Set active project after load folder is none already set

* FileView: Use git_manager property

* Allow no project to be restored. Only user sets active project.

* On close other folders make remaining active

* Set active project context menu item for git repos

* Do not signal project-chosen twice for each change

* Do not call same code twice for each time a tab closes

* ProjectChooser: signal only on manual change

* Update popover only when activated;

* Remove debug code

---------

Co-authored-by: Jeremy Wootten <jeremy@Proteus-EL07R6-9b3c42bb.localdomain>
Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent b234902 commit e90b56f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 106 deletions.
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
39 changes: 25 additions & 14 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
public const string ACTION_CHANGE_BRANCH = "change-branch";
public const string ACTION_CLOSE_FOLDER = "close-folder";
public const string ACTION_CLOSE_OTHER_FOLDERS = "close-other-folders";
public const string ACTION_SET_ACTIVE_PROJECT = "set-active-project";

private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_LAUNCH_APP_WITH_FILE_PATH, action_launch_app_with_file_path, "as" },
Expand All @@ -46,7 +47,8 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
{ ACTION_NEW_FILE, add_new_file, "s" },
{ ACTION_NEW_FOLDER, add_new_folder, "s"},
{ ACTION_CLOSE_FOLDER, action_close_folder, "s"},
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"}
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"},
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"}
};

private GLib.Settings settings;
Expand All @@ -60,11 +62,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,10 +116,29 @@ 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);
}
}

//Make remaining project the active one
git_manager.active_project_path = path;

write_settings ();
}

private void action_set_active_project (SimpleAction action, GLib.Variant? parameter) {
var path = parameter.get_string ();
if (path == null || path == "") {
return;
}

var folder_root = find_path (root, path) as ProjectFolderItem;
if (folder_root == null) {
return;
}

git_manager.active_project_path = path;

write_settings ();
}

Expand Down Expand Up @@ -176,14 +192,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
32 changes: 22 additions & 10 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,28 @@ namespace Scratch.FolderManager {
warning (e.message);
}

var open_in_terminal_pane_item = new GLib.MenuItem (
_("Open in Terminal Pane"),
GLib.Action.print_detailed_name (
MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
new Variant.string (
Services.GitManager.get_instance ().get_default_build_dir (path)
MenuItem set_active_folder_item;
if (is_git_repo) {
set_active_folder_item = new GLib.MenuItem (
_("Set as Active Project"),
GLib.Action.print_detailed_name (
FileView.ACTION_PREFIX + FileView.ACTION_SET_ACTIVE_PROJECT,
new Variant.string (file.path)
)
)
);
open_in_terminal_pane_item.set_attribute_value (
);
} else {
set_active_folder_item = new GLib.MenuItem (
_("Open in Terminal Pane"),
GLib.Action.print_detailed_name (
MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
new Variant.string (
Services.GitManager.get_instance ().get_default_build_dir (path)
)
)
);
}

set_active_folder_item.set_attribute_value (
"accel",
Utils.get_accel_for_action (
GLib.Action.print_detailed_name (
Expand All @@ -154,7 +166,7 @@ namespace Scratch.FolderManager {
);

var external_actions_section = new GLib.Menu ();
external_actions_section.append_item (open_in_terminal_pane_item);
external_actions_section.append_item (set_active_folder_item);
external_actions_section.append_item (create_submenu_for_open_in (file_type));

var folder_actions_section = new GLib.Menu ();
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 @@ -610,7 +621,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 @@ -1204,8 +1214,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 @@ -1225,7 +1237,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 @@ -1236,9 +1260,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
Loading

0 comments on commit e90b56f

Please sign in to comment.