Skip to content

Truly independent web browser

UI/Qt: Show the hovered link's address over the page on Wayland

Andreas Klingd628b951201e
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 28daaa94542..043ec489ef2 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -70,6 +70,8 @@
namespace Ladybird {
+static constexpr int HOVER_TEXT_CORNER_RADIUS = 4;
+
static constexpr auto WINDOW_DRAG_REGION_PROPERTY = "LadybirdWindowDragRegion";
static constexpr int BOOKMARKS_BAR_HOVER_COLLAPSE_POLL_INTERVAL_MS = 250;
@@ -1388,15 +1390,16 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)
});
};
+ QObject::connect(m_view, &WebContentView::image_overlay_unavailable, this, &Tab::update_hover_label);
+
view().on_link_hover = [this](auto const& url) {
- m_hover_label->setText(qstring_from_ak_string(url.to_byte_string()));
+ m_hover_text = qstring_from_ak_string(url.to_byte_string());
+ m_hover_text_is_on_right = false;
update_hover_label();
- m_hover_label->setCursor(view().cursor());
- m_hover_label->show();
};
view().on_link_unhover = [this]() {
- m_hover_label->hide();
+ hide_hover_label();
};
view().on_loading_state_change = [this](bool is_loading) {
@@ -2298,7 +2301,7 @@ void Tab::resizeEvent(QResizeEvent* event)
if (!is_loaded())
return;
- if (m_hover_label->isVisible())
+ if (!m_hover_text.isEmpty())
update_hover_label();
}
@@ -2309,12 +2312,32 @@ void Tab::hideEvent(QHideEvent* event)
return;
set_bookmarks_bar_hover_expanded(false);
- m_hover_label->hide();
+ hide_hover_label();
}
void Tab::update_hover_label()
{
- m_hover_label->setText(QFontMetrics(m_hover_label->font()).elidedText(m_hover_label->text(), Qt::ElideRight, width() / 2 - 10));
+ if (m_hover_text.isEmpty())
+ return;
+ auto text = QFontMetrics(m_hover_label->font()).elidedText(m_hover_text, Qt::ElideRight, width() / 2 - 10);
+
+ // NB: Where the view can stack the text above the page itself, it stays inside the window and costs the page
+ // nothing. A tooltip is a window of its own instead, which can stick out of the browser window.
+ auto image = render_hover_text(text);
+ auto size = image.deviceIndependentSize().toSize();
+ QPoint position { m_hover_text_is_on_right ? m_view->width() - size.width() : 0, m_view->height() - size.height() };
+ if (m_view->show_image_over_page(image, position)) {
+ m_hover_text_rect_over_page = { position, size };
+ m_hover_label->hide();
+ return;
+ }
+ if (!m_hover_text_rect_over_page.isEmpty()) {
+ m_view->hide_image_over_page();
+ m_hover_text_rect_over_page = {};
+ }
+
+ m_hover_label->setText(text);
+ m_hover_label->setCursor(view().cursor());
m_hover_label->resize(m_hover_label->sizeHint());
auto hover_label_height = height() - m_hover_label->height();
@@ -2327,13 +2350,59 @@ void Tab::update_hover_label()
m_hover_label->move(left_position);
m_hover_label->raise();
+ m_hover_label->show();
}
void Tab::hide_hover_label()
{
+ m_hover_text = {};
+ if (!m_hover_text_rect_over_page.isEmpty()) {
+ m_view->hide_image_over_page();
+ m_hover_text_rect_over_page = {};
+ }
m_hover_label->hide();
}
+// Paints the hover text the way the hover label's style sheet does. It sits in a bottom corner of the page, and only
+// its corner toward the page is rounded.
+QImage Tab::render_hover_text(QString const& text) const
+{
+ static constexpr int horizontal_padding = 4 + 1;
+ static constexpr int vertical_padding = 2 + 1;
+
+ QFontMetrics metrics { m_hover_label->font() };
+ QSize size { metrics.horizontalAdvance(text) + 2 * horizontal_padding, metrics.height() + 2 * vertical_padding };
+ auto device_pixel_ratio = devicePixelRatioF();
+ QImage image { size * device_pixel_ratio, QImage::Format_ARGB32_Premultiplied };
+ image.setDevicePixelRatio(device_pixel_ratio);
+ image.fill(Qt::transparent);
+
+ auto palette = Theme::chrome_palette(*this);
+ QPainter painter { &image };
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(ChromeStyle::chrome_border(palette));
+ painter.setBrush(ChromeStyle::chrome_surface(palette));
+ auto rect = QRectF { QPointF {}, size }.adjusted(0.5, 0.5, -0.5, -0.5);
+ QPainterPath path;
+ path.moveTo(rect.bottomLeft());
+ if (m_hover_text_is_on_right) {
+ path.lineTo(rect.left(), rect.top() + HOVER_TEXT_CORNER_RADIUS);
+ path.arcTo(rect.left(), rect.top(), 2 * HOVER_TEXT_CORNER_RADIUS, 2 * HOVER_TEXT_CORNER_RADIUS, 180, -90);
+ path.lineTo(rect.topRight());
+ } else {
+ path.lineTo(rect.topLeft());
+ path.lineTo(rect.right() - HOVER_TEXT_CORNER_RADIUS, rect.top());
+ path.arcTo(rect.right() - 2 * HOVER_TEXT_CORNER_RADIUS, rect.top(), 2 * HOVER_TEXT_CORNER_RADIUS, 2 * HOVER_TEXT_CORNER_RADIUS, 90, -90);
+ }
+ path.lineTo(rect.bottomRight());
+ path.closeSubpath();
+ painter.drawPath(path);
+ painter.setFont(m_hover_label->font());
+ painter.setPen(ChromeStyle::chrome_text(palette));
+ painter.drawText(QRect { QPoint {}, size }, Qt::AlignCenter, text);
+ return image;
+}
+
bool Tab::event(QEvent* event)
{
// An inactive window's toolbar icons drop to the muted color.
@@ -2357,6 +2426,11 @@ bool Tab::eventFilter(QObject* watched, QEvent* event)
if (static_cast<QDynamicPropertyChangeEvent*>(event)->propertyName() == "autocompleteOpen")
update_bookmarks_bar_hover();
}
+ // NB: The hover text over the page lets the pointer through, so it moves aside when the pointer comes over it.
+ if (watched == m_view && event->type() == QEvent::MouseMove && m_hover_text_rect_over_page.contains(static_cast<QMouseEvent*>(event)->position().toPoint())) {
+ m_hover_text_is_on_right = !m_hover_text_is_on_right;
+ update_hover_label();
+ }
if (watched == m_view && (event->type() == QEvent::Resize || event->type() == QEvent::Move)) {
update_bookmarks_bar_geometry();
if (m_find_in_page->isVisible())
@@ -2392,8 +2466,8 @@ void Tab::update_chrome_style()
auto hover_surface = ChromeStyle::style_sheet_color(ChromeStyle::chrome_surface(Theme::chrome_palette(*this)));
auto hover_border = ChromeStyle::style_sheet_color(ChromeStyle::chrome_border(Theme::chrome_palette(*this)));
auto hover_text = ChromeStyle::style_sheet_color(ChromeStyle::chrome_text(Theme::chrome_palette(*this)));
- m_hover_label->setStyleSheet(qformatted("background: {}; color: {}; border: 1px solid {}; border-radius: 6px;",
- hover_surface, hover_text, hover_border));
+ m_hover_label->setStyleSheet(qformatted("background: {}; color: {}; border: 1px solid {}; border-top-right-radius: {}px;",
+ hover_surface, hover_text, hover_border, HOVER_TEXT_CORNER_RADIUS));
if (m_downloads_popover)
m_downloads_popover->update_chrome_style(palette());
m_is_updating_chrome_style = false;
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index a939a097290..bb8f44258fb 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -232,6 +232,7 @@ private:
void position_downloads_popover();
void position_private_session_popover();
void hide_hover_label();
+ QImage render_hover_text(QString const&) const;
void set_loading(bool);
void update_tab_icon();
int tab_index();
@@ -283,6 +284,11 @@ private:
Optional<u64> m_report_review_navigation_listener;
bool m_has_loaded_report_review_page { false };
HyperlinkLabel* m_hover_label { nullptr };
+ // The address of the hovered link, which shows over the bottom corner of the page that the pointer is not over.
+ QString m_hover_text;
+ bool m_hover_text_is_on_right { false };
+ // Where the hover text shows over the page, in the view, when the view can stack it above the page itself.
+ QRect m_hover_text_rect_over_page;
QIcon m_favicon;
bool m_is_loading { false };
bool m_needs_attention { false };
diff --git a/UI/Qt/WaylandPageSurface.cpp b/UI/Qt/WaylandPageSurface.cpp
index 14bf3d36dbe..d5109868ab1 100644
--- a/UI/Qt/WaylandPageSurface.cpp
+++ b/UI/Qt/WaylandPageSurface.cpp
@@ -11,11 +11,14 @@
#include <UI/Qt/WaylandRegistry.h>
#include <QGuiApplication>
+#include <QImage>
#include <QWindow>
#include <qpa/qplatformwindow_p.h>
#include <libdrm/drm_fourcc.h>
+#include <sys/mman.h>
#include <sys/stat.h>
+#include <unistd.h>
#include <wayland-client.h>
// NB: The generated listener functions cast the const away from the listeners they pass to libwayland.
@@ -50,12 +53,13 @@ OwnPtr<WaylandPageSurface> WaylandPageSurface::create()
.subcompositor = registry->bind<wl_subcompositor>(wl_subcompositor_interface, 1),
.viewporter = registry->bind<wp_viewporter>(wp_viewporter_interface, 1),
.dmabuf = registry->bind<zwp_linux_dmabuf_v1>(zwp_linux_dmabuf_v1_interface, 3),
+ .shm = registry->bind<wl_shm>(wl_shm_interface, 1),
};
- for (void* proxy : { static_cast<void*>(globals.subcompositor), static_cast<void*>(globals.viewporter), static_cast<void*>(globals.dmabuf) }) {
+ for (void* proxy : { static_cast<void*>(globals.subcompositor), static_cast<void*>(globals.viewporter), static_cast<void*>(globals.dmabuf), static_cast<void*>(globals.shm) }) {
if (proxy)
WaylandRegistry::move_to_qt_queue(proxy);
}
- if (!globals.subcompositor || !globals.viewporter || !globals.dmabuf)
+ if (!globals.subcompositor || !globals.viewporter || !globals.dmabuf || !globals.shm)
return {};
return globals;
}();
@@ -70,10 +74,13 @@ WaylandPageSurface::WaylandPageSurface(Globals globals)
{
m_surface = wl_compositor_create_surface(m_globals.compositor);
m_viewport = wp_viewporter_get_viewport(m_globals.viewporter, m_surface);
+ m_overlay_surface = wl_compositor_create_surface(m_globals.compositor);
+ m_overlay_viewport = wp_viewporter_get_viewport(m_globals.viewporter, m_overlay_surface);
// NB: Input goes to the browser window underneath, where Qt delivers it to the view.
auto* empty_region = wl_compositor_create_region(m_globals.compositor);
wl_surface_set_input_region(m_surface, empty_region);
+ wl_surface_set_input_region(m_overlay_surface, empty_region);
wl_region_destroy(empty_region);
}
@@ -85,6 +92,10 @@ WaylandPageSurface::~WaylandPageSurface()
QObject::disconnect(m_parent_surface_created_connection);
for (auto& buffer : m_buffers)
destroy_buffer(*buffer);
+ for (auto* buffer : m_overlay_buffers)
+ wl_buffer_destroy(buffer);
+ wp_viewport_destroy(m_overlay_viewport);
+ wl_surface_destroy(m_overlay_surface);
wp_viewport_destroy(m_viewport);
wl_surface_destroy(m_surface);
}
@@ -110,6 +121,9 @@ bool WaylandPageSurface::ensure_subsurface(QWindow& parent)
wl_subsurface_set_sync(m_subsurface);
else
wl_subsurface_set_desync(m_subsurface);
+ // NB: A new subsurface goes on top of its siblings, so the overlay is above the page. It stays synchronized, so
+ // that what it shows and where it shows it change together.
+ m_overlay_subsurface = wl_subcompositor_get_subsurface(m_globals.subcompositor, m_overlay_surface, parent_surface);
m_parent = &parent;
m_parent_surface = parent_surface;
m_parent_surface_destroyed_connection = QObject::connect(wayland_window, &QNativeInterface::Private::QWaylandWindow::surfaceDestroyed, [this, wayland_window] {
@@ -137,10 +151,15 @@ void WaylandPageSurface::destroy_subsurface()
wl_subsurface_destroy(m_subsurface);
m_subsurface = nullptr;
}
+ if (m_overlay_subsurface) {
+ wl_subsurface_destroy(m_overlay_subsurface);
+ m_overlay_subsurface = nullptr;
+ }
m_parent = nullptr;
m_parent_surface = nullptr;
m_position.clear();
hide();
+ hide_overlay();
}
WaylandPageSurface::Buffer* WaylandPageSurface::buffer_for(Frame const& frame)
@@ -382,6 +401,61 @@ void WaylandPageSurface::set_synchronized(bool synchronized)
wl_subsurface_set_desync(m_subsurface);
}
+bool WaylandPageSurface::show_overlay(QWindow& parent, QImage const& image, QPoint position)
+{
+ if (image.isNull() || !ensure_subsurface(parent))
+ return false;
+
+ // NB: Wayland's ARGB8888 is Qt's premultiplied ARGB32 on a little-endian machine.
+ auto pixels = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
+ auto size = static_cast<size_t>(pixels.sizeInBytes());
+ auto fd = memfd_create("ladybird-overlay", MFD_CLOEXEC);
+ if (fd < 0)
+ return false;
+ auto* memory = ftruncate(fd, static_cast<off_t>(size)) == 0 ? mmap(nullptr, size, PROT_WRITE, MAP_SHARED, fd, 0) : MAP_FAILED;
+ if (memory == MAP_FAILED) {
+ ::close(fd);
+ return false;
+ }
+ memcpy(memory, pixels.constBits(), size);
+ munmap(memory, size);
+
+ auto* pool = wl_shm_create_pool(m_globals.shm, fd, static_cast<i32>(size));
+ auto* buffer = wl_shm_pool_create_buffer(pool, 0, pixels.width(), pixels.height(), static_cast<i32>(pixels.bytesPerLine()), WL_SHM_FORMAT_ARGB8888);
+ wl_shm_pool_destroy(pool);
+ ::close(fd);
+ static wl_buffer_listener const buffer_listener {
+ .release = overlay_buffer_released,
+ };
+ wl_buffer_add_listener(buffer, &buffer_listener, this);
+ m_overlay_buffers.append(buffer);
+
+ auto logical_size = image.deviceIndependentSize().toSize();
+ wl_subsurface_set_position(m_overlay_subsurface, position.x(), position.y());
+ wp_viewport_set_destination(m_overlay_viewport, logical_size.width(), logical_size.height());
+ wl_surface_attach(m_overlay_surface, buffer, 0, 0);
+ wl_surface_damage_buffer(m_overlay_surface, 0, 0, INT32_MAX, INT32_MAX);
+ wl_surface_commit(m_overlay_surface);
+ m_overlay_is_attached = true;
+ return true;
+}
+
+void WaylandPageSurface::hide_overlay()
+{
+ if (!m_overlay_is_attached)
+ return;
+ wl_surface_attach(m_overlay_surface, nullptr, 0, 0);
+ wl_surface_commit(m_overlay_surface);
+ m_overlay_is_attached = false;
+}
+
+void WaylandPageSurface::overlay_buffer_released(void* data, wl_buffer* buffer)
+{
+ auto& surface = *static_cast<WaylandPageSurface*>(data);
+ surface.m_overlay_buffers.remove_first_matching([&](auto* candidate) { return candidate == buffer; });
+ wl_buffer_destroy(buffer);
+}
+
bool WaylandPageSurface::holds_backing_store(u64 page_id, i32 bitmap_id) const
{
for (auto const& buffer : m_buffers) {
diff --git a/UI/Qt/WaylandPageSurface.h b/UI/Qt/WaylandPageSurface.h
index a2bae65f56c..aa1f428f5d4 100644
--- a/UI/Qt/WaylandPageSurface.h
+++ b/UI/Qt/WaylandPageSurface.h
@@ -22,6 +22,7 @@
#include <sys/types.h>
+class QImage;
class QWindow;
namespace QNativeInterface::Private {
@@ -32,6 +33,7 @@ struct QWaylandWindow;
struct wl_buffer;
struct wl_compositor;
+struct wl_shm;
struct wl_subcompositor;
struct wl_subsurface;
struct wl_surface;
@@ -93,6 +95,11 @@ public:
// Whether the compositor may still read the buffer that shows this backing store.
bool holds_backing_store(u64 page_id, i32 bitmap_id) const;
+ // An image over the page, such as the address of a hovered link, at a position in the logical coordinates of the
+ // window's surface. It changes on screen together with the parent's next commit. False if the window has no surface.
+ bool show_overlay(QWindow& parent, QImage const&, QPoint position);
+ void hide_overlay();
+
struct HeldBackingStore {
u64 page_id;
i32 bitmap_id;
@@ -111,6 +118,7 @@ private:
wl_subcompositor* subcompositor { nullptr };
wp_viewporter* viewporter { nullptr };
zwp_linux_dmabuf_v1* dmabuf { nullptr };
+ wl_shm* shm { nullptr };
};
enum class BufferState {
@@ -151,6 +159,7 @@ private:
static void buffer_created(void*, zwp_linux_buffer_params_v1*, wl_buffer*);
static void buffer_failed(void*, zwp_linux_buffer_params_v1*);
static void buffer_released(void*, wl_buffer*);
+ static void overlay_buffer_released(void*, wl_buffer*);
Globals m_globals;
wl_surface* m_surface { nullptr };
@@ -167,6 +176,13 @@ private:
u64 m_commit_count { 0 };
Vector<NonnullOwnPtr<Buffer>> m_buffers;
+
+ wl_surface* m_overlay_surface { nullptr };
+ wp_viewport* m_overlay_viewport { nullptr };
+ wl_subsurface* m_overlay_subsurface { nullptr };
+ bool m_overlay_is_attached { false };
+ // The compositor may read these until it releases them.
+ Vector<wl_buffer*> m_overlay_buffers;
};
}
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index d8083a6d968..aa83d046bd4 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -1939,6 +1939,17 @@ void WebContentView::set_page_dim(qreal)
}
#endif
+#ifndef LADYBIRD_HAS_WAYLAND_GPU_QUERY
+bool WebContentView::show_image_over_page(QImage const&, QPoint)
+{
+ return false;
+}
+
+void WebContentView::hide_image_over_page()
+{
+}
+#endif
+
void WebContentView::set_bookmarks_bar_overlay_height([[maybe_unused]] int height)
{
#ifdef LADYBIRD_QT_USE_VULKAN_WINDOW
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index e2a7aa34a11..8b53b8bcc9f 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -133,6 +133,11 @@ public:
static Vector<u64> presentation_drm_format_modifiers();
#endif
+ // Shows an image over the page, at a position in the view, where the window system can stack it above the page
+ // itself. Returns false where it cannot, and Qt has to show the image some other way.
+ bool show_image_over_page(QImage const&, QPoint position);
+ void hide_image_over_page();
+
#if !defined(LADYBIRD_QT_USE_RHI_WIDGET)
virtual void paintEvent(QPaintEvent*) override;
#endif
@@ -205,6 +210,7 @@ public:
signals:
void ready_to_paint();
void crash_overlay_changed();
+ void image_overlay_unavailable();
void urls_dropped(QList<QUrl> const&);
private:
diff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cpp
index 50d16704c2f..27bc0505da1 100644
--- a/UI/Qt/WebContentViewLinux.cpp
+++ b/UI/Qt/WebContentViewLinux.cpp
@@ -1435,6 +1435,24 @@ Vector<Gfx::SharedImageBuffer const*> WebContentView::backing_stores() const
return backing_stores;
}
+bool WebContentView::show_image_over_page(QImage const& image, QPoint position)
+{
+ auto* parent = window()->windowHandle();
+ if (!m_page_surface || !parent || !m_page_surface->show_overlay(*parent, image, mapTo(window(), position)))
+ return false;
+ // NB: The overlay changes on screen when the browser window commits next, so have Qt repaint a pixel of it.
+ update(0, 0, 1, 1);
+ return true;
+}
+
+void WebContentView::hide_image_over_page()
+{
+ if (!m_page_surface)
+ return;
+ m_page_surface->hide_overlay();
+ update(0, 0, 1, 1);
+}
+
void WebContentView::destroy_page_surface()
{
m_page_surface->retire_buffers([this](WaylandPageSurface::HeldBackingStore const& held) -> Function<void()> {
@@ -1501,6 +1519,7 @@ void WebContentView::fall_back_from_page_surface()
create_vulkan_window();
schedule_vulkan_window_update();
update();
+ emit image_overlay_unavailable();
}
#endif