Commit 6a7a379f authored by Ian Vollick's avatar Ian Vollick Committed by Commit Bot

[vr] Handle alignment in linear layout

This CL follows the approach described on crbug.com/808527 for
respecting element alignment in the linear layout.

Bug: 808527
Cq-Include-Trybots: luci.chromium.try:linux_optional_gpu_tests_rel;master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_vr;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Ia11640da59ebcaf14ddd5d82df182ed7cacfd027
Reviewed-on: https://chromium-review.googlesource.com/964787Reviewed-by: default avatarChristopher Grant <cjgrant@chromium.org>
Commit-Queue: Ian Vollick <vollick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543507}
parent 9d4a34ca
...@@ -77,10 +77,31 @@ void LinearLayout::LayOutChildren() { ...@@ -77,10 +77,31 @@ void LinearLayout::LayOutChildren() {
for (auto& child : children()) { for (auto& child : children()) {
if (!child->requires_layout()) if (!child->requires_layout())
continue; continue;
float extent = GetExtent(*child, horizontal); float child_extent = GetExtent(*child, horizontal);
float offset = cumulative_offset + 0.5 * extent; float child_minor_extent = GetExtent(*child, !horizontal);
child->SetLayoutOffset(offset * x_factor, offset * y_factor); float offset = cumulative_offset + 0.5 * child_extent;
cumulative_offset += extent + margin_; float x_align_offset = 0.0f;
float y_align_offset = 0.0f;
if (Horizontal()) {
DCHECK_NE(RIGHT, child->x_anchoring());
DCHECK_NE(LEFT, child->x_anchoring());
if (child->y_anchoring() == TOP || child->y_anchoring() == BOTTOM) {
y_align_offset = 0.5f * (minor_extent - child_minor_extent);
if (child->y_anchoring() == BOTTOM)
y_align_offset *= -1.0f;
}
} else {
DCHECK_NE(TOP, child->y_anchoring());
DCHECK_NE(BOTTOM, child->y_anchoring());
if (child->x_anchoring() == RIGHT || child->x_anchoring() == LEFT) {
x_align_offset = 0.5f * (minor_extent - child_minor_extent);
if (child->x_anchoring() == LEFT)
x_align_offset *= -1.0f;
}
}
child->SetLayoutOffset(offset * x_factor + x_align_offset,
offset * y_factor + y_align_offset);
cumulative_offset += child_extent + margin_;
} }
SetSize(horizontal ? total_extent : minor_extent, SetSize(horizontal ? total_extent : minor_extent,
......
...@@ -26,7 +26,7 @@ class TestElement : public UiElement { ...@@ -26,7 +26,7 @@ class TestElement : public UiElement {
} // namespace } // namespace
TEST(LinearLayout, HorizontalLayout) { TEST(LinearLayout, HorizontalVerticalLayout) {
LinearLayout layout(LinearLayout::kRight); LinearLayout layout(LinearLayout::kRight);
layout.set_margin(10); layout.set_margin(10);
auto element = std::make_unique<UiElement>(); auto element = std::make_unique<UiElement>();
...@@ -62,6 +62,72 @@ TEST(LinearLayout, HorizontalLayout) { ...@@ -62,6 +62,72 @@ TEST(LinearLayout, HorizontalLayout) {
rect_a->set_requires_layout(false); rect_a->set_requires_layout(false);
layout.LayOutChildren(); layout.LayOutChildren();
EXPECT_FLOAT_EQ(20.0f, layout.size().width());
}
TEST(LinearLayout, Alignment) {
LinearLayout layout(LinearLayout::kRight);
layout.set_margin(10);
auto element = std::make_unique<UiElement>();
UiElement* rect_a = element.get();
rect_a->SetSize(1, 1);
layout.AddChild(std::move(element));
element = std::make_unique<UiElement>();
UiElement* rect_b = element.get();
rect_b->SetSize(10, 10);
rect_b->SetScale(2.0f, 2.0f, 0.0f);
layout.AddChild(std::move(element));
gfx::Point3F position_a;
rect_a->set_y_anchoring(TOP);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(9.5f, position_a.y());
position_a = gfx::Point3F();
rect_a->set_y_anchoring(BOTTOM);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(-9.5f, position_a.y());
layout.set_direction(LinearLayout::kLeft);
position_a = gfx::Point3F();
rect_a->set_y_anchoring(TOP);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(9.5f, position_a.y());
position_a = gfx::Point3F();
rect_a->set_y_anchoring(BOTTOM);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(-9.5f, position_a.y());
layout.set_direction(LinearLayout::kDown);
position_a = gfx::Point3F();
rect_a->set_x_anchoring(LEFT);
rect_a->set_y_anchoring(NONE);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(-9.5f, position_a.x());
position_a = gfx::Point3F();
rect_a->set_x_anchoring(RIGHT);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(9.5f, position_a.x());
layout.set_direction(LinearLayout::kUp);
position_a = gfx::Point3F();
rect_a->set_x_anchoring(LEFT);
rect_a->set_y_anchoring(NONE);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(-9.5f, position_a.x());
position_a = gfx::Point3F();
rect_a->set_x_anchoring(RIGHT);
layout.LayOutChildren();
rect_a->LocalTransform().TransformPoint(&position_a);
EXPECT_FLOAT_EQ(9.5f, position_a.x());
} }
TEST(LinearLayout, Orientations) { TEST(LinearLayout, Orientations) {
......
...@@ -1032,6 +1032,7 @@ void UiSceneCreator::CreateContentQuad() { ...@@ -1032,6 +1032,7 @@ void UiSceneCreator::CreateContentQuad() {
VR_BIND_LAMBDA([](ContentElement* e, VR_BIND_LAMBDA([](ContentElement* e,
const EditedText& value) { e->UpdateInput(value); }, const EditedText& value) { e->UpdateInput(value); },
base::Unretained(main_content.get())))); base::Unretained(main_content.get()))));
shadow->AddChild(std::move(main_content)); shadow->AddChild(std::move(main_content));
resizer->AddChild(std::move(shadow)); resizer->AddChild(std::move(shadow));
scene_->AddUiElement(k2dBrowsingContentGroup, std::move(resizer)); scene_->AddUiElement(k2dBrowsingContentGroup, std::move(resizer));
...@@ -1965,7 +1966,6 @@ void UiSceneCreator::CreateOmnibox() { ...@@ -1965,7 +1966,6 @@ void UiSceneCreator::CreateOmnibox() {
omnibox_text_field->set_hit_testable(false); omnibox_text_field->set_hit_testable(false);
omnibox_text_field->SetHintText( omnibox_text_field->SetHintText(
l10n_util::GetStringUTF16(IDS_SEARCH_OR_TYPE_WEB_ADDRESS)); l10n_util::GetStringUTF16(IDS_SEARCH_OR_TYPE_WEB_ADDRESS));
omnibox_text_field->set_x_anchoring(LEFT);
omnibox_text_field->SetSize(kOmniboxWidthDMM - 2 * kOmniboxTextMarginDMM - omnibox_text_field->SetSize(kOmniboxWidthDMM - 2 * kOmniboxTextMarginDMM -
kOmniboxTextFieldIconButtonSizeDMM - kOmniboxTextFieldIconButtonSizeDMM -
kOmniboxTextFieldRightMargin, kOmniboxTextFieldRightMargin,
......
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