Commit 458a9c0c authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Allow some FlexLayout functions to work directly in bounds space.

This makes functioning of FlexLayout a bit more uniform by removing a
conditional of whether the main axis is bounded.  With the addition of
SizeBounds and its various methods, very little syntactic change is needed.

This will also help when fixing bug 1012136.

Bug: 1012136
Change-Id: I0d768bed020c5039ddc326de86aa5b490780c11b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036638
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806898}
parent 8d569016
...@@ -106,9 +106,9 @@ class FlexLayout::ChildViewSpacing { ...@@ -106,9 +106,9 @@ class FlexLayout::ChildViewSpacing {
// Returns the maximum size for the child at |view_index|, given its // Returns the maximum size for the child at |view_index|, given its
// |current_size| and the amount of |available_space| for flex allocation. // |current_size| and the amount of |available_space| for flex allocation.
int GetMaxSize(size_t view_index, SizeBound GetMaxSize(size_t view_index,
int current_size, int current_size,
int available_space) const; const SizeBound& available_space) const;
// Returns the change in total allocated size if the child at |view_index| is // Returns the change in total allocated size if the child at |view_index| is
// resized from |current_size| to |new_size|. // resized from |current_size| to |new_size|.
...@@ -185,9 +185,10 @@ int FlexLayout::ChildViewSpacing::GetTotalSpace() const { ...@@ -185,9 +185,10 @@ int FlexLayout::ChildViewSpacing::GetTotalSpace() const {
[](int total, const auto& value) { return total + value.second; }); [](int total, const auto& value) { return total + value.second; });
} }
int FlexLayout::ChildViewSpacing::GetMaxSize(size_t view_index, SizeBound FlexLayout::ChildViewSpacing::GetMaxSize(
int current_size, size_t view_index,
int available_space) const { int current_size,
const SizeBound& available_space) const {
DCHECK_GE(available_space, 0); DCHECK_GE(available_space, 0);
if (HasViewIndex(view_index)) if (HasViewIndex(view_index))
...@@ -202,7 +203,7 @@ int FlexLayout::ChildViewSpacing::GetMaxSize(size_t view_index, ...@@ -202,7 +203,7 @@ int FlexLayout::ChildViewSpacing::GetMaxSize(size_t view_index,
// available space can cause the first view to be smaller than we would expect // available space can cause the first view to be smaller than we would expect
// (see TODOs in unit tests for examples). We should look into ways to make // (see TODOs in unit tests for examples). We should look into ways to make
// this "feel" better (but in the meantime, specify reasonable margins). // this "feel" better (but in the meantime, specify reasonable margins).
return std::max(available_space - GetAddDelta(view_index), 0); return std::max<SizeBound>(available_space - GetAddDelta(view_index), 0);
} }
int FlexLayout::ChildViewSpacing::GetTotalSizeChangeForNewSize( int FlexLayout::ChildViewSpacing::GetTotalSizeChangeForNewSize(
...@@ -423,18 +424,16 @@ ProposedLayout FlexLayout::CalculateProposedLayout( ...@@ -423,18 +424,16 @@ ProposedLayout FlexLayout::CalculateProposedLayout(
base::Unretained(this), std::cref(data))); base::Unretained(this), std::cref(data)));
UpdateLayoutFromChildren(bounds, &data, &child_spacing); UpdateLayoutFromChildren(bounds, &data, &child_spacing);
if (bounds.main().is_bounded()) { // Flex up to preferred size.
// Flex up to preferred size. CalculateNonFlexAvailableSpace(
CalculateNonFlexAvailableSpace( &data, std::max<SizeBound>(bounds.main() - data.total_size.main(), 0),
&data, std::max(bounds.main().value() - data.total_size.main(), 0), child_spacing, order_to_view_index);
child_spacing, order_to_view_index); FlexOrderToViewIndexMap expandable_views;
FlexOrderToViewIndexMap expandable_views; AllocateFlexSpace(bounds, order_to_view_index, &data, &child_spacing,
AllocateFlexSpace(bounds, order_to_view_index, &data, &child_spacing, &expandable_views);
&expandable_views);
// Flex up to maximum size.
// Flex up to maximum size. AllocateFlexSpace(bounds, expandable_views, &data, &child_spacing, nullptr);
AllocateFlexSpace(bounds, expandable_views, &data, &child_spacing, nullptr);
}
// Calculate the size of the host view. // Calculate the size of the host view.
NormalizedSize host_size = data.total_size; NormalizedSize host_size = data.total_size;
...@@ -583,7 +582,7 @@ void FlexLayout::CalculateChildBounds(const SizeBounds& size_bounds, ...@@ -583,7 +582,7 @@ void FlexLayout::CalculateChildBounds(const SizeBounds& size_bounds,
void FlexLayout::CalculateNonFlexAvailableSpace( void FlexLayout::CalculateNonFlexAvailableSpace(
FlexLayoutData* data, FlexLayoutData* data,
int available_space, const SizeBound& available_space,
const ChildViewSpacing& child_spacing, const ChildViewSpacing& child_spacing,
const FlexOrderToViewIndexMap& flex_views) const { const FlexOrderToViewIndexMap& flex_views) const {
// Add all views which are participating in flex (and will have their // Add all views which are participating in flex (and will have their
...@@ -603,7 +602,7 @@ void FlexLayout::CalculateNonFlexAvailableSpace( ...@@ -603,7 +602,7 @@ void FlexLayout::CalculateNonFlexAvailableSpace(
// Cross-axis available size is already set in InitializeChildData(), so // Cross-axis available size is already set in InitializeChildData(), so
// just set the main axis here. // just set the main axis here.
const int max_size = child_spacing.GetMaxSize( const SizeBound max_size = child_spacing.GetMaxSize(
index, data->child_data[index].current_size.main(), available_space); index, data->child_data[index].current_size.main(), available_space);
SetMainAxis(&data->layout.child_layouts[index].available_size, SetMainAxis(&data->layout.child_layouts[index].available_size,
orientation(), max_size); orientation(), max_size);
...@@ -751,13 +750,12 @@ void FlexLayout::AllocateFlexSpace( ...@@ -751,13 +750,12 @@ void FlexLayout::AllocateFlexSpace(
FlexLayoutData* data, FlexLayoutData* data,
ChildViewSpacing* child_spacing, ChildViewSpacing* child_spacing,
FlexOrderToViewIndexMap* expandable_views) const { FlexOrderToViewIndexMap* expandable_views) const {
DCHECK(bounds.main().is_bounded());
// Step through each flex priority allocating as much remaining space as // Step through each flex priority allocating as much remaining space as
// possible to each flex view. // possible to each flex view.
for (const auto& flex_elem : order_to_index) { for (const auto& flex_elem : order_to_index) {
// Check to see we haven't filled available space. // Check to see we haven't filled available space.
const int remaining_at_priority = const SizeBound remaining_at_priority =
std::max(0, bounds.main().value() - data->total_size.main()); std::max<SizeBound>(0, bounds.main() - data->total_size.main());
// The flex algorithm we're using works as follows: // The flex algorithm we're using works as follows:
// * For each child view at a particular flex order: // * For each child view at a particular flex order:
...@@ -785,7 +783,7 @@ void FlexLayout::AllocateFlexSpace( ...@@ -785,7 +783,7 @@ void FlexLayout::AllocateFlexSpace(
// Flex children at this priority order. // Flex children at this priority order.
const int flex_order = flex_elem.first; const int flex_order = flex_elem.first;
int remaining = remaining_at_priority; SizeBound remaining = remaining_at_priority;
int flex_total = int flex_total =
std::accumulate(flex_elem.second.begin(), flex_elem.second.end(), 0, std::accumulate(flex_elem.second.begin(), flex_elem.second.end(), 0,
[data](int total, size_t index) { [data](int total, size_t index) {
...@@ -814,8 +812,8 @@ void FlexLayout::AllocateFlexSpace( ...@@ -814,8 +812,8 @@ void FlexLayout::AllocateFlexSpace(
// total remaining flex space at this priority. Note that this is not // total remaining flex space at this priority. Note that this is not
// the actual remaining space at this step, which will be based on flex // the actual remaining space at this step, which will be based on flex
// used by previous children at the same priority. // used by previous children at the same priority.
const int max_size = child_spacing->GetMaxSize(view_index, current_size, const SizeBound max_size = child_spacing->GetMaxSize(
remaining_at_priority); view_index, current_size, remaining_at_priority);
SetMainAxis(&child_layout.available_size, orientation(), max_size); SetMainAxis(&child_layout.available_size, orientation(), max_size);
} }
...@@ -825,12 +823,12 @@ void FlexLayout::AllocateFlexSpace( ...@@ -825,12 +823,12 @@ void FlexLayout::AllocateFlexSpace(
continue; continue;
// Offer a share of the remaining space to the view. // Offer a share of the remaining space to the view.
int flex_amount = remaining; SizeBound flex_amount = remaining;
const int flex_weight = flex_child.flex.weight(); const int flex_weight = flex_child.flex.weight();
if (flex_weight > 0) { if ((flex_weight > 0) && remaining.is_bounded()) {
// Round up so we give slightly greater weight to earlier views. // Round up so we give slightly greater weight to earlier views.
flex_amount = flex_amount = base::ClampCeil(remaining.value() * flex_weight /
base::ClampCeil(remaining * flex_weight / float{flex_total}); float{flex_total});
} }
flex_total -= flex_weight; flex_total -= flex_weight;
......
...@@ -181,7 +181,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -181,7 +181,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
// Calculates available space for non-flex views. // Calculates available space for non-flex views.
void CalculateNonFlexAvailableSpace( void CalculateNonFlexAvailableSpace(
FlexLayoutData* data, FlexLayoutData* data,
int available_space, const SizeBound& available_space,
const ChildViewSpacing& child_spacing, const ChildViewSpacing& child_spacing,
const FlexOrderToViewIndexMap& flex_views) const; const FlexOrderToViewIndexMap& flex_views) const;
......
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