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

Commit d8bf10f

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 820e093 commit d8bf10f

7 files changed

+61
-6
lines changed

BrowserWindow.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,16 @@ void BrowserWindow::debug_request(String const& request, String const& argument)
281281
}
282282

283283
void BrowserWindow::new_tab()
284+
{
285+
new_tab_with_url({});
286+
}
287+
288+
void BrowserWindow::new_tab_with_url(const QUrl &url)
284289
{
285290
auto tab = make<Tab>(this);
286291
auto tab_ptr = tab.ptr();
292+
if (!url.isEmpty())
293+
tab->navigate(url.toString());
287294
m_tabs.append(std::move(tab));
288295

289296
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)
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

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
66

7-
#define AK_DONT_REPLACE_STD
87

98
#include "Utilities.h"
109
#include <AK/LexicalPath.h>
1110
#include <AK/Platform.h>
1211
#include <LibCore/File.h>
12+
#include <LibGfx/Point.h>
1313
#include <QCoreApplication>
14+
#include <QPoint>
1415

1516
String s_serenity_resource_root;
1617

@@ -45,3 +46,13 @@ void platform_init()
4546
}();
4647
#endif
4748
}
49+
50+
QUrl qurl_from_akurl(AK::URL const& akurl)
51+
{
52+
return QUrl(qstring_from_akstring(akurl.to_string()));
53+
}
54+
55+
QPoint intpoint_to_qpoint(Gfx::IntPoint const& position)
56+
{
57+
return QPoint(position.x(), position.y());
58+
}

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 intpoint_to_qpoint(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
@@ -804,11 +804,13 @@ void WebContentView::notify_server_did_request_context_menu(Badge<WebContentClie
804804
(void)content_position;
805805
}
806806

807-
void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, AK::URL const& url, String const&, unsigned)
807+
void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint const& content_position, AK::URL const& url, String const&, unsigned modifiers)
808808
{
809-
// FIXME
810-
(void)content_position;
811-
(void)url;
809+
auto view_offset = QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value());
810+
auto position_in_page = intpoint_to_qpoint(content_position);
811+
auto position_in_view = position_in_page - view_offset;
812+
auto qurl = qurl_from_akurl(url);
813+
emit link_content_menu(position_in_view, qurl, modifiers);
812814
}
813815

814816
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
@@ -153,6 +153,7 @@ class WebContentView final
153153
void title_changed(QString);
154154
void favicon_changed(QIcon);
155155
void got_source(URL, QString);
156+
void link_content_menu(QPoint &local_position, const QUrl&, unsigned modifiers);
156157

157158
private:
158159
void request_repaint();

0 commit comments

Comments
 (0)