BoxLayout.cpp 7.9 KB

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