From 75091016d750bcecd91d834de2014bdabf81ddfb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 11 Aug 2025 11:30:20 +0200 Subject: [PATCH] LibCore: Remove parent/child concept from EventReceiver This ownership model is no longer used. --- Libraries/LibCore/EventReceiver.cpp | 91 +---------------------------- Libraries/LibCore/EventReceiver.h | 76 +----------------------- 2 files changed, 5 insertions(+), 162 deletions(-) diff --git a/Libraries/LibCore/EventReceiver.cpp b/Libraries/LibCore/EventReceiver.cpp index 64585dec6f6..59b82ce6f2e 100644 --- a/Libraries/LibCore/EventReceiver.cpp +++ b/Libraries/LibCore/EventReceiver.cpp @@ -14,26 +14,11 @@ namespace Core { -EventReceiver::EventReceiver(EventReceiver* parent) - : m_parent(parent) -{ - if (m_parent) - m_parent->add_child(*this); -} +EventReceiver::EventReceiver() = default; EventReceiver::~EventReceiver() { - // NOTE: We move our children out to a stack vector to prevent other - // code from trying to iterate over them. - auto children = move(m_children); - // NOTE: We also unparent the children, so that they won't try to unparent - // themselves in their own destructors. - for (auto& child : children) - child->m_parent = nullptr; - stop_timer(); - if (m_parent) - m_parent->remove_child(*this); } void EventReceiver::event(Core::Event& event) @@ -56,54 +41,6 @@ void EventReceiver::event(Core::Event& event) } } -ErrorOr EventReceiver::try_add_child(EventReceiver& object) -{ - // FIXME: Should we support reparenting objects? - VERIFY(!object.parent() || object.parent() == this); - TRY(m_children.try_append(object)); - object.m_parent = this; - Core::ChildEvent child_event(Core::Event::ChildAdded, object); - event(child_event); - return {}; -} - -void EventReceiver::add_child(EventReceiver& object) -{ - MUST(try_add_child(object)); -} - -void EventReceiver::insert_child_before(EventReceiver& new_child, EventReceiver& before_child) -{ - // FIXME: Should we support reparenting objects? - VERIFY(!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; }); - Core::ChildEvent child_event(Core::Event::ChildAdded, new_child, &before_child); - event(child_event); -} - -void EventReceiver::remove_child(EventReceiver& object) -{ - for (size_t i = 0; i < m_children.size(); ++i) { - if (m_children[i] == &object) { - // NOTE: We protect the child so it survives the handling of ChildRemoved. - NonnullRefPtr protector = object; - object.m_parent = nullptr; - m_children.remove(i); - Core::ChildEvent child_event(Core::Event::ChildRemoved, object); - event(child_event); - return; - } - } - VERIFY_NOT_REACHED(); -} - -void EventReceiver::remove_all_children() -{ - while (!m_children.is_empty()) - m_children.first()->remove_from_parent(); -} - void EventReceiver::timer_event(Core::TimerEvent&) { } @@ -139,35 +76,13 @@ void EventReceiver::deferred_invoke(Function invokee) Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); }); } -bool EventReceiver::is_ancestor_of(EventReceiver const& other) const +void EventReceiver::dispatch_event(Core::Event& e) { - if (&other == this) - return false; - for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) { - if (ancestor == this) - return true; - } - return false; -} - -void EventReceiver::dispatch_event(Core::Event& e, EventReceiver* stay_within) -{ - VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this)); - auto* target = this; - do { - target->event(e); - target = target->parent(); - if (target == stay_within) { - // Prevent the event from bubbling any further. - return; - } - } while (target && !e.is_accepted()); + event(e); } bool EventReceiver::is_visible_for_timer_purposes() const { - if (parent()) - return parent()->is_visible_for_timer_purposes(); return true; } diff --git a/Libraries/LibCore/EventReceiver.h b/Libraries/LibCore/EventReceiver.h index 547bac9ec69..c90571017cf 100644 --- a/Libraries/LibCore/EventReceiver.h +++ b/Libraries/LibCore/EventReceiver.h @@ -64,73 +64,18 @@ public: template bool fast_is() const = delete; - virtual bool is_widget() const { return false; } - - Vector>& children() { return m_children; } - Vector> const& children() const { return m_children; } - - template - void for_each_child(Callback callback) - { - for (auto& child : m_children) { - if (callback(*child) == IterationDecision::Break) - return; - } - } - - template - void for_each_child_of_type(Callback callback) - requires IsBaseOf; - - bool is_ancestor_of(EventReceiver const&) const; - - EventReceiver* parent() { return m_parent; } - EventReceiver const* parent() const { return m_parent; } - void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No); void stop_timer(); bool has_timer() const { return m_timer_id; } - ErrorOr try_add_child(EventReceiver&); - - void add_child(EventReceiver&); - void insert_child_before(EventReceiver& new_child, EventReceiver& before_child); - void remove_child(EventReceiver&); - void remove_all_children(); - void deferred_invoke(Function); - void dispatch_event(Core::Event&, EventReceiver* stay_within = nullptr); - - void remove_from_parent() - { - if (m_parent) - m_parent->remove_child(*this); - - // The call to `remove_child` may have deleted the object. - // Do not dereference `this` from this point forward. - } - - template - inline T& add(Args&&... args) - { - auto child = T::construct(forward(args)...); - add_child(*child); - return child; - } - - template - inline ErrorOr> try_add(Args&&... args) - { - auto child = TRY(T::try_create(forward(args)...)); - TRY(try_add_child(*child)); - return child; - } + void dispatch_event(Core::Event&); virtual bool is_visible_for_timer_purposes() const; protected: - explicit EventReceiver(EventReceiver* parent = nullptr); + EventReceiver(); virtual void event(Core::Event&); @@ -141,9 +86,7 @@ protected: virtual void child_event(ChildEvent&); private: - EventReceiver* m_parent { nullptr }; intptr_t m_timer_id { 0 }; - Vector> m_children; }; } @@ -155,18 +98,3 @@ struct AK::Formatter : AK::Formatter { return AK::Formatter::format(builder, "{}({})"sv, value.class_name(), &value); } }; - -namespace Core { - -template -inline void EventReceiver::for_each_child_of_type(Callback callback) -requires IsBaseOf -{ - for_each_child([&](auto& child) { - if (is(child)) - return callback(static_cast(child)); - return IterationDecision::Continue; - }); -} - -}