2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-12-26 07:50:25 -03:00
|
|
|
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 13:09:45 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-01-20 01:49:48 -02:00
|
|
|
#pragma once
|
|
|
|
|
|
2024-12-26 07:50:25 -03:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2023-12-16 11:19:34 -03:00
|
|
|
#include <AK/ByteString.h>
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <AK/Forward.h>
|
2023-08-06 10:14:59 -03:00
|
|
|
#include <AK/Function.h>
|
2019-09-21 19:17:53 -03:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-05-19 09:35:34 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2022-03-16 21:26:15 -03:00
|
|
|
#include <AK/StringView.h>
|
2020-07-26 12:16:35 -03:00
|
|
|
#include <AK/TypeCasts.h>
|
2024-12-08 14:26:06 -03:00
|
|
|
#include <AK/Vector.h>
|
2019-01-20 01:49:48 -02:00
|
|
|
#include <AK/Weakable.h>
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <LibCore/Forward.h>
|
2019-08-18 15:36:37 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
|
|
|
|
|
2019-12-29 11:58:07 -03:00
|
|
|
enum class TimerShouldFireWhenNotVisible {
|
|
|
|
|
No = 0,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-14 12:38:36 -03:00
|
|
|
#define C_OBJECT(klass) \
|
|
|
|
|
public: \
|
2022-10-16 19:06:11 -03:00
|
|
|
virtual StringView class_name() const override \
|
|
|
|
|
{ \
|
|
|
|
|
return #klass##sv; \
|
|
|
|
|
} \
|
2022-11-14 12:38:36 -03:00
|
|
|
template<typename Klass = klass, class... Args> \
|
|
|
|
|
static NonnullRefPtr<klass> construct(Args&&... args) \
|
|
|
|
|
{ \
|
|
|
|
|
return adopt_ref(*new Klass(::forward<Args>(args)...)); \
|
|
|
|
|
} \
|
|
|
|
|
template<typename Klass = klass, class... Args> \
|
|
|
|
|
static ErrorOr<NonnullRefPtr<klass>> try_create(Args&&... args) \
|
|
|
|
|
{ \
|
|
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) Klass(::forward<Args>(args)...)); \
|
2019-09-21 05:13:34 -03:00
|
|
|
}
|
2019-12-29 11:58:07 -03:00
|
|
|
|
2022-10-16 19:06:11 -03:00
|
|
|
#define C_OBJECT_ABSTRACT(klass) \
|
|
|
|
|
public: \
|
|
|
|
|
virtual StringView class_name() const override \
|
|
|
|
|
{ \
|
|
|
|
|
return #klass##sv; \
|
|
|
|
|
}
|
2019-07-25 14:49:28 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
class EventReceiver
|
2024-12-26 07:50:25 -03:00
|
|
|
: public AtomicRefCounted<EventReceiver>
|
2023-08-06 13:09:39 -03:00
|
|
|
, public Weakable<EventReceiver> {
|
|
|
|
|
// NOTE: No C_OBJECT macro for Core::EventReceiver itself.
|
2019-09-21 19:17:53 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
AK_MAKE_NONCOPYABLE(EventReceiver);
|
|
|
|
|
AK_MAKE_NONMOVABLE(EventReceiver);
|
2020-08-26 16:52:24 -03:00
|
|
|
|
2020-10-16 17:04:19 -03:00
|
|
|
public:
|
2023-08-06 13:09:39 -03:00
|
|
|
virtual ~EventReceiver();
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2022-03-16 21:26:15 -03:00
|
|
|
virtual StringView class_name() const = 0;
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2022-09-30 12:59:41 -03:00
|
|
|
template<typename T>
|
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
|
|
|
|
|
virtual bool is_widget() const { return false; }
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString const& name() const { return m_name; }
|
|
|
|
|
void set_name(ByteString name) { m_name = move(name); }
|
2019-07-10 15:33:53 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
Vector<NonnullRefPtr<EventReceiver>>& children() { return m_children; }
|
|
|
|
|
Vector<NonnullRefPtr<EventReceiver>> const& children() const { return m_children; }
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2019-05-26 22:52:19 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_child(Callback callback)
|
|
|
|
|
{
|
2019-09-21 19:17:53 -03:00
|
|
|
for (auto& child : m_children) {
|
2023-03-06 10:17:01 -03:00
|
|
|
if (callback(*child) == IterationDecision::Break)
|
2019-05-26 22:52:19 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
template<typename T, typename Callback>
|
2022-10-16 19:06:11 -03:00
|
|
|
void for_each_child_of_type(Callback callback)
|
2023-08-06 13:09:39 -03:00
|
|
|
requires IsBaseOf<EventReceiver, T>;
|
2021-01-01 04:46:51 -03:00
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-14 16:20:02 -03:00
|
|
|
T* find_child_of_type_named(StringView)
|
2023-08-06 13:09:39 -03:00
|
|
|
requires IsBaseOf<EventReceiver, T>;
|
2021-01-01 04:46:51 -03:00
|
|
|
|
2023-05-14 16:20:02 -03:00
|
|
|
template<typename T, size_t N>
|
|
|
|
|
ALWAYS_INLINE T* find_child_of_type_named(char const (&string_literal)[N])
|
2023-08-06 13:09:39 -03:00
|
|
|
requires IsBaseOf<EventReceiver, T>
|
2023-05-14 16:20:02 -03:00
|
|
|
{
|
|
|
|
|
return find_child_of_type_named<T>(StringView { string_literal, N - 1 });
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 04:46:51 -03:00
|
|
|
template<typename T>
|
2023-05-14 16:20:02 -03:00
|
|
|
T* find_descendant_of_type_named(StringView)
|
2023-08-06 13:09:39 -03:00
|
|
|
requires IsBaseOf<EventReceiver, T>;
|
2019-05-26 23:18:24 -03:00
|
|
|
|
2023-05-14 16:20:02 -03:00
|
|
|
template<typename T, size_t N>
|
|
|
|
|
ALWAYS_INLINE T* find_descendant_of_type_named(char const (&string_literal)[N])
|
2023-08-06 13:09:39 -03:00
|
|
|
requires IsBaseOf<EventReceiver, T>
|
2023-05-14 16:20:02 -03:00
|
|
|
{
|
|
|
|
|
return find_descendant_of_type_named<T>(StringView { string_literal, N - 1 });
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
bool is_ancestor_of(EventReceiver const&) const;
|
2019-09-20 15:37:31 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
EventReceiver* parent() { return m_parent; }
|
|
|
|
|
EventReceiver const* parent() const { return m_parent; }
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2019-12-29 11:58:07 -03:00
|
|
|
void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
|
2019-01-31 14:31:23 -02:00
|
|
|
void stop_timer();
|
|
|
|
|
bool has_timer() const { return m_timer_id; }
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
ErrorOr<void> try_add_child(EventReceiver&);
|
2021-11-24 09:09:51 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
void add_child(EventReceiver&);
|
|
|
|
|
void insert_child_before(EventReceiver& new_child, EventReceiver& before_child);
|
|
|
|
|
void remove_child(EventReceiver&);
|
2020-12-27 13:14:44 -03:00
|
|
|
void remove_all_children();
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2021-03-28 06:16:33 -03:00
|
|
|
void set_event_filter(Function<bool(Core::Event&)>);
|
|
|
|
|
|
2021-08-30 07:43:28 -03:00
|
|
|
void deferred_invoke(Function<void()>);
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
void dispatch_event(Core::Event&, EventReceiver* stay_within = nullptr);
|
2019-09-20 15:37:31 -03:00
|
|
|
|
2019-09-21 19:41:01 -03:00
|
|
|
void remove_from_parent()
|
|
|
|
|
{
|
|
|
|
|
if (m_parent)
|
|
|
|
|
m_parent->remove_child(*this);
|
2024-03-04 07:13:17 -03:00
|
|
|
|
|
|
|
|
// The call to `remove_child` may have deleted the object.
|
|
|
|
|
// Do not dereference `this` from this point forward.
|
2019-09-21 19:41:01 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-23 03:13:09 -03:00
|
|
|
template<class T, class... Args>
|
2020-03-04 15:07:55 -03:00
|
|
|
inline T& add(Args&&... args)
|
2020-02-23 03:13:09 -03:00
|
|
|
{
|
2020-03-04 15:07:55 -03:00
|
|
|
auto child = T::construct(forward<Args>(args)...);
|
|
|
|
|
add_child(*child);
|
|
|
|
|
return child;
|
2020-02-23 03:13:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-24 09:09:51 -03:00
|
|
|
template<class T, class... Args>
|
|
|
|
|
inline ErrorOr<NonnullRefPtr<T>> try_add(Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
auto child = TRY(T::try_create(forward<Args>(args)...));
|
|
|
|
|
TRY(try_add_child(*child));
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 11:58:07 -03:00
|
|
|
virtual bool is_visible_for_timer_purposes() const;
|
|
|
|
|
|
2019-03-15 12:12:06 -03:00
|
|
|
protected:
|
2023-08-06 13:09:39 -03:00
|
|
|
explicit EventReceiver(EventReceiver* parent = nullptr);
|
2019-07-25 14:49:28 -03:00
|
|
|
|
2021-03-28 06:24:22 -03:00
|
|
|
virtual void event(Core::Event&);
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual void timer_event(TimerEvent&);
|
|
|
|
|
virtual void custom_event(CustomEvent&);
|
2019-01-20 01:49:48 -02:00
|
|
|
|
2019-07-27 04:31:46 -03:00
|
|
|
// NOTE: You may get child events for children that are not yet fully constructed!
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual void child_event(ChildEvent&);
|
2019-07-27 04:31:46 -03:00
|
|
|
|
2019-03-15 12:12:06 -03:00
|
|
|
private:
|
2023-08-06 13:09:39 -03:00
|
|
|
EventReceiver* m_parent { nullptr };
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString m_name;
|
2024-02-18 02:11:38 -03:00
|
|
|
intptr_t m_timer_id { 0 };
|
2023-08-06 13:09:39 -03:00
|
|
|
Vector<NonnullRefPtr<EventReceiver>> m_children;
|
2021-03-28 06:16:33 -03:00
|
|
|
Function<bool(Core::Event&)> m_event_filter;
|
2019-01-20 01:49:48 -02:00
|
|
|
};
|
2019-05-26 23:06:01 -03:00
|
|
|
|
|
|
|
|
}
|
2019-05-26 23:18:24 -03:00
|
|
|
|
2020-10-15 16:07:36 -03:00
|
|
|
template<>
|
2023-08-06 13:09:39 -03:00
|
|
|
struct AK::Formatter<Core::EventReceiver> : AK::Formatter<FormatString> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Core::EventReceiver const& value)
|
2021-01-08 21:00:22 -03:00
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "{}({})"sv, value.class_name(), &value);
|
2021-01-08 21:00:22 -03:00
|
|
|
}
|
2020-10-15 16:07:36 -03:00
|
|
|
};
|
|
|
|
|
|
2020-07-26 12:16:35 -03:00
|
|
|
namespace Core {
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2019-05-26 23:18:24 -03:00
|
|
|
template<typename T, typename Callback>
|
2023-08-06 13:09:39 -03:00
|
|
|
inline void EventReceiver::for_each_child_of_type(Callback callback)
|
|
|
|
|
requires IsBaseOf<EventReceiver, T>
|
2021-01-01 04:46:51 -03:00
|
|
|
{
|
|
|
|
|
for_each_child([&](auto& child) {
|
2021-04-17 18:01:24 -03:00
|
|
|
if (is<T>(child))
|
|
|
|
|
return callback(static_cast<T&>(child));
|
2021-01-01 04:46:51 -03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2023-08-06 13:09:39 -03:00
|
|
|
T* EventReceiver::find_child_of_type_named(StringView name)
|
|
|
|
|
requires IsBaseOf<EventReceiver, T>
|
2021-01-01 04:46:51 -03:00
|
|
|
{
|
|
|
|
|
T* found_child = nullptr;
|
|
|
|
|
for_each_child_of_type<T>([&](auto& child) {
|
|
|
|
|
if (child.name() == name) {
|
|
|
|
|
found_child = &child;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return found_child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2023-08-06 13:09:39 -03:00
|
|
|
T* EventReceiver::find_descendant_of_type_named(StringView name)
|
|
|
|
|
requires IsBaseOf<EventReceiver, T>
|
2019-05-26 23:18:24 -03:00
|
|
|
{
|
2021-04-17 18:01:24 -03:00
|
|
|
if (is<T>(*this) && this->name() == name) {
|
|
|
|
|
return static_cast<T*>(this);
|
|
|
|
|
}
|
2021-01-01 04:46:51 -03:00
|
|
|
T* found_child = nullptr;
|
2019-05-28 06:53:16 -03:00
|
|
|
for_each_child([&](auto& child) {
|
2021-01-01 04:46:51 -03:00
|
|
|
found_child = child.template find_descendant_of_type_named<T>(name);
|
|
|
|
|
if (found_child)
|
|
|
|
|
return IterationDecision::Break;
|
2019-05-26 23:18:24 -03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-01-01 04:46:51 -03:00
|
|
|
return found_child;
|
2019-05-26 23:18:24 -03:00
|
|
|
}
|
2019-07-14 05:59:26 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
}
|