Skip to content

Commit e90b56f

Browse files
jeremypwJeremy Woottenzeebok
authored
Store and restore active-project-path explicitly (#1472)
* 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>
1 parent b234902 commit e90b56f

8 files changed

Lines changed: 140 additions & 106 deletions

File tree

data/io.elementary.code.gschema.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@
152152
<summary>Remember the last focused document.</summary>
153153
<description>Restore the focused document from a previous session when opening Code.</description>
154154
</key>
155+
<key name="active-project-path" type="s">
156+
<default>''</default>
157+
<summary>The active project path.</summary>
158+
<description>The path to the folder containing the active project.</description>
159+
</key>
155160
<key name="default-build-directory" type="s">
156161
<default>''</default>
157162
<summary>The default build directory's relative path.</summary>

src/FolderManager/FileView.vala

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
3535
public const string ACTION_CHANGE_BRANCH = "change-branch";
3636
public const string ACTION_CLOSE_FOLDER = "close-folder";
3737
public const string ACTION_CLOSE_OTHER_FOLDERS = "close-other-folders";
38+
public const string ACTION_SET_ACTIVE_PROJECT = "set-active-project";
3839

3940
private const ActionEntry[] ACTION_ENTRIES = {
4041
{ ACTION_LAUNCH_APP_WITH_FILE_PATH, action_launch_app_with_file_path, "as" },
@@ -46,7 +47,8 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
4647
{ ACTION_NEW_FILE, add_new_file, "s" },
4748
{ ACTION_NEW_FOLDER, add_new_folder, "s"},
4849
{ ACTION_CLOSE_FOLDER, action_close_folder, "s"},
49-
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"}
50+
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"},
51+
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"}
5052
};
5153

5254
private GLib.Settings settings;
@@ -60,11 +62,6 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
6062
public ActionGroup toplevel_action_group { get; private set; }
6163
public string icon_name { get; set; }
6264
public string title { get; set; }
63-
public string active_project_path {
64-
get {
65-
return git_manager.active_project_path;
66-
}
67-
}
6865

6966
public FileView (Scratch.Services.PluginsManager plugins_manager) {
7067
plugins = plugins_manager;
@@ -119,10 +116,29 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
119116
if (project_folder_item != folder_root) {
120117
toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (project_folder_item.path));
121118
root.remove (project_folder_item);
122-
Scratch.Services.GitManager.get_instance ().remove_project (project_folder_item);
119+
git_manager.remove_project (project_folder_item);
123120
}
124121
}
125122

123+
//Make remaining project the active one
124+
git_manager.active_project_path = path;
125+
126+
write_settings ();
127+
}
128+
129+
private void action_set_active_project (SimpleAction action, GLib.Variant? parameter) {
130+
var path = parameter.get_string ();
131+
if (path == null || path == "") {
132+
return;
133+
}
134+
135+
var folder_root = find_path (root, path) as ProjectFolderItem;
136+
if (folder_root == null) {
137+
return;
138+
}
139+
140+
git_manager.active_project_path = path;
141+
126142
write_settings ();
127143
}
128144

@@ -176,14 +192,9 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
176192
selected = null;
177193
}
178194

