mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +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
Notes:
sideshowbarker
2024-07-18 07:14:44 +09:00
Author: https://github.com/sin-ack Commit: https://github.com/SerenityOS/serenity/commit/d73116e5d5f Pull-request: https://github.com/SerenityOS/serenity/pull/9269 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/awesomekling
7 changed files with 221 additions and 1 deletions
|
@ -71,6 +71,7 @@ set(SOURCES
|
|||
Painter.cpp
|
||||
PasswordInputDialog.cpp
|
||||
PasswordInputDialogGML.h
|
||||
PersistentModelIndex.cpp
|
||||
ProcessChooser.cpp
|
||||
Progressbar.cpp
|
||||
RadioButton.cpp
|
||||
|
|
|
@ -49,6 +49,8 @@ class MultiView;
|
|||
class OpacitySlider;
|
||||
class PaintEvent;
|
||||
class Painter;
|
||||
class PersistentHandle;
|
||||
class PersistentModelIndex;
|
||||
class RadioButton;
|
||||
class ResizeCorner;
|
||||
class ResizeEvent;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -8,10 +8,13 @@
|
|||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <LibGUI/ModelIndex.h>
|
||||
#include <LibGUI/ModelRole.h>
|
||||
#include <LibGUI/ModelSelection.h>
|
||||
|
@ -84,6 +87,8 @@ public:
|
|||
void register_client(ModelClient&);
|
||||
void unregister_client(ModelClient&);
|
||||
|
||||
WeakPtr<PersistentHandle> register_persistent_index(Badge<PersistentModelIndex>, ModelIndex const&);
|
||||
|
||||
protected:
|
||||
Model();
|
||||
|
||||
|
|
|
@ -77,7 +77,10 @@ struct Formatter<GUI::ModelIndex> : Formatter<FormatString> {
|
|||
|
||||
template<>
|
||||
struct Traits<GUI::ModelIndex> : public GenericTraits<GUI::ModelIndex> {
|
||||
static unsigned hash(const GUI::ModelIndex& index) { return pair_int_hash(index.row(), index.column()); }
|
||||
static unsigned hash(const GUI::ModelIndex& index)
|
||||
{
|
||||
return pair_int_hash(pair_int_hash(index.row(), index.column()), reinterpret_cast<FlatPtr>(index.internal_data()));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
96
Userland/Libraries/LibGUI/PersistentModelIndex.cpp
Normal file
96
Userland/Libraries/LibGUI/PersistentModelIndex.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGUI/PersistentModelIndex.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
PersistentModelIndex::PersistentModelIndex(ModelIndex const& index)
|
||||
{
|
||||
if (!index.is_valid())
|
||||
return;
|
||||
|
||||
auto* model = const_cast<Model*>(index.model());
|
||||
m_handle = model->register_persistent_index({}, index);
|
||||
}
|
||||
|
||||
int PersistentModelIndex::row() const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return -1;
|
||||
return m_handle->m_index.row();
|
||||
}
|
||||
|
||||
int PersistentModelIndex::column() const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return -1;
|
||||
return m_handle->m_index.column();
|
||||
}
|
||||
|
||||
PersistentModelIndex PersistentModelIndex::parent() const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return {};
|
||||
return { m_handle->m_index.parent() };
|
||||
}
|
||||
|
||||
PersistentModelIndex PersistentModelIndex::sibling_at_column(int column) const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return {};
|
||||
|
||||
return { m_handle->m_index.sibling_at_column(column) };
|
||||
}
|
||||
|
||||
Variant PersistentModelIndex::data(ModelRole role) const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return {};
|
||||
return { m_handle->m_index.data(role) };
|
||||
}
|
||||
|
||||
PersistentModelIndex::operator ModelIndex() const
|
||||
{
|
||||
if (!has_valid_handle())
|
||||
return {};
|
||||
else
|
||||
return m_handle->m_index;
|
||||
}
|
||||
|
||||
bool PersistentModelIndex::operator==(PersistentModelIndex const& other) const
|
||||
{
|
||||
bool is_this_valid = has_valid_handle();
|
||||
bool is_other_valid = other.has_valid_handle();
|
||||
|
||||
if (!is_this_valid && !is_other_valid)
|
||||
return true;
|
||||
if (is_this_valid != is_other_valid)
|
||||
return false;
|
||||
|
||||
return m_handle->m_index == other.m_handle->m_index;
|
||||
}
|
||||
|
||||
bool PersistentModelIndex::operator!=(PersistentModelIndex const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool PersistentModelIndex::operator==(ModelIndex const& other) const
|
||||
{
|
||||
if (!has_valid_handle()) {
|
||||
return !other.is_valid();
|
||||
}
|
||||
|
||||
return m_handle->m_index == other;
|
||||
}
|
||||
|
||||
bool PersistentModelIndex::operator!=(ModelIndex const& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
}
|
93
Userland/Libraries/LibGUI/PersistentModelIndex.h
Normal file
93
Userland/Libraries/LibGUI/PersistentModelIndex.h
Normal file
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/ModelIndex.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
/// A PersistentHandle is an internal data structure used to keep track of the
|
||||
/// target of multiple PersistentModelIndex instances.
|
||||
class PersistentHandle : public Weakable<PersistentHandle> {
|
||||
friend Model;
|
||||
friend PersistentModelIndex;
|
||||
friend AK::Traits<GUI::PersistentModelIndex>;
|
||||
|
||||
PersistentHandle(ModelIndex const& index)
|
||||
: m_index(index)
|
||||
{
|
||||
}
|
||||
|
||||
ModelIndex m_index;
|
||||
};
|
||||
|
||||
class PersistentModelIndex {
|
||||
public:
|
||||
PersistentModelIndex() { }
|
||||
PersistentModelIndex(ModelIndex const&);
|
||||
PersistentModelIndex(PersistentModelIndex const&) = default;
|
||||
PersistentModelIndex(PersistentModelIndex&&) = default;
|
||||
|
||||
PersistentModelIndex& operator=(PersistentModelIndex const&) = default;
|
||||
PersistentModelIndex& operator=(PersistentModelIndex&&) = default;
|
||||
|
||||
bool is_valid() const { return has_valid_handle() && m_handle->m_index.is_valid(); }
|
||||
bool has_valid_handle() const { return !m_handle.is_null(); }
|
||||
|
||||
int row() const;
|
||||
int column() const;
|
||||
PersistentModelIndex parent() const;
|
||||
PersistentModelIndex sibling_at_column(int column) const;
|
||||
Variant data(ModelRole = ModelRole::Display) const;
|
||||
|
||||
void* internal_data() const
|
||||
{
|
||||
if (has_valid_handle())
|
||||
return m_handle->m_index.internal_data();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
operator ModelIndex() const;
|
||||
bool operator==(PersistentModelIndex const&) const;
|
||||
bool operator!=(PersistentModelIndex const&) const;
|
||||
bool operator==(ModelIndex const&) const;
|
||||
bool operator!=(ModelIndex const&) const;
|
||||
|
||||
private:
|
||||
friend AK::Traits<GUI::PersistentModelIndex>;
|
||||
|
||||
WeakPtr<PersistentHandle> m_handle;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<>
|
||||
struct Formatter<GUI::PersistentModelIndex> : Formatter<FormatString> {
|
||||
void format(FormatBuilder& builder, const GUI::PersistentModelIndex& value)
|
||||
{
|
||||
return Formatter<FormatString>::format(builder, "PersistentModelIndex({},{},{})", value.row(), value.column(), value.internal_data());
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<GUI::PersistentModelIndex> : public GenericTraits<GUI::PersistentModelIndex> {
|
||||
static unsigned hash(const GUI::PersistentModelIndex& index)
|
||||
{
|
||||
if (index.has_valid_handle())
|
||||
return Traits<GUI::ModelIndex>::hash(index.m_handle->m_index);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue