Object.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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##sv, []() { 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##sv, []() { 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. ObjectClassRegistration const* parent_class() const { return m_parent_class; }
  41. RefPtr<Object> construct() const { return m_factory(); }
  42. bool is_derived_from(ObjectClassRegistration const& base_class) const;
  43. static void for_each(Function<void(ObjectClassRegistration const&)>);
  44. static ObjectClassRegistration const* 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##sv; } \
  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##sv; }
  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. template<typename T>
  82. bool fast_is() const = delete;
  83. virtual bool is_widget() const { return false; }
  84. String const& name() const { return m_name; }
  85. void set_name(String name) { m_name = move(name); }
  86. NonnullRefPtrVector<Object>& children() { return m_children; }
  87. NonnullRefPtrVector<Object> const& children() const { return m_children; }
  88. template<typename Callback>
  89. void for_each_child(Callback callback)
  90. {
  91. for (auto& child : m_children) {
  92. if (callback(child) == IterationDecision::Break)
  93. return;
  94. }
  95. }
  96. template<typename T, typename Callback>
  97. void for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>;
  98. template<typename T>
  99. T* find_child_of_type_named(String const&) requires IsBaseOf<Object, T>;
  100. template<typename T>
  101. T* find_descendant_of_type_named(String const&) requires IsBaseOf<Object, T>;
  102. bool is_ancestor_of(Object const&) const;
  103. Object* parent() { return m_parent; }
  104. Object const* parent() const { return m_parent; }
  105. void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
  106. void stop_timer();
  107. bool has_timer() const { return m_timer_id; }
  108. ErrorOr<void> try_add_child(Object&);
  109. void add_child(Object&);
  110. void insert_child_before(Object& new_child, Object& before_child);
  111. void remove_child(Object&);
  112. void remove_all_children();
  113. void set_event_filter(Function<bool(Core::Event&)>);
  114. void dump_tree(int indent = 0);
  115. void deferred_invoke(Function<void()>);
  116. void save_to(JsonObject&);
  117. bool set_property(String const& name, JsonValue const& value);
  118. JsonValue property(String const& name) const;
  119. HashMap<String, NonnullOwnPtr<Property>> const& properties() const { return m_properties; }
  120. static IntrusiveList<&Object::m_all_objects_list_node>& all_objects();
  121. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  122. void remove_from_parent()
  123. {
  124. if (m_parent)
  125. m_parent->remove_child(*this);
  126. }
  127. template<class T, class... Args>
  128. inline T& add(Args&&... args)
  129. {
  130. auto child = T::construct(forward<Args>(args)...);
  131. add_child(*child);
  132. return child;
  133. }
  134. template<class T, class... Args>
  135. inline ErrorOr<NonnullRefPtr<T>> try_add(Args&&... args)
  136. {
  137. auto child = TRY(T::try_create(forward<Args>(args)...));
  138. TRY(try_add_child(*child));
  139. return child;
  140. }
  141. virtual bool is_visible_for_timer_purposes() const;
  142. bool is_being_inspected() const { return m_inspector_count; }
  143. void increment_inspector_count(Badge<InspectorServerConnection>);
  144. void decrement_inspector_count(Badge<InspectorServerConnection>);
  145. protected:
  146. explicit Object(Object* parent = nullptr);
  147. void register_property(String const& name, Function<JsonValue()> getter, Function<bool(JsonValue const&)> setter = nullptr);
  148. virtual void event(Core::Event&);
  149. virtual void timer_event(TimerEvent&);
  150. virtual void custom_event(CustomEvent&);
  151. // NOTE: You may get child events for children that are not yet fully constructed!
  152. virtual void child_event(ChildEvent&);
  153. virtual void did_begin_inspection() { }
  154. virtual void did_end_inspection() { }
  155. private:
  156. Object* m_parent { nullptr };
  157. String m_name;
  158. int m_timer_id { 0 };
  159. unsigned m_inspector_count { 0 };
  160. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  161. NonnullRefPtrVector<Object> m_children;
  162. Function<bool(Core::Event&)> m_event_filter;
  163. };
  164. }
  165. template<>
  166. struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
  167. ErrorOr<void> format(FormatBuilder& builder, Core::Object const& value)
  168. {
  169. return AK::Formatter<FormatString>::format(builder, "{}({})"sv, value.class_name(), &value);
  170. }
  171. };
  172. namespace Core {
  173. template<typename T, typename Callback>
  174. inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>
  175. {
  176. for_each_child([&](auto& child) {
  177. if (is<T>(child))
  178. return callback(static_cast<T&>(child));
  179. return IterationDecision::Continue;
  180. });
  181. }
  182. template<typename T>
  183. T* Object::find_child_of_type_named(String const& name) requires IsBaseOf<Object, T>
  184. {
  185. T* found_child = nullptr;
  186. for_each_child_of_type<T>([&](auto& child) {
  187. if (child.name() == name) {
  188. found_child = &child;
  189. return IterationDecision::Break;
  190. }
  191. return IterationDecision::Continue;
  192. });
  193. return found_child;
  194. }
  195. template<typename T>
  196. T* Object::find_descendant_of_type_named(String const& name) requires IsBaseOf<Object, T>
  197. {
  198. if (is<T>(*this) && this->name() == name) {
  199. return static_cast<T*>(this);
  200. }
  201. T* found_child = nullptr;
  202. for_each_child([&](auto& child) {
  203. found_child = child.template find_descendant_of_type_named<T>(name);
  204. if (found_child)
  205. return IterationDecision::Break;
  206. return IterationDecision::Continue;
  207. });
  208. return found_child;
  209. }
  210. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  211. register_property( \
  212. property_name, \
  213. [this] { return this->getter(); }, \
  214. [this](auto& value) { \
  215. this->setter(value.template to_number<int>()); \
  216. return true; \
  217. });
  218. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  219. register_property( \
  220. property_name, \
  221. [this] { return this->getter(); }, \
  222. [this](auto& value) { \
  223. this->setter(value.to_bool()); \
  224. return true; \
  225. });
  226. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  227. register_property( \
  228. property_name, \
  229. [this] { return this->getter(); }, \
  230. [this](auto& value) { \
  231. this->setter(value.to_string()); \
  232. return true; \
  233. });
  234. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  235. register_property( \
  236. property_name, \
  237. [this] { return this->getter(); }, \
  238. {});
  239. #define REGISTER_READONLY_SIZE_PROPERTY(property_name, getter) \
  240. register_property( \
  241. property_name, \
  242. [this] { \
  243. auto size = this->getter(); \
  244. JsonArray size_array; \
  245. size_array.append(size.width()); \
  246. size_array.append(size.height()); \
  247. return size_array; \
  248. }, \
  249. {});
  250. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  251. register_property( \
  252. property_name, \
  253. [this] { \
  254. auto rect = this->getter(); \
  255. JsonObject rect_object; \
  256. rect_object.set("x"sv, rect.x()); \
  257. rect_object.set("y"sv, rect.y()); \
  258. rect_object.set("width"sv, rect.width()); \
  259. rect_object.set("height"sv, rect.height()); \
  260. return rect_object; \
  261. }, \
  262. [this](auto& value) { \
  263. Gfx::IntRect rect; \
  264. if (value.is_object()) { \
  265. rect.set_x(value.as_object().get("x"sv).to_i32()); \
  266. rect.set_y(value.as_object().get("y"sv).to_i32()); \
  267. rect.set_width(value.as_object().get("width"sv).to_i32()); \
  268. rect.set_height(value.as_object().get("height"sv).to_i32()); \
  269. } else if (value.is_array() && value.as_array().size() == 4) { \
  270. rect.set_x(value.as_array()[0].to_i32()); \
  271. rect.set_y(value.as_array()[1].to_i32()); \
  272. rect.set_width(value.as_array()[2].to_i32()); \
  273. rect.set_height(value.as_array()[3].to_i32()); \
  274. } else { \
  275. return false; \
  276. } \
  277. setter(rect); \
  278. \
  279. return true; \
  280. });
  281. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  282. register_property( \
  283. property_name, \
  284. [this] { \
  285. auto size = this->getter(); \
  286. JsonArray size_array; \
  287. size_array.append(size.width()); \
  288. size_array.append(size.height()); \
  289. return size_array; \
  290. }, \
  291. [this](auto& value) { \
  292. if (!value.is_array()) \
  293. return false; \
  294. Gfx::IntSize size; \
  295. size.set_width(value.as_array()[0].to_i32()); \
  296. size.set_height(value.as_array()[1].to_i32()); \
  297. setter(size); \
  298. return true; \
  299. });
  300. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  301. register_property( \
  302. property_name, \
  303. [this]() -> JsonValue { \
  304. struct { \
  305. EnumType enum_value; \
  306. String string_value; \
  307. } options[] = { __VA_ARGS__ }; \
  308. auto enum_value = getter(); \
  309. for (size_t i = 0; i < array_size(options); ++i) { \
  310. auto& option = options[i]; \
  311. if (enum_value == option.enum_value) \
  312. return option.string_value; \
  313. } \
  314. return JsonValue(); \
  315. }, \
  316. [this](auto& value) { \
  317. struct { \
  318. EnumType enum_value; \
  319. String string_value; \
  320. } options[] = { __VA_ARGS__ }; \
  321. if (!value.is_string()) \
  322. return false; \
  323. auto string_value = value.as_string(); \
  324. for (size_t i = 0; i < array_size(options); ++i) { \
  325. auto& option = options[i]; \
  326. if (string_value == option.string_value) { \
  327. setter(option.enum_value); \
  328. return true; \
  329. } \
  330. } \
  331. return false; \
  332. })
  333. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  334. REGISTER_ENUM_PROPERTY( \
  335. property_name, getter, setter, Gfx::TextAlignment, \
  336. { Gfx::TextAlignment::Center, "Center" }, \
  337. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  338. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  339. { Gfx::TextAlignment::TopCenter, "TopCenter" }, \
  340. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  341. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  342. { Gfx::TextAlignment::BottomCenter, "BottomCenter" }, \
  343. { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
  344. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  345. #define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
  346. REGISTER_ENUM_PROPERTY( \
  347. property_name, getter, setter, unsigned, \
  348. { Gfx::FontWeight::Thin, "Thin" }, \
  349. { Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
  350. { Gfx::FontWeight::Light, "Light" }, \
  351. { Gfx::FontWeight::Regular, "Regular" }, \
  352. { Gfx::FontWeight::Medium, "Medium" }, \
  353. { Gfx::FontWeight::SemiBold, "SemiBold" }, \
  354. { Gfx::FontWeight::Bold, "Bold" }, \
  355. { Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
  356. { Gfx::FontWeight::Black, "Black" }, \
  357. { Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
  358. #define REGISTER_TEXT_WRAPPING_PROPERTY(property_name, getter, setter) \
  359. REGISTER_ENUM_PROPERTY( \
  360. property_name, getter, setter, Gfx::TextWrapping, \
  361. { Gfx::TextWrapping::Wrap, "Wrap" }, \
  362. { Gfx::TextWrapping::DontWrap, "DontWrap" })
  363. }