Skip to content

Commit d065ae9

Browse files
jeremypwzeebok
andauthored
Ensure terminal pane shell location correct when opened (elementary#1574)
* Save terminal-pane state explicitly - destroy signal unsuitable * Use common action to set active project * Disambiguate action name * Match self in get_project_for_file * Lose FileView.ACTION_SET_PROJECT_ACTIVE call MainWindow action directly * Revert renaming function --------- Co-authored-by: Ryan Kornheisl <[email protected]>
1 parent 10c54f4 commit d065ae9

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

src/FolderManager/FileView.vala

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
3636
public const string ACTION_CHECKOUT_REMOTE_BRANCH = "checkout-remote-branch";
3737
public const string ACTION_CLOSE_FOLDER = "close-folder";
3838
public const string ACTION_CLOSE_OTHER_FOLDERS = "close-other-folders";
39-
public const string ACTION_SET_ACTIVE_PROJECT = "set-active-project";
4039

4140
private const ActionEntry[] ACTION_ENTRIES = {
4241
{ ACTION_LAUNCH_APP_WITH_FILE_PATH, action_launch_app_with_file_path, "as" },
@@ -48,8 +47,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
4847
{ ACTION_NEW_FILE, add_new_file, "s" },
4948
{ ACTION_NEW_FOLDER, add_new_folder, "s"},
5049
{ ACTION_CLOSE_FOLDER, action_close_folder, "s"},
51-
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"},
52-
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"}
50+
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"}
5351
};
5452

5553
private GLib.Settings settings;
@@ -115,32 +113,23 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
115113
foreach (var child in root.children) {
116114
var project_folder_item = (ProjectFolderItem) child;
117115
if (project_folder_item != folder_root) {
118-
toplevel_action_group.activate_action (MainWindow.ACTION_CLOSE_PROJECT_DOCS, new Variant.string (project_folder_item.path));
116+
toplevel_action_group.activate_action (
117+
MainWindow.ACTION_CLOSE_PROJECT_DOCS,
118+
new Variant.string (project_folder_item.path)
119+
);
119120
root.remove (project_folder_item);
120121
git_manager.remove_project (project_folder_item);
121122
}
122123
}
123-
124124
//Make remaining project the active one
125-
git_manager.active_project_path = path;
126-
127-
write_settings ();
125+
set_project_active (path);
128126
}
129127

