Commit facc520a authored by Christopher Grant's avatar Christopher Grant Committed by Commit Bot

VR: Reuse more common size and layout logic in LinearLayout

This change reduces the amount of custom code in LinearLayout to the
minimum, reducing duplication and keeping things more consistent.

BUG=
R=acondor

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: I11a9c0a766845f9b5b7a86378a12b5e0b6202115
Reviewed-on: https://chromium-review.googlesource.com/1053754Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Commit-Queue: Christopher Grant <cjgrant@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557688}
parent 7536cdd0
......@@ -23,35 +23,23 @@ float GetExtent(const UiElement& element, bool horizontal) {
LinearLayout::LinearLayout(Direction direction) : direction_(direction) {}
LinearLayout::~LinearLayout() {}
bool LinearLayout::SizeAndLayOut() {
if (!IsVisible())
return false;
bool changed = false;
if (layout_length > 0.0f) {
UiElement* element_to_resize = nullptr;
for (auto& child : children()) {
if (child->resizable_by_layout()) {
DCHECK_EQ(nullptr, element_to_resize);
element_to_resize = child.get();
} else {
changed |= child->SizeAndLayOut();
}
}
DCHECK_NE(element_to_resize, nullptr);
changed |= element_to_resize->SizeAndLayOut();
changed |= AdjustResizableElement(element_to_resize);
changed |= element_to_resize->SizeAndLayOut();
} else {
for (auto& child : children()) {
changed |= child->SizeAndLayOut();
bool LinearLayout::SizeAndLayOutChildren() {
bool changed = UiElement::SizeAndLayOutChildren();
if (layout_length_ == 0.0f)
return changed;
// We need to adjust one of the elements' size to ensure a fixed total layout
// width. Find that element, set its size, and lay it out again.
UiElement* element_to_resize = nullptr;
for (auto& child : children()) {
if (child->resizable_by_layout()) {
element_to_resize = child.get();
break;
}
}
changed |= PrepareToDraw();
set_update_phase(kUpdatedSize);
DoLayOutChildren();
set_update_phase(kUpdatedLayout);
DCHECK_NE(element_to_resize, nullptr);
changed |= AdjustResizableElement(element_to_resize);
changed |= element_to_resize->SizeAndLayOut();
return changed;
}
......@@ -141,7 +129,7 @@ bool LinearLayout::AdjustResizableElement(UiElement* element_to_resize) {
float minor = 0;
GetTotalExtent(element_to_resize, &minimum_total, &minor);
float extent = layout_length - minimum_total;
float extent = layout_length_ - minimum_total;
extent = std::max(extent, 0.f);
auto new_size = element_to_resize->size();
......
......@@ -18,10 +18,10 @@ class LinearLayout : public UiElement {
void set_margin(float margin) { margin_ = margin; }
void set_direction(Direction direction) { direction_ = direction; }
void set_layout_length(float extent) { layout_length = extent; }
void set_layout_length(float extent) { layout_length_ = extent; }
// UiElement overrides.
bool SizeAndLayOut() override;
bool SizeAndLayOutChildren() override;
void LayOutChildren() override;
private:
......@@ -39,12 +39,14 @@ class LinearLayout : public UiElement {
bool AdjustResizableElement(UiElement* element_to_resize);
Direction direction_;
// The spacing between elements.
float margin_ = 0.0f;
// If non-zero, LinearLayout will look for an element tagged as allowing
// sizing by its parent, and set that element's size such that the total
// layout's length is attained.
float layout_length = 0.0f;
float layout_length_ = 0.0f;
};
} // namespace vr
......
......@@ -799,9 +799,10 @@ bool UiElement::IsAnimatingProperty(TargetProperty property) const {
bool UiElement::SizeAndLayOut() {
if (!IsVisible() && kEnableOptimizedTreeWalks)
return false;
bool changed = false;
for (auto& child : children_)
changed |= child->SizeAndLayOut();
// May be overridden by layout elements.
bool changed = SizeAndLayOutChildren();
changed |= PrepareToDraw();
set_update_phase(kUpdatedSize);
DoLayOutChildren();
......@@ -809,6 +810,13 @@ bool UiElement::SizeAndLayOut() {
return changed;
}
bool UiElement::SizeAndLayOutChildren() {
bool changed = false;
for (auto& child : children_)
changed |= child->SizeAndLayOut();
return changed;
}
void UiElement::DoLayOutChildren() {
LayOutChildren();
if (!bounds_contain_children_) {
......
......@@ -397,9 +397,12 @@ class UiElement : public cc::AnimationTarget {
void RemoveKeyframeModels(int target_property);
bool IsAnimatingProperty(TargetProperty property) const;
// Recursive method that sizes and lays out element subtrees. This method may
// be overridden by elements that have custom layout requirements.
virtual bool SizeAndLayOut();
// Recursive method that sizes and lays out element subtrees.
bool SizeAndLayOut();
// This method may be overridden by elements that have custom layout
// requirements.
virtual bool SizeAndLayOutChildren();
void DoLayOutChildren();
......
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