mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:18:11 +00:00
LibGUI: Add a simple GWidget class registry/factory
You can now register a GWidget subclass with REGISTER_GWIDGET(class) and it will be available for factory construction through the new GWidgetClassRegistration interface. To obtain a GWidgetClassRegistration for a given class name, you call GWidgetClassRegistration::find(class_name). You can also iterate over all the registered classes using GWCR::for_each(callback). This will be very useful for implementing a proper GUI designer, and also in the future for things like script bindings. NOTE: All of the registrations are done in GWidget.cpp at the moment since I ran into trouble with the fricken linker pruning the global constructors this mechanism relies on. :^)
This commit is contained in:
parent
a353d16ff4
commit
ca538b6cee
10 changed files with 109 additions and 8 deletions
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/CElapsedTimer.h>
|
||||
#include <LibCore/CObject.h>
|
||||
#include <LibDraw/Color.h>
|
||||
|
@ -12,6 +12,10 @@
|
|||
#include <LibGUI/GEvent.h>
|
||||
#include <LibGUI/GShortcut.h>
|
||||
|
||||
#define REGISTER_GWIDGET(class_name) \
|
||||
extern GWidgetClassRegistration registration_##class_name; \
|
||||
GWidgetClassRegistration registration_##class_name(#class_name, [](GWidget* parent) { return class_name::construct(parent); });
|
||||
|
||||
class GraphicsBitmap;
|
||||
class GAction;
|
||||
class GLayout;
|
||||
|
@ -42,6 +46,26 @@ enum class VerticalDirection {
|
|||
Down
|
||||
};
|
||||
|
||||
class GWidget;
|
||||
|
||||
class GWidgetClassRegistration {
|
||||
AK_MAKE_NONCOPYABLE(GWidgetClassRegistration)
|
||||
AK_MAKE_NONMOVABLE(GWidgetClassRegistration)
|
||||
public:
|
||||
GWidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<GWidget>(GWidget*)> factory);
|
||||
~GWidgetClassRegistration();
|
||||
|
||||
String class_name() const { return m_class_name; }
|
||||
NonnullRefPtr<GWidget> construct(GWidget* parent) const { return m_factory(parent); }
|
||||
|
||||
static void for_each(Function<void(const GWidgetClassRegistration&)>);
|
||||
static const GWidgetClassRegistration* find(const String& class_name);
|
||||
|
||||
private:
|
||||
String m_class_name;
|
||||
Function<NonnullRefPtr<GWidget>(GWidget*)> m_factory;
|
||||
};
|
||||
|
||||
class GWidget : public CObject {
|
||||
C_OBJECT(GWidget)
|
||||
public:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue