AriaPropertiesStateModel.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "AriaPropertiesStateModel.h"
  7. namespace WebView {
  8. AriaPropertiesStateModel::AriaPropertiesStateModel(JsonObject properties_state)
  9. : m_properties_state(move(properties_state))
  10. {
  11. m_properties_state.for_each_member([&](auto property_name, JsonValue const& values) {
  12. Value value;
  13. value.name = property_name;
  14. value.value = "";
  15. m_values.append(value);
  16. values.as_object().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. }
  24. AriaPropertiesStateModel::~AriaPropertiesStateModel() = default;
  25. int AriaPropertiesStateModel::row_count(GUI::ModelIndex const&) const
  26. {
  27. return m_values.size();
  28. }
  29. ErrorOr<String> AriaPropertiesStateModel::column_name(int column_index) const
  30. {
  31. switch (column_index) {
  32. case Column::PropertyName:
  33. return "Name"_short_string;
  34. case Column::PropertyValue:
  35. return "Value"_short_string;
  36. default:
  37. return Error::from_string_view("Unexpected column index"sv);
  38. }
  39. }
  40. GUI::Variant AriaPropertiesStateModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  41. {
  42. auto& value = m_values[index.row()];
  43. if (role == GUI::ModelRole::Display) {
  44. if (index.column() == Column::PropertyName)
  45. return value.name;
  46. if (index.column() == Column::PropertyValue)
  47. return value.value;
  48. }
  49. return {};
  50. }
  51. Vector<GUI::ModelIndex> AriaPropertiesStateModel::matches(AK::StringView searching, unsigned int flags, GUI::ModelIndex const& parent)
  52. {
  53. if (m_values.is_empty())
  54. return {};
  55. Vector<GUI::ModelIndex> found_indices;
  56. for (auto it = m_values.begin(); !it.is_end(); ++it) {
  57. GUI::ModelIndex index = this->index(it.index(), Column::PropertyName, parent);
  58. if (!string_matches(data(index, GUI::ModelRole::Display).as_string(), searching, flags))
  59. continue;
  60. found_indices.append(index);
  61. if (flags & FirstMatchOnly)
  62. break;
  63. }
  64. return found_indices;
  65. }
  66. }