BoxLayout.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Widget.h>
  9. #include <LibGfx/Orientation.h>
  10. #include <stdio.h>
  11. REGISTER_CORE_OBJECT(GUI, HorizontalBoxLayout)
  12. REGISTER_CORE_OBJECT(GUI, VerticalBoxLayout)
  13. namespace GUI {
  14. BoxLayout::BoxLayout(Orientation orientation)
  15. : m_orientation(orientation)
  16. {
  17. register_property(
  18. "orientation", [this] { return m_orientation == Gfx::Orientation::Vertical ? "Vertical" : "Horizontal"; }, nullptr);
  19. }
  20. Gfx::IntSize BoxLayout::preferred_size() const
  21. {
  22. Gfx::IntSize size;
  23. size.set_primary_size_for_orientation(orientation(), preferred_primary_size());
  24. size.set_secondary_size_for_orientation(orientation(), preferred_secondary_size());
  25. return size;
  26. }
  27. int BoxLayout::preferred_primary_size() const
  28. {
  29. int size = 0;
  30. for (auto& entry : m_entries) {
  31. if (!entry.widget || !entry.widget->is_visible())
  32. continue;
  33. int min_size = entry.widget->min_size().primary_size_for_orientation(orientation());
  34. int max_size = entry.widget->max_size().primary_size_for_orientation(orientation());
  35. int preferred_primary_size = -1;
  36. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  37. preferred_primary_size = entry.widget->layout()->preferred_size().primary_size_for_orientation(orientation());
  38. }
  39. int item_size = max(0, preferred_primary_size);
  40. item_size = max(min_size, item_size);
  41. item_size = min(max_size, item_size);
  42. size += item_size + spacing();
  43. }
  44. if (size > 0)
  45. size -= spacing();
  46. if (orientation() == Gfx::Orientation::Horizontal)
  47. size += margins().left() + margins().right();
  48. else
  49. size += margins().top() + margins().bottom();
  50. if (!size)
  51. return -1;
  52. return size;
  53. }
  54. int BoxLayout::preferred_secondary_size() const
  55. {
  56. int size = 0;
  57. for (auto& entry : m_entries) {
  58. if (!entry.widget || !entry.widget->is_visible())
  59. continue;
  60. int min_size = entry.widget->min_size().secondary_size_for_orientation(orientation());
  61. int preferred_secondary_size = -1;
  62. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  63. preferred_secondary_size = entry.widget->layout()->preferred_size().secondary_size_for_orientation(orientation());
  64. size = max(size, preferred_secondary_size);
  65. }
  66. size = max(min_size, size);
  67. }
  68. if (orientation() == Gfx::Orientation::Horizontal)
  69. size += margins().top() + margins().bottom();
  70. else
  71. size += margins().left() + margins().right();
  72. if (!size)
  73. return -1;
  74. return size;
  75. }
  76. void BoxLayout::run(Widget& widget)
  77. {
  78. if (m_entries.is_empty())
  79. return;
  80. struct Item {
  81. Widget* widget { nullptr };
  82. int min_size { -1 };
  83. int max_size { -1 };
  84. int size { 0 };
  85. bool final { false };
  86. };
  87. Vector<Item, 32> items;
  88. for (size_t i = 0; i < m_entries.size(); ++i) {
  89. auto& entry = m_entries[i];
  90. if (entry.type == Entry::Type::Spacer) {
  91. items.append(Item { nullptr, -1, -1 });
  92. continue;
  93. }
  94. if (!entry.widget)
  95. continue;
  96. if (!entry.widget->is_visible())
  97. continue;
  98. auto min_size = entry.widget->min_size();
  99. auto max_size = entry.widget->max_size();
  100. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  101. auto preferred_size = entry.widget->layout()->preferred_size();
  102. min_size = max_size = preferred_size;
  103. }
  104. items.append(Item { entry.widget.ptr(), min_size.primary_size_for_orientation(orientation()), max_size.primary_size_for_orientation(orientation()) });
  105. }
  106. if (items.is_empty())
  107. return;
  108. int available_size = widget.size().primary_size_for_orientation(orientation()) - spacing() * (items.size() - 1);
  109. int unfinished_items = items.size();
  110. if (orientation() == Gfx::Orientation::Horizontal)
  111. available_size -= margins().left() + margins().right();
  112. else
  113. available_size -= margins().top() + margins().bottom();
  114. // Pass 1: Set all items to their minimum size.
  115. for (auto& item : items) {
  116. item.size = 0;
  117. if (item.min_size >= 0)
  118. item.size = item.min_size;
  119. available_size -= item.size;
  120. if (item.min_size >= 0 && item.max_size >= 0 && item.min_size == item.max_size) {
  121. // Fixed-size items finish immediately in the first pass.
  122. item.final = true;
  123. --unfinished_items;
  124. }
  125. }
  126. // Pass 2: Distribute remaining available size evenly, respecting each item's maximum size.
  127. while (unfinished_items && available_size > 0) {
  128. int slice = available_size / unfinished_items;
  129. available_size = 0;
  130. for (auto& item : items) {
  131. if (item.final)
  132. continue;
  133. int item_size_with_full_slice = item.size + slice;
  134. item.size = item_size_with_full_slice;
  135. if (item.max_size >= 0)
  136. item.size = min(item.max_size, item_size_with_full_slice);
  137. // If the slice was more than we needed, return remained to available_size.
  138. int remainder_to_give_back = item_size_with_full_slice - item.size;
  139. available_size += remainder_to_give_back;
  140. if (item.max_size >= 0 && item.size == item.max_size) {
  141. // We've hit the item's max size. Don't give it any more space.
  142. item.final = true;
  143. --unfinished_items;
  144. }
  145. }
  146. }
  147. // Pass 3: Place the widgets.
  148. int current_x = margins().left();
  149. int current_y = margins().top();
  150. auto widget_rect_with_margins_subtracted = widget.rect();
  151. widget_rect_with_margins_subtracted.take_from_left(margins().left());
  152. widget_rect_with_margins_subtracted.take_from_top(margins().top());
  153. widget_rect_with_margins_subtracted.take_from_right(margins().right());
  154. widget_rect_with_margins_subtracted.take_from_bottom(margins().bottom());
  155. for (auto& item : items) {
  156. Gfx::IntRect rect { current_x, current_y, 0, 0 };
  157. rect.set_primary_size_for_orientation(orientation(), item.size);
  158. if (item.widget) {
  159. int secondary = widget.size().secondary_size_for_orientation(orientation());
  160. if (orientation() == Gfx::Orientation::Horizontal)
  161. secondary -= margins().top() + margins().bottom();
  162. else
  163. secondary -= margins().left() + margins().right();
  164. int min_secondary = item.widget->min_size().secondary_size_for_orientation(orientation());
  165. int max_secondary = item.widget->max_size().secondary_size_for_orientation(orientation());
  166. if (min_secondary >= 0)
  167. secondary = max(secondary, min_secondary);
  168. if (max_secondary >= 0)
  169. secondary = min(secondary, max_secondary);
  170. rect.set_secondary_size_for_orientation(orientation(), secondary);
  171. if (orientation() == Gfx::Orientation::Horizontal)
  172. rect.center_vertically_within(widget_rect_with_margins_subtracted);
  173. else
  174. rect.center_horizontally_within(widget_rect_with_margins_subtracted);
  175. item.widget->set_relative_rect(rect);
  176. }
  177. if (orientation() == Gfx::Orientation::Horizontal)
  178. current_x += rect.width() + spacing();
  179. else
  180. current_y += rect.height() + spacing();
  181. }
  182. }
  183. }