Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ namespace Terminal {
public const string ACTION_SEARCH_ACCEL = "<Control><Shift>f";
public const string ACTION_SEARCH_NEXT = "action-search-next";
public const string ACTION_SEARCH_PREVIOUS = "action-search-previous";
public const string ACTION_OPEN_IN_BROWSER = "action-open-in-browser";
public const string ACTION_OPEN_IN_BROWSER_ACCEL = "<Control><Shift>e";

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

Expand All @@ -69,8 +67,7 @@ namespace Terminal {
{ ACTION_MOVE_TAB_TO_NEW_WINDOW, action_move_tab_to_new_window},
{ ACTION_SEARCH, action_search, null, "false", action_search_change_state },
{ ACTION_SEARCH_NEXT, action_search_next },
{ ACTION_SEARCH_PREVIOUS, action_search_previous },
{ ACTION_OPEN_IN_BROWSER, action_open_in_browser }
{ ACTION_SEARCH_PREVIOUS, action_search_previous }
};

public MainWindow (Terminal.Application app, bool recreate_tabs = true) {
Expand All @@ -93,7 +90,6 @@ namespace Terminal {
action_accelerators[ACTION_MOVE_TAB_RIGHT] = "<Control><Alt>Right";
action_accelerators[ACTION_MOVE_TAB_LEFT] = "<Control><Alt>Left";
action_accelerators[ACTION_SEARCH] = ACTION_SEARCH_ACCEL;
action_accelerators[ACTION_OPEN_IN_BROWSER] = ACTION_OPEN_IN_BROWSER_ACCEL;
}

construct {
Expand Down Expand Up @@ -149,12 +145,11 @@ namespace Terminal {
var show_in_browser_uri = get_current_selection_link_or_pwd ();
var appinfo = Utils.get_default_app_for_uri (show_in_browser_uri);

get_simple_action (ACTION_OPEN_IN_BROWSER).set_enabled (appinfo != null);
var open_in_browser_menuitem = new MenuItem (
_("Show in %s").printf (appinfo != null ? appinfo.get_display_name () : _("Default application")),
ACTION_PREFIX + ACTION_OPEN_IN_BROWSER
TerminalWidget.ACTION_OPEN_IN_BROWSER
);
open_in_browser_menuitem.set_attribute_value ("accel", ACTION_OPEN_IN_BROWSER_ACCEL);
open_in_browser_menuitem.set_attribute_value ("accel", new Variant ("s", TerminalWidget.ACCELS_OPEN_IN_BROWSER[0]));

var copy_menuitem = new MenuItem (
_("Copy"),
Expand Down Expand Up @@ -610,19 +605,6 @@ namespace Terminal {
}
}

private void action_open_in_browser () requires (current_terminal != null) {
var uri = get_current_selection_link_or_pwd ();
var context = Gdk.Display.get_default ().get_app_launch_context ();
AppInfo.launch_default_for_uri_async.begin (uri, context, null, (obj, res) => {
try {
AppInfo.launch_default_for_uri_async.end (res);
} catch (Error e) {
warning ("Launcher failed with error %s", e.message);
//TODO Handle launch failure - message box?
}
});
}

private string? get_current_selection_link_or_pwd () requires (current_terminal != null) {
var link_uri = current_terminal.link_uri;
if (link_uri == null) {
Expand Down
68 changes: 58 additions & 10 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Terminal {
public unowned Adw.TabPage? tab;
public string? link_uri;

public const string ACTION_OPEN_IN_BROWSER = "term.open-in-browser";
public const string ACTION_COPY = "term.copy";
public const string ACTION_COPY_OUTPUT = "term.copy-output";
public const string ACTION_CLEAR_SCREEN = "term.clear-screen";
Expand All @@ -43,7 +44,7 @@ namespace Terminal {
public const string ACTION_SCROLL_TO_COMMAND = "term.scroll-to-command";
public const string ACTION_SELECT_ALL = "term.select-all";


public const string[] ACCELS_OPEN_IN_BROWSER = { "<Control><Shift>E", null };
public const string[] ACCELS_COPY = { "<Control><Shift>C", null };
public const string[] ACCELS_COPY_OUTPUT = { "<Alt>C", null };
public const string[] ACCELS_CLEAR_SCREEN = { "<Control><Shift>L", null };
Expand Down Expand Up @@ -103,6 +104,7 @@ namespace Terminal {

private unowned Gdk.Clipboard clipboard;

private GLib.SimpleAction open_in_browser_action;
private GLib.SimpleAction copy_action;
private GLib.SimpleAction copy_output_action;
private GLib.SimpleAction clear_screen_action;
Expand Down Expand Up @@ -248,6 +250,11 @@ namespace Terminal {
var action_group = new GLib.SimpleActionGroup ();
insert_action_group ("term", action_group);

open_in_browser_action = new GLib.SimpleAction ("open-in-browser", null);
open_in_browser_action.set_enabled (false);
open_in_browser_action.activate.connect (open_in_browser);
action_group.add_action (open_in_browser_action);

copy_action = new GLib.SimpleAction ("copy", null);
copy_action.set_enabled (false);
copy_action.activate.connect (() => copy_clipboard.emit ());
Expand Down Expand Up @@ -316,7 +323,7 @@ namespace Terminal {
link_uri = get_link (x, y);

if (link_uri != null && !get_has_selection ()) {
main_window.get_simple_action (MainWindow.ACTION_OPEN_IN_BROWSER).activate (null);
open_in_browser_action.activate (null);
}
} else {
allow_hyperlink = true;
Expand Down Expand Up @@ -482,15 +489,13 @@ namespace Terminal {
}

private void setup_menu () {
// Update the "Paste" menu option
var formats = clipboard.get_formats ();
bool can_paste = false;
// Update the "Open in" menu option
var appinfo = Utils.get_default_app_for_uri (get_current_selection_link_or_pwd ());
open_in_browser_action.set_enabled (appinfo != null);

if (formats != null) {
can_paste = formats.contain_gtype (Type.STRING);
}

paste_action.set_enabled (can_paste);
// Update the "Paste" menu option
var clipboard_has_string = clipboard.formats != null && clipboard.formats.contain_gtype (Type.STRING);
paste_action.set_enabled (clipboard_has_string);

// Update the "Copy Last Output" menu option
var has_output = !resized && get_last_output ().length > 0;
Expand Down Expand Up @@ -1073,5 +1078,48 @@ namespace Terminal {
contents_changed_timeout_id = 0;
}
}

private void open_in_browser (GLib.SimpleAction action, GLib.Variant? parameter) {
var uri = get_current_selection_link_or_pwd ();
var context = Gdk.Display.get_default ().get_app_launch_context ();
AppInfo.launch_default_for_uri_async.begin (uri, context, null, (obj, res) => {
try {
AppInfo.launch_default_for_uri_async.end (res);
} catch (Error e) {
warning ("Launcher failed with error %s", e.message);
//TODO Handle launch failure - message box?
}
});
}

private string? get_current_selection_link_or_pwd () {
if (link_uri == null) {
if (get_has_selection ()) {
copy_primary ();
try {
var cp = Gdk.Display.get_default ().get_primary_clipboard ().get_content ();
if (cp != null) {
var val = Value (typeof (string));
cp.get_value (ref val);
return val.dup_string ();
}
} catch (Error e) {
critical ("Unable to get clipboard contents");
}

return null;
} else {
var shell_location_path = get_shell_location ();
var shell_location_file = GLib.File.new_for_path (shell_location_path);
return shell_location_file.get_uri ();
}
} else {
if (!link_uri.contains ("://")) {
link_uri = "http://" + link_uri;
}

return link_uri;
}
}
}
}