Commit 62894f91 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Factor out methods for getting the preferred/current size of a view.

This makes InitializeChildData() easier to understand, allows a tiny bit
of code reuse, and will make future changes to both these functions and
their callers simpler.

Bug: none
Change-Id: If19e736dbdc00d252c6043d6cf499cffd5d33386
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2026352
Commit-Queue: Dana Fried <dfried@chromium.org>
Reviewed-by: default avatarDana Fried <dfried@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736275}
parent e26a5e64
...@@ -377,6 +377,37 @@ ProposedLayout FlexLayout::CalculateProposedLayout( ...@@ -377,6 +377,37 @@ ProposedLayout FlexLayout::CalculateProposedLayout(
return data.layout; return data.layout;
} }
NormalizedSize FlexLayout::GetPreferredSizeForRule(
const FlexRule& rule,
const View* child,
const base::Optional<int>& available_cross) const {
const NormalizedSize default_size =
Normalize(orientation(), rule.Run(child, SizeBounds()));
if (orientation() != LayoutOrientation::kVertical)
return default_size;
// In vertical layouts it's important to consider height-for-width type
// calculations.
const NormalizedSize stretch_size =
Normalize(orientation(),
rule.Run(child, SizeBounds(available_cross, base::nullopt)));
if (cross_axis_alignment() == LayoutAlignment::kStretch)
return stretch_size;
// In non-stretch environments, we don't want the cross-axis size to exceed
// the default, or the main-axis size to shrink below the default.
return NormalizedSize(std::max(default_size.main(), stretch_size.main()),
std::min(default_size.cross(), stretch_size.cross()));
}
NormalizedSize FlexLayout::GetCurrentSizeForRule(
const FlexRule& rule,
const View* child,
const NormalizedSizeBounds& available) const {
return Normalize(orientation(),
rule.Run(child, Denormalize(orientation(), available)));
}
void FlexLayout::InitializeChildData( void FlexLayout::InitializeChildData(
const NormalizedSizeBounds& bounds, const NormalizedSizeBounds& bounds,
FlexLayoutData* data, FlexLayoutData* data,
...@@ -403,47 +434,17 @@ void FlexLayout::InitializeChildData( ...@@ -403,47 +434,17 @@ void FlexLayout::InitializeChildData(
orientation(), orientation(),
GetViewProperty(child, layout_defaults_, views::kInternalPaddingKey)); GetViewProperty(child, layout_defaults_, views::kInternalPaddingKey));
// FlexSpecification defines "preferred size" as the size returned when we
// do not bound the inputs (specifically the main axis). This is different
// from View::GetPreferredSize() which may not take into account e.g. an
// the necessity to alter a view's height in a vertical layout if the width
// is bounded. In the common case this will be equivalent to calling
// GetPreferredSize().
const base::Optional<int> available_cross = const base::Optional<int> available_cross =
GetAvailableCrossAxisSize(*data, view_index, bounds); GetAvailableCrossAxisSize(*data, view_index, bounds);
const NormalizedSize default_size = Normalize( flex_child.preferred_size =
orientation(), flex_child.flex.rule().Run(child, SizeBounds())); GetPreferredSizeForRule(flex_child.flex.rule(), child, available_cross);
// In vertical layouts it's important to consider height-for-width type
// calculations.
if (orientation() == LayoutOrientation::kVertical) {
const NormalizedSize stretch_size =
Normalize(orientation(),
flex_child.flex.rule().Run(
child, SizeBounds(available_cross, base::nullopt)));
if (cross_axis_alignment() == LayoutAlignment::kStretch) {
flex_child.preferred_size = stretch_size;
} else {
// In non-stretch environments, we don't want the cross-axis size to
// exceed the default, or the main-axis size to shrink below the
// default.
flex_child.preferred_size = NormalizedSize(
std::max(default_size.main(), stretch_size.main()),
std::min(default_size.cross(), stretch_size.cross()));
}
} else {
// Just use the default preferred size.
flex_child.preferred_size = default_size;
}
// gfx::Size calculation depends on whether flex is allowed. // gfx::Size calculation depends on whether flex is allowed.
if (main_axis_bounded) { if (main_axis_bounded) {
flex_child.current_size = Normalize( flex_child.current_size =
orientation(), GetCurrentSizeForRule(flex_child.flex.rule(), child,
flex_child.flex.rule().Run( NormalizedSizeBounds(0, available_cross));
child, Denormalize(orientation(),
NormalizedSizeBounds(0, available_cross))));
SetCrossAxis(&child_layout.available_size, orientation(), SetCrossAxis(&child_layout.available_size, orientation(),
available_cross); available_cross);
...@@ -801,10 +802,8 @@ void FlexLayout::AllocateFlexSpace( ...@@ -801,10 +802,8 @@ void FlexLayout::AllocateFlexSpace(
flex_amount + old_size - margin_delta, flex_amount + old_size - margin_delta,
GetCrossAxis(orientation(), child_layout.available_size)); GetCrossAxis(orientation(), child_layout.available_size));
NormalizedSize desired_size = Normalize( NormalizedSize desired_size = GetCurrentSizeForRule(
orientation(), flex_child.flex.rule(), child_layout.child_view, available);
flex_child.flex.rule().Run(child_layout.child_view,
Denormalize(orientation(), available)));
if (desired_size.main() <= 0) if (desired_size.main() <= 0)
continue; continue;
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace views { namespace views {
class NormalizedSize;
class NormalizedSizeBounds; class NormalizedSizeBounds;
class View; class View;
...@@ -145,6 +146,21 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -145,6 +146,21 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
// See FlexSpecification::order(). // See FlexSpecification::order().
using FlexOrderToViewIndexMap = std::map<int, std::vector<size_t>>; using FlexOrderToViewIndexMap = std::map<int, std::vector<size_t>>;
// Returns the preferred size for a given |rule| and |child| given unbounded
// space, with the caveat that for vertical layouts the horizontal axis is
// bounded to |available_cross| to factor in height-for-width considerations.
// This corresponds to the FlexSpecification "preferred size".
NormalizedSize GetPreferredSizeForRule(
const FlexRule& rule,
const View* child,
const base::Optional<int>& available_cross) const;
// Returns the size for a given |rule| and |child| with |available| space.
NormalizedSize GetCurrentSizeForRule(
const FlexRule& rule,
const View* child,
const NormalizedSizeBounds& available) const;
// Fills out the child entries for |data| and generates some initial size // Fills out the child entries for |data| and generates some initial size
// and visibility data, and stores off information about which views can // and visibility data, and stores off information about which views can
// expand in |flex_order_to_index|. // expand in |flex_order_to_index|.
......
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