ladybird/Userland/Libraries/LibWebView/PropertyTableModel.h
Timothy Flynn 4d356cfca5 LibWebView: Create a PropertyTableModel helper class for table models
We currently have StylePropertiesModel and AriaPropertiesStateModel in
LibWebView. These depend on GUI::Model and GUI::ModelIndex, which is the
only reason that the non-Serenity Ladybird chromes require LibGUI.
Further, these classes are very nearly idenitical.

This creates a PropertyTableModel to provide the base functionality for
all table-based model types used by all Ladybird chromes. It contains
code common to the style / ARIA table models, and handles the slight
differences between the two (namely, just the manner in which the values
are flattened into a list during construction).

The Qt and Serenity chromes can create thin wrappers around this class
to adapt its interface to their chrome-specific model classes (i.e.
QAbstractItemModel and GUI::Model).
2023-11-05 09:11:23 +01:00

60 lines
1.5 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/IterationDecision.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibWebView/ModelIndex.h>
namespace WebView {
class PropertyTableModel {
public:
enum class Type {
ARIAProperties,
StyleProperties,
};
enum class Column : int {
PropertyName,
PropertyValue,
};
PropertyTableModel(Type, JsonValue const&);
~PropertyTableModel();
template<typename Callback>
void for_each_property_name(Callback&& callback)
{
for (size_t i = 0; i < m_values.size(); ++i) {
ModelIndex index { static_cast<int>(i), to_underlying(WebView::PropertyTableModel::Column::PropertyName) };
auto const& property_name = m_values[i].name;
if (callback(index, property_name) == IterationDecision::Break)
break;
}
}
int row_count(ModelIndex const& parent) const;
int column_count(ModelIndex const& parent) const;
ErrorOr<String> column_name(int) const;
ModelIndex index(int row, int column, ModelIndex const& parent) const;
String text_for_display(ModelIndex const& index) const;
private:
struct Value {
String name;
String value;
};
Vector<Value> m_values;
};
}