BoxLayout.cpp 8.4 KB

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