mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-29 19:10:26 +00:00
LibGUI: Implement granular operations for Model subclasses
These can be used by Model subclasses to signal the exact operations that happened to a model, so that persistent model indices in that area are not invalidated.
This commit is contained in:
parent
d73116e5d5
commit
7ba321c91b
Notes:
sideshowbarker
2024-07-18 07:14:40 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/7ba321c91b8 Pull-request: https://github.com/SerenityOS/serenity/pull/9269 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/awesomekling
2 changed files with 448 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -32,6 +33,7 @@ void Model::unregister_view(Badge<AbstractView>, AbstractView& view)
|
|||
|
||||
void Model::invalidate()
|
||||
{
|
||||
m_persistent_handles.clear();
|
||||
did_update();
|
||||
}
|
||||
|
||||
|
@ -41,10 +43,17 @@ void Model::for_each_view(Function<void(AbstractView&)> callback)
|
|||
callback(*view);
|
||||
}
|
||||
|
||||
void Model::did_update(unsigned flags)
|
||||
void Model::for_each_client(Function<void(ModelClient&)> callback)
|
||||
{
|
||||
for (auto* client : m_clients)
|
||||
client->model_did_update(flags);
|
||||
callback(*client);
|
||||
}
|
||||
|
||||
void Model::did_update(unsigned flags)
|
||||
{
|
||||
for_each_client([flags](ModelClient& client) {
|
||||
client.model_did_update(flags);
|
||||
});
|
||||
}
|
||||
|
||||
ModelIndex Model::create_index(int row, int column, const void* data) const
|
||||
|
@ -127,4 +136,354 @@ RefPtr<Core::MimeData> Model::mime_data(const ModelSelection& selection) const
|
|||
return mime_data;
|
||||
}
|
||||
|
||||
void Model::begin_insert_rows(ModelIndex const& parent, int first, int last)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
m_operation_stack.empend(OperationType::Insert, Direction::Row, parent, first, last);
|
||||
}
|
||||
|
||||
void Model::begin_insert_columns(ModelIndex const& parent, int first, int last)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
m_operation_stack.empend(OperationType::Insert, Direction::Column, parent, first, last);
|
||||
}
|
||||
|
||||
void Model::begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
VERIFY(target_index >= 0);
|
||||
m_operation_stack.empend(OperationType::Move, Direction::Row, source_parent, first, last, target_parent, target_index);
|
||||
}
|
||||
|
||||
void Model::begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
VERIFY(target_index >= 0);
|
||||
m_operation_stack.empend(OperationType::Move, Direction::Column, source_parent, first, last, target_parent, target_index);
|
||||
}
|
||||
|
||||
void Model::begin_delete_rows(ModelIndex const& parent, int first, int last)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
VERIFY(last < row_count(parent));
|
||||
|
||||
save_deleted_indices<true>(parent, first, last);
|
||||
m_operation_stack.empend(OperationType::Delete, Direction::Row, parent, first, last);
|
||||
}
|
||||
|
||||
void Model::begin_delete_columns(ModelIndex const& parent, int first, int last)
|
||||
{
|
||||
VERIFY(first >= 0);
|
||||
VERIFY(first <= last);
|
||||
VERIFY(last < column_count(parent));
|
||||
|
||||
save_deleted_indices<false>(parent, first, last);
|
||||
m_operation_stack.empend(OperationType::Delete, Direction::Column, parent, first, last);
|
||||
}
|
||||
|
||||
template<bool IsRow>
|
||||
void Model::save_deleted_indices(ModelIndex const& parent, int first, int last)
|
||||
{
|
||||
Vector<ModelIndex> deleted_indices;
|
||||
|
||||
for (auto& entry : m_persistent_handles) {
|
||||
auto current_index = entry.key;
|
||||
|
||||
// Walk up the persistent handle's parents to see if it is contained
|
||||
// within the range that is being deleted.
|
||||
while (current_index.is_valid()) {
|
||||
auto current_parent = current_index.parent();
|
||||
|
||||
if (current_parent == parent) {
|
||||
if constexpr (IsRow) {
|
||||
if (current_index.row() >= first && current_index.row() <= last)
|
||||
deleted_indices.append(current_index);
|
||||
} else {
|
||||
if (current_index.column() >= first && current_index.column() <= last)
|
||||
deleted_indices.append(current_index);
|
||||
}
|
||||
}
|
||||
|
||||
current_index = current_parent;
|
||||
}
|
||||
}
|
||||
|
||||
m_deleted_indices_stack.append(move(deleted_indices));
|
||||
}
|
||||
|
||||
void Model::end_insert_rows()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Insert);
|
||||
VERIFY(operation.direction == Direction::Row);
|
||||
handle_insert(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_insert_rows(operation.source_parent, operation.first, operation.last);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::end_insert_columns()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Insert);
|
||||
VERIFY(operation.direction == Direction::Column);
|
||||
handle_insert(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_insert_columns(operation.source_parent, operation.first, operation.last);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::end_move_rows()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Move);
|
||||
VERIFY(operation.direction == Direction::Row);
|
||||
handle_move(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_move_rows(operation.source_parent, operation.first, operation.last, operation.target_parent, operation.target);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::end_move_columns()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Move);
|
||||
VERIFY(operation.direction == Direction::Column);
|
||||
handle_move(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_move_columns(operation.source_parent, operation.first, operation.last, operation.target_parent, operation.target);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::end_delete_rows()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Delete);
|
||||
VERIFY(operation.direction == Direction::Row);
|
||||
handle_delete(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_delete_rows(operation.source_parent, operation.first, operation.last);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::end_delete_columns()
|
||||
{
|
||||
auto operation = m_operation_stack.take_last();
|
||||
VERIFY(operation.type == OperationType::Delete);
|
||||
VERIFY(operation.direction == Direction::Column);
|
||||
handle_delete(operation);
|
||||
|
||||
for_each_client([&operation](ModelClient& client) {
|
||||
client.model_did_delete_columns(operation.source_parent, operation.first, operation.last);
|
||||
});
|
||||
}
|
||||
|
||||
void Model::change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices)
|
||||
{
|
||||
VERIFY(old_indices.size() == new_indices.size());
|
||||
|
||||
for (size_t i = 0; i < old_indices.size(); i++) {
|
||||
auto it = m_persistent_handles.find(old_indices.at(i));
|
||||
if (it == m_persistent_handles.end())
|
||||
continue;
|
||||
|
||||
auto handle = move(it->value);
|
||||
m_persistent_handles.remove(it);
|
||||
|
||||
auto new_index = new_indices.at(i);
|
||||
if (new_index.is_valid()) {
|
||||
handle->m_index = new_index;
|
||||
m_persistent_handles.set(new_index, move(handle));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Model::handle_insert(Operation const& operation)
|
||||
{
|
||||
bool is_row = operation.direction == Direction::Row;
|
||||
Vector<ModelIndex*> to_shift;
|
||||
|
||||
for (auto& entry : m_persistent_handles) {
|
||||
if (entry.key.parent() == operation.source_parent) {
|
||||
if (is_row && entry.key.row() >= operation.first) {
|
||||
to_shift.append(&entry.key);
|
||||
} else if (!is_row && entry.key.column() >= operation.first) {
|
||||
to_shift.append(&entry.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int offset = operation.last - operation.first + 1;
|
||||
|
||||
for (auto current_index : to_shift) {
|
||||
int new_row = is_row ? current_index->row() + offset : current_index->row();
|
||||
int new_column = is_row ? current_index->column() : current_index->column() + offset;
|
||||
auto new_index = create_index(new_row, new_column, current_index->internal_data());
|
||||
|
||||
auto it = m_persistent_handles.find(*current_index);
|
||||
auto handle = move(it->value);
|
||||
|
||||
handle->m_index = new_index;
|
||||
|
||||
m_persistent_handles.remove(it);
|
||||
m_persistent_handles.set(move(new_index), move(handle));
|
||||
}
|
||||
}
|
||||
|
||||
void Model::handle_delete(Operation const& operation)
|
||||
{
|
||||
bool is_row = operation.direction == Direction::Row;
|
||||
Vector<ModelIndex> deleted_indices = m_deleted_indices_stack.take_last();
|
||||
Vector<ModelIndex*> to_shift;
|
||||
|
||||
// Get rid of all persistent handles which have been marked for death
|
||||
for (auto& deleted_index : deleted_indices) {
|
||||
m_persistent_handles.remove(deleted_index);
|
||||
}
|
||||
|
||||
for (auto& entry : m_persistent_handles) {
|
||||
if (entry.key.parent() == operation.source_parent) {
|
||||
if (is_row) {
|
||||
if (entry.key.row() > operation.last) {
|
||||
to_shift.append(&entry.key);
|
||||
}
|
||||
} else {
|
||||
if (entry.key.column() > operation.last) {
|
||||
to_shift.append(&entry.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int offset = operation.last - operation.first + 1;
|
||||
|
||||
for (auto current_index : to_shift) {
|
||||
int new_row = is_row ? current_index->row() - offset : current_index->row();
|
||||
int new_column = is_row ? current_index->column() : current_index->column() - offset;
|
||||
auto new_index = create_index(new_row, new_column, current_index->internal_data());
|
||||
|
||||
auto it = m_persistent_handles.find(*current_index);
|
||||
auto handle = move(it->value);
|
||||
|
||||
handle->m_index = new_index;
|
||||
|
||||
m_persistent_handles.remove(it);
|
||||
m_persistent_handles.set(move(new_index), move(handle));
|
||||
}
|
||||
}
|
||||
|
||||
void Model::handle_move(Operation const& operation)
|
||||
{
|
||||
bool is_row = operation.direction == Direction::Row;
|
||||
bool move_within = operation.source_parent == operation.target_parent;
|
||||
bool moving_down = operation.target > operation.first;
|
||||
|
||||
if (move_within && operation.first == operation.target)
|
||||
return;
|
||||
|
||||
if (is_row) {
|
||||
VERIFY(operation.target <= row_count(operation.target_parent));
|
||||
VERIFY(operation.last < row_count(operation.source_parent));
|
||||
} else {
|
||||
VERIFY(operation.target <= column_count(operation.target_parent));
|
||||
VERIFY(operation.last < column_count(operation.source_parent));
|
||||
}
|
||||
|
||||
// NOTE: to_shift_down is used as a generic "to shift" when move_within is true.
|
||||
Vector<ModelIndex*> to_move; // Items to be moved between the source and target
|
||||
Vector<ModelIndex*> to_shift_down; // Items to be shifted down after a move-to
|
||||
Vector<ModelIndex*> to_shift_up; // Items to be shifted up after a move-from
|
||||
|
||||
int count = operation.last - operation.first + 1;
|
||||
// [start, end)
|
||||
int work_area_start = min(operation.first, operation.target);
|
||||
int work_area_end = max(operation.last + 1, operation.target + count);
|
||||
|
||||
for (auto& entry : m_persistent_handles) {
|
||||
int dimension = is_row ? entry.key.row() : entry.key.column();
|
||||
|
||||
if (move_within) {
|
||||
if (entry.key.parent() == operation.source_parent) {
|
||||
if (dimension >= operation.first && dimension <= operation.last) {
|
||||
to_move.append(&entry.key);
|
||||
} else if (moving_down && dimension > operation.last && dimension < work_area_end) {
|
||||
to_shift_down.append(&entry.key);
|
||||
} else if (!moving_down && dimension >= work_area_start && dimension < operation.first) {
|
||||
to_shift_down.append(&entry.key);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (entry.key.parent() == operation.source_parent) {
|
||||
if (dimension >= operation.first && dimension <= operation.last) {
|
||||
to_move.append(&entry.key);
|
||||
} else if (dimension > operation.last) {
|
||||
to_shift_up.append(&entry.key);
|
||||
}
|
||||
} else if (entry.key.parent() == operation.target_parent) {
|
||||
if (dimension >= operation.target) {
|
||||
to_shift_down.append(&entry.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto replace_handle = [&](ModelIndex const& current_index, int new_dimension, bool relative) {
|
||||
int new_row = is_row
|
||||
? (relative
|
||||
? current_index.row() + new_dimension
|
||||
: new_dimension)
|
||||
: current_index.row();
|
||||
int new_column = !is_row
|
||||
? (relative
|
||||
? current_index.column() + new_dimension
|
||||
: new_dimension)
|
||||
: current_index.column();
|
||||
auto new_index = index(new_row, new_column, operation.target_parent);
|
||||
|
||||
auto it = m_persistent_handles.find(current_index);
|
||||
auto handle = move(it->value);
|
||||
|
||||
handle->m_index = new_index;
|
||||
|
||||
m_persistent_handles.remove(it);
|
||||
m_persistent_handles.set(move(new_index), move(handle));
|
||||
};
|
||||
|
||||
for (auto current_index : to_move) {
|
||||
int dimension = is_row ? current_index->row() : current_index->column();
|
||||
int target_offset = dimension - operation.first;
|
||||
int new_dimension = operation.target + target_offset;
|
||||
|
||||
replace_handle(*current_index, new_dimension, false);
|
||||
}
|
||||
|
||||
if (move_within) {
|
||||
for (auto current_index : to_shift_down) {
|
||||
int dimension = is_row ? current_index->row() : current_index->column();
|
||||
int target_offset = moving_down ? dimension - (operation.last + 1) : dimension - work_area_start + count;
|
||||
int new_dimension = work_area_start + target_offset;
|
||||
|
||||
replace_handle(*current_index, new_dimension, false);
|
||||
}
|
||||
} else {
|
||||
for (auto current_index : to_shift_down) {
|
||||
replace_handle(*current_index, count, true);
|
||||
}
|
||||
|
||||
for (auto current_index : to_shift_up) {
|
||||
replace_handle(*current_index, count, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -35,6 +36,13 @@ public:
|
|||
virtual ~ModelClient() { }
|
||||
|
||||
virtual void model_did_update(unsigned flags) = 0;
|
||||
|
||||
virtual void model_did_insert_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
||||
virtual void model_did_insert_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
||||
virtual void model_did_move_rows([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
|
||||
virtual void model_did_move_columns([[maybe_unused]] ModelIndex const& source_parent, [[maybe_unused]] int first, [[maybe_unused]] int last, [[maybe_unused]] ModelIndex const& target_parent, [[maybe_unused]] int target_index) { }
|
||||
virtual void model_did_delete_rows([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
||||
virtual void model_did_delete_columns([[maybe_unused]] ModelIndex const& parent, [[maybe_unused]] int first, [[maybe_unused]] int last) { }
|
||||
};
|
||||
|
||||
class Model : public RefCounted<Model> {
|
||||
|
@ -93,6 +101,7 @@ protected:
|
|||
Model();
|
||||
|
||||
void for_each_view(Function<void(AbstractView&)>);
|
||||
void for_each_client(Function<void(ModelClient&)>);
|
||||
void did_update(unsigned flags = UpdateFlag::InvalidateAllIndices);
|
||||
|
||||
static bool string_matches(const StringView& str, const StringView& needle, unsigned flags)
|
||||
|
@ -107,7 +116,85 @@ protected:
|
|||
|
||||
ModelIndex create_index(int row, int column, const void* data = nullptr) const;
|
||||
|
||||
void begin_insert_rows(ModelIndex const& parent, int first, int last);
|
||||
void begin_insert_columns(ModelIndex const& parent, int first, int last);
|
||||
void begin_move_rows(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
|
||||
void begin_move_columns(ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target_index);
|
||||
void begin_delete_rows(ModelIndex const& parent, int first, int last);
|
||||
void begin_delete_columns(ModelIndex const& parent, int first, int last);
|
||||
|
||||
void end_insert_rows();
|
||||
void end_insert_columns();
|
||||
void end_move_rows();
|
||||
void end_move_columns();
|
||||
void end_delete_rows();
|
||||
void end_delete_columns();
|
||||
|
||||
void change_persistent_index_list(Vector<ModelIndex> const& old_indices, Vector<ModelIndex> const& new_indices);
|
||||
|
||||
private:
|
||||
enum class OperationType {
|
||||
Invalid = 0,
|
||||
Insert,
|
||||
Move,
|
||||
Delete,
|
||||
Reset
|
||||
};
|
||||
enum class Direction {
|
||||
Row,
|
||||
Column
|
||||
};
|
||||
|
||||
struct Operation {
|
||||
OperationType type { OperationType::Invalid };
|
||||
Direction direction { Direction::Row };
|
||||
ModelIndex source_parent;
|
||||
int first { 0 };
|
||||
int last { 0 };
|
||||
ModelIndex target_parent;
|
||||
int target { 0 };
|
||||
|
||||
Operation(OperationType type)
|
||||
: type(type)
|
||||
{
|
||||
}
|
||||
|
||||
Operation(OperationType type, Direction direction, ModelIndex const& parent, int first, int last)
|
||||
: type(type)
|
||||
, direction(direction)
|
||||
, source_parent(parent)
|
||||
, first(first)
|
||||
, last(last)
|
||||
{
|
||||
}
|
||||
|
||||
Operation(OperationType type, Direction direction, ModelIndex const& source_parent, int first, int last, ModelIndex const& target_parent, int target)
|
||||
: type(type)
|
||||
, direction(direction)
|
||||
, source_parent(source_parent)
|
||||
, first(first)
|
||||
, last(last)
|
||||
, target_parent(target_parent)
|
||||
, target(target)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
void handle_insert(Operation const&);
|
||||
void handle_move(Operation const&);
|
||||
void handle_delete(Operation const&);
|
||||
|
||||
template<bool IsRow>
|
||||
void save_deleted_indices(ModelIndex const& parent, int first, int last);
|
||||
|
||||
HashMap<ModelIndex, OwnPtr<PersistentHandle>> m_persistent_handles;
|
||||
Vector<Operation> m_operation_stack;
|
||||
// NOTE: We need to save which indices have been deleted before the delete
|
||||
// actually happens, because we can't figure out which persistent handles
|
||||
// belong to us in end_delete_rows/columns (because accessing the parents of
|
||||
// the indices might be impossible).
|
||||
Vector<Vector<ModelIndex>> m_deleted_indices_stack;
|
||||
|
||||
HashTable<AbstractView*> m_views;
|
||||
HashTable<ModelClient*> m_clients;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue