Object.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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(const String& 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(const String& class_name);
  37. private:
  38. String 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 save_to(JsonObject&);
  103. bool set_property(String const& name, const JsonValue& value);
  104. JsonValue property(String const& name) const;
  105. const HashMap<String, NonnullOwnPtr<Property>>& properties() const { return m_properties; }
  106. static IntrusiveList<Object, RawPtr<Object>, &Object::m_all_objects_list_node>& all_objects();
  107. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  108. void remove_from_parent()
  109. {
  110. if (m_parent)
  111. m_parent->remove_child(*this);
  112. }
  113. template<class T, class... Args>
  114. inline T& add(Args&&... args)
  115. {
  116. auto child = T::construct(forward<Args>(args)...);
  117. add_child(*child);
  118. return child;
  119. }
  120. virtual bool is_visible_for_timer_purposes() const;
  121. bool is_being_inspected() const { return m_inspector_count; }
  122. void increment_inspector_count(Badge<InspectorServerConnection>);
  123. void decrement_inspector_count(Badge<InspectorServerConnection>);
  124. virtual bool load_from_json(const JsonObject&, RefPtr<Core::Object> (*)(const String&)) { return false; }
  125. protected:
  126. explicit Object(Object* parent = nullptr);
  127. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  128. virtual void event(Core::Event&);
  129. virtual void timer_event(TimerEvent&);
  130. virtual void custom_event(CustomEvent&);
  131. // NOTE: You may get child events for children that are not yet fully constructed!
  132. virtual void child_event(ChildEvent&);
  133. virtual void did_begin_inspection() { }
  134. virtual void did_end_inspection() { }
  135. private:
  136. Object* m_parent { nullptr };
  137. String m_name;
  138. int m_timer_id { 0 };
  139. unsigned m_inspector_count { 0 };
  140. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  141. NonnullRefPtrVector<Object> m_children;
  142. Function<bool(Core::Event&)> m_event_filter;
  143. };
  144. }
  145. template<>
  146. struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
  147. void format(FormatBuilder& builder, const Core::Object& value)
  148. {
  149. return AK::Formatter<FormatString>::format(builder, "{}({})", value.class_name(), &value);
  150. }
  151. };
  152. namespace Core {
  153. template<typename T, typename Callback>
  154. inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>
  155. {
  156. for_each_child([&](auto& child) {
  157. if (is<T>(child))
  158. return callback(static_cast<T&>(child));
  159. return IterationDecision::Continue;
  160. });
  161. }
  162. template<typename T>
  163. T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>
  164. {
  165. T* found_child = nullptr;
  166. for_each_child_of_type<T>([&](auto& child) {
  167. if (child.name() == name) {
  168. found_child = &child;
  169. return IterationDecision::Break;
  170. }
  171. return IterationDecision::Continue;
  172. });
  173. return found_child;
  174. }
  175. template<typename T>
  176. T* Object::find_descendant_of_type_named(String const& name) requires IsBaseOf<Object, T>
  177. {
  178. if (is<T>(*this) && this->name() == name) {
  179. return static_cast<T*>(this);
  180. }
  181. T* found_child = nullptr;
  182. for_each_child([&](auto& child) {
  183. found_child = child.template find_descendant_of_type_named<T>(name);
  184. if (found_child)
  185. return IterationDecision::Break;
  186. return IterationDecision::Continue;
  187. });
  188. return found_child;
  189. }
  190. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  191. register_property( \
  192. property_name, \
  193. [this] { return this->getter(); }, \
  194. [this](auto& value) { \
  195. this->setter(value.template to_number<int>()); \
  196. return true; \
  197. });
  198. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  199. register_property( \
  200. property_name, \
  201. [this] { return this->getter(); }, \
  202. [this](auto& value) { \
  203. this->setter(value.to_bool()); \
  204. return true; \
  205. });
  206. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  207. register_property( \
  208. property_name, \
  209. [this] { return this->getter(); }, \
  210. [this](auto& value) { \
  211. this->setter(value.to_string()); \
  212. return true; \
  213. });
  214. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  215. register_property( \
  216. property_name, \
  217. [this] { return this->getter(); }, \
  218. {});
  219. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  220. register_property( \
  221. property_name, \
  222. [this] { \
  223. auto rect = this->getter(); \
  224. JsonObject rect_object; \
  225. rect_object.set("x", rect.x()); \
  226. rect_object.set("y", rect.y()); \
  227. rect_object.set("width", rect.width()); \
  228. rect_object.set("height", rect.height()); \
  229. return rect_object; \
  230. }, \
  231. [this](auto& value) { \
  232. if (!value.is_object()) \
  233. return false; \
  234. Gfx::IntRect rect; \
  235. rect.set_x(value.as_object().get("x").to_i32()); \
  236. rect.set_y(value.as_object().get("y").to_i32()); \
  237. rect.set_width(value.as_object().get("width").to_i32()); \
  238. rect.set_height(value.as_object().get("height").to_i32()); \
  239. setter(rect); \
  240. return true; \
  241. });
  242. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  243. register_property( \
  244. property_name, \
  245. [this] { \
  246. auto size = this->getter(); \
  247. JsonObject size_object; \
  248. size_object.set("width", size.width()); \
  249. size_object.set("height", size.height()); \
  250. return size_object; \
  251. }, \
  252. [this](auto& value) { \
  253. if (!value.is_object()) \
  254. return false; \
  255. Gfx::IntSize size; \
  256. size.set_width(value.as_object().get("width").to_i32()); \
  257. size.set_height(value.as_object().get("height").to_i32()); \
  258. setter(size); \
  259. return true; \
  260. });
  261. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  262. register_property( \
  263. property_name, \
  264. [this]() -> JsonValue { \
  265. struct { \
  266. EnumType enum_value; \
  267. String string_value; \
  268. } options[] = { __VA_ARGS__ }; \
  269. auto enum_value = getter(); \
  270. for (size_t i = 0; i < array_size(options); ++i) { \
  271. auto& option = options[i]; \
  272. if (enum_value == option.enum_value) \
  273. return option.string_value; \
  274. } \
  275. return JsonValue(); \
  276. }, \
  277. [this](auto& value) { \
  278. struct { \
  279. EnumType enum_value; \
  280. String string_value; \
  281. } options[] = { __VA_ARGS__ }; \
  282. if (!value.is_string()) \
  283. return false; \
  284. auto string_value = value.as_string(); \
  285. for (size_t i = 0; i < array_size(options); ++i) { \
  286. auto& option = options[i]; \
  287. if (string_value == option.string_value) { \
  288. setter(option.enum_value); \
  289. return true; \
  290. } \
  291. } \
  292. return false; \
  293. })
  294. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  295. REGISTER_ENUM_PROPERTY( \
  296. property_name, getter, setter, Gfx::TextAlignment, \
  297. { Gfx::TextAlignment::Center, "Center" }, \
  298. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  299. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  300. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  301. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  302. { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
  303. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  304. #define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
  305. REGISTER_ENUM_PROPERTY( \
  306. property_name, getter, setter, unsigned, \
  307. { Gfx::FontWeight::Thin, "Thin" }, \
  308. { Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
  309. { Gfx::FontWeight::Light, "Light" }, \
  310. { Gfx::FontWeight::Regular, "Regular" }, \
  311. { Gfx::FontWeight::Medium, "Medium" }, \
  312. { Gfx::FontWeight::SemiBold, "SemiBold" }, \
  313. { Gfx::FontWeight::Bold, "Bold" }, \
  314. { Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
  315. { Gfx::FontWeight::Black, "Black" }, \
  316. { Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
  317. }