Skip to content
This repository was archived by the owner on Dec 25, 2022. It is now read-only.

Commit 12a650c

Browse files
committed
Context menu for links
This patch implements a context menu for links. In the future, we could even check for the modifiers and open links in new windows, or private windows - but not today.
1 parent a42adcf commit 12a650c

7 files changed

+63
-5
lines changed

BrowserWindow.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,17 @@ void BrowserWindow::debug_request(String const& request, String const& argument)
282282
}
283283

284284
void BrowserWindow::new_tab()
285+
{
286+
auto tab = make<Tab>(this, m_webdriver_fd_passing_socket);
287+
new_tab_with_url({});
288+
}
289+
290+
void BrowserWindow::new_tab_with_url(const QUrl &url)
285291
{
286292
auto tab = make<Tab>(this, m_webdriver_fd_passing_socket);
287293
auto tab_ptr = tab.ptr();
294+
if (!url.isEmpty())
295+
tab->navigate(url.toString());
288296
m_tabs.append(std::move(tab));
289297

290298
if (m_current_tab == nullptr) {

BrowserWindow.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public slots:
3232
void tab_title_changed(int index, QString const&);
3333
void tab_favicon_changed(int index, QIcon icon);
3434
void new_tab();
35+
void new_tab_with_url(QUrl const&);
3536
void close_tab(int index);
3637
void close_current_tab();
3738
void open_next_tab();

Tab.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
#include "Settings.h"
1111
#include "Utilities.h"
1212
#include <Browser/History.h>
13-
#include <QCoreApplication>
1413
#include <QFont>
1514
#include <QFontMetrics>
1615
#include <QPlainTextEdit>
16+
#include <QClipboard>
17+
#include <QFont>
18+
#include <QFontMetrics>
19+
#include <QGuiApplication>
1720
#include <QPoint>
1821
#include <QResizeEvent>
22+
#include <QUrl>
1923

2024
extern String s_serenity_resource_root;
2125
extern Browser::Settings* s_settings;
@@ -93,6 +97,27 @@ Tab::Tab(BrowserWindow* window, int webdriver_fd_passing_socket)
9397
m_back_action->setEnabled(m_history.can_go_back());
9498
m_forward_action->setEnabled(m_history.can_go_forward());
9599
});
100+
101+
QObject::connect(m_view, &WebContentView::link_content_menu, [this] (QPoint &local_position, const QUrl& url, unsigned ){
102+
auto global_position = this->mapToGlobal(local_position);
103+
auto menu = QMenu();
104+
105+
auto copy_link_action = new QAction(tr("&Copy Link"));
106+
auto open_link_in_tab_action = new QAction(tr("Open link in a &new tab"));
107+
108+
menu.addAction(open_link_in_tab_action);
109+
menu.addAction(copy_link_action);
110+
auto res = menu.exec(global_position);
111+
112+
if (res == copy_link_action) {
113+
QClipboard *clipboard = QGuiApplication::clipboard();
114+
clipboard->setText(url.toString());
115+
qDebug() << "Copied to clipboard text:" << url.toString();
116+
} else if (res == open_link_in_tab_action){
117+
auto browser_window = static_cast<BrowserWindow*>(m_window);
118+
return browser_window->new_tab_with_url(url);
119+
}
120+
});
96121
QObject::connect(m_location_edit, &QLineEdit::returnPressed, this, &Tab::location_edit_return_pressed);
97122
QObject::connect(m_view, &WebContentView::title_changed, this, &Tab::page_title_changed);
98123
QObject::connect(m_view, &WebContentView::favicon_changed, this, &Tab::page_favicon_changed);

Utilities.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <AK/LexicalPath.h>
1111
#include <AK/Platform.h>
1212
#include <LibCore/File.h>
13+
#include <LibGfx/Point.h>
1314
#include <QCoreApplication>
15+
#include <QPoint>
1416

1517
String s_serenity_resource_root;
1618

@@ -24,6 +26,16 @@ QString qstring_from_akstring(AK::String const& akstring)
2426
return QString::fromUtf8(akstring.characters(), akstring.length());
2527
}
2628

29+
QUrl qurl_from_akurl(AK::URL const& akurl)
30+
{
31+
return QUrl(qstring_from_akstring(akurl.to_string()));
32+
}
33+
34+
QPoint qpoint_from_intpoint(Gfx::IntPoint const& position)
35+
{
36+
return QPoint(position.x(), position.y());
37+
}
38+
2739
void platform_init()
2840
{
2941
#ifdef AK_OS_ANDROID
@@ -45,3 +57,4 @@ void platform_init()
4557
}();
4658
#endif
4759
}
60+

Utilities.h

+8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
#pragma once
88

9+
#define AK_DONT_REPLACE_STD
10+
#include <AK/URL.h>
11+
#include <LibGfx/Forward.h>
912
#include <AK/String.h>
1013
#include <QString>
14+
#include <QPoint>
15+
#include <QUrl>
1116

1217
AK::String akstring_from_qstring(QString const&);
1318
QString qstring_from_akstring(AK::String const&);
19+
QUrl qurl_from_akurl(AK::URL const& akurl);
20+
QPoint qpoint_from_intpoint(Gfx::IntPoint const& position);
21+
1422
void platform_init();
1523

1624
extern String s_serenity_resource_root;

WebContentView.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,13 @@ void WebContentView::notify_server_did_request_context_menu(Badge<WebContentClie
833833
(void)content_position;
834834
}
835835

836-
void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, AK::URL const& url, String const&, unsigned)
836+
void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, AK::URL const& url, String const&, unsigned modifiers)
837837
{
838-
// FIXME
839-
(void)content_position;
840-
(void)url;
838+
auto view_offset = QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value());
839+
auto position_in_page = qpoint_from_intpoint(content_position);
840+
auto position_in_view = position_in_page - view_offset;
841+
auto qurl = qurl_from_akurl(url);
842+
emit link_content_menu(position_in_view, qurl, modifiers);
841843
}
842844

843845
void WebContentView::notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, AK::URL const& url, String const&, unsigned, Gfx::ShareableBitmap const& bitmap)

WebContentView.h

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class WebContentView final
169169
Gfx::IntRect maximize_window();
170170
Gfx::IntRect minimize_window();
171171
Gfx::IntRect fullscreen_window();
172+
void link_content_menu(QPoint &local_position, const QUrl&, unsigned modifiers);
172173

173174
private:
174175
void request_repaint();

0 commit comments

Comments
 (0)