BoxLayout.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_LAYOUT(GUI, HorizontalBoxLayout)
  12. REGISTER_LAYOUT(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. // If available_size does not divide evenly by unfinished_items,
  130. // there are some extra pixels that have to be distributed.
  131. int pixels = available_size - slice * unfinished_items;
  132. available_size = 0;
  133. for (auto& item : items) {
  134. if (item.final)
  135. continue;
  136. int pixel = pixels ? 1 : 0;
  137. pixels -= pixel;
  138. int item_size_with_full_slice = item.size + slice + pixel;
  139. item.size = item_size_with_full_slice;
  140. if (item.max_size >= 0)
  141. item.size = min(item.max_size, item_size_with_full_slice);
  142. // If the slice was more than we needed, return remained to available_size.
  143. int remainder_to_give_back = item_size_with_full_slice - item.size;
  144. available_size += remainder_to_give_back;
  145. if (item.max_size >= 0 && item.size == item.max_size) {
  146. // We've hit the item's max size. Don't give it any more space.
  147. item.final = true;
  148. --unfinished_items;
  149. }
  150. }
  151. }
  152. // Pass 3: Place the widgets.
  153. int current_x = margins().left();
  154. int current_y = margins().top();
  155. auto widget_rect_with_margins_subtracted = widget.rect();
  156. widget_rect_with_margins_subtracted.take_from_left(margins().left());
  157. widget_rect_with_margins_subtracted.take_from_top(margins().top());
  158. widget_rect_with_margins_subtracted.take_from_right(margins().right());
  159. widget_rect_with_margins_subtracted.take_from_bottom(margins().bottom());
  160. for (auto& item : items) {
  161. Gfx::IntRect rect { current_x, current_y, 0, 0 };
  162. rect.set_primary_size_for_orientation(orientation(), item.size);
  163. if (item.widget) {
  164. int secondary = widget.size().secondary_size_for_orientation(orientation());
  165. if (orientation() == Gfx::Orientation::Horizontal)
  166. secondary -= margins().top() + margins().bottom();
  167. else
  168. secondary -= margins().left() + margins().right();
  169. int min_secondary = item.widget->min_size().secondary_size_for_orientation(orientation());
  170. int max_secondary = item.widget->max_size().secondary_size_for_orientation(orientation());
  171. if (min_secondary >= 0)
  172. secondary = max(secondary, min_secondary);
  173. if (max_secondary >= 0)
  174. secondary = min(secondary, max_secondary);
  175. rect.set_secondary_size_for_orientation(orientation(), secondary);
  176. if (orientation() == Gfx::Orientation::Horizontal)
  177. rect.center_vertically_within(widget_rect_with_margins_subtracted);
  178. else
  179. rect.center_horizontally_within(widget_rect_with_margins_subtracted);
  180. item.widget->set_relative_rect(rect);
  181. }
  182. if (orientation() == Gfx::Orientation::Horizontal)
  183. current_x += rect.width() + spacing();
  184. else
  185. current_y += rect.height() + spacing();
  186. }
  187. }
  188. }