DataView.cpp 4.5 KB

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