Layout.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/Badge.h>
  27. #include <AK/JsonObject.h>
  28. #include <LibGUI/Layout.h>
  29. #include <LibGUI/Widget.h>
  30. namespace GUI {
  31. Layout::Layout()
  32. {
  33. REGISTER_INT_PROPERTY("spacing", spacing, set_spacing);
  34. register_property(
  35. "margins",
  36. [this] {
  37. JsonObject margins_object;
  38. margins_object.set("left", m_margins.left());
  39. margins_object.set("right", m_margins.right());
  40. margins_object.set("top", m_margins.top());
  41. margins_object.set("bottom", m_margins.bottom());
  42. return margins_object;
  43. },
  44. [this](auto value) {
  45. if (!value.is_array() || value.as_array().size() != 4)
  46. return false;
  47. int m[4];
  48. for (size_t i = 0; i < 4; ++i)
  49. m[i] = value.as_array().at(i).to_i32();
  50. set_margins({ m[0], m[1], m[2], m[3] });
  51. return true;
  52. });
  53. register_property("entries",
  54. [this] {
  55. JsonArray entries_array;
  56. for (auto& entry : m_entries) {
  57. JsonObject entry_object;
  58. if (entry.type == Entry::Type::Widget) {
  59. entry_object.set("type", "Widget");
  60. entry_object.set("widget", (FlatPtr)entry.widget.ptr());
  61. } else if (entry.type == Entry::Type::Spacer) {
  62. entry_object.set("type", "Spacer");
  63. } else {
  64. ASSERT_NOT_REACHED();
  65. }
  66. entries_array.append(move(entry_object));
  67. }
  68. return entries_array;
  69. });
  70. }
  71. Layout::~Layout()
  72. {
  73. }
  74. void Layout::notify_adopted(Badge<Widget>, Widget& widget)
  75. {
  76. if (m_owner == &widget)
  77. return;
  78. m_owner = widget.make_weak_ptr();
  79. }
  80. void Layout::notify_disowned(Badge<Widget>, Widget& widget)
  81. {
  82. ASSERT(m_owner == &widget);
  83. m_owner.clear();
  84. }
  85. void Layout::add_entry(Entry&& entry)
  86. {
  87. m_entries.append(move(entry));
  88. if (m_owner)
  89. m_owner->notify_layout_changed({});
  90. }
  91. void Layout::add_spacer()
  92. {
  93. Entry entry;
  94. entry.type = Entry::Type::Spacer;
  95. add_entry(move(entry));
  96. }
  97. void Layout::add_layout(OwnPtr<Layout>&& layout)
  98. {
  99. Entry entry;
  100. entry.type = Entry::Type::Layout;
  101. entry.layout = move(layout);
  102. add_entry(move(entry));
  103. }
  104. void Layout::add_widget(Widget& widget)
  105. {
  106. Entry entry;
  107. entry.type = Entry::Type::Widget;
  108. entry.widget = widget.make_weak_ptr();
  109. add_entry(move(entry));
  110. }
  111. void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
  112. {
  113. Entry entry;
  114. entry.type = Entry::Type::Widget;
  115. entry.widget = widget.make_weak_ptr();
  116. m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
  117. return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
  118. });
  119. if (m_owner)
  120. m_owner->notify_layout_changed({});
  121. }
  122. void Layout::remove_widget(Widget& widget)
  123. {
  124. m_entries.remove_first_matching([&](auto& entry) {
  125. return entry.widget == &widget;
  126. });
  127. if (m_owner)
  128. m_owner->notify_layout_changed({});
  129. }
  130. void Layout::set_spacing(int spacing)
  131. {
  132. if (m_spacing == spacing)
  133. return;
  134. m_spacing = spacing;
  135. if (m_owner)
  136. m_owner->notify_layout_changed({});
  137. }
  138. void Layout::set_margins(const Margins& margins)
  139. {
  140. if (m_margins == margins)
  141. return;
  142. m_margins = margins;
  143. if (m_owner)
  144. m_owner->notify_layout_changed({});
  145. }
  146. }