Layout.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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("entries",
  20. [this] {
  21. JsonArray entries_array;
  22. for (auto& entry : m_entries) {
  23. JsonObject entry_object;
  24. if (entry.type == Entry::Type::Widget) {
  25. entry_object.set("type", "Widget");
  26. entry_object.set("widget", (FlatPtr)entry.widget.ptr());
  27. } else if (entry.type == Entry::Type::Spacer) {
  28. entry_object.set("type", "Spacer");
  29. } else {
  30. VERIFY_NOT_REACHED();
  31. }
  32. entries_array.must_append(move(entry_object));
  33. }
  34. return entries_array;
  35. });
  36. }
  37. Layout::~Layout() = default;
  38. void Layout::notify_adopted(Badge<Widget>, Widget& widget)
  39. {
  40. if (m_owner == &widget)
  41. return;
  42. m_owner = widget;
  43. m_owner->for_each_child_widget([&](Widget& child) {
  44. add_widget(child);
  45. return IterationDecision::Continue;
  46. });
  47. }
  48. void Layout::notify_disowned(Badge<Widget>, Widget& widget)
  49. {
  50. VERIFY(m_owner == &widget);
  51. m_owner.clear();
  52. m_entries.clear();
  53. }
  54. void Layout::add_entry(Entry&& entry)
  55. {
  56. m_entries.append(move(entry));
  57. if (m_owner)
  58. m_owner->notify_layout_changed({});
  59. }
  60. void Layout::add_spacer()
  61. {
  62. add_entry(Entry { .type = Entry::Type::Spacer });
  63. }
  64. void Layout::add_layout(OwnPtr<Layout>&& layout)
  65. {
  66. Entry entry;
  67. entry.type = Entry::Type::Layout;
  68. entry.layout = move(layout);
  69. add_entry(move(entry));
  70. }
  71. void Layout::add_widget(Widget& widget)
  72. {
  73. add_entry(Entry {
  74. .type = Entry::Type::Widget,
  75. .widget = widget,
  76. });
  77. }
  78. void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
  79. {
  80. Entry entry;
  81. entry.type = Entry::Type::Widget;
  82. entry.widget = widget;
  83. m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
  84. return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
  85. });
  86. if (m_owner)
  87. m_owner->notify_layout_changed({});
  88. }
  89. void Layout::remove_widget(Widget& widget)
  90. {
  91. m_entries.remove_first_matching([&](auto& entry) {
  92. return entry.widget == &widget;
  93. });
  94. if (m_owner)
  95. m_owner->notify_layout_changed({});
  96. }
  97. void Layout::set_spacing(int spacing)
  98. {
  99. if (m_spacing == spacing)
  100. return;
  101. m_spacing = spacing;
  102. if (m_owner)
  103. m_owner->notify_layout_changed({});
  104. }
  105. void Layout::set_margins(Margins const& margins)
  106. {
  107. if (m_margins == margins)
  108. return;
  109. m_margins = margins;
  110. if (m_owner)
  111. m_owner->notify_layout_changed({});
  112. }
  113. }