1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:17:35 +00:00

LibWeb: Add per-Frame EventHandler, handle mouse events recursively

We now handle mouse events by recursing into subframes. This makes
links, tooltips, etc, work inside <iframe> content.
This commit is contained in:
Andreas Kling 2020-06-07 14:40:38 +02:00
parent 896db187e5
commit d1852b4547
7 changed files with 355 additions and 144 deletions

View file

@ -0,0 +1,221 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGUI/Event.h>
#include <LibGUI/Window.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/HTMLAnchorElement.h>
#include <LibWeb/DOM/HTMLIFrameElement.h>
#include <LibWeb/DOM/MouseEvent.h>
#include <LibWeb/Frame/EventHandler.h>
#include <LibWeb/Frame/Frame.h>
#include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/PageView.h>
namespace Web {
static Gfx::Point compute_mouse_event_offset(const Gfx::Point& position, const LayoutNode& layout_node)
{
auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
return {
position.x() - static_cast<int>(top_left_of_layout_node.x()),
position.y() - static_cast<int>(top_left_of_layout_node.y())
};
}
EventHandler::EventHandler(Badge<Frame>, Frame& frame)
: m_frame(frame)
{
}
EventHandler::~EventHandler()
{
}
const LayoutDocument* EventHandler::layout_root() const
{
if (!m_frame.document())
return nullptr;
return static_cast<LayoutDocument*>(m_frame.document()->layout_node());
}
LayoutDocument* EventHandler::layout_root()
{
if (!m_frame.document())
return nullptr;
return const_cast<LayoutDocument*>(m_frame.document()->layout_node());
}
bool EventHandler::handle_mouseup(const Gfx::Point& position, unsigned button, unsigned modifiers)
{
auto* layout_root_ptr = this->layout_root();
if (!layout_root_ptr)
return false;
auto& layout_root = *layout_root_ptr;
bool handled_event = false;
auto result = layout_root.hit_test(position);
if (result.layout_node && result.layout_node->node()) {
RefPtr<Node> node = result.layout_node->node();
if (is<HTMLIFrameElement>(*node)) {
if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
return false;
}
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(MouseEvent::create("mouseup", offset.x(), offset.y()));
handled_event = true;
}
if (button == GUI::MouseButton::Left) {
dump_selection("MouseUp");
m_in_mouse_selection = false;
}
return handled_event;
}
bool EventHandler::handle_mousedown(const Gfx::Point& position, unsigned button, unsigned modifiers)
{
auto* layout_root_ptr = this->layout_root();
if (!layout_root_ptr)
return false;
auto& layout_root = *layout_root_ptr;
auto& document = *m_frame.document();
auto& page_view = *m_frame.page_view();
auto result = layout_root.hit_test(position);
if (!result.layout_node)
return false;
RefPtr<Node> node = result.layout_node->node();
document.set_hovered_node(node);
if (!node)
return false;
if (is<HTMLIFrameElement>(*node)) {
if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
return false;
}
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y()));
if (RefPtr<HTMLAnchorElement> link = node->enclosing_link_element()) {
dbg() << "Web::EventHandler: Clicking on a link to " << link->href();
if (button == GUI::MouseButton::Left) {
auto href = link->href();
if (href.starts_with("javascript:")) {
document.run_javascript(href.substring_view(11, href.length() - 11));
} else {
page_view.notify_link_click({}, m_frame, link->href(), link->target(), modifiers);
}
} else if (button == GUI::MouseButton::Right) {
page_view.notify_link_context_menu_request({}, m_frame, 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);
}
} else {
if (button == GUI::MouseButton::Left) {
layout_root.selection().set({ result.layout_node, result.index_in_node }, {});
dump_selection("MouseDown");
m_in_mouse_selection = true;
}
}
return true;
}
bool EventHandler::handle_mousemove(const Gfx::Point& position, unsigned buttons, unsigned modifiers)
{
auto* layout_root_ptr = this->layout_root();
if (!layout_root_ptr)
return false;
auto& layout_root = *layout_root_ptr;
auto& document = *m_frame.document();
auto& page_view = *m_frame.page_view();
bool hovered_node_changed = false;
bool is_hovering_link = false;
bool was_hovering_link = document.hovered_node() && document.hovered_node()->is_link();
auto result = layout_root.hit_test(position);
const HTMLAnchorElement* hovered_link_element = nullptr;
if (result.layout_node) {
RefPtr<Node> node = result.layout_node->node();
if (node && is<HTMLIFrameElement>(*node)) {
if (auto* subframe = to<HTMLIFrameElement>(*node).hosted_frame())
return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
return false;
}
hovered_node_changed = node != document.hovered_node();
document.set_hovered_node(node);
if (node) {
hovered_link_element = node->enclosing_link_element();
if (hovered_link_element) {
#ifdef HTML_DEBUG
dbg() << "PageView: hovering over a link to " << hovered_link_element->href();
#endif
is_hovering_link = true;
}
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
}
if (m_in_mouse_selection) {
layout_root.selection().set_end({ result.layout_node, result.index_in_node });
dump_selection("MouseMove");
page_view.update();
}
}
if (page_view.window())
page_view.window()->set_override_cursor(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());
} else {
page_view.notify_tooltip_area_leave({}, m_frame);
}
}
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());
}
return true;
}
void EventHandler::dump_selection(const char* event_name) const
{
UNUSED_PARAM(event_name);
#ifdef SELECTION_DEBUG
dbg() << event_name << " selection start: "
<< layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
<< layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
#endif
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibGUI/Forward.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Forward.h>
namespace Web {
class Frame;
class EventHandler {
public:
explicit EventHandler(Badge<Frame>, Frame&);
~EventHandler();
bool handle_mouseup(const Gfx::Point&, unsigned button, unsigned modifiers);
bool handle_mousedown(const Gfx::Point&, unsigned button, unsigned modifiers);
bool handle_mousemove(const Gfx::Point&, unsigned buttons, unsigned modifiers);
private:
LayoutDocument* layout_root();
const LayoutDocument* layout_root() const;
void dump_selection(const char* event_name) const;
Frame& m_frame;
bool m_in_mouse_selection { false };
};
}

View file

@ -35,6 +35,7 @@ namespace Web {
Frame::Frame(Element& host_element, Frame& main_frame)
: m_main_frame(main_frame)
, m_loader(*this)
, m_event_handler({}, *this)
, m_host_element(host_element.make_weak_ptr())
{
}
@ -42,6 +43,7 @@ Frame::Frame(Element& host_element, Frame& main_frame)
Frame::Frame(PageView& page_view)
: m_main_frame(*this)
, m_loader(*this)
, m_event_handler({}, *this)
, m_page_view(page_view.make_weak_ptr())
{
}

View file

@ -33,14 +33,12 @@
#include <LibGfx/Bitmap.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibWeb/Frame/EventHandler.h>
#include <LibWeb/Loader/FrameLoader.h>
#include <LibWeb/TreeNode.h>
namespace Web {
class Document;
class PageView;
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)); }
@ -76,6 +74,9 @@ public:
FrameLoader& loader() { return m_loader; }
const FrameLoader& loader() const { return m_loader; }
EventHandler& event_handler() { return m_event_handler; }
const EventHandler& event_handler() const { return m_event_handler; }
void scroll_to_anchor(const String&);
Frame& main_frame() { return m_main_frame; }
@ -91,6 +92,7 @@ private:
Frame& m_main_frame;
FrameLoader m_loader;
EventHandler m_event_handler;
WeakPtr<Element> m_host_element;
WeakPtr<PageView> m_page_view;