mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:17:35 +00:00
LibWeb: Use DOM Selection instead of ad-hoc layout tree selection
Before this patch, we were expressing the current selection as a range between two points in the layout tree. This was a made-up concept I called LayoutRange (2x LayoutPosition) and as it turns out, we don't actually need it! Instead, we can just use the Selection API from the Selection API spec. This API expresses selection in terms of the DOM, and we already had many of the building blocks implemented. To ensure that selections get visually updated when the underlying Range of an active Selection is programmatically manipulated, Range now has an "associated Selection". If a range is updated while associated with a selection, we recompute layout tree selection states and repaint the page to make it user-visible.
This commit is contained in:
parent
1c4328902d
commit
b79bc25a1f
9 changed files with 161 additions and 71 deletions
|
@ -399,7 +399,9 @@ bool EventHandler::handle_mousedown(CSSPixelPoint position, unsigned button, uns
|
|||
// FIXME: This is all rather strange. Find a better solution.
|
||||
if (!did_focus_something) {
|
||||
m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), result->index_in_node));
|
||||
layout_root()->set_selection({ { paintable->layout_node(), result->index_in_node }, {} });
|
||||
if (auto selection = document->get_selection()) {
|
||||
(void)selection->set_base_and_extent(*paintable->dom_node(), result->index_in_node, *paintable->dom_node(), result->index_in_node);
|
||||
}
|
||||
m_in_mouse_selection = true;
|
||||
}
|
||||
}
|
||||
|
@ -495,7 +497,13 @@ bool EventHandler::handle_mousemove(CSSPixelPoint position, unsigned buttons, un
|
|||
auto hit = paint_root()->hit_test(position, Painting::HitTestType::TextCursor);
|
||||
if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
|
||||
m_browsing_context.set_cursor_position(DOM::Position(*hit->dom_node(), *start_index));
|
||||
layout_root()->set_selection_end({ hit->paintable->layout_node(), hit->index_in_node });
|
||||
if (auto selection = document.get_selection()) {
|
||||
auto anchor_node = selection->anchor_node();
|
||||
if (anchor_node)
|
||||
(void)selection->set_base_and_extent(*anchor_node, selection->anchor_offset(), *hit->paintable->dom_node(), hit->index_in_node);
|
||||
else
|
||||
(void)selection->set_base_and_extent(*hit->paintable->dom_node(), hit->index_in_node, *hit->paintable->dom_node(), hit->index_in_node);
|
||||
}
|
||||
m_browsing_context.set_needs_display();
|
||||
}
|
||||
if (auto* page = m_browsing_context.page())
|
||||
|
@ -607,7 +615,9 @@ bool EventHandler::handle_doubleclick(CSSPixelPoint position, unsigned button, u
|
|||
}();
|
||||
|
||||
m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), first_word_break_after));
|
||||
layout_root()->set_selection({ { paintable->layout_node(), first_word_break_before }, { paintable->layout_node(), first_word_break_after } });
|
||||
if (auto selection = node->document().get_selection()) {
|
||||
(void)selection->set_base_and_extent(*paintable->dom_node(), first_word_break_before, *paintable->dom_node(), first_word_break_after);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -696,18 +706,16 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
|
|||
if (!document->layout_node())
|
||||
return false;
|
||||
|
||||
JS::NonnullGCPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
|
||||
|
||||
if (key == KeyCode::Key_Tab) {
|
||||
if (modifiers & KeyModifier::Mod_Shift)
|
||||
return focus_previous_element();
|
||||
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()) {
|
||||
layout_root->set_selection({});
|
||||
if (auto selection = document->get_selection()) {
|
||||
auto range = selection->range();
|
||||
if (range && range->start_container()->is_editable()) {
|
||||
selection->remove_all_ranges();
|
||||
|
||||
// FIXME: This doesn't work for some reason?
|
||||
m_browsing_context.set_cursor_position({ *range->start_container(), range->start_offset() });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue