From f8e3c8326de9c5a3de42542614575ab95357bff2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 12 May 2020 18:45:03 +0200 Subject: [PATCH] Terminal: When offering to open a URL, show name + icon for handler app Instead of just saying "Open URL" when you right click on a hyperlink in the terminal, we now say something like "Open in Browser". :^) --- Libraries/LibVT/TerminalWidget.cpp | 45 ++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/Libraries/LibVT/TerminalWidget.cpp b/Libraries/LibVT/TerminalWidget.cpp index c8db17f80c..e076e0c30d 100644 --- a/Libraries/LibVT/TerminalWidget.cpp +++ b/Libraries/LibVT/TerminalWidget.cpp @@ -26,11 +26,13 @@ #include "TerminalWidget.h" #include "XtermColors.h" +#include #include #include #include #include #include +#include #include #include #include @@ -131,17 +133,6 @@ TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtradd_action(copy_action()); m_context_menu->add_action(paste_action()); - - m_context_menu_for_hyperlink = GUI::Menu::construct(); - m_context_menu_for_hyperlink->add_action(GUI::Action::create("Open URL", [this](auto&) { - Desktop::Launcher::open(m_context_menu_href); - })); - m_context_menu_for_hyperlink->add_action(GUI::Action::create("Copy URL", [this](auto&) { - GUI::Clipboard::the().set_data(m_context_menu_href); - })); - m_context_menu_for_hyperlink->add_separator(); - m_context_menu_for_hyperlink->add_action(copy_action()); - m_context_menu_for_hyperlink->add_action(paste_action()); } TerminalWidget::~TerminalWidget() @@ -849,6 +840,38 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event) m_context_menu->popup(event.screen_position()); } else { m_context_menu_href = m_hovered_href; + + // Ask LaunchServer for a list of programs that can handle the right-clicked URL. + auto handlers = Desktop::Launcher::get_handlers_for_url(m_hovered_href); + if (handlers.is_empty()) { + m_context_menu->popup(event.screen_position()); + return; + } + + m_context_menu_for_hyperlink = GUI::Menu::construct(); + + // Go through the list of handlers and see if we can find a nice display name + icon for them. + // Then add them to the context menu. + // FIXME: Adapt this code when we actually support calling LaunchServer with a specific handler in mind. + for (auto& handler : handlers) { + auto af_path = String::format("/res/apps/%s.af", FileSystemPath(handler).basename().characters()); + auto af = Core::ConfigFile::open(af_path); + auto handler_name = af->read_entry("App", "Name", handler); + auto handler_icon = af->read_entry("Icons", "16x16", {}); + + auto icon = Gfx::Bitmap::load_from_file(handler_icon); + + m_context_menu_for_hyperlink->add_action(GUI::Action::create(String::format("Open in %s", handler_name.characters()), move(icon), [this](auto&) { + Desktop::Launcher::open(m_context_menu_href); + })); + } + m_context_menu_for_hyperlink->add_action(GUI::Action::create("Copy URL", [this](auto&) { + GUI::Clipboard::the().set_data(m_context_menu_href); + })); + m_context_menu_for_hyperlink->add_separator(); + m_context_menu_for_hyperlink->add_action(copy_action()); + m_context_menu_for_hyperlink->add_action(paste_action()); + m_context_menu_for_hyperlink->popup(event.screen_position()); } }