1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:07:45 +00:00

LibWeb: Add Page abstraction between PageView and main Frame

* A PageView is a view onto a Page object.
* A Page always has a main Frame (root of Frame tree.)
* Page has a PageClient. PageView is a PageClient.

The goal here is to allow building another kind of view onto
a Page while keeping the rest of LibWeb intact.
This commit is contained in:
Andreas Kling 2020-06-08 20:31:49 +02:00
parent 5072d4e02d
commit 92392398a2
16 changed files with 351 additions and 179 deletions

View file

@ -105,7 +105,7 @@ bool EventHandler::handle_mousedown(const Gfx::Point& position, unsigned button,
return false;
auto& layout_root = *layout_root_ptr;
auto& document = *m_frame.document();
auto& page_view = *m_frame.page_view();
auto& page_client = m_frame.page().client();
auto result = layout_root.hit_test(position);
if (!result.layout_node)
@ -133,16 +133,16 @@ bool EventHandler::handle_mousedown(const Gfx::Point& position, unsigned button,
document.run_javascript(href.substring_view(11, href.length() - 11));
} else {
if (m_frame.is_main_frame()) {
page_view.notify_link_click({}, m_frame, link->href(), link->target(), modifiers);
page_client.page_did_click_link(link->href(), link->target(), modifiers);
} else {
// FIXME: Handle different targets!
m_frame.loader().load(document.complete_url(link->href()));
}
}
} else if (button == GUI::MouseButton::Right) {
page_view.notify_link_context_menu_request({}, m_frame, position, link->href(), link->target(), modifiers);
page_client.page_did_request_link_context_menu(m_frame.to_main_frame_position(position), link->href(), link->target(), modifiers);
} else if (button == GUI::MouseButton::Middle) {
page_view.notify_link_middle_click({}, m_frame, link->href(), link->target(), modifiers);
page_client.page_did_middle_click_link(link->href(), link->target(), modifiers);
}
} else {
if (button == GUI::MouseButton::Left) {
@ -161,7 +161,7 @@ bool EventHandler::handle_mousemove(const Gfx::Point& position, unsigned buttons
return false;
auto& layout_root = *layout_root_ptr;
auto& document = *m_frame.document();
auto& page_view = *m_frame.page_view();
auto& page_client = m_frame.page().client();
bool hovered_node_changed = false;
bool is_hovering_link = false;
@ -193,22 +193,23 @@ bool EventHandler::handle_mousemove(const Gfx::Point& position, unsigned buttons
if (m_in_mouse_selection) {
layout_root.selection().set_end({ result.layout_node, result.index_in_node });
dump_selection("MouseMove");
page_view.update();
page_client.page_did_change_selection();
}
}
if (page_view.window())
page_view.window()->set_override_cursor(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
page_client.page_did_request_cursor_change(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None);
if (hovered_node_changed) {
page_view.update();
RefPtr<HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element() : nullptr;
if (hovered_html_element && !hovered_html_element->title().is_null()) {
page_view.notify_tooltip_area_enter({}, m_frame, position, hovered_html_element->title());
page_client.page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
} else {
page_view.notify_tooltip_area_leave({}, m_frame);
page_client.page_did_leave_tooltip_area();
}
}
if (is_hovering_link != was_hovering_link) {
page_view.notify_link_hover({}, m_frame, hovered_link_element ? document.complete_url(hovered_link_element->href()).to_string() : String());
if (is_hovering_link)
page_client.page_did_hover_link(document.complete_url(hovered_link_element->href()));
else
page_client.page_did_unhover_link();
}
return true;
}

View file

@ -33,18 +33,19 @@
namespace Web {
Frame::Frame(Element& host_element, Frame& main_frame)
: m_main_frame(main_frame)
: m_page(main_frame.page())
, m_main_frame(main_frame)
, m_loader(*this)
, m_event_handler({}, *this)
, m_host_element(host_element.make_weak_ptr())
{
}
Frame::Frame(PageView& page_view)
: m_main_frame(*this)
Frame::Frame(Page& page)
: m_page(page)
, m_main_frame(*this)
, m_loader(*this)
, m_event_handler({}, *this)
, m_page_view(page_view.make_weak_ptr())
{
}
@ -94,8 +95,7 @@ void Frame::set_needs_display(const Gfx::Rect& rect)
return;
if (is_main_frame()) {
if (page_view())
page_view()->notify_needs_display({}, *this, rect);
page().client().page_did_invalidate(to_main_frame_rect(rect));
return;
}
@ -117,12 +117,31 @@ void Frame::did_scroll(Badge<PageView>)
void Frame::scroll_to_anchor(const String& fragment)
{
// FIXME: We should be able to scroll iframes to an anchor, too!
if (!m_page_view)
return;
// 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.
m_page_view->scroll_to_anchor(fragment);
page().client().page_did_request_scroll_to_anchor(fragment);
}
Gfx::Rect Frame::to_main_frame_rect(const Gfx::Rect& a_rect)
{
auto rect = a_rect;
rect.set_location(to_main_frame_position(a_rect.location()));
return rect;
}
Gfx::Point Frame::to_main_frame_position(const Gfx::Point& a_position)
{
auto position = a_position;
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (ancestor->is_main_frame())
break;
if (!ancestor->host_element())
return {};
if (!ancestor->host_element()->layout_node())
return {};
position.move_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_int_point());
}
return position;
}
}

View file

@ -42,7 +42,7 @@ namespace Web {
class Frame : public TreeNode<Frame> {
public:
static NonnullRefPtr<Frame> create_subframe(Element& host_element, Frame& main_frame) { return adopt(*new Frame(host_element, main_frame)); }
static NonnullRefPtr<Frame> create(PageView& page_view) { return adopt(*new Frame(page_view)); }
static NonnullRefPtr<Frame> create(Page& page) { return adopt(*new Frame(page)); }
~Frame();
bool is_main_frame() const { return this == &m_main_frame; }
@ -52,8 +52,8 @@ public:
void set_document(Document*);
PageView* page_view() { return is_main_frame() ? m_page_view : main_frame().m_page_view; }
const PageView* page_view() const { return is_main_frame() ? m_page_view : main_frame().m_page_view; }
Page& page() { return m_page; }
const Page& page() const { return m_page; }
const Gfx::Size& size() const { return m_size; }
void set_size(const Gfx::Size&);
@ -84,17 +84,20 @@ public:
Element* host_element() { return m_host_element; }
const Element* host_element() const { return m_host_element; }
Gfx::Point to_main_frame_position(const Gfx::Point&);
Gfx::Rect to_main_frame_rect(const Gfx::Rect&);
private:
explicit Frame(Element& host_element, Frame& main_frame);
explicit Frame(PageView&);
explicit Frame(Page&);
Page& m_page;
Frame& m_main_frame;
FrameLoader m_loader;
EventHandler m_event_handler;
WeakPtr<Element> m_host_element;
WeakPtr<PageView> m_page_view;
RefPtr<Document> m_document;
Gfx::Size m_size;
Gfx::Rect m_viewport_rect;