Layout.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Badge.h>
  8. #include <AK/JsonObject.h>
  9. #include <LibGUI/Layout.h>
  10. #include <LibGUI/Widget.h>
  11. REGISTER_ABSTRACT_GUI_OBJECT(GUI, Layout)
  12. namespace GUI {
  13. Layout::Layout(Margins initial_margins, int spacing)
  14. : m_margins(initial_margins)
  15. , m_spacing(spacing)
  16. {
  17. REGISTER_INT_PROPERTY("spacing", spacing, set_spacing);
  18. REGISTER_MARGINS_PROPERTY("margins", margins, set_margins);
  19. register_property(
  20. "entries"sv,
  21. [this] {
  22. JsonArray entries_array;
  23. for (auto& entry : m_entries) {
  24. JsonObject entry_object;
  25. if (entry.type == Entry::Type::Widget) {
  26. entry_object.set("type", "Widget");
  27. entry_object.set("widget", (FlatPtr)entry.widget.ptr());
  28. } else if (entry.type == Entry::Type::Spacer) {
  29. entry_object.set("type", "Spacer");
  30. } else {
  31. VERIFY_NOT_REACHED();
  32. }
  33. entries_array.must_append(move(entry_object));
  34. }
  35. return entries_array;
  36. },
  37. nullptr, nullptr);
  38. }
  39. Layout::~Layout() = default;
  40. void Layout::notify_adopted(Badge<Widget>, Widget& widget)
  41. {
  42. if (m_owner == &widget)
  43. return;
  44. m_owner = widget;
  45. m_owner->for_each_child_widget([&](Widget& child) {
  46. add_widget(child);
  47. return IterationDecision::Continue;
  48. });
  49. }
  50. void Layout::notify_disowned(Badge<Widget>, Widget& widget)
  51. {
  52. VERIFY(m_owner == &widget);
  53. m_owner.clear();
  54. m_entries.clear();
  55. }
  56. void Layout::add_entry(Entry&& entry)
  57. {
  58. m_entries.append(move(entry));
  59. if (m_owner)
  60. m_owner->notify_layout_changed({});
  61. }
  62. void Layout::add_spacer()
  63. {
  64. add_entry(Entry { .type = Entry::Type::Spacer });
  65. }
  66. void Layout::add_layout(OwnPtr<Layout>&& layout)
  67. {
  68. Entry entry;
  69. entry.type = Entry::Type::Layout;
  70. entry.layout = move(layout);
  71. add_entry(move(entry));
  72. }
  73. void Layout::add_widget(Widget& widget)
  74. {
  75. add_entry(Entry {
  76. .type = Entry::Type::Widget,
  77. .widget = widget,
  78. });
  79. }
  80. void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
  81. {
  82. Entry entry;
  83. entry.type = Entry::Type::Widget;
  84. entry.widget = widget;
  85. m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
  86. return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
  87. });
  88. if (m_owner)
  89. m_owner->notify_layout_changed({});
  90. }
  91. void Layout::remove_widget(Widget& widget)
  92. {
  93. m_entries.remove_first_matching([&](auto& entry) {
  94. return entry.widget == &widget;
  95. });
  96. if (m_owner)
  97. m_owner->notify_layout_changed({});
  98. }
  99. void Layout::set_spacing(int spacing)
  100. {
  101. if (m_spacing == spacing)
  102. return;
  103. m_spacing = spacing;
  104. if (m_owner)
  105. m_owner->notify_layout_changed({});
  106. }
  107. void Layout::set_margins(Margins const& margins)
  108. {
  109. if (m_margins == margins)
  110. return;
  111. m_margins = margins;
  112. if (m_owner)
  113. m_owner->notify_layout_changed({});
  114. }
  115. }