diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-05 20:41:27 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-05 20:41:27 +0100 |
commit | d3558b6137dfe9f8e895dc34844d35082eff5e3d (patch) | |
tree | 4b47d3352f8c6b1629fafc7927142c6187eeef63 /Libraries/LibCore | |
parent | 0f81eaf8a25aa7f87faf934ec0c1fa33e9f4b10d (diff) | |
download | serenity-d3558b6137dfe9f8e895dc34844d35082eff5e3d.zip |
LibCore+LibGUI: Allow inserting a CObject/GWidget before another
When adding a widget to a parent, you don't always want to append it to
the set of existing children, but instead insert it before one of them.
This patch makes that possible by adding CObject::insert_child_before()
which also produces a ChildAdded event with an additional before_child
pointer. This pointer is then used by GWidget to make sure that any
layout present maintains the correct order. (Without doing that, newly
added children would still be appended into the layout order, despite
having a different widget sibling order.)
Diffstat (limited to 'Libraries/LibCore')
-rw-r--r-- | Libraries/LibCore/CEvent.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibCore/CEvent.h | 6 | ||||
-rw-r--r-- | Libraries/LibCore/CObject.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibCore/CObject.h | 1 |
4 files changed, 17 insertions, 2 deletions
diff --git a/Libraries/LibCore/CEvent.cpp b/Libraries/LibCore/CEvent.cpp index eacce964c2..ebc9eeff3d 100644 --- a/Libraries/LibCore/CEvent.cpp +++ b/Libraries/LibCore/CEvent.cpp @@ -1,9 +1,10 @@ #include <LibCore/CEvent.h> #include <LibCore/CObject.h> -CChildEvent::CChildEvent(Type type, CObject& child) +CChildEvent::CChildEvent(Type type, CObject& child, CObject* insertion_before_child) : CEvent(type) , m_child(child.make_weak_ptr()) + , m_insertion_before_child(insertion_before_child ? insertion_before_child->make_weak_ptr() : nullptr) { } diff --git a/Libraries/LibCore/CEvent.h b/Libraries/LibCore/CEvent.h index 8e49c07fd4..28c593be45 100644 --- a/Libraries/LibCore/CEvent.h +++ b/Libraries/LibCore/CEvent.h @@ -100,14 +100,18 @@ private: class CChildEvent final : public CEvent { public: - CChildEvent(Type, CObject& child); + CChildEvent(Type, CObject& child, CObject* insertion_before_child = nullptr); ~CChildEvent(); CObject* child() { return m_child.ptr(); } const CObject* child() const { return m_child.ptr(); } + CObject* insertion_before_child() { return m_insertion_before_child.ptr(); } + const CObject* insertion_before_child() const { return m_insertion_before_child.ptr(); } + private: WeakPtr<CObject> m_child; + WeakPtr<CObject> m_insertion_before_child; }; class CCustomEvent : public CEvent { diff --git a/Libraries/LibCore/CObject.cpp b/Libraries/LibCore/CObject.cpp index 0058de5b39..7de5a5a044 100644 --- a/Libraries/LibCore/CObject.cpp +++ b/Libraries/LibCore/CObject.cpp @@ -64,6 +64,15 @@ void CObject::add_child(CObject& object) event(*make<CChildEvent>(CEvent::ChildAdded, object)); } +void CObject::insert_child_before(CObject& new_child, CObject& before_child) +{ + // FIXME: Should we support reparenting objects? + ASSERT(!new_child.parent() || new_child.parent() == this); + new_child.m_parent = this; + m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; }); + event(*make<CChildEvent>(CEvent::ChildAdded, new_child, &before_child)); +} + void CObject::remove_child(CObject& object) { for (int i = 0; i < m_children.size(); ++i) { diff --git a/Libraries/LibCore/CObject.h b/Libraries/LibCore/CObject.h index 7a5ce63785..709d2e607e 100644 --- a/Libraries/LibCore/CObject.h +++ b/Libraries/LibCore/CObject.h @@ -70,6 +70,7 @@ public: bool has_timer() const { return m_timer_id; } void add_child(CObject&); + void insert_child_before(CObject& new_child, CObject& before_child); void remove_child(CObject&); void dump_tree(int indent = 0); |