BoxLayout.cpp 8.4 KB

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