BoxLayout.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. namespace GUI {
  33. BoxLayout::BoxLayout(Orientation orientation)
  34. : m_orientation(orientation)
  35. {
  36. register_property(
  37. "orientation", [this] { return m_orientation == Gfx::Orientation::Vertical ? "Vertical" : "Horizontal"; }, nullptr);
  38. }
  39. void BoxLayout::run(Widget& widget)
  40. {
  41. if (m_entries.is_empty())
  42. return;
  43. struct Item {
  44. Widget* widget { nullptr };
  45. int min_size { -1 };
  46. int max_size { -1 };
  47. int size { 0 };
  48. bool final { false };
  49. };
  50. Vector<Item, 32> items;
  51. for (size_t i = 0; i < m_entries.size(); ++i) {
  52. auto& entry = m_entries[i];
  53. if (entry.type == Entry::Type::Spacer) {
  54. items.append(Item { nullptr, -1, -1 });
  55. continue;
  56. }
  57. if (!entry.widget)
  58. continue;
  59. if (!entry.widget->is_visible())
  60. continue;
  61. auto min_size = entry.widget->min_size();
  62. auto max_size = entry.widget->max_size();
  63. items.append(Item { entry.widget.ptr(), min_size.primary_size_for_orientation(orientation()), max_size.primary_size_for_orientation(orientation()) });
  64. }
  65. if (items.is_empty())
  66. return;
  67. int available_size = widget.size().primary_size_for_orientation(orientation()) - spacing() * (items.size() - 1);
  68. int unfinished_items = items.size();
  69. if (orientation() == Gfx::Orientation::Horizontal)
  70. available_size -= margins().left() + margins().right();
  71. else
  72. available_size -= margins().top() + margins().bottom();
  73. // Pass 1: Set all items to their minimum size.
  74. for (auto& item : items) {
  75. item.size = 0;
  76. if (item.min_size >= 0)
  77. item.size = item.min_size;
  78. available_size -= item.size;
  79. if (item.min_size >= 0 && item.max_size >= 0 && item.min_size == item.max_size) {
  80. // Fixed-size items finish immediately in the first pass.
  81. item.final = true;
  82. --unfinished_items;
  83. }
  84. }
  85. // Pass 2: Distribute remaining available size evenly, respecting each item's maximum size.
  86. while (unfinished_items && available_size > 0) {
  87. int slice = available_size / unfinished_items;
  88. available_size = 0;
  89. for (auto& item : items) {
  90. if (item.final)
  91. continue;
  92. int item_size_with_full_slice = item.size + slice;
  93. item.size = item_size_with_full_slice;
  94. if (item.max_size >= 0)
  95. item.size = min(item.max_size, item_size_with_full_slice);
  96. // If the slice was more than we needed, return remained to available_size.
  97. int remainder_to_give_back = item_size_with_full_slice - item.size;
  98. available_size += remainder_to_give_back;
  99. if (item.max_size >= 0 && item.size == item.max_size) {
  100. // We've hit the item's max size. Don't give it any more space.
  101. item.final = true;
  102. --unfinished_items;
  103. }
  104. }
  105. }
  106. // Pass 3: Place the widgets.
  107. int current_x = margins().left();
  108. int current_y = margins().top();
  109. for (auto& item : items) {
  110. Gfx::IntRect rect { current_x, current_y, 0, 0 };
  111. rect.set_primary_size_for_orientation(orientation(), item.size);
  112. if (item.widget) {
  113. int secondary = widget.size().secondary_size_for_orientation(orientation());
  114. if (orientation() == Gfx::Orientation::Horizontal)
  115. secondary -= margins().top() + margins().bottom();
  116. else
  117. secondary -= margins().left() + margins().right();
  118. int min_secondary = item.widget->min_size().secondary_size_for_orientation(orientation());
  119. int max_secondary = item.widget->max_size().secondary_size_for_orientation(orientation());
  120. if (min_secondary >= 0)
  121. secondary = max(secondary, min_secondary);
  122. if (max_secondary >= 0)
  123. secondary = min(secondary, max_secondary);
  124. rect.set_secondary_size_for_orientation(orientation(), secondary);
  125. if (orientation() == Gfx::Orientation::Horizontal)
  126. rect.center_vertically_within(widget.rect());
  127. else
  128. rect.center_horizontally_within(widget.rect());
  129. item.widget->set_relative_rect(rect);
  130. }
  131. if (orientation() == Gfx::Orientation::Horizontal)
  132. current_x += rect.width() + spacing();
  133. else
  134. current_y += rect.height() + spacing();
  135. }
  136. }
  137. }