mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-06 08:36:15 +00:00
LibGUI: Implement persistent indices for models
This patch adds persistent indices to models. A PersistentModelIndex is a ModelIndex that will survive most model updates (provided that the data the PersistentModelIndex points to has not been removed by the model's data store). PersistentModelIndex objects can be safely held by objects outside the model they originated from.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <LibGUI/AbstractView.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/PersistentModelIndex.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
@@ -71,6 +72,25 @@ void Model::unregister_client(ModelClient& client)
|
||||
m_clients.remove(&client);
|
||||
}
|
||||
|
||||
WeakPtr<PersistentHandle> Model::register_persistent_index(Badge<PersistentModelIndex>, const ModelIndex& index)
|
||||
{
|
||||
if (!index.is_valid())
|
||||
return {};
|
||||
|
||||
auto it = m_persistent_handles.find(index);
|
||||
// Easy modo: we already have a handle for this model index.
|
||||
if (it != m_persistent_handles.end()) {
|
||||
return it->value->make_weak_ptr();
|
||||
}
|
||||
|
||||
// Hard modo: create a new persistent handle.
|
||||
auto handle = adopt_own(*new PersistentHandle(index));
|
||||
auto weak_handle = handle->make_weak_ptr();
|
||||
m_persistent_handles.set(index, move(handle));
|
||||
|
||||
return weak_handle;
|
||||
}
|
||||
|
||||
RefPtr<Core::MimeData> Model::mime_data(const ModelSelection& selection) const
|
||||
{
|
||||
auto mime_data = Core::MimeData::construct();
|
||||
|
||||
Reference in New Issue
Block a user