Skip to content

Commit

Permalink
MainWindow: move terminal related actions to TerminalWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukesu committed Nov 26, 2023
1 parent 2720165 commit 2d0f480
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 210 deletions.
9 changes: 9 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ public class Terminal.Application : Gtk.Application {

set_accels_for_action ("app.new-window", { "<Control><Shift>N" });
set_accels_for_action ("app.quit", { "<Control><Shift>Q" });

set_accels_for_action (TerminalWidget.ACTION_COPY, TerminalWidget.ACCELS_COPY);
set_accels_for_action (TerminalWidget.ACTION_COPY_OUTPUT, TerminalWidget.ACCELS_COPY_OUTPUT);
set_accels_for_action (TerminalWidget.ACTION_PASTE, TerminalWidget.ACCELS_PASTE);
set_accels_for_action (TerminalWidget.ACTION_RELOAD, TerminalWidget.ACCELS_RELOAD);
set_accels_for_action (TerminalWidget.ACTION_SELECT_ALL, TerminalWidget.ACCELS_SELECT_ALL);
set_accels_for_action (TerminalWidget.ACTION_ZOOM_DEFAULT, TerminalWidget.ACCELS_ZOOM_DEFAULT);
set_accels_for_action (TerminalWidget.ACTION_ZOOM_IN, TerminalWidget.ACCELS_ZOOM_IN);
set_accels_for_action (TerminalWidget.ACTION_ZOOM_OUT, TerminalWidget.ACCELS_ZOOM_OUT);
}

protected override int command_line (ApplicationCommandLine command_line) {
Expand Down
172 changes: 11 additions & 161 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,14 @@ namespace Terminal {
public const string ACTION_FULLSCREEN = "action-fullscreen";
public const string ACTION_NEW_TAB = "action-new-tab";
public const string ACTION_DUPLICATE_TAB = "action-duplicate-tab";
public const string ACTION_RELOAD_TAB = "action-reload-tab";
public const string ACTION_NEXT_TAB = "action-next-tab";
public const string ACTION_PREVIOUS_TAB = "action-previous-tab";
public const string ACTION_MOVE_TAB_RIGHT = "action-move-tab-right";
public const string ACTION_MOVE_TAB_LEFT = "action-move-tab-left";
public const string ACTION_ZOOM_DEFAULT_FONT = "action-zoom-default-font";
public const string ACTION_ZOOM_IN_FONT = "action-zoom-in-font";
public const string ACTION_ZOOM_OUT_FONT = "action-zoom-out-font";
public const string ACTION_COPY = "action-copy";
public const string ACTION_COPY_LAST_OUTPUT = "action-copy-last-output";
public const string ACTION_PASTE = "action-paste";
public const string ACTION_SEARCH = "action-search";
public const string ACTION_SEARCH_NEXT = "action-search-next";
public const string ACTION_SEARCH_PREVIOUS = "action-search-previous";
public const string ACTION_SELECT_ALL = "action-select-all";
public const string ACTION_SCROLL_TO_LAST_COMMAND = "action-scroll-to-last-command";
public const string ACTION_OPEN_IN_BROWSER = "action-open-in-browser";
public const string ACTION_RELOAD_PREFERRED_ACCEL = "<Shift><Control>R"; // Shown in context menu

public const string[] ACCELS_ZOOM_DEFAULT_FONT = { "<control>0", "<Control>KP_0", null };
public const string[] ACCELS_ZOOM_IN_FONT = { "<Control>plus", "<Control>equal", "<Control>KP_Add", null };
public const string[] ACCELS_ZOOM_OUT_FONT = { "<Control>minus", "<Control>KP_Subtract", null };

private static Gee.MultiMap<string, string> action_accelerators = new Gee.HashMultiMap<string, string> ();

Expand All @@ -86,22 +72,13 @@ namespace Terminal {
{ ACTION_FULLSCREEN, action_fullscreen },
{ ACTION_NEW_TAB, action_new_tab },
{ ACTION_DUPLICATE_TAB, action_duplicate_tab },
{ ACTION_RELOAD_TAB, action_reload_tab },
{ ACTION_NEXT_TAB, action_next_tab },
{ ACTION_PREVIOUS_TAB, action_previous_tab },
{ ACTION_MOVE_TAB_RIGHT, action_move_tab_right},
{ ACTION_MOVE_TAB_LEFT, action_move_tab_left},
{ ACTION_ZOOM_DEFAULT_FONT, action_zoom_default_font },
{ ACTION_ZOOM_IN_FONT, action_zoom_in_font },
{ ACTION_ZOOM_OUT_FONT, action_zoom_out_font },
{ ACTION_COPY, action_copy },
{ ACTION_COPY_LAST_OUTPUT, action_copy_last_output },
{ ACTION_PASTE, action_paste },
{ ACTION_SEARCH, action_search, null, "false" },
{ ACTION_SEARCH_NEXT, action_search_next },
{ ACTION_SEARCH_PREVIOUS, action_search_previous },
{ ACTION_SELECT_ALL, action_select_all },
{ ACTION_SCROLL_TO_LAST_COMMAND, action_scroll_to_last_command },
{ ACTION_OPEN_IN_BROWSER, action_open_in_browser }
};

Expand All @@ -121,21 +98,14 @@ namespace Terminal {
action_accelerators[ACTION_FULLSCREEN] = "F11";
action_accelerators[ACTION_NEW_TAB] = "<Control><Shift>t";
action_accelerators[ACTION_DUPLICATE_TAB] = "<Control><Shift>d";
action_accelerators[ACTION_RELOAD_TAB] = ACTION_RELOAD_PREFERRED_ACCEL;
action_accelerators[ACTION_RELOAD_TAB] = "<Shift>F5";
action_accelerators[ACTION_NEXT_TAB] = "<Control>Tab";
action_accelerators[ACTION_NEXT_TAB] = "<Control>Page_Down";
action_accelerators[ACTION_PREVIOUS_TAB] = "<Control><Shift>Tab";
action_accelerators[ACTION_PREVIOUS_TAB] = "<Control>Page_Up";
action_accelerators[ACTION_MOVE_TAB_RIGHT] = "<Control><Alt>Right";
action_accelerators[ACTION_MOVE_TAB_LEFT] = "<Control><Alt>Left";
action_accelerators[ACTION_COPY] = "<Control><Shift>c";
action_accelerators[ACTION_COPY_LAST_OUTPUT] = "<Alt>c";
action_accelerators[ACTION_PASTE] = "<Control><Shift>v";
action_accelerators[ACTION_SEARCH] = "<Control><Shift>f";
action_accelerators[ACTION_SELECT_ALL] = "<Control><Shift>a";
action_accelerators[ACTION_OPEN_IN_BROWSER] = "<Control><Shift>e";
action_accelerators[ACTION_SCROLL_TO_LAST_COMMAND] = "<Alt>Up";
}

construct {
Expand All @@ -154,18 +124,12 @@ namespace Terminal {
app.set_accels_for_action (ACTION_PREFIX + action, accels_array);
}

app.set_accels_for_action (ACTION_PREFIX + ACTION_ZOOM_DEFAULT_FONT, ACCELS_ZOOM_DEFAULT_FONT);
app.set_accels_for_action (ACTION_PREFIX + ACTION_ZOOM_IN_FONT, ACCELS_ZOOM_IN_FONT);
app.set_accels_for_action (ACTION_PREFIX + ACTION_ZOOM_OUT_FONT, ACCELS_ZOOM_OUT_FONT);

set_visual (Gdk.Screen.get_default ().get_rgba_visual ());

title = TerminalWidget.DEFAULT_LABEL;
restore_saved_state ();

clipboard = Gtk.Clipboard.get (Gdk.Atom.intern ("CLIPBOARD", false));
clipboard.owner_change.connect (update_context_menu);

primary_selection = Gtk.Clipboard.get (Gdk.Atom.intern ("PRIMARY", false));

var open_in_browser_menuitem = new Gtk.MenuItem () {
Expand All @@ -177,27 +141,27 @@ namespace Terminal {
open_in_browser_menuitem.add (open_in_browser_menuitem_label);

var copy_menuitem = new Gtk.MenuItem () {
action_name = ACTION_PREFIX + ACTION_COPY
action_name = TerminalWidget.ACTION_COPY
};
copy_menuitem.add (new Granite.AccelLabel.from_action_name (_("Copy"), copy_menuitem.action_name));
copy_menuitem.add (new Granite.AccelLabel.from_action_name (_("Copy"), TerminalWidget.ACTION_COPY));

var copy_last_output_menuitem = new Gtk.MenuItem () {
action_name = ACTION_PREFIX + ACTION_COPY_LAST_OUTPUT
action_name = TerminalWidget.ACTION_COPY_OUTPUT
};
copy_last_output_menuitem.add (
new Granite.AccelLabel.from_action_name (_("Copy Last Output"), copy_last_output_menuitem.action_name)
new Granite.AccelLabel.from_action_name (_("Copy Last Output"), TerminalWidget.ACTION_COPY_OUTPUT)
);

var paste_menuitem = new Gtk.MenuItem () {
action_name = ACTION_PREFIX + ACTION_PASTE
action_name = TerminalWidget.ACTION_PASTE
};
paste_menuitem.add (new Granite.AccelLabel.from_action_name (_("Paste"), paste_menuitem.action_name));
paste_menuitem.add (new Granite.AccelLabel.from_action_name (_("Paste"), TerminalWidget.ACTION_PASTE));

var select_all_menuitem = new Gtk.MenuItem () {
action_name = ACTION_PREFIX + ACTION_SELECT_ALL
action_name = TerminalWidget.ACTION_SELECT_ALL
};
select_all_menuitem.add (
new Granite.AccelLabel.from_action_name (_("Select All"), select_all_menuitem.action_name)
new Granite.AccelLabel.from_action_name (_("Select All"), TerminalWidget.ACTION_SELECT_ALL)
);

var search_menuitem = new Gtk.MenuItem () {
Expand All @@ -216,10 +180,6 @@ namespace Terminal {
menu.append (search_menuitem);
menu.insert_action_group ("win", actions);

menu.popped_up.connect (() => {
update_copy_output_sensitive ();
});

setup_ui ();
show_all ();

Expand Down Expand Up @@ -326,11 +286,6 @@ namespace Terminal {
unowned Gtk.StyleContext header_context = header.get_style_context ();
header_context.add_class ("default-decoration");

get_simple_action (ACTION_COPY).set_enabled (false);
get_simple_action (ACTION_COPY_LAST_OUTPUT).set_enabled (false);
get_simple_action (ACTION_SCROLL_TO_LAST_COMMAND).set_enabled (false);


notebook = new Granite.Widgets.DynamicNotebook.with_accellabels (
new Granite.AccelLabel.from_action_name (_("New Tab"), ACTION_PREFIX + ACTION_NEW_TAB)
) {
Expand Down Expand Up @@ -580,9 +535,6 @@ namespace Terminal {
}

public void update_context_menu () {
/* Update the "Paste" menu option */
clipboard.request_targets (update_context_menu_cb);

/* Update the "Show in ..." menu option */
get_current_selection_link_or_pwd ((clipboard, uri) => {
update_menu_label (Utils.sanitize_path (uri, current_terminal.get_shell_location ()));
Expand Down Expand Up @@ -637,16 +589,6 @@ namespace Terminal {
return appinfo;
}

private void update_context_menu_cb (Gtk.Clipboard clipboard_, Gdk.Atom[]? atoms) {
bool can_paste = false;

if (atoms != null && atoms.length > 0) {
can_paste = Gtk.targets_include_text (atoms) || Gtk.targets_include_uri (atoms);
}

get_simple_action (ACTION_PASTE).set_enabled (can_paste);
}

protected override bool configure_event (Gdk.EventConfigure event) {
// triggered when the size, position or stacking of the window has changed
// it is delayed 400ms to prevent spamming gsettings
Expand Down Expand Up @@ -689,7 +631,7 @@ namespace Terminal {
title = current_terminal.window_title != "" ? current_terminal.window_title
: current_terminal.current_working_directory;

get_simple_action (ACTION_COPY_LAST_OUTPUT).set_enabled (current_terminal.has_output ());
menu.insert_action_group ("term", current_terminal.get_action_group ("term"));
current_terminal.grab_focus ();
}

Expand Down Expand Up @@ -807,6 +749,7 @@ namespace Terminal {
}
});

terminal_widget.notify["font-scale"].connect (() => save_opened_terminals (false, true));
terminal_widget.window_title_changed.connect (check_for_tabs_with_same_name);
terminal_widget.cwd_changed.connect (cwd_changed);

Expand Down Expand Up @@ -873,7 +816,7 @@ namespace Terminal {
/* Granite.Accel.from_action_name () does not allow control of which accel is used when
* there are multiple so we have to use the other constructor to specify it. */
var reload_menu_item = new Gtk.MenuItem () {
child = new Granite.AccelLabel (_("Reload"), ACTION_RELOAD_PREFERRED_ACCEL)
child = new Granite.AccelLabel (_("Reload"), TerminalWidget.ACCELS_RELOAD[0]),
};
tab.menu.append (reload_menu_item);
reload_menu_item.activate.connect (term.reload);
Expand Down Expand Up @@ -949,74 +892,6 @@ namespace Terminal {
base.destroy ();
}

private void on_get_text (Gtk.Clipboard board, string? intext) {
if (Application.settings.get_boolean ("unsafe-paste-alert") && !unsafe_ignored ) {

if (intext == null) {
return;
}

if (!intext.validate ()) {
warning ("Dropping invalid UTF-8 paste");
return;
}

var text = intext.strip ();

string? unsafe_warning = null;

if ((text.index_of ("sudo") > -1) && (text.index_of ("\n") != 0)) {
unsafe_warning = _("The pasted text may be trying to gain administrative access");
} else if (text.index_of ("\n") != -1) {
unsafe_warning = _("The pasted text may contain multiple commands");
}

if (unsafe_warning != null) {
var unsafe_paste_dialog = new UnsafePasteDialog (
this,
unsafe_warning,
text
);

if (unsafe_paste_dialog.run () != Gtk.ResponseType.ACCEPT) {
unsafe_paste_dialog.destroy ();
return;
}

unsafe_paste_dialog.destroy ();
}
}

current_terminal.remember_command_start_position ();

if (board == primary_selection) {
current_terminal.paste_primary ();
} else {
current_terminal.paste_clipboard ();
}
}

private void action_copy () {
if (current_terminal.link_uri != null && ! current_terminal.get_has_selection ())
clipboard.set_text (current_terminal.link_uri,
current_terminal.link_uri.length);
else
current_terminal.copy_clipboard ();
}

private void action_copy_last_output () {
string output = current_terminal.get_last_output ();
Gtk.Clipboard.get_default (Gdk.Display.get_default ()).set_text (output, output.length);
}

private void action_paste () {
clipboard.request_text (on_get_text);
}

private void action_select_all () {
current_terminal.select_all ();
}

private void action_open_in_browser () {
get_current_selection_link_or_pwd ((clipboard, uri) => {
string? to_open = Utils.sanitize_path (uri, current_terminal.get_shell_location ());
Expand Down Expand Up @@ -1048,12 +923,6 @@ namespace Terminal {
}
}

private void action_scroll_to_last_command () {
current_terminal.scroll_to_last_command ();
/* Repeated presses are ignored */
get_simple_action (ACTION_SCROLL_TO_LAST_COMMAND).set_enabled (false);
}

private void action_close_tab () {
current_terminal.tab.close ();
current_terminal.grab_focus ();
Expand All @@ -1072,25 +941,6 @@ namespace Terminal {
new_tab (current_terminal.get_shell_location ());
}

private void action_reload_tab () {
current_terminal.reload ();
}

private void action_zoom_in_font () {
current_terminal.increment_size ();
save_opened_terminals (false, true);
}

private void action_zoom_out_font () {
current_terminal.decrement_size ();
save_opened_terminals (false, true);
}

private void action_zoom_default_font () {
current_terminal.set_default_font_size ();
save_opened_terminals (false, true);
}

private void action_next_tab () {
notebook.next_page ();
}
Expand Down
13 changes: 7 additions & 6 deletions src/Widgets/SettingsPopover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {

set {
terminal_binding.source = value;
insert_action_group ("term", value.get_action_group ("term"));
}
}

Expand All @@ -34,28 +35,28 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {

construct {
var zoom_out_button = new Gtk.Button.from_icon_name ("zoom-out-symbolic", MENU) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_ZOOM_OUT_FONT,
tooltip_markup = Granite.markup_accel_tooltip (
MainWindow.ACCELS_ZOOM_OUT_FONT,
TerminalWidget.ACCELS_ZOOM_OUT,
_("Zoom out")
)
};
zoom_out_button.set_detailed_action_name (TerminalWidget.ACTION_ZOOM_OUT);

var zoom_default_button = new Gtk.Button () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_ZOOM_DEFAULT_FONT,
tooltip_markup = Granite.markup_accel_tooltip (
MainWindow.ACCELS_ZOOM_DEFAULT_FONT,
TerminalWidget.ACCELS_ZOOM_DEFAULT,
_("Default zoom level")
)
};
zoom_default_button.set_detailed_action_name (TerminalWidget.ACTION_ZOOM_DEFAULT);

var zoom_in_button = new Gtk.Button.from_icon_name ("zoom-in-symbolic", MENU) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_ZOOM_IN_FONT,
tooltip_markup = Granite.markup_accel_tooltip (
MainWindow.ACCELS_ZOOM_IN_FONT,
TerminalWidget.ACCELS_ZOOM_IN,
_("Zoom in")
)
};
zoom_in_button.set_detailed_action_name (TerminalWidget.ACTION_ZOOM_IN);

var font_size_box = new Gtk.Box (HORIZONTAL, 0) {
homogeneous = true,
Expand Down
Loading

0 comments on commit 2d0f480

Please sign in to comment.