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