1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:47:34 +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

@ -799,7 +799,11 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent)
form_widgets_toolbar.add_action(cursor_tool_action);
GUI::WidgetClassRegistration::for_each([&, this](const GUI::WidgetClassRegistration& reg) {
auto& widget_class = *Core::ObjectClassRegistration::find("GUI::Widget");
Core::ObjectClassRegistration::for_each([&, this](const Core::ObjectClassRegistration& reg) {
if (!reg.is_derived_from(widget_class))
return;
constexpr size_t gui_namespace_prefix_length = sizeof("GUI::") - 1;
auto icon_path = String::formatted("/res/icons/hackstudio/G{}.png",
reg.class_name().substring(gui_namespace_prefix_length, reg.class_name().length() - gui_namespace_prefix_length));
@ -808,7 +812,7 @@ void HackStudioWidget::create_form_editor(GUI::Widget& parent)
auto action = GUI::Action::create_checkable(reg.class_name(), Gfx::Bitmap::load_from_file(icon_path), [&reg, this](auto&) {
m_form_editor_widget->set_tool(make<WidgetTool>(*m_form_editor_widget, reg));
auto widget = reg.construct();
auto widget = static_ptr_cast<Widget>(reg.construct());
m_form_editor_widget->form_widget().add_child(widget);
widget->set_relative_rect(30, 30, 30, 30);
m_form_editor_widget->model().update();

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Noncopyable.h>
#include <LibCore/Forward.h>
#include <LibGUI/Forward.h>
namespace HackStudio {

View file

@ -12,7 +12,7 @@ namespace HackStudio {
class WidgetTool final : public Tool {
public:
explicit WidgetTool(FormEditorWidget& editor, const GUI::WidgetClassRegistration& meta_class)
explicit WidgetTool(FormEditorWidget& editor, const Core::ObjectClassRegistration& meta_class)
: Tool(editor)
, m_meta_class(meta_class)
{
@ -26,7 +26,7 @@ private:
virtual void on_mousemove(GUI::MouseEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;
const GUI::WidgetClassRegistration& m_meta_class;
const Core::ObjectClassRegistration& m_meta_class;
};
}

View file

@ -102,6 +102,8 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
state = previous_states.take_last();
}
auto& widget_class = *Core::ObjectClassRegistration::find("GUI::Widget");
Vector<GUI::AutocompleteProvider::Entry> class_entries, identifier_entries;
switch (state) {
case Free:
@ -110,7 +112,9 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
// Nothing to put here.
break;
}
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
if (!registration.is_derived_from(widget_class))
return;
class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
});
break;
@ -122,12 +126,14 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
// TODO: Suggest braces?
break;
}
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
if (!registration.is_derived_from(widget_class))
return;
if (registration.class_name().starts_with(class_names.last()))
identifier_entries.empend(registration.class_name(), class_names.last().length());
});
break;
case InIdentifier:
case InIdentifier: {
if (class_names.is_empty())
break;
if (last_seen_token && last_seen_token->m_end.column + 1 != cursor.column() && last_seen_token->m_end.line == cursor.line()) {
@ -135,7 +141,8 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
// TODO: Maybe suggest a colon?
break;
}
if (auto registration = GUI::WidgetClassRegistration::find(class_names.last())) {
auto registration = Core::ObjectClassRegistration::find(class_names.last());
if (registration && registration->is_derived_from(widget_class)) {
auto instance = registration->construct();
for (auto& it : instance->properties()) {
if (it.key.starts_with(identifier_string))
@ -148,7 +155,8 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
if (identifier_entries.size() == 1 && identifier_entries.first().completion == identifier_string)
identifier_entries.clear();
break;
case AfterClassName:
}
case AfterClassName: {
if (last_seen_token && last_seen_token->m_end.line == cursor.line()) {
if (last_seen_token->m_type != GUI::GMLToken::Type::Identifier || last_seen_token->m_end.column + 1 != cursor.column()) {
// Inside braces, but on the same line as some other stuff (and not the continuation of one!)
@ -157,7 +165,8 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
}
}
if (!class_names.is_empty()) {
if (auto registration = GUI::WidgetClassRegistration::find(class_names.last())) {
auto registration = Core::ObjectClassRegistration::find(class_names.last());
if (registration && registration->is_derived_from(widget_class)) {
auto instance = registration->construct();
for (auto& it : instance->properties()) {
if (!it.value->is_readonly())
@ -165,16 +174,21 @@ void GMLAutocompleteProvider::provide_completions(Function<void(Vector<Entry>)>
}
}
}
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
if (!registration.is_derived_from(widget_class))
return;
class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
});
break;
}
case AfterIdentifier:
if (last_seen_token && last_seen_token->m_end.line != cursor.line()) {
break;
}
if (identifier_string == "layout") {
GUI::WidgetClassRegistration::for_each([&](const GUI::WidgetClassRegistration& registration) {
Core::ObjectClassRegistration::for_each([&](const Core::ObjectClassRegistration& registration) {
if (!registration.is_derived_from(widget_class))
return;
if (registration.class_name().contains("Layout"))
class_entries.empend(String::formatted("@{}", registration.class_name()), 0u);
});

View file

@ -128,7 +128,7 @@ int main(int argc, char** argv)
editor.on_change = [&] {
preview.remove_all_children();
preview.load_from_gml(editor.text(), [](const String& class_name) -> RefPtr<GUI::Widget> {
preview.load_from_gml(editor.text(), [](const String& class_name) -> RefPtr<Core::Object> {
return UnregisteredWidget::construct(class_name);
});
};