Skip to content

Commit 6308224

Browse files
committed
Make active_project_path local per window
1 parent 70ff85c commit 6308224

6 files changed

Lines changed: 51 additions & 36 deletions

File tree

src/FolderManager/FileView.vala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
3939
public const string ACTION_SET_ACTIVE_PROJECT = "set-active-project";
4040

4141
private const ActionEntry[] PRIMARY_ONLY_ACTION_ENTRIES = {
42-
{ ACTION_SHOW_APP_CHOOSER, action_show_app_chooser, "s" },
4342
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"},
4443
{ ACTION_RENAME_FILE, action_rename_file, "s" },
4544
{ ACTION_RENAME_FOLDER, action_rename_folder, "s" }
4645
};
4746

4847
private const ActionEntry[] ACTION_ENTRIES = {
4948
{ ACTION_LAUNCH_APP_WITH_FILE_PATH, action_launch_app_with_file_path, "as" },
49+
{ ACTION_SHOW_APP_CHOOSER, action_show_app_chooser, "s" },
5050
{ ACTION_EXECUTE_CONTRACT_WITH_FILE_PATH, action_execute_contract_with_file_path, "as" },
5151
{ ACTION_DELETE, action_delete, "s" },
5252
{ ACTION_NEW_FILE, add_new_file, "s" },
@@ -57,6 +57,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
5757

5858
private GLib.Settings settings;
5959
private Scratch.Services.GitManager git_manager;
60+
private unowned Scratch.MainWindow window;
6061

6162
public Scratch.Services.PluginsManager plugins { get; construct; }
6263
public bool is_primary { get; construct; }
@@ -100,6 +101,10 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
100101
});
101102

102103
show_all ();
104+
105+
realize.connect (() => {
106+
window = (Scratch.MainWindow) (this.get_toplevel ());
107+
});
103108
}
104109

105110
private void action_close_folder (SimpleAction action, GLib.Variant? parameter) {
@@ -137,7 +142,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
137142
}
138143

139144
//Make remaining project the active one
140-
git_manager.active_project_path = path;
145+
window.active_project_path = path; //Temporary fix
141146

142147
write_settings ();
143148
}
@@ -153,7 +158,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
153158
return;
154159
}
155160

156-
git_manager.active_project_path = path;
161+
window.active_project_path = path;
157162

158163
write_settings ();
159164
}
@@ -210,7 +215,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
210215

