From d74802e4e25e6dd1118fab43befffcb661052962 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 9 Sep 2022 14:23:36 +0200 Subject: [PATCH] Ladybird: Show hovered link URLs in a conditional UI label The tooltips for hovered links were super awkward when in a tooltip --- Ladybird/Tab.cpp | 31 ++++++++++++++++++++++++++----- Ladybird/Tab.h | 6 ++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Ladybird/Tab.cpp b/Ladybird/Tab.cpp index d45b2ca8b6..eb6fc56b91 100644 --- a/Ladybird/Tab.cpp +++ b/Ladybird/Tab.cpp @@ -10,8 +10,10 @@ #include "History.h" #include "Settings.h" #include +#include +#include #include -#include +#include extern String s_serenity_resource_root; extern Browser::Settings* s_settings; @@ -26,6 +28,10 @@ Tab::Tab(QMainWindow* window) m_toolbar = new QToolBar; m_location_edit = new QLineEdit; + m_hover_label = new QLabel(this); + m_hover_label->setFrameShape(QFrame::Shape::Box); + m_hover_label->setAutoFillBackground(true); + auto* focus_location_edit_action = new QAction("Edit Location"); focus_location_edit_action->setShortcut(QKeySequence("Ctrl+L")); addAction(focus_location_edit_action); @@ -52,11 +58,12 @@ Tab::Tab(QMainWindow* window) m_toolbar->addWidget(m_location_edit); QObject::connect(m_view, &WebView::linkHovered, [this](QString const& title) { - const QPoint* pos = new QPoint(0, size().height() - 15); - QToolTip::showText(*pos, title, this); + m_hover_label->setText(title); + update_hover_label(); + m_hover_label->show(); }); - QObject::connect(m_view, &WebView::linkUnhovered, [] { - QToolTip::hideText(); + QObject::connect(m_view, &WebView::linkUnhovered, [this] { + m_hover_label->hide(); }); QObject::connect(m_view, &WebView::loadStarted, [this](const URL& url) { @@ -138,3 +145,17 @@ void Tab::debug_request(String const& request, String const& argument) { m_view->debug_request(request, argument); } + +void Tab::resizeEvent(QResizeEvent* event) +{ + QWidget::resizeEvent(event); + if (m_hover_label->isVisible()) + update_hover_label(); +} + +void Tab::update_hover_label() +{ + m_hover_label->resize(QFontMetrics(m_hover_label->font()).boundingRect(m_hover_label->text()).adjusted(-4, -2, 4, 2).size()); + m_hover_label->move(6, height() - m_hover_label->height() - 8); + m_hover_label->raise(); +} diff --git a/Ladybird/Tab.h b/Ladybird/Tab.h index 2ff3237497..12ed11c3a1 100644 --- a/Ladybird/Tab.h +++ b/Ladybird/Tab.h @@ -12,6 +12,7 @@ #include "History.h" #include "WebView.h" #include +#include #include #include #include @@ -41,6 +42,10 @@ signals: void favicon_changed(int id, QIcon); private: + virtual void resizeEvent(QResizeEvent*) override; + + void update_hover_label(); + QBoxLayout* m_layout; QToolBar* m_toolbar { nullptr }; QLineEdit* m_location_edit { nullptr }; @@ -48,6 +53,7 @@ private: QMainWindow* m_window { nullptr }; Browser::History m_history; QString m_title; + QLabel* m_hover_label { nullptr }; OwnPtr m_back_action; OwnPtr m_forward_action;