mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibWeb: Move fragment link handling to Frame
Activating a "#foo" fragment link will now be handled internally by the Frame instead of involving the widget layer. If the viewport needs to be scrolled as a result, we will simply ask the PageClient to scroll a new rect into view.
This commit is contained in:
parent
2b80a45f82
commit
56d14d5701
Notes:
sideshowbarker
2024-07-19 05:08:25 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/56d14d57013
7 changed files with 39 additions and 49 deletions
|
@ -159,13 +159,8 @@ Tab::Tab()
|
|||
auto url = m_page_view->document()->complete_url(href);
|
||||
on_tab_open_request(url);
|
||||
} else {
|
||||
if (href.starts_with("#")) {
|
||||
auto anchor = href.substring_view(1, href.length() - 1);
|
||||
m_page_view->scroll_to_anchor(anchor);
|
||||
} else {
|
||||
auto url = m_page_view->document()->complete_url(href);
|
||||
m_page_view->load(url);
|
||||
}
|
||||
auto url = m_page_view->document()->complete_url(href);
|
||||
m_page_view->load(url);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -131,6 +131,9 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
|
|||
auto href = link->href();
|
||||
if (href.starts_with("javascript:")) {
|
||||
document.run_javascript(href.substring_view(11, href.length() - 11));
|
||||
} else if (href.starts_with('#')) {
|
||||
auto anchor = href.substring_view(1, href.length() - 1);
|
||||
m_frame.scroll_to_anchor(anchor);
|
||||
} else {
|
||||
if (m_frame.is_main_frame()) {
|
||||
page_client.page_did_click_link(link->href(), link->target(), modifiers);
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/HTMLAnchorElement.h>
|
||||
#include <LibWeb/Frame/Frame.h>
|
||||
#include <LibWeb/Layout/LayoutDocument.h>
|
||||
#include <LibWeb/Layout/LayoutWidget.h>
|
||||
|
@ -116,9 +117,33 @@ void Frame::did_scroll(Badge<PageView>)
|
|||
|
||||
void Frame::scroll_to_anchor(const String& fragment)
|
||||
{
|
||||
// FIXME: This logic is backwards, the work should be done in here,
|
||||
// and then we just request that the "view" scrolls to a certain content offset.
|
||||
page().client().page_did_request_scroll_to_anchor(fragment);
|
||||
if (!document())
|
||||
return;
|
||||
|
||||
const auto* element = document()->get_element_by_id(fragment);
|
||||
if (!element) {
|
||||
auto candidates = document()->get_elements_by_name(fragment);
|
||||
for (auto* candidate : candidates) {
|
||||
if (is<HTMLAnchorElement>(*candidate)) {
|
||||
element = to<HTMLAnchorElement>(candidate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!element || !element->layout_node())
|
||||
return;
|
||||
|
||||
auto& layout_node = *element->layout_node();
|
||||
|
||||
Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
|
||||
if (is<LayoutBox>(layout_node)) {
|
||||
auto& layout_box = to<LayoutBox>(layout_node);
|
||||
auto padding_box = layout_box.box_model().padding_box(layout_box);
|
||||
float_rect.move_by(-padding_box.left, -padding_box.top);
|
||||
}
|
||||
|
||||
page().client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
|
||||
}
|
||||
|
||||
Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
|
||||
|
|
|
@ -71,6 +71,7 @@ public:
|
|||
EventHandler& event_handler() { return m_event_handler; }
|
||||
const EventHandler& event_handler() const { return m_event_handler; }
|
||||
|
||||
void scroll_to(const Gfx::IntPoint&);
|
||||
void scroll_to_anchor(const String&);
|
||||
|
||||
Frame& main_frame() { return m_main_frame; }
|
||||
|
@ -82,6 +83,7 @@ public:
|
|||
Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&);
|
||||
Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&);
|
||||
|
||||
|
||||
private:
|
||||
explicit Frame(Element& host_element, Frame& main_frame);
|
||||
explicit Frame(Page&);
|
||||
|
|
|
@ -82,10 +82,10 @@ public:
|
|||
virtual void page_did_leave_tooltip_area() { }
|
||||
virtual void page_did_hover_link(const URL&) { }
|
||||
virtual void page_did_unhover_link() { }
|
||||
virtual void page_did_request_scroll_to_anchor([[maybe_unused]] const String& fragment) { }
|
||||
virtual void page_did_invalidate(const Gfx::IntRect&) { }
|
||||
virtual void page_did_change_favicon(const Gfx::Bitmap&) { }
|
||||
virtual void page_did_layout() { }
|
||||
virtual void page_did_request_scroll_into_view(const Gfx::IntRect&) { }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -239,11 +239,6 @@ void PageView::page_did_unhover_link()
|
|||
{
|
||||
}
|
||||
|
||||
void PageView::page_did_request_scroll_to_anchor(const String& fragment)
|
||||
{
|
||||
scroll_to_anchor(fragment);
|
||||
}
|
||||
|
||||
void PageView::page_did_invalidate(const Gfx::IntRect&)
|
||||
{
|
||||
update();
|
||||
|
@ -403,38 +398,9 @@ LayoutDocument* PageView::layout_root()
|
|||
return const_cast<LayoutDocument*>(document()->layout_node());
|
||||
}
|
||||
|
||||
void PageView::scroll_to_anchor(const StringView& name)
|
||||
void PageView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
|
||||
{
|
||||
if (!document())
|
||||
return;
|
||||
|
||||
const auto* element = document()->get_element_by_id(name);
|
||||
if (!element) {
|
||||
auto candidates = document()->get_elements_by_name(name);
|
||||
for (auto* candidate : candidates) {
|
||||
if (is<HTMLAnchorElement>(*candidate)) {
|
||||
element = to<HTMLAnchorElement>(candidate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!element) {
|
||||
dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'";
|
||||
return;
|
||||
}
|
||||
if (!element->layout_node()) {
|
||||
dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
|
||||
return;
|
||||
}
|
||||
auto& layout_node = *element->layout_node();
|
||||
Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } };
|
||||
if (is<LayoutBox>(layout_node)) {
|
||||
auto& layout_box = to<LayoutBox>(layout_node);
|
||||
auto padding_box = layout_box.box_model().padding_box(layout_box);
|
||||
float_rect.move_by(-padding_box.left, -padding_box.top);
|
||||
}
|
||||
scroll_into_view(enclosing_int_rect(float_rect), true, true);
|
||||
scroll_into_view(rect, true, true);
|
||||
window()->set_override_cursor(GUI::StandardCursor::None);
|
||||
}
|
||||
|
||||
|
|
|
@ -54,7 +54,6 @@ public:
|
|||
|
||||
void reload();
|
||||
bool load(const URL&);
|
||||
void scroll_to_anchor(const StringView&);
|
||||
|
||||
URL url() const;
|
||||
|
||||
|
@ -110,10 +109,10 @@ private:
|
|||
virtual void page_did_leave_tooltip_area() override;
|
||||
virtual void page_did_hover_link(const URL&) override;
|
||||
virtual void page_did_unhover_link() override;
|
||||
virtual void page_did_request_scroll_to_anchor(const String& fragment) override;
|
||||
virtual void page_did_invalidate(const Gfx::IntRect&) override;
|
||||
virtual void page_did_change_favicon(const Gfx::Bitmap&) override;
|
||||
virtual void page_did_layout() override;
|
||||
virtual void page_did_request_scroll_into_view(const Gfx::IntRect&) override;
|
||||
|
||||
void layout_and_sync_size();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue