From e8dd10eec26d26c3fb30f2712ccf58ac72edc8a2 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Tue, 17 Sep 2019 16:05:17 +0800 Subject: ... --- src/ui/controls/flex_layout.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'src/ui/controls/flex_layout.cpp') diff --git a/src/ui/controls/flex_layout.cpp b/src/ui/controls/flex_layout.cpp index a8200a12..cf6d4aad 100644 --- a/src/ui/controls/flex_layout.cpp +++ b/src/ui/controls/flex_layout.cpp @@ -14,31 +14,29 @@ render::RenderObject* FlexLayout::GetRenderObject() const { return render_object_.get(); } -FlexChildLayoutData FlexLayout::GetChildLayoutData(Control* control) { - const auto& render_objects = render_object_->GetChildren(); +namespace { +int FindPosition(render::RenderObject* parent, render::RenderObject* child) { + const auto& render_objects = parent->GetChildren(); const auto find_result = - std::find(render_objects.cbegin(), render_objects.cend(), - control->GetRenderObject()); + std::find(render_objects.cbegin(), render_objects.cend(), child); if (find_result == render_objects.cend()) { throw std::logic_error("Control is not a child of FlexLayout."); } - int position = static_cast(find_result - render_objects.cbegin()); - return *(render_object_->GetChildLayoutData(position)); + return static_cast(find_result - render_objects.cbegin()); +} +} // namespace + +FlexChildLayoutData FlexLayout::GetChildLayoutData(Control* control) { + assert(control); + return render_object_->GetChildLayoutData( + FindPosition(render_object_.get(), control->GetRenderObject())); } void FlexLayout::SetChildLayoutData(Control* control, const FlexChildLayoutData& data) { - const auto& render_objects = render_object_->GetChildren(); - const auto find_result = - std::find(render_objects.cbegin(), render_objects.cend(), - control->GetRenderObject()); - if (find_result == render_objects.cend()) { - throw std::logic_error("Control is not a child of FlexLayout."); - } - int position = static_cast(find_result - render_objects.cbegin()); - const auto d = render_object_->GetChildLayoutData(position); - *d = data; - if (const auto window = GetWindow()) window->InvalidateLayout(); + assert(control); + render_object_->SetChildLayoutData( + FindPosition(render_object_.get(), control->GetRenderObject()), data); } void FlexLayout::OnAddChild(Control* child, int position) { -- cgit v1.2.3