mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:47:35 +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:
parent
1408aa1295
commit
d73116e5d5
7 changed files with 221 additions and 1 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue