mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 13:47:35 +00:00
LibGUI: Move widget registration to LibCore
This also moves Widget::load_from_json into Core::Object as a virtual function in order to allow loading non-widget objects in GML (e.g. BoxLayout). Co-authored-by: Gunnar Beutner <gbeutner@serenityos.org>
This commit is contained in:
parent
6e101adc28
commit
3aaffa2c47
13 changed files with 134 additions and 85 deletions
|
@ -27,6 +27,7 @@ class NetworkJob;
|
|||
class NetworkResponse;
|
||||
class Notifier;
|
||||
class Object;
|
||||
class ObjectClassRegistration;
|
||||
class ProcessStatisticsReader;
|
||||
class Socket;
|
||||
class SocketAddress;
|
||||
|
|
|
@ -250,4 +250,44 @@ void Object::set_event_filter(Function<bool(Core::Event&)> filter)
|
|||
m_event_filter = move(filter);
|
||||
}
|
||||
|
||||
static HashMap<String, ObjectClassRegistration*>& object_classes()
|
||||
{
|
||||
static HashMap<String, ObjectClassRegistration*>* map;
|
||||
if (!map)
|
||||
map = new HashMap<String, ObjectClassRegistration*>;
|
||||
return *map;
|
||||
}
|
||||
|
||||
ObjectClassRegistration::ObjectClassRegistration(const String& class_name, Function<NonnullRefPtr<Object>()> factory, ObjectClassRegistration* parent_class)
|
||||
: m_class_name(class_name)
|
||||
, m_factory(move(factory))
|
||||
, m_parent_class(parent_class)
|
||||
{
|
||||
object_classes().set(class_name, this);
|
||||
}
|
||||
|
||||
ObjectClassRegistration::~ObjectClassRegistration()
|
||||
{
|
||||
}
|
||||
|
||||
bool ObjectClassRegistration::is_derived_from(const ObjectClassRegistration& base_class) const
|
||||
{
|
||||
if (&base_class == this)
|
||||
return true;
|
||||
if (!m_parent_class)
|
||||
return false;
|
||||
return m_parent_class->is_derived_from(base_class);
|
||||
}
|
||||
|
||||
void ObjectClassRegistration::for_each(Function<void(const ObjectClassRegistration&)> callback)
|
||||
{
|
||||
for (auto& it : object_classes()) {
|
||||
callback(*it.value);
|
||||
}
|
||||
}
|
||||
|
||||
const ObjectClassRegistration* ObjectClassRegistration::find(const String& class_name)
|
||||
{
|
||||
return object_classes().get(class_name).value_or(nullptr);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,35 @@
|
|||
|
||||
namespace Core {
|
||||
|
||||
#define REGISTER_CORE_OBJECT(namespace_, class_name) \
|
||||
namespace Core { \
|
||||
namespace Registration { \
|
||||
Core::ObjectClassRegistration registration_##class_name(#namespace_ "::" #class_name, []() { return namespace_::class_name::construct(); }); \
|
||||
} \
|
||||
}
|
||||
|
||||
class ObjectClassRegistration {
|
||||
AK_MAKE_NONCOPYABLE(ObjectClassRegistration);
|
||||
AK_MAKE_NONMOVABLE(ObjectClassRegistration);
|
||||
|
||||
public:
|
||||
ObjectClassRegistration(const String& class_name, Function<NonnullRefPtr<Object>()> factory, ObjectClassRegistration* parent_class = nullptr);
|
||||
~ObjectClassRegistration();
|
||||
|
||||
String class_name() const { return m_class_name; }
|
||||
const ObjectClassRegistration* parent_class() const { return m_parent_class; }
|
||||
NonnullRefPtr<Object> construct() const { return m_factory(); }
|
||||
bool is_derived_from(const ObjectClassRegistration& base_class) const;
|
||||
|
||||
static void for_each(Function<void(const ObjectClassRegistration&)>);
|
||||
static const ObjectClassRegistration* find(const String& class_name);
|
||||
|
||||
private:
|
||||
String m_class_name;
|
||||
Function<NonnullRefPtr<Object>()> m_factory;
|
||||
ObjectClassRegistration* m_parent_class { nullptr };
|
||||
};
|
||||
|
||||
class RPCClient;
|
||||
|
||||
enum class TimerShouldFireWhenNotVisible {
|
||||
|
@ -129,6 +158,8 @@ public:
|
|||
void increment_inspector_count(Badge<RPCClient>);
|
||||
void decrement_inspector_count(Badge<RPCClient>);
|
||||
|
||||
virtual bool load_from_json(const JsonObject&, RefPtr<Core::Object> (*)(const String&)) { return false; }
|
||||
|
||||
protected:
|
||||
explicit Object(Object* parent = nullptr);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue