Layout.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  34. Layout::~Layout()
  35. {
  36. }
  37. void Layout::notify_adopted(Badge<Widget>, Widget& widget)
  38. {
  39. if (m_owner == &widget)
  40. return;
  41. m_owner = widget.make_weak_ptr();
  42. }
  43. void Layout::notify_disowned(Badge<Widget>, Widget& widget)
  44. {
  45. ASSERT(m_owner == &widget);
  46. m_owner.clear();
  47. }
  48. void Layout::add_entry(Entry&& entry)
  49. {
  50. m_entries.append(move(entry));
  51. if (m_owner)
  52. m_owner->notify_layout_changed({});
  53. }
  54. void Layout::add_spacer()
  55. {
  56. Entry entry;
  57. entry.type = Entry::Type::Spacer;
  58. add_entry(move(entry));
  59. }
  60. void Layout::add_layout(OwnPtr<Layout>&& layout)
  61. {
  62. Entry entry;
  63. entry.type = Entry::Type::Layout;
  64. entry.layout = move(layout);
  65. add_entry(move(entry));
  66. }
  67. void Layout::add_widget(Widget& widget)
  68. {
  69. Entry entry;
  70. entry.type = Entry::Type::Widget;
  71. entry.widget = widget.make_weak_ptr();
  72. add_entry(move(entry));
  73. }
  74. void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
  75. {
  76. Entry entry;
  77. entry.type = Entry::Type::Widget;
  78. entry.widget = widget.make_weak_ptr();
  79. m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
  80. return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
  81. });
  82. if (m_owner)
  83. m_owner->notify_layout_changed({});
  84. }
  85. void Layout::remove_widget(Widget& widget)
  86. {
  87. m_entries.remove_first_matching([&](auto& entry) {
  88. return entry.widget == &widget;
  89. });
  90. if (m_owner)
  91. m_owner->notify_layout_changed({});
  92. }
  93. void Layout::set_spacing(int spacing)
  94. {
  95. if (m_spacing == spacing)
  96. return;
  97. m_spacing = spacing;
  98. if (m_owner)
  99. m_owner->notify_layout_changed({});
  100. }
  101. void Layout::set_margins(const Margins& margins)
  102. {
  103. if (m_margins == margins)
  104. return;
  105. m_margins = margins;
  106. if (m_owner)
  107. m_owner->notify_layout_changed({});
  108. }
  109. void Layout::save_to(JsonObject& json)
  110. {
  111. Core::Object::save_to(json);
  112. json.set("spacing", m_spacing);
  113. JsonObject margins_object;
  114. margins_object.set("left", m_margins.left());
  115. margins_object.set("right", m_margins.right());
  116. margins_object.set("top", m_margins.top());
  117. margins_object.set("bottom", m_margins.bottom());
  118. json.set("margins", move(margins_object));
  119. JsonArray entries_array;
  120. for (auto& entry : m_entries) {
  121. JsonObject entry_object;
  122. if (entry.type == Entry::Type::Widget) {
  123. entry_object.set("type", "Widget");
  124. entry_object.set("widget", (FlatPtr)entry.widget.ptr());
  125. } else if (entry.type == Entry::Type::Spacer) {
  126. entry_object.set("type", "Spacer");
  127. } else {
  128. ASSERT_NOT_REACHED();
  129. }
  130. entries_array.append(move(entry_object));
  131. }
  132. json.set("entries", move(entries_array));
  133. }
  134. }