130-
private void action_set_active_project (SimpleAction action, GLib.Variant? parameter) {
131-
var path = parameter.get_string ();
132-
if (path == null || path == "") {
133-
return;
134-
}
135-
136-
var folder_root = find_path (root, path) as ProjectFolderItem;
137-
if (folder_root == null) {
138-
return;
139-
}
140-
141-
git_manager.active_project_path = path;
142-
143-
write_settings ();
128+
private void set_project_active (string path) {
129+
toplevel_action_group.activate_action (
130+
MainWindow.ACTION_SET_ACTIVE_PROJECT,
131+
new Variant.string (path)
132+
);
144133
}
145134

146135
public async void restore_saved_state () {
@@ -251,11 +240,15 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
251240
return null;
252241
}
253242

243+
public bool project_is_open (string project_path) {
244+
return get_project_for_file (GLib.File.new_for_path (project_path)) != null;
245+
}
246+
254247
public ProjectFolderItem? get_project_for_file (GLib.File file) {
255248
foreach (var item in root.children) {
256249
if (item is ProjectFolderItem) {
257250
var folder = (ProjectFolderItem)item;
258-
if (folder.contains_file (file)) {
251+
if (folder.file.file.equal (file) || folder.contains_file (file)) {
259252
return folder;
260253
}
261254
}

src/FolderManager/FolderItem.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ namespace Scratch.FolderManager {
6262
}
6363
}
6464

65-
6665
public void load_children () {
6766
if (loading_required) {
6867
foreach (var child in file.children) {

src/FolderManager/ProjectFolderItem.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ namespace Scratch.FolderManager {
146146
set_active_folder_item = new GLib.MenuItem (
147147
_("Set as Active Project"),
148148
GLib.Action.print_detailed_name (
149-
FileView.ACTION_PREFIX + FileView.ACTION_SET_ACTIVE_PROJECT,
149+
MainWindow.ACTION_PREFIX + MainWindow.ACTION_SET_ACTIVE_PROJECT,
150150
new Variant.string (file.path)
151151
)
152152
);

src/MainWindow.vala

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ namespace Scratch {
110110
public const string ACTION_TOGGLE_OUTLINE = "action-toggle-outline";
111111
public const string ACTION_TOGGLE_TERMINAL = "action-toggle-terminal";
112112
public const string ACTION_OPEN_IN_TERMINAL = "action-open-in-terminal";
113+
public const string ACTION_SET_ACTIVE_PROJECT = "action-set-active-project";
113114
public const string ACTION_NEXT_TAB = "action-next-tab";
114115
public const string ACTION_PREVIOUS_TAB = "action-previous-tab";
115116
public const string ACTION_CLEAR_LINES = "action-clear-lines";
@@ -169,6 +170,7 @@ namespace Scratch {
169170
{ ACTION_TOGGLE_SIDEBAR, action_toggle_sidebar, null, "true" },
170171
{ ACTION_TOGGLE_TERMINAL, action_toggle_terminal, null, "false"},
171172
{ ACTION_OPEN_IN_TERMINAL, action_open_in_terminal, "s"},
173+
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"},
172174
{ ACTION_TOGGLE_OUTLINE, action_toggle_outline, null, "false" },
173175
{ ACTION_NEXT_TAB, action_next_tab },
174176
{ ACTION_PREVIOUS_TAB, action_previous_tab },
@@ -633,14 +635,6 @@ namespace Scratch {
633635
}
634636
});
635637

636-
sidebar.choose_project_button.project_chosen.connect (() => {
637-
folder_manager_view.collapse_other_projects ();
638-
if (terminal.visible) {
639-
var open_in_terminal_action = Utils.action_from_group (ACTION_OPEN_IN_TERMINAL, actions);
640-
var param = new Variant.string (Services.GitManager.get_instance ().get_default_build_dir (null));
641-
open_in_terminal_action.activate (param);
642-
}
643-
});
644638

645639
set_widgets_sensitive (false);
646640
}
@@ -865,6 +859,8 @@ namespace Scratch {
865859
// Plugin panes size
866860
Scratch.saved_state.set_int ("hp1-size", hp1.get_position ());
867861
Scratch.saved_state.set_int ("vp-size", vp.get_position ());
862+
863+
terminal.save_settings ();
868864
}
869865

870866
// SIGTERM/SIGINT Handling
@@ -1450,7 +1446,7 @@ namespace Scratch {
14501446

14511447
private void action_open_in_terminal (SimpleAction action, Variant? param) {
14521448
// Ensure terminal is visible
1453-
if (terminal == null || !terminal.visible) {
1449+
if (!terminal.visible) {
14541450
var toggle_terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
14551451
toggle_terminal_action.activate (null);
14561452
}
@@ -1462,6 +1458,24 @@ namespace Scratch {
14621458
terminal.terminal.grab_focus ();
14631459
}
14641460

1461+
private void action_set_active_project (SimpleAction action, Variant? param) {
1462+
var project_path = param.get_string ();
1463+
if (folder_manager_view.project_is_open (project_path)) {
1464+
git_manager.active_project_path = project_path;
1465+
folder_manager_view.collapse_other_projects ();
1466+
//The opened folders are not changed so no need to update "opened-folders" setting
1467+
} else {
1468+
warning ("Attempt to set folder path %s which is not opened as active project ignored", project_path);
1469+
//TODO Handle this by opening the folder
1470+
}
1471+
1472+
var new_build_dir = Services.GitManager.get_instance ().get_default_build_dir (null);
1473+
terminal.change_location (new_build_dir);
1474+
if (terminal.visible) {
1475+
terminal.terminal.grab_focus ();
1476+
}
1477+
}
1478+
14651479
private void action_toggle_outline (SimpleAction action) {
14661480
action.set_state (!action.get_state ().get_boolean ());
14671481
document_view.outline_visible = action.get_state ().get_boolean ();

src/Widgets/ChooseProjectButton.vala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
2424
private Gtk.Label label_widget;
2525
private Gtk.ListBox project_listbox;
2626

27+
public ActionGroup toplevel_action_group { get; construct; }
2728
public signal void project_chosen ();
2829

2930
construct {
31+
realize.connect (() => {
32+
toplevel_action_group = get_action_group (Scratch.MainWindow.ACTION_GROUP);
33+
assert_nonnull (toplevel_action_group);
34+
});
35+
3036
var img = new Gtk.Image () {
3137
gicon = new ThemedIcon ("git-symbolic"),
3238
icon_size = Gtk.IconSize.SMALL_TOOLBAR
@@ -146,8 +152,10 @@ public class Code.ChooseProjectButton : Gtk.MenuButton {
146152

147153
project_listbox.row_activated.connect ((row) => {
148154
var project_entry = ((ProjectRow) row);
149-
git_manager.active_project_path = project_entry.project_path;
150-
project_chosen ();
155+
toplevel_action_group.activate_action (
156+
Scratch.MainWindow.ACTION_SET_ACTIVE_PROJECT,
157+
new Variant.string (project_entry.project_path)
158+
);
151159
});
152160

153161
toggled.connect (() => {

src/Widgets/Terminal.vala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ public class Code.Terminal : Gtk.Box {
173173
scrolled_window.add (terminal);
174174

175175
add (scrolled_window);
176-
177-
destroy.connect (() => {
178-
Scratch.saved_state.set_string ("last-opened-path", get_shell_location ());
179-
});
180-
181176
show_all ();
182177
}
183178

@@ -298,6 +293,10 @@ public class Code.Terminal : Gtk.Box {
298293
} //No suitable system keys
299294
}
300295

296+
public void save_settings () {
297+
Scratch.saved_state.set_string ("last-opened-path", get_shell_location ());
298+
}
299+
301300
public void increment_size () {
302301
terminal.font_scale = (terminal.font_scale + 0.1).clamp (MIN_SCALE, MAX_SCALE);
303302
}

0 commit comments

Comments
 (0)