Fix ui::impl::box_layout_base handling added elements

This commit is contained in:
Nikita Lisitsa 2024-07-30 13:03:41 +03:00
parent 9234a28344
commit 7346aff9f6

View file

@ -14,14 +14,11 @@ namespace psemek::ui::impl
size_constraints compute_size_constraints(std::vector<box_layout::size_policy> const & size_policies,
std::vector<size_constraints> const & children_size_constraints, float padding)
{
if (size_policies.size() != children_size_constraints.size())
return size_constraints::max();
size_constraints minimized = size_constraints::max();
size_constraints weight_unit = minimized;
float weight_sum = 0.f;
for (std::size_t i = 0; i < size_policies.size(); ++i)
for (std::size_t i = 0; i < std::min(size_policies.size(), children_size_constraints.size()); ++i)
{
if (std::get_if<box_layout::minimized>(&size_policies[i]))
{
@ -53,17 +50,20 @@ namespace psemek::ui::impl
std::vector<float> allocate(float total_size, std::vector<react::value<box_layout::size_policy>> const & size_policies,
util::span<std::unique_ptr<component> const> children, float padding)
{
std::vector<float> result(size_policies.size(), 0.f);
std::vector<float> result(std::min(size_policies.size(), children.size()), 0.f);
if (!size_policies.empty())
total_size -= padding * (size_policies.size() - 1);
total_size -= padding * (result.size() - 1);
float total_weight = 0.f;
// First, allocate minimized elements
// Also compute the total weight for weighted elements
for (std::size_t i = 0; i < size_policies.size(); ++i)
for (std::size_t i = 0; i < result.size(); ++i)
{
if (!children[i])
continue;
auto const policy = size_policies[i] ? *size_policies[i] : default_policy;
if (std::get_if<box_layout::minimized>(&policy))
@ -78,7 +78,7 @@ namespace psemek::ui::impl
}
// Next, allocate weighted elements
for (std::size_t i = 0; i < size_policies.size(); ++i)
for (std::size_t i = 0; i < result.size(); ++i)
{
auto const policy = size_policies[i] ? *size_policies[i] : default_policy;