Skip to content

Commit 0f7623d

Browse files
circl-lastnameawesomekling
authored andcommitted
LibWeb+UI/Qt: Display 'title' tooltips only when the mouse stops moving
Now instead of sending the position in which the user entered the tooltip area, send just the text, and let the chrome figure out how to display it. In the case of Qt, wait for 600 milliseconds of no mouse movement, then display it under the mouse cursor.
1 parent 6623177 commit 0f7623d

File tree

12 files changed

+34
-21
lines changed

12 files changed

+34
-21
lines changed

Ladybird/AppKit/UI/LadybirdWebView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ - (void)setWebViewCallbacks
532532
[self reload];
533533
};
534534

535-
m_web_view_bridge->on_enter_tooltip_area = [weak_self](auto, auto const& tooltip) {
535+
m_web_view_bridge->on_enter_tooltip_area = [weak_self](auto const& tooltip) {
536536
LadybirdWebView* self = weak_self;
537537
if (self == nil) {
538538
return;

Ladybird/Qt/WebContentView.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ WebContentView::WebContentView(QWidget* window, WebContentOptions const& web_con
7979
update_screen_rects();
8080
});
8181

82+
m_tooltip_hover_timer.setSingleShot(true);
83+
84+
QObject::connect(&m_tooltip_hover_timer, &QTimer::timeout, [this] {
85+
if (m_tooltip_text.has_value())
86+
QToolTip::showText(
87+
QCursor::pos(),
88+
qstring_from_ak_string(m_tooltip_text.value()),
89+
this);
90+
});
91+
8292
initialize_client((parent_client == nullptr) ? CreateNewClient::Yes : CreateNewClient::No);
8393

8494
on_ready_to_paint = [this]() {
@@ -89,18 +99,14 @@ WebContentView::WebContentView(QWidget* window, WebContentOptions const& web_con
8999
update_cursor(cursor);
90100
};
91101

92-
on_enter_tooltip_area = [this](auto position, auto const& tooltip) {
93-
auto tooltip_without_carriage_return = tooltip.contains("\r"sv)
102+
on_enter_tooltip_area = [this](auto const& tooltip) {
103+
m_tooltip_text = tooltip.contains("\r"sv)
94104
? tooltip.replace("\r\n"sv, "\n"sv, ReplaceMode::All).replace("\r"sv, "\n"sv, ReplaceMode::All)
95105
: tooltip;
96-
QToolTip::showText(
97-
mapToGlobal(QPoint(position.x(), position.y())),
98-
qstring_from_ak_string(tooltip_without_carriage_return),
99-
this);
100106
};
101107

102-
on_leave_tooltip_area = []() {
103-
QToolTip::hideText();
108+
on_leave_tooltip_area = [this]() {
109+
m_tooltip_text.clear();
104110
};
105111

106112
on_finish_handling_key_event = [this](auto const& event) {
@@ -326,6 +332,10 @@ void WebContentView::keyReleaseEvent(QKeyEvent* event)
326332

327333
void WebContentView::mouseMoveEvent(QMouseEvent* event)
328334
{
335+
if (QToolTip::isVisible())
336+
QToolTip::hideText();
337+
m_tooltip_hover_timer.start(600);
338+
329339
enqueue_native_event(Web::MouseEvent::Type::MouseMove, *event);
330340
}
331341

Ladybird/Qt/WebContentView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <LibWeb/HTML/ActivateTab.h>
2424
#include <LibWebView/ViewImplementation.h>
2525
#include <QAbstractScrollArea>
26+
#include <QTimer>
2627
#include <QUrl>
2728

2829
class QKeyEvent;
@@ -103,6 +104,9 @@ class WebContentView final
103104
void finish_handling_key_event(Web::KeyEvent const&);
104105
void update_screen_rects();
105106

107+
Optional<ByteString> m_tooltip_text;
108+
QTimer m_tooltip_hover_timer;
109+
106110
bool m_should_show_line_box_borders { false };
107111

108112
Gfx::IntSize m_viewport_size;

Userland/Libraries/LibWeb/Page/EventHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ bool EventHandler::handle_mousemove(CSSPixelPoint viewport_position, CSSPixelPoi
567567
if (hovered_node_changed) {
568568
JS::GCPtr<HTML::HTMLElement const> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
569569
if (hovered_html_element && hovered_html_element->title().has_value()) {
570-
page.client().page_did_enter_tooltip_area(viewport_position, hovered_html_element->title()->to_byte_string());
570+
page.client().page_did_enter_tooltip_area(hovered_html_element->title()->to_byte_string());
571571
} else {
572572
page.client().page_did_leave_tooltip_area();
573573
}

Userland/Libraries/LibWeb/Page/Page.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class PageClient : public JS::Cell {
328328
virtual void page_did_request_image_context_menu(CSSPixelPoint, URL::URL const&, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Gfx::Bitmap const*) { }
329329
virtual void page_did_request_media_context_menu(CSSPixelPoint, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers, Page::MediaContextMenu) { }
330330
virtual void page_did_middle_click_link(URL::URL const&, [[maybe_unused]] ByteString const& target, [[maybe_unused]] unsigned modifiers) { }
331-
virtual void page_did_enter_tooltip_area(CSSPixelPoint, ByteString const&) { }
331+
virtual void page_did_enter_tooltip_area(ByteString const&) { }
332332
virtual void page_did_leave_tooltip_area() { }
333333
virtual void page_did_hover_link(URL::URL const&) { }
334334
virtual void page_did_unhover_link() { }

Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mousemove(Badge<E
351351
set_volume(media_element, *cached_layout_boxes.volume_scrub_rect, position);
352352

353353
auto volume = static_cast<u8>(media_element.volume() * 100.0);
354-
browsing_context().page().client().page_did_enter_tooltip_area(position, ByteString::formatted("{}%", volume));
354+
browsing_context().page().client().page_did_enter_tooltip_area(ByteString::formatted("{}%", volume));
355355
}
356356

357357
break;

Userland/Libraries/LibWebView/ViewImplementation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ViewImplementation {
163163
Function<void()> on_refresh;
164164
Function<void(Gfx::Bitmap const&)> on_favicon_change;
165165
Function<void(Gfx::StandardCursor)> on_cursor_change;
166-
Function<void(Gfx::IntPoint, ByteString const&)> on_enter_tooltip_area;
166+
Function<void(ByteString const&)> on_enter_tooltip_area;
167167
Function<void()> on_leave_tooltip_area;
168168
Function<void(String const& message)> on_request_alert;
169169
Function<void(String const& message)> on_request_confirm;

Userland/Libraries/LibWebView/WebContentClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ void WebContentClient::did_change_url(u64 page_id, URL::URL const& url)
170170
}
171171
}
172172

173-
void WebContentClient::did_enter_tooltip_area(u64 page_id, Gfx::IntPoint content_position, ByteString const& title)
173+
void WebContentClient::did_enter_tooltip_area(u64 page_id, ByteString const& title)
174174
{
175175
if (auto view = view_for_page_id(page_id); view.has_value()) {
176176
if (view->on_enter_tooltip_area)
177-
view->on_enter_tooltip_area(view->to_widget_position(content_position), title);
177+
view->on_enter_tooltip_area(title);
178178
}
179179
}
180180

Userland/Libraries/LibWebView/WebContentClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class WebContentClient final
5555
virtual void did_layout(u64 page_id, Gfx::IntSize) override;
5656
virtual void did_change_title(u64 page_id, ByteString const&) override;
5757
virtual void did_change_url(u64 page_id, URL::URL const&) override;
58-
virtual void did_enter_tooltip_area(u64 page_id, Gfx::IntPoint, ByteString const&) override;
58+
virtual void did_enter_tooltip_area(u64 page_id, ByteString const&) override;
5959
virtual void did_leave_tooltip_area(u64 page_id) override;
6060
virtual void did_hover_link(u64 page_id, URL::URL const&) override;
6161
virtual void did_unhover_link(u64 page_id) override;

Userland/Services/WebContent/PageClient.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,9 @@ Gfx::IntRect PageClient::page_did_request_fullscreen_window()
318318
return client().did_request_fullscreen_window(m_id);
319319
}
320320

321-
void PageClient::page_did_enter_tooltip_area(Web::CSSPixelPoint content_position, ByteString const& title)
321+
void PageClient::page_did_enter_tooltip_area(ByteString const& title)
322322
{
323-
auto device_position = page().css_to_device_point(content_position);
324-
client().async_did_enter_tooltip_area(m_id, { device_position.x(), device_position.y() }, title);
323+
client().async_did_enter_tooltip_area(m_id, title);
325324
}
326325

327326
void PageClient::page_did_leave_tooltip_area()

0 commit comments

Comments
 (0)