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

Libraries: Move to Userland/Libraries/

This commit is contained in:
Andreas Kling 2021-01-12 12:17:30 +01:00
parent dc28c07fa5
commit 13d7c09125
1857 changed files with 266 additions and 274 deletions

View file

@ -0,0 +1,124 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* 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 <AK/StringBuilder.h>
#include <LibWeb/DOM/Position.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Layout/LayoutPosition.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include "EditEventHandler.h"
namespace Web {
// This method is quite convoluted but this is necessary to make editing feel intuitive.
void EditEventHandler::handle_delete(DOM::Range& range)
{
auto* start = downcast<DOM::Text>(range.start_container());
auto* end = downcast<DOM::Text>(range.end_container());
if (start == end) {
StringBuilder builder;
builder.append(start->data().substring_view(0, range.start_offset()));
builder.append(end->data().substring_view(range.end_offset()));
start->set_data(builder.to_string());
} else {
// Remove all the nodes that are fully enclosed in the range.
HashTable<DOM::Node*> queued_for_deletion;
for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
if (node == end)
break;
queued_for_deletion.set(node);
}
for (auto* parent = start->parent(); parent; parent = parent->parent())
queued_for_deletion.remove(parent);
for (auto* parent = end->parent(); parent; parent = parent->parent())
queued_for_deletion.remove(parent);
for (auto* node : queued_for_deletion)
node->parent()->remove_child(*node);
// Join the parent nodes of start and end.
DOM::Node *insert_after = start, *remove_from = end, *parent_of_end = end->parent();
while (remove_from) {
auto* next_sibling = remove_from->next_sibling();
remove_from->parent()->remove_child(*remove_from);
insert_after->parent()->insert_before(*remove_from, *insert_after);
insert_after = remove_from;
remove_from = next_sibling;
}
if (!parent_of_end->has_children()) {
if (parent_of_end->parent())
parent_of_end->parent()->remove_child(*parent_of_end);
}
// Join the start and end nodes.
StringBuilder builder;
builder.append(start->data().substring_view(0, range.start_offset()));
builder.append(end->data().substring_view(range.end_offset()));
start->set_data(builder.to_string());
start->parent()->remove_child(*end);
}
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
// remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
// which really hurts performance.
m_frame.document()->force_layout();
m_frame.did_edit({});
}
void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
{
if (is<DOM::Text>(*position.node())) {
auto& node = downcast<DOM::Text>(*position.node());
StringBuilder builder;
builder.append(node.data().substring_view(0, position.offset()));
builder.append_code_point(code_point);
builder.append(node.data().substring_view(position.offset()));
node.set_data(builder.to_string());
node.invalidate_style();
}
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
// remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
// which really hurts performance.
m_frame.document()->force_layout();
m_frame.did_edit({});
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* 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 <LibWeb/Forward.h>
namespace Web {
class EditEventHandler {
public:
explicit EditEventHandler(Frame& frame)
: m_frame(frame)
{
}
virtual ~EditEventHandler() = default;
virtual void handle_delete(DOM::Range&);
virtual void handle_insert(DOM::Position, u32 code_point);
private:
Frame& m_frame;
};
}

View file

@ -0,0 +1,414 @@
/*
* 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/Range.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/MouseEvent.h>
namespace Web {
static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, const Layout::Node& 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)
, m_edit_event_handler(make<EditEventHandler>(frame))
{
}
EventHandler::~EventHandler()
{
}
const Layout::InitialContainingBlockBox* EventHandler::layout_root() const
{
if (!m_frame.document())
return nullptr;
return m_frame.document()->layout_node();
}
Layout::InitialContainingBlockBox* EventHandler::layout_root()
{
if (!m_frame.document())
return nullptr;
return m_frame.document()->layout_node();
}
bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
if (!layout_root())
return false;
if (m_mouse_event_tracking_layout_node) {
m_mouse_event_tracking_layout_node->handle_mouseup({}, position, button, modifiers);
return true;
}
bool handled_event = false;
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (result.layout_node && result.layout_node->wants_mouse_events()) {
result.layout_node->handle_mouseup({}, position, button, modifiers);
// Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
if (!layout_root())
return true;
result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
}
if (result.layout_node && result.layout_node->dom_node()) {
RefPtr<DOM::Node> node = result.layout_node->dom_node();
if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_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(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y()));
handled_event = true;
}
if (button == GUI::MouseButton::Left)
m_in_mouse_selection = false;
return handled_event;
}
bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
if (!layout_root())
return false;
if (m_mouse_event_tracking_layout_node) {
m_mouse_event_tracking_layout_node->handle_mousedown({}, position, button, modifiers);
return true;
}
NonnullRefPtr document = *m_frame.document();
RefPtr<DOM::Node> node;
{
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (!result.layout_node)
return false;
node = result.layout_node->dom_node();
document->set_hovered_node(node);
if (result.layout_node->wants_mouse_events()) {
result.layout_node->handle_mousedown({}, position, button, modifiers);
return true;
}
if (!node)
return false;
if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
return false;
}
if (auto* page = m_frame.page())
page->set_focused_frame({}, m_frame);
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y()));
}
// NOTE: Dispatching an event may have disturbed the world.
if (!layout_root() || layout_root() != node->document().layout_node())
return true;
if (button == GUI::MouseButton::Right && is<HTML::HTMLImageElement>(*node)) {
auto& image_element = downcast<HTML::HTMLImageElement>(*node);
auto image_url = image_element.document().complete_url(image_element.src());
if (auto* page = m_frame.page())
page->client().page_did_request_image_context_menu(m_frame.to_main_frame_position(position), image_url, "", modifiers, image_element.bitmap());
return true;
}
if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
auto href = link->href();
auto url = document->complete_url(href);
dbgln("Web::EventHandler: Clicking on a link to {}", url);
if (button == GUI::MouseButton::Left) {
auto href = link->href();
auto url = document->complete_url(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()) {
if (auto* page = m_frame.page())
page->client().page_did_click_link(url, link->target(), modifiers);
} else {
// FIXME: Handle different targets!
m_frame.loader().load(url, FrameLoader::Type::Navigation);
}
}
} else if (button == GUI::MouseButton::Right) {
if (auto* page = m_frame.page())
page->client().page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
} else if (button == GUI::MouseButton::Middle) {
if (auto* page = m_frame.page())
page->client().page_did_middle_click_link(url, link->target(), modifiers);
}
} else {
if (button == GUI::MouseButton::Left) {
auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
if (result.layout_node && result.layout_node->dom_node()) {
m_frame.set_cursor_position(DOM::Position(*node, result.index_in_node));
layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
m_in_mouse_selection = true;
}
} else if (button == GUI::MouseButton::Right) {
if (auto* page = m_frame.page())
page->client().page_did_request_context_menu(m_frame.to_main_frame_position(position));
}
}
return true;
}
bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
{
if (!layout_root())
return false;
if (m_mouse_event_tracking_layout_node) {
m_mouse_event_tracking_layout_node->handle_mousemove({}, position, buttons, modifiers);
return true;
}
auto& document = *m_frame.document();
bool hovered_node_changed = false;
bool is_hovering_link = false;
bool is_hovering_text = false;
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
if (result.layout_node) {
if (result.layout_node->wants_mouse_events()) {
document.set_hovered_node(result.layout_node->dom_node());
result.layout_node->handle_mousemove({}, position, buttons, modifiers);
// FIXME: It feels a bit aggressive to always update the cursor like this.
if (auto* page = m_frame.page())
page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
return true;
}
RefPtr<DOM::Node> node = result.layout_node->dom_node();
if (node && is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_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) {
if (node->is_text())
is_hovering_text = true;
hovered_link_element = node->enclosing_link_element();
if (hovered_link_element)
is_hovering_link = true;
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y()));
// NOTE: Dispatching an event may have disturbed the world.
if (!layout_root() || layout_root() != node->document().layout_node())
return true;
}
if (m_in_mouse_selection) {
auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
if (hit.layout_node && hit.layout_node->dom_node()) {
layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
}
if (auto* page = m_frame.page())
page->client().page_did_change_selection();
}
}
if (auto* page = m_frame.page()) {
if (is_hovering_link)
page->client().page_did_request_cursor_change(Gfx::StandardCursor::Hand);
else if (is_hovering_text)
page->client().page_did_request_cursor_change(Gfx::StandardCursor::IBeam);
else
page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
if (hovered_node_changed) {
RefPtr<HTML::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->client().page_did_enter_tooltip_area(m_frame.to_main_frame_position(position), hovered_html_element->title());
} else {
page->client().page_did_leave_tooltip_area();
}
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;
}
bool EventHandler::focus_next_element()
{
if (!m_frame.document())
return false;
auto* element = m_frame.document()->focused_element();
if (!element) {
element = m_frame.document()->first_child_of_type<DOM::Element>();
if (element && element->is_focusable()) {
m_frame.document()->set_focused_element(element);
return true;
}
}
for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
;
m_frame.document()->set_focused_element(element);
return element;
}
bool EventHandler::focus_previous_element()
{
// FIXME: Implement Shift-Tab cycling backwards through focusable elements!
return false;
}
bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
{
if (key == KeyCode::Key_Tab) {
if (modifiers & KeyModifier::Mod_Shift)
return focus_previous_element();
else
return focus_next_element();
}
if (layout_root()->selection().is_valid()) {
auto range = layout_root()->selection().to_dom_range()->normalized();
if (range->start_container()->is_editable()) {
m_frame.document()->layout_node()->set_selection({});
// FIXME: This doesn't work for some reason?
m_frame.set_cursor_position({ *range->start_container(), range->start_offset() });
if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
m_edit_event_handler->handle_delete(range);
return true;
} else {
m_edit_event_handler->handle_delete(range);
m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
m_frame.cursor_position().set_offset(m_frame.cursor_position().offset() + 1);
return true;
}
}
}
if (m_frame.cursor_position().is_valid() && m_frame.cursor_position().node()->is_editable()) {
if (key == KeyCode::Key_Backspace) {
auto position = m_frame.cursor_position();
if (position.offset() == 0)
TODO();
m_frame.cursor_position().set_offset(position.offset() - 1);
m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset() - 1, *position.node(), position.offset()));
return true;
} else if (key == KeyCode::Key_Delete) {
auto position = m_frame.cursor_position();
if (position.offset() >= downcast<DOM::Text>(position.node())->data().length())
TODO();
m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset(), *position.node(), position.offset() + 1));
return true;
} else if (key == KeyCode::Key_Right) {
auto position = m_frame.cursor_position();
if (position.offset() >= downcast<DOM::Text>(position.node())->data().length())
TODO();
m_frame.cursor_position().set_offset(position.offset() + 1);
return true;
} else if (key == KeyCode::Key_Left) {
auto position = m_frame.cursor_position();
if (position.offset() == 0)
TODO();
m_frame.cursor_position().set_offset(position.offset() - 1);
return true;
} else {
m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point);
m_frame.cursor_position().set_offset(m_frame.cursor_position().offset() + 1);
return true;
}
}
return false;
}
void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
{
if (layout_node)
m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
else
m_mouse_event_tracking_layout_node = nullptr;
}
}

View file

@ -0,0 +1,72 @@
/*
* 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 <AK/WeakPtr.h>
#include <Kernel/API/KeyCode.h>
#include <LibGUI/Forward.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Page/EditEventHandler.h>
namespace Web {
class Frame;
class EventHandler {
public:
explicit EventHandler(Badge<Frame>, Frame&);
~EventHandler();
bool handle_mouseup(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousedown(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousemove(const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point);
void set_mouse_event_tracking_layout_node(Layout::Node*);
void set_edit_event_handler(NonnullOwnPtr<EditEventHandler> value) { m_edit_event_handler = move(value); }
private:
bool focus_next_element();
bool focus_previous_element();
Layout::InitialContainingBlockBox* layout_root();
const Layout::InitialContainingBlockBox* layout_root() const;
Frame& m_frame;
bool m_in_mouse_selection { false };
WeakPtr<Layout::Node> m_mouse_event_tracking_layout_node;
NonnullOwnPtr<EditEventHandler> m_edit_event_handler;
};
}

View file

@ -0,0 +1,274 @@
/*
* Copyright (c) 2018-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 <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Layout/WidgetBox.h>
#include <LibWeb/Page/Frame.h>
namespace Web {
Frame::Frame(DOM::Element& host_element, 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)
{
setup();
}
Frame::Frame(Page& page)
: m_page(page)
, m_main_frame(*this)
, m_loader(*this)
, m_event_handler({}, *this)
{
setup();
}
Frame::~Frame()
{
}
void Frame::setup()
{
m_cursor_blink_timer = Core::Timer::construct(500, [this] {
if (!is_focused_frame())
return;
if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) {
m_cursor_blink_state = !m_cursor_blink_state;
m_cursor_position.node()->layout_node()->set_needs_display();
}
});
}
void Frame::did_edit(Badge<EditEventHandler>)
{
// The user has edited the content, restart the cursor blink cycle so that
// the cursor doesn't disappear during rapid continuous editing.
m_cursor_blink_state = true;
m_cursor_blink_timer->restart();
}
bool Frame::is_focused_frame() const
{
return m_page && &m_page->focused_frame() == this;
}
void Frame::set_document(DOM::Document* document)
{
if (m_document == document)
return;
m_cursor_position = {};
if (m_document)
m_document->detach_from_frame({}, *this);
m_document = document;
if (m_document) {
m_document->attach_to_frame({}, *this);
if (m_page)
m_page->client().page_did_change_title(m_document->title());
}
if (m_page)
m_page->client().page_did_set_document_in_main_frame(m_document);
}
void Frame::set_size(const Gfx::IntSize& size)
{
if (m_size == size)
return;
m_size = size;
if (m_document)
m_document->update_layout();
}
void Frame::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
{
if (m_viewport_scroll_offset == offset)
return;
m_viewport_scroll_offset = offset;
if (m_document && m_document->layout_node())
m_document->layout_node()->did_set_viewport_rect({}, viewport_rect());
}
void Frame::set_needs_display(const Gfx::IntRect& rect)
{
if (!viewport_rect().intersects(rect))
return;
if (is_main_frame()) {
if (m_page)
m_page->client().page_did_invalidate(to_main_frame_rect(rect));
return;
}
if (host_element() && host_element()->layout_node())
host_element()->layout_node()->set_needs_display();
}
void Frame::did_scroll(Badge<InProcessWebView>)
{
if (!m_document)
return;
if (!m_document->layout_node())
return;
m_document->layout_node()->for_each_in_subtree_of_type<Layout::WidgetBox>([&](auto& layout_widget) {
layout_widget.update_widget();
return IterationDecision::Continue;
});
}
void Frame::scroll_to_anchor(const String& fragment)
{
if (!document())
return;
auto element = document()->get_element_by_id(fragment);
if (!element) {
auto candidates = document()->get_elements_by_name(fragment);
for (auto& candidate : candidates) {
if (is<HTML::HTMLAnchorElement>(candidate)) {
element = downcast<HTML::HTMLAnchorElement>(candidate);
break;
}
}
}
// FIXME: This is overly aggressive and should be something more like a "update_layout_if_needed()"
document()->force_layout();
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<Layout::Box>(layout_node)) {
auto& layout_box = downcast<Layout::Box>(layout_node);
auto padding_box = layout_box.box_model().padding_box();
float_rect.move_by(-padding_box.left, -padding_box.top);
}
if (m_page)
m_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)
{
auto rect = a_rect;
rect.set_location(to_main_frame_position(a_rect.location()));
return rect;
}
Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& 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_type<int>());
}
return position;
}
void Frame::set_cursor_position(const DOM::Position& position)
{
if (m_cursor_position == position)
return;
if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
m_cursor_position.node()->layout_node()->set_needs_display();
m_cursor_position = position;
if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
m_cursor_position.node()->layout_node()->set_needs_display();
dbgln("Cursor position: {}", m_cursor_position);
}
String Frame::selected_text() const
{
StringBuilder builder;
if (!m_document)
return {};
auto* layout_root = m_document->layout_node();
if (!layout_root)
return {};
if (!layout_root->selection().is_valid())
return {};
auto selection = layout_root->selection().normalized();
if (selection.start().layout_node == selection.end().layout_node) {
if (!is<Layout::TextNode>(*selection.start().layout_node))
return "";
return downcast<Layout::TextNode>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node);
}
// Start node
auto layout_node = selection.start().layout_node;
if (is<Layout::TextNode>(*layout_node)) {
auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
}
// Middle nodes
layout_node = layout_node->next_in_pre_order();
while (layout_node && layout_node != selection.end().layout_node) {
if (is<Layout::TextNode>(*layout_node))
builder.append(downcast<Layout::TextNode>(*layout_node).text_for_rendering());
else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockBox>(*layout_node))
builder.append('\n');
layout_node = layout_node->next_in_pre_order();
}
// End node
ASSERT(layout_node == selection.end().layout_node);
if (is<Layout::TextNode>(*layout_node)) {
auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
builder.append(text.substring(0, selection.end().index_in_node));
}
return builder.to_string();
}
}

View file

@ -0,0 +1,121 @@
/*
* Copyright (c) 2018-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/Function.h>
#include <AK/Noncopyable.h>
#include <AK/RefPtr.h>
#include <AK/WeakPtr.h>
#include <LibCore/Timer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibWeb/DOM/Position.h>
#include <LibWeb/Loader/FrameLoader.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/TreeNode.h>
namespace Web {
class Frame : public TreeNode<Frame> {
public:
static NonnullRefPtr<Frame> create_subframe(DOM::Element& host_element, Frame& main_frame) { return adopt(*new Frame(host_element, main_frame)); }
static NonnullRefPtr<Frame> create(Page& page) { return adopt(*new Frame(page)); }
~Frame();
bool is_main_frame() const { return this == &m_main_frame; }
bool is_focused_frame() const;
const DOM::Document* document() const { return m_document; }
DOM::Document* document() { return m_document; }
void set_document(DOM::Document*);
Page* page() { return m_page; }
const Page* page() const { return m_page; }
const Gfx::IntSize& size() const { return m_size; }
void set_size(const Gfx::IntSize&);
void set_needs_display(const Gfx::IntRect&);
void set_viewport_scroll_offset(const Gfx::IntPoint&);
Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
void did_scroll(Badge<InProcessWebView>);
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(const Gfx::IntPoint&);
void scroll_to_anchor(const String&);
Frame& main_frame() { return m_main_frame; }
const Frame& main_frame() const { return m_main_frame; }
DOM::Element* host_element() { return m_host_element; }
const DOM::Element* host_element() const { return m_host_element; }
Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&);
Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&);
DOM::Position& cursor_position() { return m_cursor_position; }
const DOM::Position& cursor_position() const { return m_cursor_position; }
void set_cursor_position(const DOM::Position&);
bool cursor_blink_state() const { return m_cursor_blink_state; }
String selected_text() const;
void did_edit(Badge<EditEventHandler>);
private:
explicit Frame(DOM::Element& host_element, Frame& main_frame);
explicit Frame(Page&);
void setup();
WeakPtr<Page> m_page;
Frame& m_main_frame;
FrameLoader m_loader;
EventHandler m_event_handler;
WeakPtr<DOM::Element> m_host_element;
RefPtr<DOM::Document> m_document;
Gfx::IntSize m_size;
Gfx::IntPoint m_viewport_scroll_offset;
DOM::Position m_cursor_position;
RefPtr<Core::Timer> m_cursor_blink_timer;
bool m_cursor_blink_state { false };
};
}

View file

@ -0,0 +1,95 @@
/*
* 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 <LibWeb/InProcessWebView.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/Page/Page.h>
namespace Web {
Page::Page(PageClient& client)
: m_client(client)
{
m_main_frame = Frame::create(*this);
}
Page::~Page()
{
}
Frame& Page::focused_frame()
{
if (m_focused_frame)
return *m_focused_frame;
return main_frame();
}
void Page::set_focused_frame(Badge<EventHandler>, Frame& frame)
{
m_focused_frame = frame.make_weak_ptr();
}
void Page::load(const URL& url)
{
main_frame().loader().load(url, FrameLoader::Type::Navigation);
}
void Page::load(const LoadRequest& request)
{
main_frame().loader().load(request, FrameLoader::Type::Navigation);
}
void Page::load_html(const StringView& html, const URL& url)
{
main_frame().loader().load_html(html, url);
}
Gfx::Palette Page::palette() const
{
return m_client.palette();
}
bool Page::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
return main_frame().event_handler().handle_mouseup(position, button, modifiers);
}
bool Page::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
return main_frame().event_handler().handle_mousedown(position, button, modifiers);
}
bool Page::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
{
return main_frame().event_handler().handle_mousemove(position, buttons, modifiers);
}
bool Page::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
{
return focused_frame().event_handler().handle_keydown(key, modifiers, code_point);
}
}

View file

@ -0,0 +1,107 @@
/*
* 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/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/URL.h>
#include <Kernel/API/KeyCode.h>
#include <LibGUI/Window.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Palette.h>
#include <LibWeb/Forward.h>
namespace Web {
class PageClient;
class Page : public Weakable<Page> {
AK_MAKE_NONCOPYABLE(Page);
AK_MAKE_NONMOVABLE(Page);
public:
explicit Page(PageClient&);
~Page();
PageClient& client() { return m_client; }
const PageClient& client() const { return m_client; }
Web::Frame& main_frame() { return *m_main_frame; }
const Web::Frame& main_frame() const { return *m_main_frame; }
Web::Frame& focused_frame();
const Web::Frame& focused_frame() const { return const_cast<Page*>(this)->focused_frame(); }
void set_focused_frame(Badge<EventHandler>, Frame&);
void load(const URL&);
void load(const LoadRequest&);
void load_html(const StringView&, const URL&);
bool handle_mouseup(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousedown(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
bool handle_mousemove(const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point);
Gfx::Palette palette() const;
private:
PageClient& m_client;
RefPtr<Frame> m_main_frame;
WeakPtr<Frame> m_focused_frame;
};
class PageClient {
public:
virtual Gfx::Palette palette() const = 0;
virtual void page_did_set_document_in_main_frame(DOM::Document*) { }
virtual void page_did_change_title(const String&) { }
virtual void page_did_start_loading(const URL&) { }
virtual void page_did_finish_loading(const URL&) { }
virtual void page_did_change_selection() { }
virtual void page_did_request_cursor_change(Gfx::StandardCursor) { }
virtual void page_did_request_context_menu(const Gfx::IntPoint&) { }
virtual void page_did_request_link_context_menu(const Gfx::IntPoint&, const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_request_image_context_menu(const Gfx::IntPoint&, const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap*) { }
virtual void page_did_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_middle_click_link(const URL&, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers) { }
virtual void page_did_enter_tooltip_area(const Gfx::IntPoint&, const String&) { }
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_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&) { }
virtual void page_did_request_alert(const String&) { }
};
}