LayoutNode.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef RMLUI_CORE_LAYOUT_LAYOUTNODE_H
  29. #define RMLUI_CORE_LAYOUT_LAYOUTNODE_H
  30. #include "../../../Include/RmlUi/Core/Box.h"
  31. #include "../../../Include/RmlUi/Core/Element.h"
  32. #include "../../../Include/RmlUi/Core/Types.h"
  33. namespace Rml {
  34. enum class DirtyLayoutType : uint8_t {
  35. None = 0,
  36. DOM = 1 << 0,
  37. PropertyForcingLayout = 1 << 1,
  38. TableAttribute = 1 << 2,
  39. ReplacedElement = 1 << 3,
  40. IntrinsicSize = 1 << 4, // Closely tied to ReplacedElement, combine them?
  41. Text = 1 << 5,
  42. Child = 1 << 6,
  43. };
  44. inline DirtyLayoutType operator|(DirtyLayoutType lhs, DirtyLayoutType rhs)
  45. {
  46. using T = std::underlying_type_t<DirtyLayoutType>;
  47. return DirtyLayoutType(T(lhs) | T(rhs));
  48. }
  49. inline DirtyLayoutType operator&(DirtyLayoutType lhs, DirtyLayoutType rhs)
  50. {
  51. using T = std::underlying_type_t<DirtyLayoutType>;
  52. return DirtyLayoutType(T(lhs) & T(rhs));
  53. }
  54. struct CommittedLayout {
  55. Vector2f containing_block_size;
  56. Vector2f absolutely_positioning_containing_block_size;
  57. Optional<Box> override_box;
  58. Vector2f visible_overflow_size;
  59. Optional<float> baseline_of_last_line;
  60. };
  61. /*
  62. A LayoutNode TODO
  63. */
  64. class LayoutNode {
  65. public:
  66. enum class Type { Undefined, Root, BlockContainer, InlineContainer, FlexContainer, TableWrapper, Replaced };
  67. LayoutNode(Element* element) : element(element) {}
  68. // Assumes valid computed values.
  69. void DirtyUpToClosestLayoutBoundary();
  70. void ClearDirty();
  71. void SetDirty(DirtyLayoutType dirty_type);
  72. bool IsChildDirty() const { return (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None; }
  73. bool IsDirty() const { return dirty_flag != DirtyLayoutType::None; }
  74. bool IsSelfDirty() const { return !(dirty_flag == DirtyLayoutType::None || dirty_flag == DirtyLayoutType::Child); }
  75. void CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  76. Vector2f visible_overflow_size, Optional<float> baseline_of_last_line);
  77. // TODO: Currently, should only be used by `Context::SetDimensions`. Ideally, we would remove this and "commit" the
  78. // containing block (without the layout result, see comment in `CommitLayout`).
  79. void ClearCommittedLayout()
  80. {
  81. committed_layout.reset();
  82. committed_max_content_width.reset();
  83. }
  84. bool CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box) const
  85. {
  86. if (IsDirty())
  87. return false;
  88. if (!committed_layout.has_value())
  89. return false;
  90. if (committed_layout->containing_block_size != containing_block_size ||
  91. committed_layout->absolutely_positioning_containing_block_size != absolutely_positioning_containing_block_size)
  92. return false;
  93. if (!override_box)
  94. return !committed_layout->override_box.has_value();
  95. const Box& compare_box = committed_layout->override_box.has_value() ? *committed_layout->override_box : element->GetBox();
  96. if (!override_box->EqualAreaEdges(*committed_layout->override_box))
  97. return false;
  98. if (override_box->GetSize() == compare_box.GetSize())
  99. return true;
  100. // Lastly, if we have an indefinite size on the committed box, see if the layed-out size matches the input override box.
  101. Vector2f compare_size = compare_box.GetSize();
  102. if (compare_size.x < 0.f)
  103. compare_size.x = element->GetBox().GetSize().x;
  104. if (compare_size.y < 0.f)
  105. compare_size.y = element->GetBox().GetSize().y;
  106. return override_box->GetSize() == compare_size;
  107. }
  108. Optional<float> GetMaxContentWidthIfCached() const { return committed_max_content_width; }
  109. void CommitMaxContentWidth(float width) { committed_max_content_width = width; }
  110. Optional<float> GetMaxContentHeightIfCached() const { return committed_max_content_height; }
  111. void CommitMaxContentHeight(float height) { committed_max_content_height = height; }
  112. // TODO: Remove and replace with a better interface.
  113. const Optional<CommittedLayout>& GetCommittedLayout() const { return committed_layout; }
  114. // A.k.a. reflow root.
  115. bool IsLayoutBoundary() const;
  116. private:
  117. Element* element;
  118. Type type = Type::Undefined;
  119. DirtyLayoutType dirty_flag = DirtyLayoutType::None;
  120. // TODO: Cache this after running LayoutDetails::GetShrinkToFitWidth. Clear the cache whenever any child or self is dirtied.
  121. float max_content_size = 0;
  122. // Store this for partial layout updates / reflow. If this changes, always do layout again, regardless of any cache.
  123. Vector2f containing_block;
  124. Optional<CommittedLayout> committed_layout;
  125. Optional<float> committed_max_content_width;
  126. Optional<float> committed_max_content_height;
  127. };
  128. } // namespace Rml
  129. #endif