mirror of
https://github.com/RGBCube/serenity
synced 2025-07-07 07:47:36 +00:00
LibWeb: Add a "select all" action to the Web::PageView
This works by finding the very first and very last LayoutText nodes in the layout tree and then setting the selection bounds to those two nodes. For some reason it gets glitchy if we set the very first and very last *LayoutNode* as the selection bounds, but I didn't feel like investigating that too closely right now.
This commit is contained in:
parent
eec22acb8e
commit
f7ef6c65b4
2 changed files with 43 additions and 0 deletions
|
@ -67,12 +67,51 @@ PageView::PageView()
|
|||
m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
|
||||
GUI::Clipboard::the().set_data(selected_text(), "text/plain");
|
||||
});
|
||||
|
||||
m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
|
||||
select_all();
|
||||
});
|
||||
}
|
||||
|
||||
PageView::~PageView()
|
||||
{
|
||||
}
|
||||
|
||||
void PageView::select_all()
|
||||
{
|
||||
auto* layout_root = this->layout_root();
|
||||
if (!layout_root)
|
||||
return;
|
||||
|
||||
const LayoutNode* first_layout_node = layout_root;
|
||||
|
||||
for (;;) {
|
||||
auto* next = first_layout_node->next_in_pre_order();
|
||||
if (!next)
|
||||
break;
|
||||
first_layout_node = next;
|
||||
if (is<LayoutText>(*first_layout_node))
|
||||
break;
|
||||
}
|
||||
|
||||
const LayoutNode* last_layout_node = first_layout_node;
|
||||
|
||||
for (const LayoutNode* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
|
||||
if (is<LayoutText>(*layout_node))
|
||||
last_layout_node = layout_node;
|
||||
}
|
||||
|
||||
ASSERT(first_layout_node);
|
||||
ASSERT(last_layout_node);
|
||||
|
||||
int last_layout_node_index_in_node = 0;
|
||||
if (is<LayoutText>(*last_layout_node))
|
||||
last_layout_node_index_in_node = to<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 });
|
||||
update();
|
||||
}
|
||||
|
||||
String PageView::selected_text() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue