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