Object.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/IntrusiveList.h>
  11. #include <AK/Noncopyable.h>
  12. #include <AK/NonnullRefPtrVector.h>
  13. #include <AK/OwnPtr.h>
  14. #include <AK/String.h>
  15. #include <AK/StringView.h>
  16. #include <AK/TypeCasts.h>
  17. #include <AK/Weakable.h>
  18. #include <LibCore/Forward.h>
  19. #include <LibCore/Property.h>
  20. namespace Core {
  21. #define REGISTER_ABSTRACT_CORE_OBJECT(namespace_, class_name) \
  22. namespace Core { \
  23. namespace Registration { \
  24. Core::ObjectClassRegistration registration_##class_name(#namespace_ "::" #class_name, []() { return RefPtr<Object>(); }); \
  25. } \
  26. }
  27. #define REGISTER_CORE_OBJECT(namespace_, class_name) \
  28. namespace Core { \
  29. namespace Registration { \
  30. Core::ObjectClassRegistration registration_##class_name(#namespace_ "::" #class_name, []() { return namespace_::class_name::construct(); }); \
  31. } \
  32. }
  33. class ObjectClassRegistration {
  34. AK_MAKE_NONCOPYABLE(ObjectClassRegistration);
  35. AK_MAKE_NONMOVABLE(ObjectClassRegistration);
  36. public:
  37. ObjectClassRegistration(StringView class_name, Function<RefPtr<Object>()> factory, ObjectClassRegistration* parent_class = nullptr);
  38. ~ObjectClassRegistration() = default;
  39. StringView class_name() const { return m_class_name; }
  40. const ObjectClassRegistration* parent_class() const { return m_parent_class; }
  41. RefPtr<Object> construct() const { return m_factory(); }
  42. bool is_derived_from(const ObjectClassRegistration& base_class) const;
  43. static void for_each(Function<void(const ObjectClassRegistration&)>);
  44. static const ObjectClassRegistration* find(StringView class_name);
  45. private:
  46. StringView m_class_name;
  47. Function<RefPtr<Object>()> m_factory;
  48. ObjectClassRegistration* m_parent_class { nullptr };
  49. };
  50. class InspectorServerConnection;
  51. enum class TimerShouldFireWhenNotVisible {
  52. No = 0,
  53. Yes
  54. };
  55. #define C_OBJECT(klass) \
  56. public: \
  57. virtual StringView class_name() const override { return #klass; } \
  58. template<typename Klass = klass, class... Args> \
  59. static NonnullRefPtr<klass> construct(Args&&... args) \
  60. { \
  61. return adopt_ref(*new Klass(forward<Args>(args)...)); \
  62. } \
  63. template<typename Klass = klass, class... Args> \
  64. static ErrorOr<NonnullRefPtr<klass>> try_create(Args&&... args) \
  65. { \
  66. return adopt_nonnull_ref_or_enomem(new (nothrow) Klass(forward<Args>(args)...)); \
  67. }
  68. #define C_OBJECT_ABSTRACT(klass) \
  69. public: \
  70. virtual StringView class_name() const override { return #klass; }
  71. class Object
  72. : public RefCounted<Object>
  73. , public Weakable<Object> {
  74. // NOTE: No C_OBJECT macro for Core::Object itself.
  75. AK_MAKE_NONCOPYABLE(Object);
  76. AK_MAKE_NONMOVABLE(Object);
  77. IntrusiveListNode<Object> m_all_objects_list_node;
  78. public:
  79. virtual ~Object();
  80. virtual StringView class_name() const = 0;
  81. const String& name() const { return m_name; }
  82. void set_name(String name) { m_name = move(name); }
  83. NonnullRefPtrVector<Object>& children() { return m_children; }
  84. const NonnullRefPtrVector<Object>& children() const { return m_children; }
  85. template<typename Callback>
  86. void for_each_child(Callback callback)
  87. {
  88. for (auto& child : m_children) {
  89. if (callback(child) == IterationDecision::Break)
  90. return;
  91. }
  92. }
  93. template<typename T, typename Callback>
  94. void for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>;
  95. template<typename T>
  96. T* find_child_of_type_named(const String&) requires IsBaseOf<Object, T>;
  97. template<typename T>
  98. T* find_descendant_of_type_named(const String&) requires IsBaseOf<Object, T>;
  99. bool is_ancestor_of(const Object&) const;
  100. Object* parent() { return m_parent; }
  101. const Object* parent() const { return m_parent; }
  102. void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
  103. void stop_timer();
  104. bool has_timer() const { return m_timer_id; }
  105. ErrorOr<void> try_add_child(Object&);
  106. void add_child(Object&);
  107. void insert_child_before(Object& new_child, Object& before_child);
  108. void remove_child(Object&);
  109. void remove_all_children();
  110. void set_event_filter(Function<bool(Core::Event&)>);
  111. void dump_tree(int indent = 0);
  112. void deferred_invoke(Function<void()>);
  113. void save_to(JsonObject&);
  114. bool set_property(String const& name, const JsonValue& value);
  115. JsonValue property(String const& name) const;
  116. const HashMap<String, NonnullOwnPtr<Property>>& properties() const { return m_properties; }
  117. static IntrusiveList<&Object::m_all_objects_list_node>& all_objects();
  118. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  119. void remove_from_parent()
  120. {
  121. if (m_parent)
  122. m_parent->remove_child(*this);
  123. }
  124. template<class T, class... Args>
  125. inline T& add(Args&&... args)
  126. {
  127. auto child = T::construct(forward<Args>(args)...);
  128. add_child(*child);
  129. return child;
  130. }
  131. template<class T, class... Args>
  132. inline ErrorOr<NonnullRefPtr<T>> try_add(Args&&... args)
  133. {
  134. auto child = TRY(T::try_create(forward<Args>(args)...));
  135. TRY(try_add_child(*child));
  136. return child;
  137. }
  138. virtual bool is_visible_for_timer_purposes() const;
  139. bool is_being_inspected() const { return m_inspector_count; }
  140. void increment_inspector_count(Badge<InspectorServerConnection>);
  141. void decrement_inspector_count(Badge<InspectorServerConnection>);
  142. protected:
  143. explicit Object(Object* parent = nullptr);
  144. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  145. virtual void event(Core::Event&);
  146. virtual void timer_event(TimerEvent&);
  147. virtual void custom_event(CustomEvent&);
  148. // NOTE: You may get child events for children that are not yet fully constructed!
  149. virtual void child_event(ChildEvent&);
  150. virtual void did_begin_inspection() { }
  151. virtual void did_end_inspection() { }
  152. private:
  153. Object* m_parent { nullptr };
  154. String m_name;
  155. int m_timer_id { 0 };
  156. unsigned m_inspector_count { 0 };
  157. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  158. NonnullRefPtrVector<Object> m_children;
  159. Function<bool(Core::Event&)> m_event_filter;
  160. };
  161. }
  162. template<>
  163. struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
  164. ErrorOr<void> format(FormatBuilder& builder, const Core::Object& value)
  165. {
  166. return AK::Formatter<FormatString>::format(builder, "{}({})", value.class_name(), &value);
  167. }
  168. };
  169. namespace Core {
  170. template<typename T, typename Callback>
  171. inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>
  172. {
  173. for_each_child([&](auto& child) {
  174. if (is<T>(child))
  175. return callback(static_cast<T&>(child));
  176. return IterationDecision::Continue;
  177. });
  178. }
  179. template<typename T>
  180. T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>
  181. {
  182. T* found_child = nullptr;
  183. for_each_child_of_type<T>([&](auto& child) {
  184. if (child.name() == name) {
  185. found_child = &child;
  186. return IterationDecision::Break;
  187. }
  188. return IterationDecision::Continue;
  189. });
  190. return found_child;
  191. }
  192. template<typename T>
  193. T* Object::find_descendant_of_type_named(String const& name) requires IsBaseOf<Object, T>
  194. {
  195. if (is<T>(*this) && this->name() == name) {
  196. return static_cast<T*>(this);
  197. }
  198. T* found_child = nullptr;
  199. for_each_child([&](auto& child) {
  200. found_child = child.template find_descendant_of_type_named<T>(name);
  201. if (found_child)
  202. return IterationDecision::Break;
  203. return IterationDecision::Continue;
  204. });
  205. return found_child;
  206. }
  207. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  208. register_property( \
  209. property_name, \
  210. [this] { return this->getter(); }, \
  211. [this](auto& value) { \
  212. this->setter(value.template to_number<int>()); \
  213. return true; \
  214. });
  215. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  216. register_property( \
  217. property_name, \
  218. [this] { return this->getter(); }, \
  219. [this](auto& value) { \
  220. this->setter(value.to_bool()); \
  221. return true; \
  222. });
  223. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  224. register_property( \
  225. property_name, \
  226. [this] { return this->getter(); }, \
  227. [this](auto& value) { \
  228. this->setter(value.to_string()); \
  229. return true; \
  230. });
  231. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  232. register_property( \
  233. property_name, \
  234. [this] { return this->getter(); }, \
  235. {});
  236. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  237. register_property( \
  238. property_name, \
  239. [this] { \
  240. auto rect = this->getter(); \
  241. JsonObject rect_object; \
  242. rect_object.set("x", rect.x()); \
  243. rect_object.set("y", rect.y()); \
  244. rect_object.set("width", rect.width()); \
  245. rect_object.set("height", rect.height()); \
  246. return rect_object; \
  247. }, \
  248. [this](auto& value) { \
  249. if (!value.is_object()) \
  250. return false; \
  251. Gfx::IntRect rect; \
  252. rect.set_x(value.as_object().get("x").to_i32()); \
  253. rect.set_y(value.as_object().get("y").to_i32()); \
  254. rect.set_width(value.as_object().get("width").to_i32()); \
  255. rect.set_height(value.as_object().get("height").to_i32()); \
  256. setter(rect); \
  257. return true; \
  258. });
  259. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  260. register_property( \
  261. property_name, \
  262. [this] { \
  263. auto size = this->getter(); \
  264. JsonObject size_object; \
  265. size_object.set("width", size.width()); \
  266. size_object.set("height", size.height()); \
  267. return size_object; \
  268. }, \
  269. [this](auto& value) { \
  270. if (!value.is_object()) \
  271. return false; \
  272. Gfx::IntSize size; \
  273. size.set_width(value.as_object().get("width").to_i32()); \
  274. size.set_height(value.as_object().get("height").to_i32()); \
  275. setter(size); \
  276. return true; \
  277. });
  278. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  279. register_property( \
  280. property_name, \
  281. [this]() -> JsonValue { \
  282. struct { \
  283. EnumType enum_value; \
  284. String string_value; \
  285. } options[] = { __VA_ARGS__ }; \
  286. auto enum_value = getter(); \
  287. for (size_t i = 0; i < array_size(options); ++i) { \
  288. auto& option = options[i]; \
  289. if (enum_value == option.enum_value) \
  290. return option.string_value; \
  291. } \
  292. return JsonValue(); \
  293. }, \
  294. [this](auto& value) { \
  295. struct { \
  296. EnumType enum_value; \
  297. String string_value; \
  298. } options[] = { __VA_ARGS__ }; \
  299. if (!value.is_string()) \
  300. return false; \
  301. auto string_value = value.as_string(); \
  302. for (size_t i = 0; i < array_size(options); ++i) { \
  303. auto& option = options[i]; \
  304. if (string_value == option.string_value) { \
  305. setter(option.enum_value); \
  306. return true; \
  307. } \
  308. } \
  309. return false; \
  310. })
  311. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  312. REGISTER_ENUM_PROPERTY( \
  313. property_name, getter, setter, Gfx::TextAlignment, \
  314. { Gfx::TextAlignment::Center, "Center" }, \
  315. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  316. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  317. { Gfx::TextAlignment::TopCenter, "TopCenter" }, \
  318. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  319. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  320. { Gfx::TextAlignment::BottomCenter, "BottomCenter" }, \
  321. { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
  322. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  323. #define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
  324. REGISTER_ENUM_PROPERTY( \
  325. property_name, getter, setter, unsigned, \
  326. { Gfx::FontWeight::Thin, "Thin" }, \
  327. { Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
  328. { Gfx::FontWeight::Light, "Light" }, \
  329. { Gfx::FontWeight::Regular, "Regular" }, \
  330. { Gfx::FontWeight::Medium, "Medium" }, \
  331. { Gfx::FontWeight::SemiBold, "SemiBold" }, \
  332. { Gfx::FontWeight::Bold, "Bold" }, \
  333. { Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
  334. { Gfx::FontWeight::Black, "Black" }, \
  335. { Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
  336. #define REGISTER_TEXT_WRAPPING_PROPERTY(property_name, getter, setter) \
  337. REGISTER_ENUM_PROPERTY( \
  338. property_name, getter, setter, Gfx::TextWrapping, \
  339. { Gfx::TextWrapping::Wrap, "Wrap" }, \
  340. { Gfx::TextWrapping::DontWrap, "DontWrap" })
  341. }