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