BoxLayout.cpp 8.3 KB

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