1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:37:45 +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:
Tom 2021-04-04 15:40:34 -06:00 committed by Andreas Kling
parent 6e101adc28
commit 3aaffa2c47
13 changed files with 134 additions and 85 deletions

View file

@ -19,9 +19,18 @@
#include <LibGfx/Rect.h>
#include <LibGfx/StandardCursor.h>
#define REGISTER_WIDGET(namespace_, class_name) \
namespace { \
GUI::WidgetClassRegistration registration_##class_name(#namespace_ "::" #class_name, []() { return namespace_::class_name::construct(); }); \
namespace Core {
namespace Registration {
extern Core::ObjectClassRegistration registration_Widget;
}
}
#define REGISTER_WIDGET(namespace_, class_name) \
namespace Core { \
namespace Registration { \
Core::ObjectClassRegistration registration_##class_name( \
#namespace_ "::" #class_name, []() { return static_ptr_cast<Core::Object>(namespace_::class_name::construct()); }, &registration_Widget); \
} \
}
namespace GUI {
@ -35,25 +44,6 @@ enum class VerticalDirection {
Down
};
class WidgetClassRegistration {
AK_MAKE_NONCOPYABLE(WidgetClassRegistration);
AK_MAKE_NONMOVABLE(WidgetClassRegistration);
public:
WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory);
~WidgetClassRegistration();
String class_name() const { return m_class_name; }
NonnullRefPtr<Widget> construct() const { return m_factory(); }
static void for_each(Function<void(const WidgetClassRegistration&)>);
static const WidgetClassRegistration* find(const String& class_name);
private:
String m_class_name;
Function<NonnullRefPtr<Widget>()> m_factory;
};
enum class FocusPolicy {
NoFocus = 0,
TabFocus = 0x1,
@ -281,7 +271,7 @@ public:
void set_override_cursor(Gfx::StandardCursor);
bool load_from_gml(const StringView&);
bool load_from_gml(const StringView&, RefPtr<Widget> (*unregistered_child_handler)(const String&));
bool load_from_gml(const StringView&, RefPtr<Core::Object> (*unregistered_child_handler)(const String&));
void set_shrink_to_fit(bool);
bool is_shrink_to_fit() const { return m_shrink_to_fit; }
@ -341,7 +331,7 @@ private:
void focus_previous_widget(FocusSource, bool siblings_only);
void focus_next_widget(FocusSource, bool siblings_only);
bool load_from_json(const JsonObject&, RefPtr<Widget> (*unregistered_child_handler)(const String&));
virtual bool load_from_json(const JsonObject&, RefPtr<Core::Object> (*unregistered_child_handler)(const String&)) override;
// HACK: These are used as property getters for the fixed_* size property aliases.
int dummy_fixed_width() { return 0; }