WebContent: Plumb link clicks to the WebContentView :^)

You can now react to links being clicked via the on_link_click hook.
This commit is contained in:
Andreas Kling 2020-07-06 20:01:46 +02:00
parent fd65a24834
commit 32243e1df2
Notes: sideshowbarker 2024-07-19 05:06:18 +09:00
8 changed files with 48 additions and 1 deletions

View file

@ -114,3 +114,13 @@ void WebContentClient::handle(const Messages::WebContentClient::DidUnhoverLink&)
#endif
m_view.notify_server_did_unhover_link({});
}
void WebContentClient::handle(const Messages::WebContentClient::DidClickLink& message)
{
m_view.notify_server_did_click_link({}, message.url(), message.target(), message.modifiers());
}
void WebContentClient::handle(const Messages::WebContentClient::DidMiddleClickLink& message)
{
m_view.notify_server_did_middle_click_link({}, message.url(), message.target(), message.modifiers());
}

View file

@ -53,6 +53,8 @@ private:
virtual void handle(const Messages::WebContentClient::DidRequestScrollIntoView&) override;
virtual void handle(const Messages::WebContentClient::DidHoverLink&) override;
virtual void handle(const Messages::WebContentClient::DidUnhoverLink&) override;
virtual void handle(const Messages::WebContentClient::DidClickLink&) override;
virtual void handle(const Messages::WebContentClient::DidMiddleClickLink&) override;
WebContentView& m_view;
};

View file

@ -137,6 +137,18 @@ void WebContentView::notify_server_did_unhover_link(Badge<WebContentClient>)
on_link_hover({});
}
void WebContentView::notify_server_did_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
{
if (on_link_click)
on_link_click(url, target, modifiers);
}
void WebContentView::notify_server_did_middle_click_link(Badge<WebContentClient>, const URL& url, const String& target, unsigned int modifiers)
{
if (on_link_middle_click)
on_link_middle_click(url, target, modifiers);
}
void WebContentView::did_scroll()
{
client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));

View file

@ -41,6 +41,8 @@ public:
Function<void(const String&)> on_title_change;
Function<void(const URL&)> on_link_hover;
Function<void(const URL&, const String& target, unsigned modifiers)> on_link_click;
Function<void(const URL&, const String& target, unsigned modifiers)> on_link_middle_click;
void notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size);
void notify_server_did_paint(Badge<WebContentClient>, i32 shbuf_id);
@ -50,6 +52,8 @@ public:
void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, const Gfx::IntRect&);
void notify_server_did_hover_link(Badge<WebContentClient>, const URL&);
void notify_server_did_unhover_link(Badge<WebContentClient>);
void notify_server_did_click_link(Badge<WebContentClient>, const URL&, const String& target, unsigned modifiers);
void notify_server_did_middle_click_link(Badge<WebContentClient>, const URL&, const String& target, unsigned modifiers);
private:
WebContentView();

View file

@ -56,6 +56,11 @@ int main(int argc, char** argv)
statusbar.set_text("");
};
view.on_link_click = [&](auto& url, auto&, auto) {
if (url.is_valid())
view.load(url);
};
view.load("file:///res/html/misc/welcome.html");
return app->exec();

View file

@ -144,4 +144,14 @@ void PageHost::page_did_unhover_link()
m_client.post_message(Messages::WebContentClient::DidUnhoverLink());
}
void PageHost::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
{
m_client.post_message(Messages::WebContentClient::DidClickLink(url, target, modifiers));
}
void PageHost::page_did_middle_click_link(const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
{
m_client.post_message(Messages::WebContentClient::DidMiddleClickLink(url, target, modifiers));
}
}

View file

@ -49,7 +49,7 @@ public:
void set_viewport_rect(const Gfx::IntRect&);
private:
// ^PageHost
// ^PageClient
virtual Gfx::Palette palette() const override;
virtual void page_did_invalidate(const Gfx::IntRect&) override;
virtual void page_did_change_selection() override;
@ -58,6 +58,8 @@ private:
virtual void page_did_request_scroll_into_view(const Gfx::IntRect&) override;
virtual void page_did_hover_link(const URL&) override;
virtual void page_did_unhover_link() override;
virtual void page_did_click_link(const URL&, const String& target, unsigned modifiers) override;
virtual void page_did_middle_click_link(const URL&, const String& target, unsigned modifiers) override;
explicit PageHost(ClientConnection&);

View file

@ -9,4 +9,6 @@ endpoint WebContentClient = 90
DidRequestScrollIntoView(Gfx::IntRect rect) =|
DidHoverLink(URL url) =|
DidUnhoverLink() =|
DidClickLink(URL url, String target, unsigned modifiers) =|
DidMiddleClickLink(URL url, String target, unsigned modifiers) =|
}