1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 11:18:13 +00:00

LibGUI: Allow generating C++ "constructor" code for UIDimension

This will be used in the proper GML generator to initialize UI dimension
properties.
This commit is contained in:
kleines Filmröllchen 2023-02-18 12:14:42 +01:00 committed by Jelle Raaijmakers
parent d385adf6bd
commit e2dbce8fd7

View file

@ -8,6 +8,7 @@
#include <AK/JsonValue.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <initializer_list>
@ -148,6 +149,23 @@ public:
VERIFY_NOT_REACHED();
}
/// The returned source code, if any, can be used to construct this UIDimension in C++.
ErrorOr<String> as_cpp_source() const
{
String value_source = {};
if (is_int())
value_source = TRY(String::number(m_value));
else if (is_shrink())
value_source = TRY(String::from_utf8("GUI::SpecialDimension::Shrink"sv));
else if (is_grow())
value_source = TRY(String::from_utf8("GUI::SpecialDimension::Grow"sv));
else if (is_opportunistic_grow())
value_source = TRY(String::from_utf8("GUI::SpecialDimension::OpportunisticGrow"sv));
else if (is_fit())
value_source = TRY(String::from_utf8("GUI::SpecialDimension::Fit"sv));
return String::formatted("GUI::UIDimension {{ {} }}", value_source);
}
[[nodiscard]] static Optional<UIDimension> construct_from_json_value(AK::JsonValue const value)
{
if (value.is_string()) {