LayoutNode.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/Types.h"
  32. namespace Rml {
  33. class Element;
  34. // Determines if the element, or its descendants, have been changed in such a way as to require a new layout pass.
  35. enum class DirtyLayoutType : uint8_t {
  36. None = 0,
  37. Self = 1 << 1, // This element has been modified such that the layout may have changed.
  38. Child = 1 << 2, // One or more descendent elements within the same layout boundary have been modified.
  39. };
  40. inline DirtyLayoutType operator|(DirtyLayoutType lhs, DirtyLayoutType rhs)
  41. {
  42. using T = std::underlying_type_t<DirtyLayoutType>;
  43. return DirtyLayoutType(T(lhs) | T(rhs));
  44. }
  45. inline DirtyLayoutType operator&(DirtyLayoutType lhs, DirtyLayoutType rhs)
  46. {
  47. using T = std::underlying_type_t<DirtyLayoutType>;
  48. return DirtyLayoutType(T(lhs) & T(rhs));
  49. }
  50. // The conditions and result of the last committed layout on the element.
  51. struct CommittedLayout {
  52. Vector2f containing_block_size;
  53. Vector2f absolutely_positioning_containing_block_size;
  54. Optional<Box> override_box;
  55. bool layout_constraint;
  56. Vector2f visible_overflow_size;
  57. float max_content_width;
  58. Optional<float> baseline_of_last_line;
  59. };
  60. /*
  61. A LayoutNode maintains the layout state and cache of an Element.
  62. All usage of this class assumes valid computed values, except for setting its dirty state which is always allowed.
  63. */
  64. class LayoutNode {
  65. public:
  66. LayoutNode(Element* element) : element(element) {}
  67. void SetDirty(DirtyLayoutType dirty_type);
  68. void ClearDirty();
  69. bool IsDirty() const { return dirty_flag != DirtyLayoutType::None; }
  70. bool IsChildDirty() const { return (dirty_flag & DirtyLayoutType::Child) != DirtyLayoutType::None; }
  71. bool IsSelfDirty() const { return (dirty_flag & DirtyLayoutType::Self) != DirtyLayoutType::None; }
  72. void PropagateDirtyToParent();
  73. void CommitLayout(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  74. bool layout_constraint, Vector2f visible_overflow_size, float max_content_width, Optional<float> baseline_of_last_line);
  75. // TODO: Currently, should only be used by `Context::SetDimensions`. Ideally, we would remove this and "commit" the
  76. // containing block (without the layout result, see comment in `CommitLayout`).
  77. void ClearCommittedLayout()
  78. {
  79. committed_layout.reset();
  80. committed_max_content_width.reset();
  81. committed_max_content_height.reset();
  82. }
  83. bool HasCommittedLayout() const { return committed_layout.has_value(); }
  84. bool CommittedLayoutMatches(Vector2f containing_block_size, Vector2f absolutely_positioning_containing_block_size, const Box* override_box,
  85. bool layout_constraint) const;
  86. Optional<float> GetCommittedMaxContentWidth() const { return committed_max_content_width; }
  87. void CommitMaxContentWidth(float width) { committed_max_content_width = width; }
  88. Optional<float> GetCommittedMaxContentHeight() const { return committed_max_content_height; }
  89. void CommitMaxContentHeight(float height) { committed_max_content_height = height; }
  90. // TODO: Remove and replace with a better interface.
  91. const Optional<CommittedLayout>& GetCommittedLayout() const { return committed_layout; }
  92. // TODO: Can we make it private?
  93. bool IsLayoutBoundary() const;
  94. private:
  95. Element* element;
  96. DirtyLayoutType dirty_flag = DirtyLayoutType::None;
  97. Optional<CommittedLayout> committed_layout;
  98. Optional<float> committed_max_content_width;
  99. Optional<float> committed_max_content_height;
  100. };
  101. } // namespace Rml
  102. #endif