Object.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. public:
  61. IntrusiveListNode m_all_objects_list_node;
  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 dump_tree(int indent = 0);
  89. void deferred_invoke(Function<void(Object&)>);
  90. bool is_widget() const { return m_widget; }
  91. virtual bool is_action() const { return false; }
  92. virtual bool is_window() const { return false; }
  93. void save_to(AK::JsonObject&);
  94. bool set_property(const StringView& name, const JsonValue& value);
  95. JsonValue property(const StringView& name);
  96. static IntrusiveList<Object, &Object::m_all_objects_list_node>& all_objects();
  97. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  98. void remove_from_parent()
  99. {
  100. if (m_parent)
  101. m_parent->remove_child(*this);
  102. }
  103. template<class T, class... Args>
  104. inline T& add(Args&&... args)
  105. {
  106. auto child = T::construct(forward<Args>(args)...);
  107. add_child(*child);
  108. return child;
  109. }
  110. virtual bool is_visible_for_timer_purposes() const;
  111. bool is_being_inspected() const { return m_inspector_count; }
  112. void increment_inspector_count(Badge<RPCClient>);
  113. void decrement_inspector_count(Badge<RPCClient>);
  114. protected:
  115. explicit Object(Object* parent = nullptr, bool is_widget = false);
  116. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  117. virtual void timer_event(TimerEvent&);
  118. virtual void custom_event(CustomEvent&);
  119. // NOTE: You may get child events for children that are not yet fully constructed!
  120. virtual void child_event(ChildEvent&);
  121. virtual void did_begin_inspection() { }
  122. virtual void did_end_inspection() { }
  123. private:
  124. Object* m_parent { nullptr };
  125. String m_name;
  126. int m_timer_id { 0 };
  127. unsigned m_inspector_count { 0 };
  128. bool m_widget { false };
  129. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  130. NonnullRefPtrVector<Object> m_children;
  131. };
  132. }
  133. namespace Core {
  134. template<typename T, typename Callback>
  135. inline void Object::for_each_child_of_type(Callback callback)
  136. {
  137. for_each_child([&](auto& child) {
  138. if (is<T>(child))
  139. return callback(downcast<T>(child));
  140. return IterationDecision::Continue;
  141. });
  142. }
  143. const LogStream& operator<<(const LogStream&, const Object&);
  144. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  145. register_property( \
  146. property_name, \
  147. [this] { return this->getter(); }, \
  148. [this](auto& value) { \
  149. this->setter(value.template to_number<int>()); \
  150. return true; \
  151. });
  152. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  153. register_property( \
  154. property_name, \
  155. [this] { return this->getter(); }, \
  156. [this](auto& value) { \
  157. this->setter(value.to_bool()); \
  158. return true; \
  159. });
  160. #define REGISTER_STRING_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_string()); \
  166. return true; \
  167. });
  168. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  169. register_property( \
  170. property_name, \
  171. [this] { return this->getter(); }, \
  172. nullptr);
  173. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  174. register_property( \
  175. property_name, \
  176. [this] { \
  177. auto rect = this->getter(); \
  178. JsonObject rect_object; \
  179. rect_object.set("x", rect.x()); \
  180. rect_object.set("y", rect.y()); \
  181. rect_object.set("width", rect.width()); \
  182. rect_object.set("height", rect.height()); \
  183. return rect_object; \
  184. }, \
  185. [this](auto& value) { \
  186. if (!value.is_object()) \
  187. return false; \
  188. Gfx::IntRect rect; \
  189. rect.set_x(value.as_object().get("x").to_i32()); \
  190. rect.set_y(value.as_object().get("y").to_i32()); \
  191. rect.set_width(value.as_object().get("width").to_i32()); \
  192. rect.set_height(value.as_object().get("height").to_i32()); \
  193. setter(rect); \
  194. return true; \
  195. });
  196. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  197. register_property( \
  198. property_name, \
  199. [this] { \
  200. auto size = this->getter(); \
  201. JsonObject size_object; \
  202. size_object.set("width", size.width()); \
  203. size_object.set("height", size.height()); \
  204. return size_object; \
  205. }, \
  206. [this](auto& value) { \
  207. if (!value.is_object()) \
  208. return false; \
  209. Gfx::IntSize size; \
  210. size.set_width(value.as_object().get("width").to_i32()); \
  211. size.set_height(value.as_object().get("height").to_i32()); \
  212. setter(size); \
  213. return true; \
  214. });
  215. #define REGISTER_SIZE_POLICY_PROPERTY(property_name, getter, setter) \
  216. register_property( \
  217. property_name, [this]() -> JsonValue { \
  218. auto policy = this->getter(); \
  219. if (policy == GUI::SizePolicy::Fill) \
  220. return "Fill"; \
  221. if (policy == GUI::SizePolicy::Fixed) \
  222. return "Fixed"; \
  223. return JsonValue(); }, \
  224. [this](auto& value) { \
  225. if (!value.is_string()) \
  226. return false; \
  227. if (value.as_string() == "Fill") { \
  228. setter(GUI::SizePolicy::Fill); \
  229. return true; \
  230. } \
  231. if (value.as_string() == "Fixed") { \
  232. setter(GUI::SizePolicy::Fixed); \
  233. return true; \
  234. } \
  235. return false; \
  236. });
  237. }