DataView.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "../../Include/RmlUi/Core/Profiling.h"
  31. #include <algorithm>
  32. namespace Rml {
  33. DataView::~DataView() {}
  34. Element* DataView::GetElement() const
  35. {
  36. Element* result = attached_element.get();
  37. if (!result)
  38. Log::Message(Log::LT_WARNING, "Could not retrieve element in view, was it destroyed?");
  39. return result;
  40. }
  41. int DataView::GetSortOrder() const
  42. {
  43. return sort_order;
  44. }
  45. bool DataView::IsValid() const
  46. {
  47. return static_cast<bool>(attached_element);
  48. }
  49. DataView::DataView(Element* element, int bias) : attached_element(element->GetObserverPtr()), sort_order(bias + 1000)
  50. {
  51. RMLUI_ASSERT(bias >= -1000 && bias <= 999);
  52. if (element)
  53. {
  54. for (Element* parent = element->GetParentNode(); parent; parent = parent->GetParentNode())
  55. sort_order += 2000;
  56. }
  57. }
  58. DataViews::DataViews() {}
  59. DataViews::~DataViews() {}
  60. void DataViews::Add(DataViewPtr view)
  61. {
  62. views_to_add.push_back(std::move(view));
  63. }
  64. void DataViews::OnElementRemove(Element* element)
  65. {
  66. for (auto it = views.begin(); it != views.end();)
  67. {
  68. auto& view = *it;
  69. if (view && view->GetElement() == element)
  70. {
  71. views_to_remove.push_back(std::move(view));
  72. it = views.erase(it);
  73. }
  74. else
  75. ++it;
  76. }
  77. }
  78. bool DataViews::Update(DataModel& model, const DirtyVariables& dirty_variables)
  79. {
  80. RMLUI_ZoneScoped;
  81. bool result = false;
  82. size_t num_dirty_variables_prev = 0;
  83. // View updates may result in newly added views, or even new dirty variables. Thus, we do the
  84. // update recursively but with an upper limit. Without the loop, newly added views won't be
  85. // updated until the next Update() call.
  86. for (int i = 0; (i == 0 || !views_to_add.empty() || num_dirty_variables_prev != dirty_variables.size()) && i < 10; i++)
  87. {
  88. num_dirty_variables_prev = dirty_variables.size();
  89. Vector<DataView*> dirty_views;
  90. if (!views_to_add.empty())
  91. {
  92. views.reserve(views.size() + views_to_add.size());
  93. for (auto&& view : views_to_add)
  94. {
  95. dirty_views.push_back(view.get());
  96. for (const String& variable_name : view->GetVariableNameList())
  97. name_view_map.emplace(variable_name, view.get());
  98. views.push_back(std::move(view));
  99. }
  100. views_to_add.clear();
  101. }
  102. for (const String& variable_name : dirty_variables)
  103. {
  104. auto pair = name_view_map.equal_range(variable_name);
  105. for (auto it = pair.first; it != pair.second; ++it)
  106. dirty_views.push_back(it->second);
  107. }
  108. // Remove duplicate entries
  109. std::sort(dirty_views.begin(), dirty_views.end());
  110. auto it_remove = std::unique(dirty_views.begin(), dirty_views.end());
  111. dirty_views.erase(it_remove, dirty_views.end());
  112. // 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
  113. // children. Eg. the 'data-for' view will remove children if any of its data variable array size is reduced.
  114. std::sort(dirty_views.begin(), dirty_views.end(), [](auto&& left, auto&& right) { return left->GetSortOrder() < right->GetSortOrder(); });
  115. for (DataView* view : dirty_views)
  116. {
  117. RMLUI_ASSERT(view);
  118. if (!view)
  119. continue;
  120. if (view->IsValid())
  121. {
  122. RMLUI_ZoneScopedN("DataView::Update");
  123. result |= view->Update(model);
  124. }
  125. }
  126. // Destroy views marked for destruction
  127. // @performance: Horrible...
  128. if (!views_to_remove.empty())
  129. {
  130. for (const auto& view : views_to_remove)
  131. {
  132. for (auto it = name_view_map.begin(); it != name_view_map.end();)
  133. {
  134. if (it->second == view.get())
  135. it = name_view_map.erase(it);
  136. else
  137. ++it;
  138. }
  139. }
  140. views_to_remove.clear();
  141. }
  142. }
  143. return result;
  144. }
  145. } // namespace Rml