diff options
author | FrHun <28605587+frhun@users.noreply.github.com> | 2022-06-12 22:03:45 +0200 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-06-28 17:52:42 +0100 |
commit | a4aced7f3a388abbc67a1f9a21d9daaad8b53dee (patch) | |
tree | deb0b57f2e1bb3f2858f99bd86fb8536ae979d12 | |
parent | b47bf2fd7ceb12da9f66b2871a299d4c9e9d59de (diff) | |
download | serenity-a4aced7f3a388abbc67a1f9a21d9daaad8b53dee.zip |
LibGUI: Introduce UIDimension properties
-rw-r--r-- | Userland/Libraries/LibGUI/UIDimensions.h | 38 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/Widget.cpp | 12 |
2 files changed, 44 insertions, 6 deletions
diff --git a/Userland/Libraries/LibGUI/UIDimensions.h b/Userland/Libraries/LibGUI/UIDimensions.h index b2061a5b45..257ac1a6ae 100644 --- a/Userland/Libraries/LibGUI/UIDimensions.h +++ b/Userland/Libraries/LibGUI/UIDimensions.h @@ -288,3 +288,41 @@ inline auto clamp<GUI::UIDimension>(GUI::UIDimension const& input, GUI::UIDimens } } + +#define REGISTER_UI_DIMENSION_PROPERTY(property_name, getter, setter) \ + register_property( \ + property_name, \ + [this] { \ + return this->getter().as_json_value(); \ + }, \ + [this](auto& value) { \ + auto result = GUI::UIDimension::construct_from_json_value(value); \ + if (result.has_value()) \ + this->setter(result.value()); \ + return result.has_value(); \ + }); + +#define REGISTER_UI_SIZE_PROPERTY(property_name, getter, setter) \ + register_property( \ + property_name, \ + [this] { \ + auto size = this->getter(); \ + JsonObject size_object; \ + size_object.set("width", size.width().as_json_value()); \ + size_object.set("height", size.height().as_json_value()); \ + return size_object; \ + }, \ + [this](auto& value) { \ + if (!value.is_object()) \ + return false; \ + auto result_width = GUI::UIDimension::construct_from_json_value( \ + value.as_object().get("width")); \ + auto result_height = GUI::UIDimension::construct_from_json_value( \ + value.as_object().get("height")); \ + if (result_width.has_value() && result_height.has_value()) { \ + GUI::UISize size(result_width.value(), result_height.value()); \ + setter(size); \ + return true; \ + } \ + return false; \ + }); diff --git a/Userland/Libraries/LibGUI/Widget.cpp b/Userland/Libraries/LibGUI/Widget.cpp index 80cfc8265f..80d1e720df 100644 --- a/Userland/Libraries/LibGUI/Widget.cpp +++ b/Userland/Libraries/LibGUI/Widget.cpp @@ -49,14 +49,14 @@ Widget::Widget() REGISTER_BOOL_PROPERTY("enabled", is_enabled, set_enabled); REGISTER_STRING_PROPERTY("tooltip", tooltip, set_tooltip); - REGISTER_SIZE_PROPERTY("min_size", min_size, set_min_size); - REGISTER_SIZE_PROPERTY("max_size", max_size, set_max_size); + REGISTER_UI_SIZE_PROPERTY("min_size", min_size, set_min_size); + REGISTER_UI_SIZE_PROPERTY("max_size", max_size, set_max_size); REGISTER_INT_PROPERTY("width", width, set_width); - REGISTER_INT_PROPERTY("min_width", min_width, set_min_width); - REGISTER_INT_PROPERTY("max_width", max_width, set_max_width); - REGISTER_INT_PROPERTY("min_height", min_height, set_min_height); + REGISTER_UI_DIMENSION_PROPERTY("min_width", min_width, set_min_width); + REGISTER_UI_DIMENSION_PROPERTY("max_width", max_width, set_max_width); REGISTER_INT_PROPERTY("height", height, set_height); - REGISTER_INT_PROPERTY("max_height", max_height, set_max_height); + REGISTER_UI_DIMENSION_PROPERTY("min_height", min_height, set_min_height); + REGISTER_UI_DIMENSION_PROPERTY("max_height", max_height, set_max_height); REGISTER_INT_PROPERTY("fixed_width", dummy_fixed_width, set_fixed_width); REGISTER_INT_PROPERTY("fixed_height", dummy_fixed_height, set_fixed_height); |