Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include "StylePropertiesModel.h"
9#include <AK/QuickSort.h>
10
11namespace WebView {
12
13StylePropertiesModel::StylePropertiesModel(JsonObject properties)
14 : m_properties(move(properties))
15{
16 m_properties.for_each_member([&](auto& property_name, auto& property_value) {
17 Value value;
18 value.name = property_name;
19 value.value = property_value.to_deprecated_string();
20 m_values.append(value);
21 });
22
23 quick_sort(m_values, [](auto& a, auto& b) { return a.name < b.name; });
24}
25
26StylePropertiesModel::~StylePropertiesModel() = default;
27
28int StylePropertiesModel::row_count(GUI::ModelIndex const&) const
29{
30 return m_values.size();
31}
32
33DeprecatedString StylePropertiesModel::column_name(int column_index) const
34{
35 switch (column_index) {
36 case Column::PropertyName:
37 return "Name";
38 case Column::PropertyValue:
39 return "Value";
40 default:
41 VERIFY_NOT_REACHED();
42 }
43}
44
45GUI::Variant StylePropertiesModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
46{
47 auto& value = m_values[index.row()];
48 if (role == GUI::ModelRole::Display) {
49 if (index.column() == Column::PropertyName)
50 return value.name;
51 if (index.column() == Column::PropertyValue)
52 return value.value;
53 }
54 return {};
55}
56
57Vector<GUI::ModelIndex> StylePropertiesModel::matches(StringView searching, unsigned flags, GUI::ModelIndex const& parent)
58{
59 if (m_values.is_empty())
60 return {};
61 Vector<GUI::ModelIndex> found_indices;
62 for (auto it = m_values.begin(); !it.is_end(); ++it) {
63 GUI::ModelIndex index = this->index(it.index(), Column::PropertyName, parent);
64 if (!string_matches(data(index, GUI::ModelRole::Display).as_string(), searching, flags))
65 continue;
66
67 found_indices.append(index);
68 if (flags & FirstMatchOnly)
69 break;
70 }
71 return found_indices;
72}
73
74}