Object.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibCore/EventReceiver.h>
  8. #include <LibGUI/Forward.h>
  9. #include <LibGUI/Property.h>
  10. namespace GUI {
  11. #define REGISTER_ABSTRACT_GUI_OBJECT(namespace_, class_name) \
  12. namespace GUI::Registration { \
  13. ::GUI::ObjectClassRegistration registration_##class_name(#namespace_ "::" #class_name##sv, []() { return Error::from_string_literal("Attempted to construct an abstract object."); }); \
  14. }
  15. #define REGISTER_GUI_OBJECT(namespace_, class_name) \
  16. namespace GUI::Registration { \
  17. ::GUI::ObjectClassRegistration registration_##class_name(#namespace_ "::" #class_name##sv, []() { return namespace_::class_name::try_create(); }); \
  18. }
  19. class ObjectClassRegistration {
  20. AK_MAKE_NONCOPYABLE(ObjectClassRegistration);
  21. AK_MAKE_NONMOVABLE(ObjectClassRegistration);
  22. public:
  23. ObjectClassRegistration(StringView class_name, Function<ErrorOr<NonnullRefPtr<Object>>()> factory, ObjectClassRegistration* parent_class = nullptr);
  24. ~ObjectClassRegistration() = default;
  25. StringView class_name() const { return m_class_name; }
  26. ObjectClassRegistration const* parent_class() const { return m_parent_class; }
  27. ErrorOr<NonnullRefPtr<Object>> construct() const { return m_factory(); }
  28. bool is_derived_from(ObjectClassRegistration const& base_class) const;
  29. static void for_each(Function<void(ObjectClassRegistration const&)>);
  30. static ObjectClassRegistration const* find(StringView class_name);
  31. private:
  32. StringView m_class_name;
  33. Function<ErrorOr<NonnullRefPtr<Object>>()> m_factory;
  34. ObjectClassRegistration* m_parent_class { nullptr };
  35. };
  36. class Object : public Core::EventReceiver {
  37. C_OBJECT_ABSTRACT(Object);
  38. public:
  39. virtual ~Object() override;
  40. bool set_property(DeprecatedString const& name, JsonValue const& value);
  41. JsonValue property(DeprecatedString const& name) const;
  42. HashMap<DeprecatedString, NonnullOwnPtr<Property>> const& properties() const { return m_properties; }
  43. protected:
  44. explicit Object(Core::EventReceiver* parent = nullptr);
  45. void register_property(DeprecatedString const& name, Function<JsonValue()> getter, Function<bool(JsonValue const&)> setter = nullptr);
  46. private:
  47. HashMap<DeprecatedString, NonnullOwnPtr<Property>> m_properties;
  48. };
  49. }
  50. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  51. register_property( \
  52. property_name, \
  53. [this] { return this->getter(); }, \
  54. [this](auto& value) { \
  55. this->setter(value.template to_number<int>()); \
  56. return true; \
  57. });
  58. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  59. register_property( \
  60. property_name, \
  61. [this] { return this->getter(); }, \
  62. [this](auto& value) { \
  63. this->setter(value.to_bool()); \
  64. return true; \
  65. });
  66. // FIXME: Port JsonValue to the new String class.
  67. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  68. register_property( \
  69. property_name, \
  70. [this]() { return this->getter().to_deprecated_string(); }, \
  71. [this](auto& value) { \
  72. this->setter(String::from_deprecated_string(value.to_deprecated_string()).release_value_but_fixme_should_propagate_errors()); \
  73. return true; \
  74. });
  75. #define REGISTER_DEPRECATED_STRING_PROPERTY(property_name, getter, setter) \
  76. register_property( \
  77. property_name, \
  78. [this] { return this->getter(); }, \
  79. [this](auto& value) { \
  80. this->setter(value.to_deprecated_string()); \
  81. return true; \
  82. });
  83. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  84. register_property( \
  85. property_name, \
  86. [this] { return this->getter(); }, \
  87. {});
  88. #define REGISTER_WRITE_ONLY_STRING_PROPERTY(property_name, setter) \
  89. register_property( \
  90. property_name, \
  91. {}, \
  92. [this](auto& value) { \
  93. this->setter(value.to_deprecated_string()); \
  94. return true; \
  95. });
  96. #define REGISTER_READONLY_SIZE_PROPERTY(property_name, getter) \
  97. register_property( \
  98. property_name, \
  99. [this] { \
  100. auto size = this->getter(); \
  101. JsonArray size_array; \
  102. size_array.must_append(size.width()); \
  103. size_array.must_append(size.height()); \
  104. return size_array; \
  105. }, \
  106. {});
  107. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  108. register_property( \
  109. property_name, \
  110. [this] { \
  111. auto rect = this->getter(); \
  112. JsonObject rect_object; \
  113. rect_object.set("x"sv, rect.x()); \
  114. rect_object.set("y"sv, rect.y()); \
  115. rect_object.set("width"sv, rect.width()); \
  116. rect_object.set("height"sv, rect.height()); \
  117. return rect_object; \
  118. }, \
  119. [this](auto& value) { \
  120. Gfx::IntRect rect; \
  121. if (value.is_object()) { \
  122. rect.set_x(value.as_object().get_i32("x"sv).value_or(0)); \
  123. rect.set_y(value.as_object().get_i32("y"sv).value_or(0)); \
  124. rect.set_width(value.as_object().get_i32("width"sv).value_or(0)); \
  125. rect.set_height(value.as_object().get_i32("height"sv).value_or(0)); \
  126. } else if (value.is_array() && value.as_array().size() == 4) { \
  127. rect.set_x(value.as_array()[0].to_i32()); \
  128. rect.set_y(value.as_array()[1].to_i32()); \
  129. rect.set_width(value.as_array()[2].to_i32()); \
  130. rect.set_height(value.as_array()[3].to_i32()); \
  131. } else { \
  132. return false; \
  133. } \
  134. setter(rect); \
  135. \
  136. return true; \
  137. });
  138. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  139. register_property( \
  140. property_name, \
  141. [this] { \
  142. auto size = this->getter(); \
  143. JsonArray size_array; \
  144. size_array.must_append(size.width()); \
  145. size_array.must_append(size.height()); \
  146. return size_array; \
  147. }, \
  148. [this](auto& value) { \
  149. if (!value.is_array()) \
  150. return false; \
  151. Gfx::IntSize size; \
  152. size.set_width(value.as_array()[0].to_i32()); \
  153. size.set_height(value.as_array()[1].to_i32()); \
  154. setter(size); \
  155. return true; \
  156. });
  157. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  158. register_property( \
  159. property_name, \
  160. [this]() -> JsonValue { \
  161. struct { \
  162. EnumType enum_value; \
  163. DeprecatedString string_value; \
  164. } options[] = { __VA_ARGS__ }; \
  165. auto enum_value = getter(); \
  166. for (size_t i = 0; i < array_size(options); ++i) { \
  167. auto& option = options[i]; \
  168. if (enum_value == option.enum_value) \
  169. return option.string_value; \
  170. } \
  171. return JsonValue(); \
  172. }, \
  173. [this](auto& value) { \
  174. struct { \
  175. EnumType enum_value; \
  176. DeprecatedString string_value; \
  177. } options[] = { __VA_ARGS__ }; \
  178. if (!value.is_string()) \
  179. return false; \
  180. auto string_value = value.as_string(); \
  181. for (size_t i = 0; i < array_size(options); ++i) { \
  182. auto& option = options[i]; \
  183. if (string_value == option.string_value) { \
  184. setter(option.enum_value); \
  185. return true; \
  186. } \
  187. } \
  188. return false; \
  189. })
  190. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  191. REGISTER_ENUM_PROPERTY( \
  192. property_name, getter, setter, Gfx::TextAlignment, \
  193. { Gfx::TextAlignment::Center, "Center" }, \
  194. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  195. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  196. { Gfx::TextAlignment::TopCenter, "TopCenter" }, \
  197. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  198. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  199. { Gfx::TextAlignment::BottomCenter, "BottomCenter" }, \
  200. { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
  201. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  202. #define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
  203. REGISTER_ENUM_PROPERTY( \
  204. property_name, getter, setter, unsigned, \
  205. { Gfx::FontWeight::Thin, "Thin" }, \
  206. { Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
  207. { Gfx::FontWeight::Light, "Light" }, \
  208. { Gfx::FontWeight::Regular, "Regular" }, \
  209. { Gfx::FontWeight::Medium, "Medium" }, \
  210. { Gfx::FontWeight::SemiBold, "SemiBold" }, \
  211. { Gfx::FontWeight::Bold, "Bold" }, \
  212. { Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
  213. { Gfx::FontWeight::Black, "Black" }, \
  214. { Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
  215. #define REGISTER_TEXT_WRAPPING_PROPERTY(property_name, getter, setter) \
  216. REGISTER_ENUM_PROPERTY( \
  217. property_name, getter, setter, Gfx::TextWrapping, \
  218. { Gfx::TextWrapping::Wrap, "Wrap" }, \
  219. { Gfx::TextWrapping::DontWrap, "DontWrap" })