Extract ApplyAnchorLayoutParams from the anchor viewgroup

This commit is contained in:
Henrik Rydgård 2023-07-16 11:15:37 +02:00
parent f39b9640b7
commit 01191ff23a

View file

@ -824,55 +824,57 @@ void AnchorLayout::MeasureViews(const UIContext &dc, MeasureSpec horiz, MeasureS
}
}
static void ApplyAnchorLayoutParams(float measuredWidth, float measuredHeight, const Bounds &container, const AnchorLayoutParams *params, Bounds *vBounds) {
vBounds->w = measuredWidth;
vBounds->h = measuredHeight;
// Clamp width/height to our own
if (vBounds->w > container.w) vBounds->w = container.w;
if (vBounds->h > container.h) vBounds->h = container.h;
float left = 0, top = 0, right = 0, bottom = 0;
bool center = false;
if (params) {
left = params->left;
top = params->top;
right = params->right;
bottom = params->bottom;
center = params->center;
}
if (left > NONE) {
vBounds->x = container.x + left;
if (center)
vBounds->x -= vBounds->w * 0.5f;
} else if (right > NONE) {
vBounds->x = container.x2() - right - vBounds->w;
if (center) {
vBounds->x += vBounds->w * 0.5f;
}
} else {
// Both left and right are NONE. Center.
vBounds->x = (container.w - vBounds->w) / 2.0f + container.x;
}
if (top > NONE) {
vBounds->y = container.y + top;
if (center)
vBounds->y -= vBounds->h * 0.5f;
} else if (bottom > NONE) {
vBounds->y = container.y2() - bottom - vBounds->h;
if (center)
vBounds->y += vBounds->h * 0.5f;
} else {
// Both top and bottom are NONE. Center.
vBounds->y = (container.h - vBounds->h) / 2.0f + container.y;
}
}
void AnchorLayout::Layout() {
for (size_t i = 0; i < views_.size(); i++) {
const AnchorLayoutParams *params = views_[i]->GetLayoutParams()->As<AnchorLayoutParams>();
Bounds vBounds;
vBounds.w = views_[i]->GetMeasuredWidth();
vBounds.h = views_[i]->GetMeasuredHeight();
// Clamp width/height to our own
if (vBounds.w > bounds_.w) vBounds.w = bounds_.w;
if (vBounds.h > bounds_.h) vBounds.h = bounds_.h;
float left = 0, top = 0, right = 0, bottom = 0;
bool center = false;
if (params) {
left = params->left;
top = params->top;
right = params->right;
bottom = params->bottom;
center = params->center;
}
if (left > NONE) {
vBounds.x = bounds_.x + left;
if (center)
vBounds.x -= vBounds.w * 0.5f;
} else if (right > NONE) {
vBounds.x = bounds_.x2() - right - vBounds.w;
if (center) {
vBounds.x += vBounds.w * 0.5f;
}
} else {
// Both left and right are NONE. Center.
vBounds.x = (bounds_.w - vBounds.w) / 2.0f + bounds_.x;
}
if (top > NONE) {
vBounds.y = bounds_.y + top;
if (center)
vBounds.y -= vBounds.h * 0.5f;
} else if (bottom > NONE) {
vBounds.y = bounds_.y2() - bottom - vBounds.h;
if (center)
vBounds.y += vBounds.h * 0.5f;
} else {
// Both top and bottom are NONE. Center.
vBounds.y = (bounds_.h - vBounds.h) / 2.0f + bounds_.y;
}
ApplyAnchorLayoutParams(views_[i]->GetMeasuredWidth(), views_[i]->GetMeasuredHeight(), bounds_, params, &vBounds);
views_[i]->SetBounds(vBounds);
views_[i]->Layout();
}