From eb3ca6481969ff8f35c1e55ea89ae372e18f40a0 Mon Sep 17 00:00:00 2001 From: thislooksfun Date: Tue, 26 Oct 2021 02:12:16 -0500 Subject: [PATCH] LibGUI: Dynamically process layouts from GML files There are only two layouts at the moment, but that could (and probably will) change in the future. Why limit ourselves by hardcoding these checks when we can do it dynamically instead? --- Userland/Libraries/LibGUI/Widget.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGUI/Widget.cpp b/Userland/Libraries/LibGUI/Widget.cpp index 6062142819..897e35129e 100644 --- a/Userland/Libraries/LibGUI/Widget.cpp +++ b/Userland/Libraries/LibGUI/Widget.cpp @@ -1090,10 +1090,14 @@ bool Widget::load_from_json(const JsonObject& json, RefPtr (*unreg return false; } - if (class_name.to_string() == "GUI::VerticalBoxLayout") { - set_layout(); - } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") { - set_layout(); + auto& layout_class = *Core::ObjectClassRegistration::find("GUI::Layout"); + if (auto* registration = Core::ObjectClassRegistration::find(class_name.as_string())) { + auto layout = registration->construct(); + if (!layout || !registration->is_derived_from(layout_class)) { + dbgln("Invalid layout class: '{}'", class_name.to_string()); + return false; + } + set_layout(static_ptr_cast(layout).release_nonnull()); } else { dbgln("Unknown layout class: '{}'", class_name.to_string()); return false;