179-
public void collapse_other_projects (string? keep_open_path = null) {
195+
public void collapse_other_projects () {
180196
unowned string path;
181-
if (keep_open_path == null) {
182-
path = git_manager.active_project_path;
183-
} else {
184-
path = keep_open_path;
185-
git_manager.active_project_path = path;
186-
}
197+
path = git_manager.active_project_path;
187198

188199
foreach (var child in root.children) {
189200
var project_folder = ((ProjectFolderItem) child);

src/FolderManager/ProjectFolderItem.vala

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,28 @@ namespace Scratch.FolderManager {
134134
warning (e.message);
135135
}
136136

137-
var open_in_terminal_pane_item = new GLib.MenuItem (
138-
_("Open in Terminal Pane"),
139-
GLib.Action.print_detailed_name (
140-
MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
141-
new Variant.string (
142-
Services.GitManager.get_instance ().get_default_build_dir (path)
137+
MenuItem set_active_folder_item;
138+
if (is_git_repo) {
139+
set_active_folder_item = new GLib.MenuItem (
140+
_("Set as Active Project"),
141+
GLib.Action.print_detailed_name (
142+
FileView.ACTION_PREFIX + FileView.ACTION_SET_ACTIVE_PROJECT,
143+
new Variant.string (file.path)
143144
)
144-
)
145-
);
146-
open_in_terminal_pane_item.set_attribute_value (
145+
);
146+
} else {
147+
set_active_folder_item = new GLib.MenuItem (
148+
_("Open in Terminal Pane"),
149+
GLib.Action.print_detailed_name (
150+
MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
151+
new Variant.string (
152+
Services.GitManager.get_instance ().get_default_build_dir (path)
153+
)
154+
)
155+
);
156+
}
157+
158+
set_active_folder_item.set_attribute_value (
147159
"accel",
148160
Utils.get_accel_for_action (
149161
GLib.Action.print_detailed_name (
@@ -154,7 +166,7 @@ namespace Scratch.FolderManager {
154166
);
155167

156168
var external_actions_section = new GLib.Menu ();
157-
external_actions_section.append_item (open_in_terminal_pane_item);
169+
external_actions_section.append_item (set_active_folder_item);
158170
external_actions_section.append_item (create_submenu_for_open_in (file_type));
159171

160172
var folder_actions_section = new GLib.Menu ();

src/MainWindow.vala

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ namespace Scratch {
2727
public Scratch.Application app { get; private set; }
2828
public bool restore_docs { get; construct; }
2929
public RestoreOverride restore_override { get; construct set; }
30+
public string default_globalsearch_path {
31+
owned get {
32+
if (document_view.current_document != null) {
33+
if (document_view.current_document.project_path != "") {
34+
return document_view.current_document.project_path;
35+
}
36+
}
37+
38+
return git_manager.active_project_path;
39+
}
40+
}
3041

3142
public Scratch.Widgets.DocumentView document_view;
3243

@@ -610,7 +621,6 @@ namespace Scratch {
610621
title = _("%s - %s").printf (doc.get_basename (), base_title);
611622

612623
toolbar.set_document_focus (doc);
613-
git_manager.active_project_path = doc.source_view.project.path;
614624
folder_manager_view.select_path (doc.file.get_path ());
615625

616626
// Must follow setting focus document for editorconfig plug
@@ -1204,8 +1214,10 @@ namespace Scratch {
12041214
}
12051215

12061216
private void action_find_global (SimpleAction action, Variant? param) {
1217+
var selected_text = "";
1218+
var search_path = "";
1219+
12071220
var current_doc = get_current_document ();
1208-
string selected_text = "";
12091221
if (current_doc != null) {
12101222
selected_text = current_doc.get_selected_text (false);
12111223
}
@@ -1225,7 +1237,19 @@ namespace Scratch {
12251237
term = search_bar.search_entry.text;
12261238
}
12271239

1228-
folder_manager_view.search_global (get_target_path_for_actions (param), term);
1240+
if (param != null && param.get_string () != "") {
1241+
search_path = param.get_string ();
1242+
} else {
1243+
search_path = default_globalsearch_path;
1244+
}
1245+
1246+
if (search_path != "") {
1247+
folder_manager_view.search_global (search_path, term);
1248+
} else {
1249+
// Fallback to standard search
1250+
warning ("Unable to perform global search - search document instead");
1251+
action_fetch (action, param);
1252+
}
12291253
}
12301254

12311255
private void update_find_actions () {
@@ -1236,9 +1260,9 @@ namespace Scratch {
12361260
Utils.action_from_group (ACTION_SHOW_FIND, actions).set_enabled (is_current_doc);
12371261
Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc);
12381262
Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc);
1263+
var can_global_search = is_current_doc || git_manager.active_project_path != null;
1264+
Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (can_global_search);
12391265

1240-
var is_active_project = git_manager.active_project_path != "";
1241-
Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (is_active_project);
12421266
return Source.REMOVE;
12431267
});
12441268
}

src/Services/Document.vala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ namespace Scratch.Services {
6565
}
6666
}
6767

68+
public string project_path {
69+
owned get {
70+
if (source_view.project != null) {
71+
return source_view.project.path;
72+
} else {
73+
return "";
74+
}
75+
}
76+
}
77+
6878
private string _title = "";
6979
public string title {
7080
get { return _title; }

src/Services/GitManager.vala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ namespace Scratch.Services {
4444
return instance;
4545
}
4646

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

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

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

0 commit comments

Comments
 (0)