DataView.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "DataView.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include <algorithm>
  31. namespace Rml {
  32. DataView::~DataView() {}
  33. Element* DataView::GetElement() const
  34. {
  35. Element* result = attached_element.get();
  36. if (!result)
  37. Log::Message(Log::LT_WARNING, "Could not retrieve element in view, was it destroyed?");
  38. return result;
  39. }
  40. int DataView::GetSortOrder() const
  41. {
  42. return sort_order;
  43. }
  44. bool DataView::IsValid() const
  45. {
  46. return static_cast<bool>(attached_element);
  47. }
  48. DataView::DataView(Element* element, int bias) : attached_element(element->GetObserverPtr()), sort_order(bias + 1000)
  49. {
  50. RMLUI_ASSERT(bias >= -1000 && bias <= 999);
  51. if (element)
  52. {
  53. for (Element* parent = element->GetParentNode(); parent; parent = parent->GetParentNode())
  54. sort_order += 2000;
  55. }
  56. }
  57. DataViews::DataViews() {}
  58. DataViews::~DataViews() {}
  59. void DataViews::Add(DataViewPtr view)
  60. {
  61. views_to_add.push_back(std::move(view));
  62. }
  63. void DataViews::OnElementRemove(Element* element)
  64. {
  65. for (auto it = views.begin(); it != views.end();)
  66. {
  67. auto& view = *it;
  68. if (view && view->GetElement() == element)
  69. {
  70. views_to_remove.push_back(std::move(view));
  71. it = views.erase(it);
  72. }
  73. else
  74. ++it;
  75. }
  76. }
  77. bool DataViews::Update(DataModel& model, const DirtyVariables& dirty_variables)
  78. {
  79. bool result = false;
  80. size_t num_dirty_variables_prev = 0;
  81. // View updates may result in newly added views, or even new dirty variables. Thus, we do the
  82. // update recursively but with an upper limit. Without the loop, newly added views won't be
  83. // updated until the next Update() call.
  84. for (int i = 0; (i == 0 || !views_to_add.empty() || num_dirty_variables_prev != dirty_variables.size()) && i < 10; i++)
  85. {
  86. num_dirty_variables_prev = dirty_variables.size();
  87. Vector<DataView*> dirty_views;
  88. if (!views_to_add.empty())
  89. {
  90. views.reserve(views.size() + views_to_add.size());
  91. for (auto&& view : views_to_add)
  92. {
  93. dirty_views.push_back(view.get());
  94. for (const String& variable_name : view->GetVariableNameList())
  95. name_view_map.emplace(variable_name, view.get());
  96. views.push_back(std::move(view));
  97. }
  98. views_to_add.clear();
  99. }
  100. for (const String& variable_name : dirty_variables)
  101. {
  102. auto pair = name_view_map.equal_range(variable_name);
  103. for (auto it = pair.first; it != pair.second; ++it)
  104. dirty_views.push_back(it->second);
  105. }
  106. // Remove duplicate entries
  107. std::sort(dirty_views.begin(), dirty_views.end());
  108. auto it_remove = std::unique(dirty_views.begin(), dirty_views.end());
  109. dirty_views.erase(it_remove, dirty_views.end());
  110. // Sort by the element's depth in the document tree so that any structural changes due to a changed variable are reflected in the element's
  111. // children. Eg. the 'data-for' view will remove children if any of its data variable array size is reduced.
  112. std::sort(dirty_views.begin(), dirty_views.end(), [](auto&& left, auto&& right) { return left->GetSortOrder() < right->GetSortOrder(); });
  113. for (DataView* view : dirty_views)
  114. {
  115. RMLUI_ASSERT(view);
  116. if (!view)
  117. continue;
  118. if (view->IsValid())
  119. result |= view->Update(model);
  120. }
  121. // Destroy views marked for destruction
  122. // @performance: Horrible...
  123. if (!views_to_remove.empty())
  124. {
  125. for (const auto& view : views_to_remove)
  126. {
  127. for (auto it = name_view_map.begin(); it != name_view_map.end();)
  128. {
  129. if (it->second == view.get())
  130. it = name_view_map.erase(it);
  131. else
  132. ++it;
  133. }
  134. }
  135. views_to_remove.clear();
  136. }
  137. }
  138. return result;
  139. }
  140. } // namespace Rml