Commit 856e38c6 authored by Ian Vollick's avatar Ian Vollick Committed by Commit Bot

[vr] If padding doesn't affect bounds, nor should it centering/anchoring

Previously, if a shadow had set anchoring or centering, the size of the
shadow would have affected the positioning. This should not be true;
centering and anchoring should proceed as if there were no shadow at
all. We have the property bounds_contain_padding that captures this
concept in other contexts and this CL applies that notion to centering
and anchoring.

Bug: 834432
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_vr
Change-Id: I79465951c50374174710d7d76dc70d3e9cd6bdea
Reviewed-on: https://chromium-review.googlesource.com/1019600
Commit-Queue: Ian Vollick <vollick@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#552080}
parent c347e40e
...@@ -319,13 +319,21 @@ gfx::SizeF UiElement::size() const { ...@@ -319,13 +319,21 @@ gfx::SizeF UiElement::size() const {
void UiElement::SetLayoutOffset(float x, float y) { void UiElement::SetLayoutOffset(float x, float y) {
if (x_centering() == LEFT) { if (x_centering() == LEFT) {
x += size_.width() / 2; x += size_.width() / 2;
if (!bounds_contain_padding_)
x -= left_padding_;
} else if (x_centering() == RIGHT) { } else if (x_centering() == RIGHT) {
x -= size_.width() / 2; x -= size_.width() / 2;
if (!bounds_contain_padding_)
x += right_padding_;
} }
if (y_centering() == TOP) { if (y_centering() == TOP) {
y -= size_.height() / 2; y -= size_.height() / 2;
if (!bounds_contain_padding_)
y += top_padding_;
} else if (y_centering() == BOTTOM) { } else if (y_centering() == BOTTOM) {
y += size_.height() / 2; y += size_.height() / 2;
if (!bounds_contain_padding_)
y -= bottom_padding_;
} }
if (x == layout_offset_.at(0).translate.x && if (x == layout_offset_.at(0).translate.x &&
...@@ -851,14 +859,22 @@ void UiElement::LayOutChildren() { ...@@ -851,14 +859,22 @@ void UiElement::LayOutChildren() {
float x_offset = 0.0f; float x_offset = 0.0f;
if (child->x_anchoring() == LEFT) { if (child->x_anchoring() == LEFT) {
x_offset = -0.5f * size().width(); x_offset = -0.5f * size().width();
if (!child->bounds_contain_padding())
x_offset += left_padding_;
} else if (child->x_anchoring() == RIGHT) { } else if (child->x_anchoring() == RIGHT) {
x_offset = 0.5f * size().width(); x_offset = 0.5f * size().width();
if (!child->bounds_contain_padding())
x_offset -= right_padding_;
} }
float y_offset = 0.0f; float y_offset = 0.0f;
if (child->y_anchoring() == TOP) { if (child->y_anchoring() == TOP) {
y_offset = 0.5f * size().height(); y_offset = 0.5f * size().height();
if (!child->bounds_contain_padding())
y_offset -= top_padding_;
} else if (child->y_anchoring() == BOTTOM) { } else if (child->y_anchoring() == BOTTOM) {
y_offset = -0.5f * size().height(); y_offset = -0.5f * size().height();
if (!child->bounds_contain_padding())
y_offset += bottom_padding_;
} }
child->SetLayoutOffset(x_offset, y_offset); child->SetLayoutOffset(x_offset, y_offset);
} }
......
...@@ -123,6 +123,84 @@ TEST(UiElement, IgnoringAsymmetricPadding) { ...@@ -123,6 +123,84 @@ TEST(UiElement, IgnoringAsymmetricPadding) {
EXPECT_VECTOR3DF_EQ(gfx::Point3F(), p); EXPECT_VECTOR3DF_EQ(gfx::Point3F(), p);
} }
TEST(UiElement, BoundsContainPaddingWithAnchoring) {
// If an element's bounds do not contain padding, then padding should be
// discounted when doing anchoring.
auto parent = std::make_unique<UiElement>();
parent->SetSize(1.0, 1.0);
auto child = std::make_unique<UiElement>();
child->SetSize(0.5, 0.5);
child->set_padding(2.0, 2.0);
child->set_bounds_contain_padding(false);
auto* child_ptr = child.get();
parent->AddChild(std::move(child));
struct {
LayoutAlignment x_anchoring;
LayoutAlignment y_anchoring;
gfx::Point3F expected_position;
} test_cases[] = {
{LEFT, NONE, {-0.5, 0, 0}},
{RIGHT, NONE, {0.5, 0, 0}},
{NONE, TOP, {0, 0.5, 0}},
{NONE, BOTTOM, {0, -0.5, 0}},
};
for (auto test_case : test_cases) {
child_ptr->set_x_anchoring(test_case.x_anchoring);
child_ptr->set_y_anchoring(test_case.y_anchoring);
parent->DoLayOutChildren();
gfx::Point3F p;
child_ptr->LocalTransform().TransformPoint(&p);
EXPECT_VECTOR3DF_EQ(test_case.expected_position, p);
}
}
TEST(UiElement, BoundsContainPaddingWithCentering) {
// If an element's bounds do not contain padding, then padding should be
// discounted when doing centering.
auto parent = std::make_unique<UiElement>();
parent->SetSize(1.0, 1.0);
auto child = std::make_unique<UiElement>();
child->set_padding(2.0, 2.0);
child->set_bounds_contain_padding(false);
child->set_bounds_contain_children(true);
auto grandchild = std::make_unique<UiElement>();
grandchild->SetSize(0.5, 0.5);
child->AddChild(std::move(grandchild));
auto* child_ptr = child.get();
parent->AddChild(std::move(child));
struct {
LayoutAlignment x_centering;
LayoutAlignment y_centering;
gfx::Point3F expected_position;
} test_cases[] = {
{LEFT, NONE, {0.25, 0, 0}},
{RIGHT, NONE, {-0.25, 0, 0}},
{NONE, TOP, {0, -0.25, 0}},
{NONE, BOTTOM, {0, 0.25, 0}},
};
for (auto test_case : test_cases) {
child_ptr->set_x_centering(test_case.x_centering);
child_ptr->set_y_centering(test_case.y_centering);
child_ptr->DoLayOutChildren();
parent->DoLayOutChildren();
gfx::Point3F p;
child_ptr->LocalTransform().TransformPoint(&p);
EXPECT_VECTOR3DF_EQ(test_case.expected_position, p);
}
}
TEST(UiElement, BoundsContainScaledChildren) { TEST(UiElement, BoundsContainScaledChildren) {
auto a = std::make_unique<UiElement>(); auto a = std::make_unique<UiElement>();
a->SetSize(0.4, 0.3); a->SetSize(0.4, 0.3);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment