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