Ladybird+LibWebView: Migrate scrolling changes to LibWebView callbacks

This commit is contained in:
Timothy Flynn 2023-08-23 10:57:09 -04:00 committed by Tim Flynn
parent 00fe122b0a
commit 78d9339aa9
10 changed files with 73 additions and 98 deletions

View file

@ -224,7 +224,7 @@ struct HideCursor {
[[self tab] onFaviconChange:favicon];
};
m_web_view_bridge->on_scroll = [self](auto position) {
m_web_view_bridge->on_scroll_to_point = [self](auto position) {
[self scrollToPoint:Ladybird::gfx_point_to_ns_point(position)];
[[self scrollView] reflectScrolledClipView:self];
[self updateViewportRect:Ladybird::WebViewBridge::ForResize::No];

View file

@ -34,6 +34,35 @@ WebViewBridge::WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pix
m_inverse_device_pixel_ratio = 1.0 / device_pixel_ratio;
create_client(WebView::EnableCallgrindProfiling::No);
on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
// FIXME: This currently isn't reached because we do not yet propagate mouse wheel events to WebContent.
// When that is implemented, make sure our mutations to the viewport position here are correct.
auto position = m_viewport_rect.location();
position.set_x(position.x() + x_delta);
position.set_y(position.y() + y_delta);
if (on_scroll_to_point)
on_scroll_to_point(position);
};
on_scroll_into_view = [this](auto rect) {
if (m_viewport_rect.contains(rect))
return;
auto position = m_viewport_rect.location();
if (rect.top() < m_viewport_rect.top()) {
position.set_y(rect.top());
} else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom()) {
position.set_y(rect.bottom() - m_viewport_rect.height());
} else {
return;
}
if (on_scroll_to_point)
on_scroll_to_point(position);
};
}
WebViewBridge::~WebViewBridge() = default;
@ -104,43 +133,6 @@ Optional<WebViewBridge::Paintable> WebViewBridge::paintable()
return Paintable { *bitmap, bitmap_size };
}
void WebViewBridge::notify_server_did_request_scroll(Badge<WebView::WebContentClient>, i32 x_delta, i32 y_delta)
{
// FIXME: This currently isn't reached because we do not yet propagate mouse wheel events to WebContent.
// When that is implemented, make sure our mutations to the viewport position here are correct.
auto position = m_viewport_rect.location();
position.set_x(position.x() + x_delta);
position.set_y(position.y() + y_delta);
if (on_scroll)
on_scroll(position);
}
void WebViewBridge::notify_server_did_request_scroll_to(Badge<WebView::WebContentClient>, Gfx::IntPoint position)
{
if (on_scroll)
on_scroll(position);
}
void WebViewBridge::notify_server_did_request_scroll_into_view(Badge<WebView::WebContentClient>, Gfx::IntRect const& rect)
{
if (m_viewport_rect.contains(rect))
return;
auto position = m_viewport_rect.location();
if (rect.top() < m_viewport_rect.top()) {
position.set_y(rect.top());
} else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom()) {
position.set_y(rect.bottom() - m_viewport_rect.height());
} else {
return;
}
if (on_scroll)
on_scroll(position);
}
void WebViewBridge::notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const& tooltip)
{
if (on_tooltip_entered)

View file

@ -49,17 +49,12 @@ public:
};
Optional<Paintable> paintable();
Function<void(Gfx::IntPoint)> on_scroll;
Function<void(DeprecatedString const&)> on_tooltip_entered;
Function<void()> on_tooltip_left;
private:
WebViewBridge(Vector<Gfx::IntRect> screen_rects, float device_pixel_ratio, Optional<StringView> webdriver_content_ipc_path);
virtual void notify_server_did_request_scroll(Badge<WebView::WebContentClient>, i32, i32) override;
virtual void notify_server_did_request_scroll_to(Badge<WebView::WebContentClient>, Gfx::IntPoint) override;
virtual void notify_server_did_request_scroll_into_view(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override;
virtual void notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override;
virtual void notify_server_did_leave_tooltip_area(Badge<WebView::WebContentClient>) override;
virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override;

View file

@ -89,6 +89,26 @@ WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::E
viewport()->update();
};
on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
horizontalScrollBar()->setValue(max(0, horizontalScrollBar()->value() + x_delta));
verticalScrollBar()->setValue(max(0, verticalScrollBar()->value() + y_delta));
};
on_scroll_to_point = [this](auto position) {
horizontalScrollBar()->setValue(position.x());
verticalScrollBar()->setValue(position.y());
};
on_scroll_into_view = [this](auto rect) {
if (m_viewport_rect.contains(rect))
return;
if (rect.top() < m_viewport_rect.top())
verticalScrollBar()->setValue(rect.top());
else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom())
verticalScrollBar()->setValue(rect.bottom() - m_viewport_rect.height());
};
on_cursor_change = [this](auto cursor) {
update_cursor(cursor);
};
@ -667,29 +687,6 @@ void WebContentView::update_cursor(Gfx::StandardCursor cursor)
}
}
void WebContentView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
{
horizontalScrollBar()->setValue(max(0, horizontalScrollBar()->value() + x_delta));
verticalScrollBar()->setValue(max(0, verticalScrollBar()->value() + y_delta));
}
void WebContentView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint scroll_position)
{
horizontalScrollBar()->setValue(scroll_position.x());
verticalScrollBar()->setValue(scroll_position.y());
}
void WebContentView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
{
if (m_viewport_rect.contains(rect))
return;
if (rect.top() < m_viewport_rect.top())
verticalScrollBar()->setValue(rect.top());
else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom())
verticalScrollBar()->setValue(rect.bottom() - m_viewport_rect.height());
}
void WebContentView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint content_position, DeprecatedString const& tooltip)
{
auto widget_position = to_widget_position(content_position);

View file

@ -76,9 +76,6 @@ public:
};
void update_palette(PaletteMode = PaletteMode::Default);
virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) override;
virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) override;
virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) override;
virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override;
virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) override;
virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override;

View file

@ -46,6 +46,20 @@ OutOfProcessWebView::OutOfProcessWebView()
client().async_handle_file_return(0, IPC::File(file.value().stream()), request_id);
};
on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
horizontal_scrollbar().increase_slider_by(x_delta);
vertical_scrollbar().increase_slider_by(y_delta);
};
on_scroll_to_point = [this](auto position) {
horizontal_scrollbar().set_value(position.x());
vertical_scrollbar().set_value(position.y());
};
on_scroll_into_view = [this](auto rect) {
scroll_into_view(rect, true, true);
};
on_cursor_change = [this](auto cursor) {
set_override_cursor(cursor);
};
@ -187,23 +201,6 @@ void OutOfProcessWebView::screen_rects_change_event(GUI::ScreenRectsChangeEvent&
client().async_update_screen_rects(event.rects(), event.main_screen_index());
}
void OutOfProcessWebView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
{
horizontal_scrollbar().increase_slider_by(x_delta);
vertical_scrollbar().increase_slider_by(y_delta);
}
void OutOfProcessWebView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint scroll_position)
{
horizontal_scrollbar().set_value(scroll_position.x());
vertical_scrollbar().set_value(scroll_position.y());
}
void OutOfProcessWebView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
{
scroll_into_view(rect, true, true);
}
void OutOfProcessWebView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const& title)
{
GUI::Application::the()->show_tooltip(title, nullptr);

View file

@ -82,9 +82,6 @@ private:
// ^WebView::ViewImplementation
virtual void create_client(EnableCallgrindProfiling = EnableCallgrindProfiling::No) override;
virtual void update_zoom() override;
virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) override;
virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) override;
virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) override;
virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override;
virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) override;
virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override;

View file

@ -115,6 +115,9 @@ public:
Function<void()> on_navigate_forward;
Function<void()> on_refresh;
Function<void(Gfx::Bitmap const&)> on_favicon_change;
Function<void(i32, i32)> on_scroll_by_delta;
Function<void(Gfx::IntPoint)> on_scroll_to_point;
Function<void(Gfx::IntRect)> on_scroll_into_view;
Function<void(Gfx::StandardCursor)> on_cursor_change;
Function<void(String const& message)> on_request_alert;
Function<void(String const& message)> on_request_confirm;
@ -141,9 +144,6 @@ public:
Function<Gfx::IntRect()> on_minimize_window;
Function<Gfx::IntRect()> on_fullscreen_window;
virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) = 0;
virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) = 0;
virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) = 0;
virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) = 0;
virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) = 0;

View file

@ -109,18 +109,21 @@ void WebContentClient::did_change_title(DeprecatedString const& title)
void WebContentClient::did_request_scroll(i32 x_delta, i32 y_delta)
{
m_view.notify_server_did_request_scroll({}, x_delta, y_delta);
if (m_view.on_scroll_by_delta)
m_view.on_scroll_by_delta(x_delta, y_delta);
}
void WebContentClient::did_request_scroll_to(Gfx::IntPoint scroll_position)
{
m_view.notify_server_did_request_scroll_to({}, scroll_position);
if (m_view.on_scroll_to_point)
m_view.on_scroll_to_point(scroll_position);
}
void WebContentClient::did_request_scroll_into_view(Gfx::IntRect const& rect)
{
dbgln_if(SPAM_DEBUG, "handle: WebContentClient::DidRequestScrollIntoView! rect={}", rect);
m_view.notify_server_did_request_scroll_into_view({}, rect);
if (m_view.on_scroll_into_view)
m_view.on_scroll_into_view(rect);
}
void WebContentClient::did_enter_tooltip_area(Gfx::IntPoint content_position, DeprecatedString const& title)

View file

@ -102,9 +102,6 @@ public:
private:
HeadlessWebContentView() = default;
void notify_server_did_request_scroll(Badge<WebView::WebContentClient>, i32, i32) override { }
void notify_server_did_request_scroll_to(Badge<WebView::WebContentClient>, Gfx::IntPoint) override { }
void notify_server_did_request_scroll_into_view(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override { }
void notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override { }
void notify_server_did_leave_tooltip_area(Badge<WebView::WebContentClient>) override { }
void notify_server_did_finish_handling_input_event(bool) override { }