BoxLayout.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonObject.h>
  27. #include <LibGUI/BoxLayout.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGfx/Orientation.h>
  30. #include <stdio.h>
  31. REGISTER_WIDGET(GUI, HorizontalBoxLayout)
  32. REGISTER_WIDGET(GUI, VerticalBoxLayout)
  33. namespace GUI {
  34. BoxLayout::BoxLayout(Orientation orientation)
  35. : m_orientation(orientation)
  36. {
  37. register_property(
  38. "orientation", [this] { return m_orientation == Gfx::Orientation::Vertical ? "Vertical" : "Horizontal"; }, nullptr);
  39. }
  40. Gfx::IntSize BoxLayout::preferred_size() const
  41. {
  42. Gfx::IntSize size;
  43. size.set_primary_size_for_orientation(orientation(), preferred_primary_size());
  44. size.set_secondary_size_for_orientation(orientation(), preferred_secondary_size());
  45. return size;
  46. }
  47. int BoxLayout::preferred_primary_size() const
  48. {
  49. int size = 0;
  50. for (auto& entry : m_entries) {
  51. if (!entry.widget || !entry.widget->is_visible())
  52. continue;
  53. int min_size = entry.widget->min_size().primary_size_for_orientation(orientation());
  54. int max_size = entry.widget->max_size().primary_size_for_orientation(orientation());
  55. int preferred_primary_size = -1;
  56. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  57. preferred_primary_size = entry.widget->layout()->preferred_size().primary_size_for_orientation(orientation());
  58. }
  59. int item_size = max(0, preferred_primary_size);
  60. item_size = max(min_size, item_size);
  61. item_size = min(max_size, item_size);
  62. size += item_size + spacing();
  63. }
  64. if (size > 0)
  65. size -= spacing();
  66. if (orientation() == Gfx::Orientation::Horizontal)
  67. size += margins().left() + margins().right();
  68. else
  69. size += margins().top() + margins().bottom();
  70. if (!size)
  71. return -1;
  72. return size;
  73. }
  74. int BoxLayout::preferred_secondary_size() const
  75. {
  76. int size = 0;
  77. for (auto& entry : m_entries) {
  78. if (!entry.widget || !entry.widget->is_visible())
  79. continue;
  80. int min_size = entry.widget->min_size().secondary_size_for_orientation(orientation());
  81. int preferred_secondary_size = -1;
  82. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  83. preferred_secondary_size = entry.widget->layout()->preferred_size().secondary_size_for_orientation(orientation());
  84. size = max(size, preferred_secondary_size);
  85. }
  86. size = max(min_size, size);
  87. }
  88. if (orientation() == Gfx::Orientation::Horizontal)
  89. size += margins().top() + margins().bottom();
  90. else
  91. size += margins().left() + margins().right();
  92. if (!size)
  93. return -1;
  94. return size;
  95. }
  96. void BoxLayout::run(Widget& widget)
  97. {
  98. if (m_entries.is_empty())
  99. return;
  100. struct Item {
  101. Widget* widget { nullptr };
  102. int min_size { -1 };
  103. int max_size { -1 };
  104. int size { 0 };
  105. bool final { false };
  106. };
  107. Vector<Item, 32> items;
  108. for (size_t i = 0; i < m_entries.size(); ++i) {
  109. auto& entry = m_entries[i];
  110. if (entry.type == Entry::Type::Spacer) {
  111. items.append(Item { nullptr, -1, -1 });
  112. continue;
  113. }
  114. if (!entry.widget)
  115. continue;
  116. if (!entry.widget->is_visible())
  117. continue;
  118. auto min_size = entry.widget->min_size();
  119. auto max_size = entry.widget->max_size();
  120. if (entry.widget->is_shrink_to_fit() && entry.widget->layout()) {
  121. auto preferred_size = entry.widget->layout()->preferred_size();
  122. min_size = max_size = preferred_size;
  123. }
  124. items.append(Item { entry.widget.ptr(), min_size.primary_size_for_orientation(orientation()), max_size.primary_size_for_orientation(orientation()) });
  125. }
  126. if (items.is_empty())
  127. return;
  128. int available_size = widget.size().primary_size_for_orientation(orientation()) - spacing() * (items.size() - 1);
  129. int unfinished_items = items.size();
  130. if (orientation() == Gfx::Orientation::Horizontal)
  131. available_size -= margins().left() + margins().right();
  132. else
  133. available_size -= margins().top() + margins().bottom();
  134. // Pass 1: Set all items to their minimum size.
  135. for (auto& item : items) {
  136. item.size = 0;
  137. if (item.min_size >= 0)
  138. item.size = item.min_size;
  139. available_size -= item.size;
  140. if (item.min_size >= 0 && item.max_size >= 0 && item.min_size == item.max_size) {
  141. // Fixed-size items finish immediately in the first pass.
  142. item.final = true;
  143. --unfinished_items;
  144. }
  145. }
  146. // Pass 2: Distribute remaining available size evenly, respecting each item's maximum size.
  147. while (unfinished_items && available_size > 0) {
  148. int slice = available_size / unfinished_items;
  149. available_size = 0;
  150. for (auto& item : items) {
  151. if (item.final)
  152. continue;
  153. int item_size_with_full_slice = item.size + slice;
  154. item.size = item_size_with_full_slice;
  155. if (item.max_size >= 0)
  156. item.size = min(item.max_size, item_size_with_full_slice);
  157. // If the slice was more than we needed, return remained to available_size.
  158. int remainder_to_give_back = item_size_with_full_slice - item.size;
  159. available_size += remainder_to_give_back;
  160. if (item.max_size >= 0 && item.size == item.max_size) {
  161. // We've hit the item's max size. Don't give it any more space.
  162. item.final = true;
  163. --unfinished_items;
  164. }
  165. }
  166. }
  167. // Pass 3: Place the widgets.
  168. int current_x = margins().left();
  169. int current_y = margins().top();
  170. for (auto& item : items) {
  171. Gfx::IntRect rect { current_x, current_y, 0, 0 };
  172. rect.set_primary_size_for_orientation(orientation(), item.size);
  173. if (item.widget) {
  174. int secondary = widget.size().secondary_size_for_orientation(orientation());
  175. if (orientation() == Gfx::Orientation::Horizontal)
  176. secondary -= margins().top() + margins().bottom();
  177. else
  178. secondary -= margins().left() + margins().right();
  179. int min_secondary = item.widget->min_size().secondary_size_for_orientation(orientation());
  180. int max_secondary = item.widget->max_size().secondary_size_for_orientation(orientation());
  181. if (min_secondary >= 0)
  182. secondary = max(secondary, min_secondary);
  183. if (max_secondary >= 0)
  184. secondary = min(secondary, max_secondary);
  185. rect.set_secondary_size_for_orientation(orientation(), secondary);
  186. if (orientation() == Gfx::Orientation::Horizontal)
  187. rect.center_vertically_within(widget.rect());
  188. else
  189. rect.center_horizontally_within(widget.rect());
  190. item.widget->set_relative_rect(rect);
  191. }
  192. if (orientation() == Gfx::Orientation::Horizontal)
  193. current_x += rect.width() + spacing();
  194. else
  195. current_y += rect.height() + spacing();
  196. }
  197. }
  198. }