Object.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Forward.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/IntrusiveList.h>
  30. #include <AK/Noncopyable.h>
  31. #include <AK/NonnullRefPtrVector.h>
  32. #include <AK/String.h>
  33. #include <AK/TypeCasts.h>
  34. #include <AK/Weakable.h>
  35. #include <LibCore/Forward.h>
  36. #include <LibCore/Property.h>
  37. namespace Core {
  38. class RPCClient;
  39. enum class TimerShouldFireWhenNotVisible {
  40. No = 0,
  41. Yes
  42. };
  43. #define C_OBJECT(klass) \
  44. public: \
  45. virtual const char* class_name() const override { return #klass; } \
  46. template<class... Args> \
  47. static inline NonnullRefPtr<klass> construct(Args&&... args) \
  48. { \
  49. return adopt(*new klass(forward<Args>(args)...)); \
  50. }
  51. #define C_OBJECT_ABSTRACT(klass) \
  52. public: \
  53. virtual const char* class_name() const override { return #klass; }
  54. class Object
  55. : public RefCounted<Object>
  56. , public Weakable<Object> {
  57. // NOTE: No C_OBJECT macro for Core::Object itself.
  58. AK_MAKE_NONCOPYABLE(Object);
  59. AK_MAKE_NONMOVABLE(Object);
  60. IntrusiveListNode m_all_objects_list_node;
  61. public:
  62. virtual ~Object();
  63. virtual const char* class_name() const = 0;
  64. virtual void event(Core::Event&);
  65. const String& name() const { return m_name; }
  66. void set_name(const StringView& name) { m_name = name; }
  67. NonnullRefPtrVector<Object>& children() { return m_children; }
  68. const NonnullRefPtrVector<Object>& children() const { return m_children; }
  69. template<typename Callback>
  70. void for_each_child(Callback callback)
  71. {
  72. for (auto& child : m_children) {
  73. if (callback(child) == IterationDecision::Break)
  74. return;
  75. }
  76. }
  77. template<typename T, typename Callback>
  78. void for_each_child_of_type(Callback callback);
  79. bool is_ancestor_of(const Object&) const;
  80. Object* parent() { return m_parent; }
  81. const Object* parent() const { return m_parent; }
  82. void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
  83. void stop_timer();
  84. bool has_timer() const { return m_timer_id; }
  85. void add_child(Object&);
  86. void insert_child_before(Object& new_child, Object& before_child);
  87. void remove_child(Object&);
  88. void remove_all_children();
  89. void dump_tree(int indent = 0);
  90. void deferred_invoke(Function<void(Object&)>);
  91. bool is_widget() const { return m_widget; }
  92. virtual bool is_action() const { return false; }
  93. virtual bool is_window() const { return false; }
  94. void save_to(AK::JsonObject&);
  95. bool set_property(const StringView& name, const JsonValue& value);
  96. JsonValue property(const StringView& name);
  97. const HashMap<String, NonnullOwnPtr<Property>>& properties() const { return m_properties; }
  98. static IntrusiveList<Object, &Object::m_all_objects_list_node>& all_objects();
  99. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  100. void remove_from_parent()
  101. {
  102. if (m_parent)
  103. m_parent->remove_child(*this);
  104. }
  105. template<class T, class... Args>
  106. inline T& add(Args&&... args)
  107. {
  108. auto child = T::construct(forward<Args>(args)...);
  109. add_child(*child);
  110. return child;
  111. }
  112. virtual bool is_visible_for_timer_purposes() const;
  113. bool is_being_inspected() const { return m_inspector_count; }
  114. void increment_inspector_count(Badge<RPCClient>);
  115. void decrement_inspector_count(Badge<RPCClient>);
  116. protected:
  117. explicit Object(Object* parent = nullptr, bool is_widget = false);
  118. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  119. virtual void timer_event(TimerEvent&);
  120. virtual void custom_event(CustomEvent&);
  121. // NOTE: You may get child events for children that are not yet fully constructed!
  122. virtual void child_event(ChildEvent&);
  123. virtual void did_begin_inspection() { }
  124. virtual void did_end_inspection() { }
  125. private:
  126. Object* m_parent { nullptr };
  127. String m_name;
  128. int m_timer_id { 0 };
  129. unsigned m_inspector_count { 0 };
  130. bool m_widget { false };
  131. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  132. NonnullRefPtrVector<Object> m_children;
  133. };
  134. }
  135. namespace AK {
  136. template<>
  137. struct Formatter<Core::Object> : Formatter<StringView> {
  138. void format(TypeErasedFormatParams&, FormatBuilder&, const Core::Object&);
  139. };
  140. }
  141. namespace Core {
  142. template<typename T, typename Callback>
  143. inline void Object::for_each_child_of_type(Callback callback)
  144. {
  145. for_each_child([&](auto& child) {
  146. if (is<T>(child))
  147. return callback(downcast<T>(child));
  148. return IterationDecision::Continue;
  149. });
  150. }
  151. const LogStream& operator<<(const LogStream&, const Object&);
  152. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  153. register_property( \
  154. property_name, \
  155. [this] { return this->getter(); }, \
  156. [this](auto& value) { \
  157. this->setter(value.template to_number<int>()); \
  158. return true; \
  159. });
  160. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  161. register_property( \
  162. property_name, \
  163. [this] { return this->getter(); }, \
  164. [this](auto& value) { \
  165. this->setter(value.to_bool()); \
  166. return true; \
  167. });
  168. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  169. register_property( \
  170. property_name, \
  171. [this] { return this->getter(); }, \
  172. [this](auto& value) { \
  173. this->setter(value.to_string()); \
  174. return true; \
  175. });
  176. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  177. register_property( \
  178. property_name, \
  179. [this] { return this->getter(); }, \
  180. nullptr);
  181. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  182. register_property( \
  183. property_name, \
  184. [this] { \
  185. auto rect = this->getter(); \
  186. JsonObject rect_object; \
  187. rect_object.set("x", rect.x()); \
  188. rect_object.set("y", rect.y()); \
  189. rect_object.set("width", rect.width()); \
  190. rect_object.set("height", rect.height()); \
  191. return rect_object; \
  192. }, \
  193. [this](auto& value) { \
  194. if (!value.is_object()) \
  195. return false; \
  196. Gfx::IntRect rect; \
  197. rect.set_x(value.as_object().get("x").to_i32()); \
  198. rect.set_y(value.as_object().get("y").to_i32()); \
  199. rect.set_width(value.as_object().get("width").to_i32()); \
  200. rect.set_height(value.as_object().get("height").to_i32()); \
  201. setter(rect); \
  202. return true; \
  203. });
  204. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  205. register_property( \
  206. property_name, \
  207. [this] { \
  208. auto size = this->getter(); \
  209. JsonObject size_object; \
  210. size_object.set("width", size.width()); \
  211. size_object.set("height", size.height()); \
  212. return size_object; \
  213. }, \
  214. [this](auto& value) { \
  215. if (!value.is_object()) \
  216. return false; \
  217. Gfx::IntSize size; \
  218. size.set_width(value.as_object().get("width").to_i32()); \
  219. size.set_height(value.as_object().get("height").to_i32()); \
  220. setter(size); \
  221. return true; \
  222. });
  223. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  224. register_property( \
  225. property_name, \
  226. [this]() -> JsonValue { \
  227. struct { \
  228. EnumType enum_value; \
  229. String string_value; \
  230. } options[] = { __VA_ARGS__ }; \
  231. auto enum_value = getter(); \
  232. for (size_t i = 0; i < array_size(options); ++i) { \
  233. auto& option = options[i]; \
  234. if (enum_value == option.enum_value) \
  235. return option.string_value; \
  236. } \
  237. return JsonValue(); \
  238. }, \
  239. [this](auto& value) { \
  240. struct { \
  241. EnumType enum_value; \
  242. String string_value; \
  243. } options[] = { __VA_ARGS__ }; \
  244. auto string_value = value.as_string(); \
  245. for (size_t i = 0; i < array_size(options); ++i) { \
  246. auto& option = options[i]; \
  247. if (string_value == option.string_value) { \
  248. setter(option.enum_value); \
  249. return true; \
  250. } \
  251. } \
  252. return false; \
  253. })
  254. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  255. REGISTER_ENUM_PROPERTY( \
  256. property_name, getter, setter, Gfx::TextAlignment, \
  257. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  258. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  259. { Gfx::TextAlignment::Center, "Center" }, \
  260. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  261. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  262. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  263. }