Skip to content

Commit

Permalink
[Feature]Implement "open-in-terminal" action (#1405)
Browse files Browse the repository at this point in the history
* Add open in terminal action and menu option

* Add hot-key for action

* Change terminal location on active project change

* Correct variable name

* Add menu option to FileItem and FolderItem

* Add accel label to open project in terminal item
  • Loading branch information
jeremypw authored Jan 17, 2024
1 parent 9b856b7 commit 2071494
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/FolderManager/FileItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ namespace Scratch.FolderManager {
}

public override Gtk.Menu? get_context_menu () {
var open_in_terminal_pane_item = new Gtk.MenuItem.with_label (_("Open in Terminal Pane")) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
action_target = new Variant.string (file.file.get_parent ().get_path ())
};
var new_window_menuitem = new Gtk.MenuItem.with_label (_("New Window"));
new_window_menuitem.activate.connect (() => {
var new_window = new MainWindow (false);
Expand Down Expand Up @@ -126,6 +130,7 @@ namespace Scratch.FolderManager {
delete_item.activate.connect (trash);

var menu = new Gtk.Menu ();
menu.append (open_in_terminal_pane_item);
menu.append (open_in_item);
menu.append (contractor_item);
menu.append (new Gtk.SeparatorMenuItem ());
Expand Down
6 changes: 6 additions & 0 deletions src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ namespace Scratch.FolderManager {
}

public override Gtk.Menu? get_context_menu () {
var open_in_terminal_pane_item = new Gtk.MenuItem.with_label (_("Open in Terminal Pane")) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
action_target = new Variant.string (file.path)
};

var contractor_menu = new Gtk.Menu ();

GLib.FileInfo info = null;
Expand Down Expand Up @@ -131,6 +136,7 @@ namespace Scratch.FolderManager {
};

var menu = new Gtk.Menu ();
menu.append (open_in_terminal_pane_item);
menu.append (create_submenu_for_open_in (info, file_type));
menu.append (contractor_item);
menu.append (new Gtk.SeparatorMenuItem ());
Expand Down
11 changes: 11 additions & 0 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ namespace Scratch.FolderManager {
}

public override Gtk.Menu? get_context_menu () {
var open_in_terminal_pane_label = new Granite.AccelLabel.from_action_name (
_("Open in Terminal Pane"),
MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL + "::"
);
var open_in_terminal_pane_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_OPEN_IN_TERMINAL,
action_target = new Variant.string (file.file.get_path ())
};
open_in_terminal_pane_item.add (open_in_terminal_pane_label);

var close_folder_item = new Gtk.MenuItem.with_label (_("Close Folder"));
close_folder_item.activate.connect (() => {
closed ();
Expand Down Expand Up @@ -196,6 +206,7 @@ namespace Scratch.FolderManager {
}

var menu = new Gtk.Menu ();
menu.append (open_in_terminal_pane_item);
menu.append (create_submenu_for_open_in (info, file_type));
menu.append (new Gtk.SeparatorMenuItem ());
menu.append (create_submenu_for_new ());
Expand Down
29 changes: 25 additions & 4 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ namespace Scratch {
public const string ACTION_TOGGLE_SIDEBAR = "action_toggle_sidebar";
public const string ACTION_TOGGLE_OUTLINE = "action_toggle_outline";
public const string ACTION_TOGGLE_TERMINAL = "action-toggle-terminal";
public const string ACTION_OPEN_IN_TERMINAL = "action-open_in_terminal";
public const string ACTION_NEXT_TAB = "action_next_tab";
public const string ACTION_PREVIOUS_TAB = "action_previous_tab";
public const string ACTION_CLEAR_LINES = "action_clear_lines";
Expand Down Expand Up @@ -141,6 +142,7 @@ namespace Scratch {
{ ACTION_TOGGLE_COMMENT, action_toggle_comment },
{ ACTION_TOGGLE_SIDEBAR, action_toggle_sidebar, null, "true" },
{ ACTION_TOGGLE_TERMINAL, action_toggle_terminal, null, "false"},
{ ACTION_OPEN_IN_TERMINAL, action_open_in_terminal, "s"},
{ ACTION_TOGGLE_OUTLINE, action_toggle_outline, null, "false" },
{ ACTION_NEXT_TAB, action_next_tab },
{ ACTION_PREVIOUS_TAB, action_previous_tab },
Expand Down Expand Up @@ -199,6 +201,7 @@ namespace Scratch {
action_accelerators.set (ACTION_TOGGLE_SIDEBAR, "F9"); // GNOME
action_accelerators.set (ACTION_TOGGLE_SIDEBAR, "<Control>backslash"); // Atom
action_accelerators.set (ACTION_TOGGLE_TERMINAL, "<Control><Alt>t");
action_accelerators.set (ACTION_OPEN_IN_TERMINAL + "::", "<Control><Alt><Shift>t");
action_accelerators.set (ACTION_TOGGLE_OUTLINE, "<Alt>backslash");
action_accelerators.set (ACTION_NEXT_TAB, "<Control>Tab");
action_accelerators.set (ACTION_NEXT_TAB, "<Control>Page_Down");
Expand Down Expand Up @@ -600,6 +603,10 @@ namespace Scratch {

sidebar.choose_project_button.project_chosen.connect (() => {
folder_manager_view.collapse_other_projects ();
if (terminal.visible) {
var open_in_terminal_action = Utils.action_from_group (ACTION_OPEN_IN_TERMINAL, actions);
open_in_terminal_action.activate (null);
}
});

set_widgets_sensitive (false);
Expand Down Expand Up @@ -1264,18 +1271,32 @@ namespace Scratch {
}

private void action_toggle_terminal () {
var terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
terminal_action.set_state (!terminal_action.get_state ().get_boolean ());
var toggle_terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
toggle_terminal_action.set_state (!toggle_terminal_action.get_state ().get_boolean ());

terminal.visible = terminal_action.get_state ().get_boolean ();
terminal.visible = toggle_terminal_action.get_state ().get_boolean ();

if (terminal_action.get_state ().get_boolean ()) {
if (toggle_terminal_action.get_state ().get_boolean ()) {
terminal.grab_focus ();
} else if (get_current_document () != null) {
get_current_document ().focus ();
}
}

private void action_open_in_terminal (SimpleAction action, Variant? param) {
// Ensure terminal is visible
if (terminal == null || !terminal.visible) {
var toggle_terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
toggle_terminal_action.activate (null);
}

//If param is null or empty, the active project path is returned or failing that
//the active document path
var target_path = get_target_path_for_actions (param);
terminal.change_location (target_path);
terminal.terminal.grab_focus ();
}

private void action_toggle_outline (SimpleAction action) {
action.set_state (!action.get_state ().get_boolean ());
document_view.outline_visible = action.get_state ().get_boolean ();
Expand Down
39 changes: 24 additions & 15 deletions src/Widgets/Terminal.vala
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,7 @@ public class Code.Terminal : Gtk.Box {
});

var settings = new Settings (Constants.PROJECT_NAME + ".saved-state");
try {
var last_path_setting = settings.get_string ("last-opened-path");
//FIXME Replace with the async method once the .vapi is fixed upstream.
terminal.spawn_sync (
Vte.PtyFlags.DEFAULT,
last_path_setting == "" ? "~/" : last_path_setting,
{ Vte.get_user_shell () },
null,
GLib.SpawnFlags.SEARCH_PATH,
null,
out child_pid
);
} catch (GLib.Error e) {
warning (e.message);
}
spawn_shell (settings.get_string ("last-opened-path"));

var scrolled_window = new Gtk.ScrolledWindow (null, terminal.get_vadjustment ());
scrolled_window.add (terminal);
Expand All @@ -88,6 +74,29 @@ public class Code.Terminal : Gtk.Box {
show_all ();
}

private void spawn_shell (string dir = GLib.Environment.get_current_dir ()) {
try {
terminal.spawn_sync (
Vte.PtyFlags.DEFAULT,
dir,
{ Vte.get_user_shell () },
null,
SpawnFlags.SEARCH_PATH,
null,
out this.child_pid,
null
);
} catch (Error e) {
warning (e.message);
}
}

public void change_location (string dir) {
Posix.kill (child_pid, Posix.Signal.TERM);
terminal.reset (true, true);
spawn_shell (dir);
}

private string get_shell_location () {
int pid = (!) (this.child_pid);
try {
Expand Down

0 comments on commit 2071494

Please sign in to comment.