LayoutNode.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. bool layout_constraint;
  59. Vector2f visible_overflow_size;
  60. Optional<float> baseline_of_last_line;
  61. };
  62. /*
  63. A LayoutNode TODO
  64. */
  65. class LayoutNode {
  66. public:
  67. enum class Type { Undefined, Root, BlockContainer, InlineContainer, FlexContainer, TableWrapper, Replaced };
  68. LayoutNode(Element* element) : element(element) {}
  69. // Assumes valid computed values.
  70. void PropagateDirtyToParent();
  71. void ClearDirty();
  72. void SetDirty(DirtyLayoutType dirty_type);
  73. bool IsChildDirty() const { return (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None; }
  74. bool IsDirty() const { return dirty_flag != DirtyLayoutType::None; }
  75. bool IsSelfDirty() const { return !(dirty_flag == DirtyLayoutType::None || dirty_flag == DirtyLayoutType::Child); }
  76. void CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  77. bool layout_constraint, Vector2f visible_overflow_size, Optional<float> baseline_of_last_line);
  78. // TODO: Currently, should only be used by `Context::SetDimensions`. Ideally, we would remove this and "commit" the
  79. // containing block (without the layout result, see comment in `CommitLayout`).
  80. void ClearCommittedLayout()
  81. {
  82. committed_layout.reset();
  83. committed_max_content_width.reset();
  84. }
  85. bool CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  86. bool layout_constraint) const
  87. {
  88. if (IsDirty())
  89. return false;
  90. if (!committed_layout.has_value())
  91. return false;
  92. if (committed_layout->containing_block_size != containing_block_size ||
  93. committed_layout->absolutely_positioning_containing_block_size != absolutely_positioning_containing_block_size)
  94. return false;
  95. // Layout under a constraint may make some simplifications that requires re-evaluation under a normal formatting mode.
  96. if (committed_layout->layout_constraint && !layout_constraint)
  97. return false;
  98. if (!override_box)
  99. return !committed_layout->override_box.has_value();
  100. const Box& compare_box = committed_layout->override_box.has_value() ? *committed_layout->override_box : element->GetBox();
  101. // In some situations, if we have an indefinite size on the committed box, we could see if the layed-out size
  102. // matches the input override box and use the cache here. However, because of cyclic-percentage rules with
  103. // containing block sizes, this is only correct in certain situations, particularly when the vertical size of
  104. // the containing block is indefinite. In this case the used containing block size should resolve to indefinite,
  105. // even after it is sized. Although, we don't actually implement this behavior for now, but once we do, we could
  106. // implement caching here in this case. For the horizontal axis, we are always required to re-evaluate any
  107. // children for which this box acts as a containing block for, thus we cannot use a cache mechanism here. See:
  108. // https://drafts.csswg.org/css-sizing/#cyclic-percentage-contribution
  109. return *override_box == compare_box;
  110. }
  111. bool HasCommittedLayout() const { return committed_layout.has_value(); }
  112. Optional<float> GetMaxContentWidthIfCached() const { return committed_max_content_width; }
  113. void CommitMaxContentWidth(float width) { committed_max_content_width = width; }
  114. Optional<float> GetMaxContentHeightIfCached() const { return committed_max_content_height; }
  115. void CommitMaxContentHeight(float height) { committed_max_content_height = height; }
  116. // TODO: Remove and replace with a better interface.
  117. const Optional<CommittedLayout>& GetCommittedLayout() const { return committed_layout; }
  118. // A.k.a. reflow root.
  119. bool IsLayoutBoundary() const;
  120. private:
  121. Element* element;
  122. Type type = Type::Undefined;
  123. DirtyLayoutType dirty_flag = DirtyLayoutType::None;
  124. // TODO: Cache this after running LayoutDetails::GetShrinkToFitWidth. Clear the cache whenever any child or self is dirtied.
  125. float max_content_size = 0;
  126. // Store this for partial layout updates / reflow. If this changes, always do layout again, regardless of any cache.
  127. Vector2f containing_block;
  128. Optional<CommittedLayout> committed_layout;
  129. Optional<float> committed_max_content_width;
  130. Optional<float> committed_max_content_height;
  131. };
  132. } // namespace Rml
  133. #endif