Object.h 17 KB

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