From 5f7d3268e3d2ea84a1afc97c209b703bf4092c76 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 20 Sep 2018 22:50:37 +0800 Subject: Improve linear layout. --- CruUI/ui/control.h | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'CruUI/ui/control.h') diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 19995c76..c69486c9 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -1,8 +1,10 @@ #pragma once #include "system_headers.h" -#include #include +#include +#include +#include #include "base.h" #include "ui_base.h" @@ -152,19 +154,39 @@ namespace cru void SetDesiredSize(const Size& desired_size); - template - std::shared_ptr GetLayoutParams() + BasicLayoutParams* GetLayoutParams() { - static_assert(std::is_base_of_v, "TLayoutParams must be subclass of BasicLayoutParams."); - return static_cast>(layout_params_); + return &layout_params_; } - template>> - void SetLayoutParams(std::shared_ptr basic_layout_params) + //*************** region: additional properties *************** + template + std::optional GetAdditionalProperty(const String& key) { - static_assert(std::is_base_of_v, "TLayoutParams must be subclass of BasicLayoutParams."); - layout_params_ = basic_layout_params; + try + { + const auto find_result = additional_properties_.find(key); + if (find_result != additional_properties_.cend()) + return std::any_cast(find_result->second); + else + return std::nullopt; + } + catch (const std::bad_any_cast&) + { + throw std::runtime_error(fmt::format("Key \"{}\" is not of the type {}.", ToUtf8String(key), typeid(T).name())); + } + } + + template + void SetAdditionalProperty(const String& key, const T& value) + { + additional_properties_[key] = std::make_any(value); + } + + template + void SetAdditionalProperty(const String& key, T&& value) + { + additional_properties_[key] = std::make_any(std::move(value)); } //*************** region: events *************** @@ -294,8 +316,10 @@ namespace cru std::unordered_map is_mouse_leave_; // used for clicking determination - std::shared_ptr layout_params_; + BasicLayoutParams layout_params_; Size desired_size_; + + std::unordered_map additional_properties_; }; // Find the lowest common ancestor. -- cgit v1.2.3