211216
public void collapse_other_projects () {
212217
unowned string path;
213-
path = git_manager.active_project_path;
218+
path = window.active_project_path;
214219

215220
foreach (var child in root.children) {
216221
var project_folder = ((ProjectFolderItem) child);

src/FolderManager/FolderItem.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace Scratch.FolderManager {
116116
} else {
117117
var root = get_root_folder ();
118118
if (root != null &&
119-
root.monitored_repo != null) {
119+
root.has_monitored_repo) {
120120
//When toggled closed, update status to reflect hidden contents
121121
root.update_item_status (this);
122122
}

src/FolderManager/ProjectFolderItem.vala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ namespace Scratch.FolderManager {
3131

3232
public signal void closed ();
3333

34-
public Scratch.Services.MonitoredRepository? monitored_repo { get; private set; default = null; }
34+
// public Scratch.Services.MonitoredRepository? monitored_repo { get; private set; default = null; }
35+
private Scratch.Services.MonitoredRepository? monitored_repo;
36+
public bool has_monitored_repo {
37+
get {
38+
return monitored_repo != null;
39+
}
40+
}
3541
// Cache the visible item in the project.
3642
private List<VisibleItem?> visible_item_list = null;
3743

@@ -41,6 +47,7 @@ namespace Scratch.FolderManager {
4147
}
4248
}
4349

50+
public bool is_readonly { get; private set; }
4451
private Ggit.Repository? git_repo {
4552
get {
4653
return (is_git_repo ? monitored_repo.git_repo : null);
@@ -74,7 +81,7 @@ namespace Scratch.FolderManager {
7481
}
7582

7683
construct {
77-
monitored_repo = Scratch.Services.GitManager.get_instance ().add_project (this);
84+
is_readonly = Scratch.Services.GitManager.get_instance ().add_project (this, out monitored_repo);
7885
notify["name"].connect (branch_or_name_changed);
7986
if (monitored_repo != null) {
8087
checkout_local_branch_action = new SimpleAction.stateful (
@@ -142,7 +149,7 @@ namespace Scratch.FolderManager {
142149
}
143150

144151
MenuItem set_active_folder_item;
145-
if (is_git_repo) {
152+
if (is_git_repo && !is_readonly) {
146153
set_active_folder_item = new GLib.MenuItem (
147154
_("Set as Active Project"),
148155
GLib.Action.print_detailed_name (

src/MainWindow.vala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace Scratch {
2828
public Scratch.Application app { get; private set; }
2929
public bool restore_docs { get; construct; }
3030
public RestoreOverride restore_override { get; construct set; }
31+
public string active_project_path { get; set; }
32+
3133
public string default_globalsearch_path {
3234
owned get {
3335
if (document_view.current_document != null) {
@@ -36,7 +38,7 @@ namespace Scratch {
3638
}
3739
}
3840

39-
return git_manager.active_project_path;
41+
return active_project_path;
4042
}
4143
}
4244

@@ -373,6 +375,11 @@ namespace Scratch {
373375

374376
Unix.signal_add (Posix.Signal.INT, quit_source_func, Priority.HIGH);
375377
Unix.signal_add (Posix.Signal.TERM, quit_source_func, Priority.HIGH);
378+
379+
bind_property ("active-project-path", sidebar.choose_project_button, "active-project-path", BIDIRECTIONAL);
380+
if (is_primary) {
381+
settings.bind ("active-project-path", this, "active-project-path", DEFAULT);
382+
}
376383
}
377384

378385
private void update_style () {
@@ -1063,8 +1070,8 @@ namespace Scratch {
10631070

10641071
private void action_clone_repo (SimpleAction action, Variant? param) {
10651072
var default_projects_folder = Scratch.settings.get_string ("default-projects-folder");
1066-
if (default_projects_folder == "" && git_manager.active_project_path != "") {
1067-
default_projects_folder = Path.get_dirname (git_manager.active_project_path);
1073+
if (default_projects_folder == "" && active_project_path != "") {
1074+
default_projects_folder = Path.get_dirname (active_project_path);
10681075
}
10691076

10701077
var default_remote = Scratch.settings.get_string ("default-remote");
@@ -1356,7 +1363,7 @@ namespace Scratch {
13561363
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (is_current_doc);
13571364
Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc);
13581365
Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc);
1359-
var can_global_search = is_current_doc || git_manager.active_project_path != null;
1366+
var can_global_search = is_current_doc || active_project_path != null;
13601367
Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (can_global_search);
13611368

13621369
return Source.REMOVE;
@@ -1536,7 +1543,7 @@ namespace Scratch {
15361543
}
15371544

15381545
if (path == "") { // Happens when keyboard accelerator is used
1539-
path = git_manager.active_project_path;
1546+
path = active_project_path;
15401547
if (use_build_dir) {
15411548
path = git_manager.get_default_build_dir (path);
15421549
}

src/Services/GitManager.vala

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
namespace Scratch.Services {
2222
public class GitManager : Object {
2323
public ListStore project_liststore { get; private set; }
24-
public string active_project_path { get; set; default = "";}
2524

2625
static Gee.HashMap<string, MonitoredRepository> project_gitrepo_map;
2726
static GitManager? instance;
@@ -47,29 +46,27 @@ namespace Scratch.Services {
4746
construct {
4847
// Used to populate the ChooseProject popover in sorted order
4948
project_liststore = new ListStore (typeof (FolderManager.ProjectFolderItem));
50-
settings.bind ("active-project-path", this, "active-project-path", DEFAULT);
5149
}
5250

53-
public MonitoredRepository? add_project (FolderManager.ProjectFolderItem root_folder) {
51+
public bool add_project (FolderManager.ProjectFolderItem root_folder, out MonitoredRepository? monitored_repo) {
5452
var root_path = root_folder.path;
55-
MonitoredRepository? monitored_repo = null;
53+
monitored_repo = null;
5654
uint position;
5755
if (project_liststore.find_with_equal_func (
5856
root_folder,
5957
(a, b) => { return ((FolderManager.Item) a).equal ((FolderManager.Item) b); },
6058
out position
6159
)) {
6260

63-
var repo = project_gitrepo_map.@get (root_path);
64-
return repo;
61+
monitored_repo = project_gitrepo_map.@get (root_path);
62+
return true;
6563
}
6664

6765
try {
6866
var git_repo = Ggit.Repository.open (root_folder.file.file);
6967
if (!project_gitrepo_map.has_key (root_path)) {
7068
monitored_repo = new MonitoredRepository (git_repo);
7169
project_gitrepo_map.@set (root_path, monitored_repo);
72-
return project_gitrepo_map.@get (root_path);
7370
}
7471
} catch (Error e) {
7572
debug (
@@ -85,7 +82,8 @@ namespace Scratch.Services {
8582
}
8683

8784
// No longer need to set default project (restored from settings or left unset)
88-
return project_gitrepo_map.@get (root_path);
85+
monitored_repo = project_gitrepo_map.@get (root_path);
86+
return false;
8987
}
9088

9189
[CCode (instance_pos = -1)]
@@ -111,17 +109,16 @@ namespace Scratch.Services {
111109
}
112110

113111
// @project_path is the root of a project or null
114-
public string get_default_build_dir (string? project_path) {
115-
string build_path = project_path != null ? project_path : active_project_path;
112+
public string get_default_build_dir (string project_path) {
116113
var default_build_dir = Scratch.settings.get_string ("default-build-directory");
117-
var build_file = GLib.File.new_for_path (Path.build_filename (build_path, default_build_dir));
114+
var build_file = GLib.File.new_for_path (Path.build_filename (project_path, default_build_dir));
118115
if (build_file.query_exists ()) {
119-
build_path = build_file.get_path ();
116+
return build_file.get_path ();
120117
} else {
121118
warning ("build path not found %s", build_file.get_path ());
122119
}
123120

124-
return build_path;
121+
return project_path;
125122
}
126123

127124
public async bool clone_repository (

src/Widgets/ChooseProjectButton.vala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
2121
private const string PROJECT_TOOLTIP = N_("Active Git Project: %s");
2222
private Gtk.Label label_widget;
2323
private Gtk.ListBox project_listbox;
24+
public string active_project_path { get; set; }
2425

2526
public signal void project_chosen ();
2627

@@ -128,40 +129,38 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
128129

129130
project_listbox.remove.connect ((row) => {
130131
var project_row = row as ProjectRow;
131-
var current_project = Scratch.Services.GitManager.get_instance ().active_project_path;
132+
var current_project = active_project_path;
132133
if (project_row.project_path == current_project) {
133-
Scratch.Services.GitManager.get_instance ().active_project_path = "";
134+
active_project_path = "";
134135
// Label and active_path will be updated automatically
135136
}
136137
});
137138

138139
project_listbox.row_activated.connect ((row) => {
139140
var project_entry = ((ProjectRow) row);
140-
git_manager.active_project_path = project_entry.project_path;
141+
active_project_path = project_entry.project_path;
141142
project_chosen ();
142143
});
143144

144145
toggled.connect (() => {
145146
if (active) {
146-
unowned var active_path = Scratch.Services.GitManager.get_instance ().active_project_path;
147147
foreach (var child in project_listbox.get_children ()) {
148148
var project_row = ((ProjectRow) child);
149149
// All paths must not end in directory separator so can be compared directly
150-
project_row.active = active_path == project_row.project_path;
150+
project_row.active = active_project_path == project_row.project_path;
151151
}
152152
}
153153
});
154154

155-
git_manager.notify["active-project-path"].connect (update_button);
155+
notify["active-project-path"].connect (update_button);
156156
update_button ();
157157
}
158158

159159
// Set appearance (only) of project chooser button and list according to active path
160160
private void update_button () {
161-
unowned var active_path = Scratch.Services.GitManager.get_instance ().active_project_path;
162-
if (active_path != "") {
163-
label_widget.label = Path.get_basename (active_path);
164-
tooltip_text = _(PROJECT_TOOLTIP).printf (Scratch.Utils.replace_home_with_tilde (active_path));
161+
if (active_project_path != "") {
162+
label_widget.label = Path.get_basename (active_project_path);
163+
tooltip_text = _(PROJECT_TOOLTIP).printf (Scratch.Utils.replace_home_with_tilde (active_project_path));
165164
} else {
166165
label_widget.label = Path.get_basename (_(NO_PROJECT_SELECTED));
167166
tooltip_text = _(PROJECT_TOOLTIP).printf (_(NO_PROJECT_SELECTED));

0 commit comments

Comments
 (0)