LibWeb: Make selection state recomputation implicit

Add a LayoutDocument API for modifying the selection and make clients
call that so we can recompute selection states automatically.
This commit is contained in:
Andreas Kling 2020-08-21 17:54:44 +02:00
parent d47f77169f
commit 684fa0f99b
4 changed files with 18 additions and 8 deletions

View file

@ -109,8 +109,7 @@ void InProcessWebView::select_all()
if (is<LayoutText>(*last_layout_node))
last_layout_node_index_in_node = downcast<LayoutText>(*last_layout_node).text_for_rendering().length() - 1;
layout_root->selection().set({ first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node });
layout_root->recompute_selection_states();
layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
update();
}

View file

@ -146,4 +146,16 @@ void LayoutDocument::recompute_selection_states()
});
}
void LayoutDocument::set_selection(const LayoutRange & selection)
{
m_selection = selection;
recompute_selection_states();
}
void LayoutDocument::set_selection_end(const LayoutPosition& position)
{
m_selection.set_end(position);
recompute_selection_states();
}
}

View file

@ -46,7 +46,8 @@ public:
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
const LayoutRange& selection() const { return m_selection; }
LayoutRange& selection() { return m_selection; }
void set_selection(const LayoutRange&);
void set_selection_end(const LayoutPosition&);
void did_set_viewport_rect(Badge<Frame>, const Gfx::IntRect&);

View file

@ -31,10 +31,10 @@
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/LayoutDocument.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/UIEvents/MouseEvent.h>
namespace Web {
@ -156,8 +156,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
auto result = layout_root()->hit_test(position, HitTestType::TextCursor);
if (result.layout_node && result.layout_node->node()) {
m_frame.set_cursor_position(DOM::Position(*node, result.index_in_node));
layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
layout_root()->recompute_selection_states();
layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} });
dump_selection("MouseDown");
m_in_mouse_selection = true;
}
@ -209,8 +208,7 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
if (m_in_mouse_selection) {
auto hit = layout_root()->hit_test(position, HitTestType::TextCursor);
if (hit.layout_node && hit.layout_node->node()) {
layout_root()->selection().set_end({ hit.layout_node, hit.index_in_node });
layout_root()->recompute_selection_states();
layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node });
}
dump_selection("MouseMove");
page_client.page_did_change_selection();