Commit a0a331e3 authored by Dana Fried's avatar Dana Fried Committed by Commit Bot

Move proposed layout into its own file.

This prevents us from having to put our layout interpolation code in
LayoutManagerBase where it really doesn't belong.

Bug: 898632
Change-Id: I837b14ff05eee9f5ea9b2659955d787bde23958e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1815665Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Commit-Queue: Dana Fried <dfried@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698696}
parent 8495c9bf
...@@ -83,8 +83,7 @@ InterpolatingLayoutManager::GetInterpolation( ...@@ -83,8 +83,7 @@ InterpolatingLayoutManager::GetInterpolation(
return result; return result;
} }
views::LayoutManagerBase::ProposedLayout views::ProposedLayout InterpolatingLayoutManager::CalculateProposedLayout(
InterpolatingLayoutManager::CalculateProposedLayout(
const views::SizeBounds& size_bounds) const { const views::SizeBounds& size_bounds) const {
// For interpolating layout we will never call this method except for fully- // For interpolating layout we will never call this method except for fully-
// specified sizes. // specified sizes.
...@@ -93,14 +92,17 @@ InterpolatingLayoutManager::CalculateProposedLayout( ...@@ -93,14 +92,17 @@ InterpolatingLayoutManager::CalculateProposedLayout(
const gfx::Size size(*size_bounds.width(), *size_bounds.height()); const gfx::Size size(*size_bounds.width(), *size_bounds.height());
const LayoutInterpolation interpolation = GetInterpolation(size_bounds); const LayoutInterpolation interpolation = GetInterpolation(size_bounds);
const ProposedLayout first = interpolation.first->GetProposedLayout(size); const views::ProposedLayout first =
interpolation.first->GetProposedLayout(size);
if (!interpolation.second) if (!interpolation.second)
return first; return first;
// If the target size falls in an interpolation range, get the other layout. // If the target size falls in an interpolation range, get the other layout.
const ProposedLayout second = interpolation.second->GetProposedLayout(size); const views::ProposedLayout second =
return Interpolate(interpolation.percent_second, first, second); interpolation.second->GetProposedLayout(size);
return views::ProposedLayoutBetween(interpolation.percent_second, first,
second);
} }
void InterpolatingLayoutManager::SetDefaultLayout( void InterpolatingLayoutManager::SetDefaultLayout(
......
...@@ -80,7 +80,7 @@ class InterpolatingLayoutManager : public views::LayoutManagerBase { ...@@ -80,7 +80,7 @@ class InterpolatingLayoutManager : public views::LayoutManagerBase {
int width) const override; int width) const override;
protected: protected:
ProposedLayout CalculateProposedLayout( views::ProposedLayout CalculateProposedLayout(
const views::SizeBounds& size_bounds) const override; const views::SizeBounds& size_bounds) const override;
private: private:
......
...@@ -46,8 +46,8 @@ class TestLayout : public LayoutManagerBase { ...@@ -46,8 +46,8 @@ class TestLayout : public LayoutManagerBase {
mutable int num_layouts_generated_ = 0; mutable int num_layouts_generated_ = 0;
}; };
void CompareProposedLayouts(const LayoutManagerBase::ProposedLayout& left, void CompareProposedLayouts(const ProposedLayout& left,
const LayoutManagerBase::ProposedLayout& right) { const ProposedLayout& right) {
EXPECT_EQ(left.host_size, right.host_size); EXPECT_EQ(left.host_size, right.host_size);
EXPECT_EQ(left.child_layouts.size(), right.child_layouts.size()); EXPECT_EQ(left.child_layouts.size(), right.child_layouts.size());
for (auto left_it = left.child_layouts.begin(), for (auto left_it = left.child_layouts.begin(),
...@@ -284,9 +284,9 @@ TEST_F(InterpolatingLayoutManagerTest, GetProposedLayout) { ...@@ -284,9 +284,9 @@ TEST_F(InterpolatingLayoutManagerTest, GetProposedLayout) {
constexpr gfx::Size kLargeSize{11, 10}; constexpr gfx::Size kLargeSize{11, 10};
constexpr gfx::Size kOneThirdSize{7, 10}; constexpr gfx::Size kOneThirdSize{7, 10};
constexpr gfx::Size kOneHalfSize{8, 10}; constexpr gfx::Size kOneHalfSize{8, 10};
const LayoutManagerBase::ProposedLayout expected_default = const ProposedLayout expected_default =
first_layout->GetProposedLayout(kSmallSize); first_layout->GetProposedLayout(kSmallSize);
const LayoutManagerBase::ProposedLayout expected_other = const ProposedLayout expected_other =
second_layout->GetProposedLayout(kLargeSize); second_layout->GetProposedLayout(kLargeSize);
CompareProposedLayouts(expected_default, CompareProposedLayouts(expected_default,
...@@ -294,8 +294,7 @@ TEST_F(InterpolatingLayoutManagerTest, GetProposedLayout) { ...@@ -294,8 +294,7 @@ TEST_F(InterpolatingLayoutManagerTest, GetProposedLayout) {
CompareProposedLayouts(expected_other, CompareProposedLayouts(expected_other,
layout_manager()->GetProposedLayout(kLargeSize)); layout_manager()->GetProposedLayout(kLargeSize));
LayoutManagerBase::ProposedLayout actual = ProposedLayout actual = layout_manager()->GetProposedLayout(kOneThirdSize);
layout_manager()->GetProposedLayout(kOneThirdSize);
EXPECT_EQ(gfx::Tween::SizeValueBetween(0.3333, expected_default.host_size, EXPECT_EQ(gfx::Tween::SizeValueBetween(0.3333, expected_default.host_size,
expected_other.host_size), expected_other.host_size),
actual.host_size); actual.host_size);
......
...@@ -210,6 +210,7 @@ jumbo_component("views") { ...@@ -210,6 +210,7 @@ jumbo_component("views") {
"layout/layout_provider.h", "layout/layout_provider.h",
"layout/layout_types.h", "layout/layout_types.h",
"layout/normalized_geometry.h", "layout/normalized_geometry.h",
"layout/proposed_layout.h",
"masked_targeter_delegate.h", "masked_targeter_delegate.h",
"metadata/metadata_cache.h", "metadata/metadata_cache.h",
"metadata/metadata_header_macros.h", "metadata/metadata_header_macros.h",
...@@ -419,6 +420,7 @@ jumbo_component("views") { ...@@ -419,6 +420,7 @@ jumbo_component("views") {
"layout/layout_provider.cc", "layout/layout_provider.cc",
"layout/layout_types.cc", "layout/layout_types.cc",
"layout/normalized_geometry.cc", "layout/normalized_geometry.cc",
"layout/proposed_layout.cc",
"masked_targeter_delegate.cc", "masked_targeter_delegate.cc",
"metadata/metadata_cache.cc", "metadata/metadata_cache.cc",
"metadata/metadata_types.cc", "metadata/metadata_types.cc",
......
...@@ -19,12 +19,6 @@ ...@@ -19,12 +19,6 @@
namespace views { namespace views {
using layout::Denormalize;
using layout::Normalize;
using layout::NormalizedInsets;
using layout::NormalizedRect;
using layout::NormalizedSize;
// Holds data about a view that is fading in or out as part of an animation. // Holds data about a view that is fading in or out as part of an animation.
struct AnimatingLayoutManager::LayoutFadeInfo { struct AnimatingLayoutManager::LayoutFadeInfo {
// Whether the view is fading in or out. // Whether the view is fading in or out.
...@@ -381,8 +375,7 @@ void AnimatingLayoutManager::EnableAnimationForTesting() { ...@@ -381,8 +375,7 @@ void AnimatingLayoutManager::EnableAnimationForTesting() {
DCHECK(animation_delegate_->ready_to_animate()); DCHECK(animation_delegate_->ready_to_animate());
} }
LayoutManagerBase::ProposedLayout ProposedLayout AnimatingLayoutManager::CalculateProposedLayout(
AnimatingLayoutManager::CalculateProposedLayout(
const SizeBounds& size_bounds) const { const SizeBounds& size_bounds) const {
// This class directly overrides Layout() so GetProposedLayout() and // This class directly overrides Layout() so GetProposedLayout() and
// CalculateProposedLayout() are not called. // CalculateProposedLayout() are not called.
...@@ -482,7 +475,8 @@ void AnimatingLayoutManager::RunDelayedActions() { ...@@ -482,7 +475,8 @@ void AnimatingLayoutManager::RunDelayedActions() {
void AnimatingLayoutManager::UpdateCurrentLayout(double percent) { void AnimatingLayoutManager::UpdateCurrentLayout(double percent) {
// This drops out any child view elements that don't exist in the target // This drops out any child view elements that don't exist in the target
// layout. We'll add them back in later. // layout. We'll add them back in later.
current_layout_ = Interpolate(percent, starting_layout_, target_layout_); current_layout_ =
ProposedLayoutBetween(percent, starting_layout_, target_layout_);
if (default_fade_mode_ == FadeInOutMode::kHide || fade_infos_.empty()) if (default_fade_mode_ == FadeInOutMode::kHide || fade_infos_.empty())
return; return;
...@@ -676,7 +670,7 @@ void AnimatingLayoutManager::CalculateFadeInfos() { ...@@ -676,7 +670,7 @@ void AnimatingLayoutManager::CalculateFadeInfos() {
} }
} }
LayoutManagerBase::ChildLayout AnimatingLayoutManager::CalculateScaleFade( ChildLayout AnimatingLayoutManager::CalculateScaleFade(
const LayoutFadeInfo& fade_info, const LayoutFadeInfo& fade_info,
base::Optional<size_t> prev_index, base::Optional<size_t> prev_index,
base::Optional<size_t> next_index, base::Optional<size_t> next_index,
...@@ -726,7 +720,7 @@ LayoutManagerBase::ChildLayout AnimatingLayoutManager::CalculateScaleFade( ...@@ -726,7 +720,7 @@ LayoutManagerBase::ChildLayout AnimatingLayoutManager::CalculateScaleFade(
return child_layout; return child_layout;
} }
LayoutManagerBase::ChildLayout AnimatingLayoutManager::CalculateSlideFade( ChildLayout AnimatingLayoutManager::CalculateSlideFade(
const LayoutFadeInfo& fade_info, const LayoutFadeInfo& fade_info,
base::Optional<size_t> prev_index, base::Optional<size_t> prev_index,
base::Optional<size_t> next_index, base::Optional<size_t> next_index,
......
...@@ -28,15 +28,6 @@ ...@@ -28,15 +28,6 @@
namespace views { namespace views {
using layout::NormalizedInsets;
using layout::NormalizedPoint;
using layout::NormalizedRect;
using layout::NormalizedSize;
using layout::NormalizedSizeBounds;
using layout::Denormalize;
using layout::Normalize;
namespace { namespace {
// Layout information for a specific child view in a proposed layout. // Layout information for a specific child view in a proposed layout.
...@@ -334,7 +325,7 @@ FlexLayout& FlexLayout::SetBetweenChildSpacing(int between_child_spacing) { ...@@ -334,7 +325,7 @@ FlexLayout& FlexLayout::SetBetweenChildSpacing(int between_child_spacing) {
return *this; return *this;
} }
LayoutManagerBase::ProposedLayout FlexLayout::CalculateProposedLayout( ProposedLayout FlexLayout::CalculateProposedLayout(
const SizeBounds& size_bounds) const { const SizeBounds& size_bounds) const {
FlexLayoutData data; FlexLayoutData data;
...@@ -386,7 +377,7 @@ LayoutManagerBase::ProposedLayout FlexLayout::CalculateProposedLayout( ...@@ -386,7 +377,7 @@ LayoutManagerBase::ProposedLayout FlexLayout::CalculateProposedLayout(
} }
void FlexLayout::InitializeChildData( void FlexLayout::InitializeChildData(
const layout::NormalizedSizeBounds& bounds, const NormalizedSizeBounds& bounds,
FlexLayoutData* data, FlexLayoutData* data,
FlexOrderToViewIndexMap* flex_order_to_index) const { FlexOrderToViewIndexMap* flex_order_to_index) const {
// Step through the children, creating placeholder layout view elements // Step through the children, creating placeholder layout view elements
......
...@@ -22,11 +22,8 @@ ...@@ -22,11 +22,8 @@
namespace views { namespace views {
class View;
namespace layout {
class NormalizedSizeBounds; class NormalizedSizeBounds;
} class View;
// Provides CSS-like layout for a one-dimensional (vertical or horizontal) // Provides CSS-like layout for a one-dimensional (vertical or horizontal)
// arrangement of child views. Independent alignment can be specified for the // arrangement of child views. Independent alignment can be specified for the
...@@ -165,7 +162,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -165,7 +162,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
base::Optional<int> GetAvailableCrossAxisSize( base::Optional<int> GetAvailableCrossAxisSize(
const FlexLayoutData& layout, const FlexLayoutData& layout,
size_t child_index, size_t child_index,
const layout::NormalizedSizeBounds& bounds) const; const NormalizedSizeBounds& bounds) const;
// Calculates the preferred spacing between two child views, or between a // Calculates the preferred spacing between two child views, or between a
// view edge and the first or last visible child views. // view edge and the first or last visible child views.
...@@ -175,7 +172,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -175,7 +172,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
// Calculates the position of each child view and the size of the overall // Calculates the position of each child view and the size of the overall
// layout based on tentative visibilities and sizes for each child. // layout based on tentative visibilities and sizes for each child.
void UpdateLayoutFromChildren(const layout::NormalizedSizeBounds& bounds, void UpdateLayoutFromChildren(const NormalizedSizeBounds& bounds,
FlexLayoutData* data, FlexLayoutData* data,
ChildViewSpacing* child_spacing) const; ChildViewSpacing* child_spacing) const;
...@@ -190,7 +187,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -190,7 +187,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
// Typically, this method will be called once with |expandable_views| set and // Typically, this method will be called once with |expandable_views| set and
// then again with it null to allocate the remaining space. // then again with it null to allocate the remaining space.
void AllocateFlexSpace( void AllocateFlexSpace(
const layout::NormalizedSizeBounds& bounds, const NormalizedSizeBounds& bounds,
const FlexOrderToViewIndexMap& order_to_index, const FlexOrderToViewIndexMap& order_to_index,
FlexLayoutData* data, FlexLayoutData* data,
ChildViewSpacing* child_spacing, ChildViewSpacing* child_spacing,
...@@ -199,7 +196,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase { ...@@ -199,7 +196,7 @@ class VIEWS_EXPORT FlexLayout : public LayoutManagerBase {
// 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|.
void InitializeChildData(const layout::NormalizedSizeBounds& bounds, void InitializeChildData(const NormalizedSizeBounds& bounds,
FlexLayoutData* data, FlexLayoutData* data,
FlexOrderToViewIndexMap* flex_order_to_index) const; FlexOrderToViewIndexMap* flex_order_to_index) const;
......
...@@ -8,39 +8,10 @@ ...@@ -8,39 +8,10 @@
#include "base/auto_reset.h" #include "base/auto_reset.h"
#include "base/logging.h" #include "base/logging.h"
#include "ui/gfx/animation/tween.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace views { namespace views {
bool LayoutManagerBase::ChildLayout::operator==(
const ChildLayout& other) const {
// Note: if the view is not visible, the bounds do not matter as they will not
// be set.
return child_view == other.child_view && visible == other.visible &&
(!visible || bounds == other.bounds);
}
LayoutManagerBase::ProposedLayout::ProposedLayout() = default;
LayoutManagerBase::ProposedLayout::ProposedLayout(const ProposedLayout& other) =
default;
LayoutManagerBase::ProposedLayout::ProposedLayout(ProposedLayout&& other) =
default;
LayoutManagerBase::ProposedLayout::ProposedLayout(
const gfx::Size& size,
const std::initializer_list<ChildLayout>& children)
: host_size(size), child_layouts(children) {}
LayoutManagerBase::ProposedLayout::~ProposedLayout() = default;
LayoutManagerBase::ProposedLayout& LayoutManagerBase::ProposedLayout::operator=(
const ProposedLayout& other) = default;
LayoutManagerBase::ProposedLayout& LayoutManagerBase::ProposedLayout::operator=(
ProposedLayout&& other) = default;
bool LayoutManagerBase::ProposedLayout::operator==(
const ProposedLayout& other) const {
return host_size == other.host_size && child_layouts == other.child_layouts;
}
LayoutManagerBase::~LayoutManagerBase() = default; LayoutManagerBase::~LayoutManagerBase() = default;
gfx::Size LayoutManagerBase::GetPreferredSize(const View* host) const { gfx::Size LayoutManagerBase::GetPreferredSize(const View* host) const {
...@@ -74,50 +45,13 @@ void LayoutManagerBase::Layout(View* host) { ...@@ -74,50 +45,13 @@ void LayoutManagerBase::Layout(View* host) {
ApplyLayout(GetProposedLayout(size)); ApplyLayout(GetProposedLayout(size));
} }
// static
views::LayoutManagerBase::ProposedLayout LayoutManagerBase::Interpolate(
double value,
const ProposedLayout& start,
const ProposedLayout& target) {
if (value >= 1.0)
return target;
ProposedLayout layout;
// Interpolate the host size.
layout.host_size =
gfx::Tween::SizeValueBetween(value, start.host_size, target.host_size);
// The views may not be listed in the same order and some views might be
// omitted from either the |start| or |target| layout.
std::map<const views::View*, size_t> start_view_to_index;
for (size_t i = 0; i < start.child_layouts.size(); ++i)
start_view_to_index.emplace(start.child_layouts[i].child_view, i);
for (const ChildLayout& target_child : target.child_layouts) {
// Try to match the view from the target with the view from the start.
const auto start_match = start_view_to_index.find(target_child.child_view);
if (start_match == start_view_to_index.end()) {
// If there is no match, make the view present but invisible.
layout.child_layouts.push_back({target_child.child_view, false});
} else {
// Tween the two layouts.
const ChildLayout& start_child = start.child_layouts[start_match->second];
layout.child_layouts.push_back(
{target_child.child_view, start_child.visible && target_child.visible,
gfx::Tween::RectValueBetween(value, start_child.bounds,
target_child.bounds)});
}
}
return layout;
}
std::vector<View*> LayoutManagerBase::GetChildViewsInPaintOrder( std::vector<View*> LayoutManagerBase::GetChildViewsInPaintOrder(
const View* host) const { const View* host) const {
DCHECK_EQ(host_view_, host); DCHECK_EQ(host_view_, host);
return LayoutManager::GetChildViewsInPaintOrder(host); return LayoutManager::GetChildViewsInPaintOrder(host);
} }
LayoutManagerBase::ProposedLayout LayoutManagerBase::GetProposedLayout( ProposedLayout LayoutManagerBase::GetProposedLayout(
const gfx::Size& host_size) const { const gfx::Size& host_size) const {
if (cached_layout_size_ != host_size) { if (cached_layout_size_ != host_size) {
cached_layout_size_ = host_size; cached_layout_size_ = host_size;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/views/layout/layout_manager.h" #include "ui/views/layout/layout_manager.h"
#include "ui/views/layout/layout_types.h" #include "ui/views/layout/layout_types.h"
#include "ui/views/layout/proposed_layout.h"
#include "ui/views/views_export.h" #include "ui/views/views_export.h"
namespace views { namespace views {
...@@ -28,43 +29,6 @@ class View; ...@@ -28,43 +29,6 @@ class View;
// CalculateProposedLayout(). Used in interpolating and animating layouts. // CalculateProposedLayout(). Used in interpolating and animating layouts.
class VIEWS_EXPORT LayoutManagerBase : public LayoutManager { class VIEWS_EXPORT LayoutManagerBase : public LayoutManager {
public: public:
// Represents layout information for a child view within a host being laid
// out.
struct VIEWS_EXPORT ChildLayout {
bool operator==(const ChildLayout& other) const;
bool operator!=(const ChildLayout& other) const {
return !(*this == other);
}
View* child_view = nullptr;
bool visible = false;
gfx::Rect bounds;
};
// Contains a full layout specification for the children of the host view.
struct VIEWS_EXPORT ProposedLayout {
ProposedLayout();
~ProposedLayout();
ProposedLayout(const ProposedLayout& other);
ProposedLayout(ProposedLayout&& other);
ProposedLayout(const gfx::Size& size,
const std::initializer_list<ChildLayout>& children);
ProposedLayout& operator=(const ProposedLayout& other);
ProposedLayout& operator=(ProposedLayout&& other);
bool operator==(const ProposedLayout& other) const;
bool operator!=(const ProposedLayout& other) const {
return !(*this == other);
}
// The size of the host view given the size bounds for this layout. If both
// dimensions of the size bounds are specified, this will be the same size.
gfx::Size host_size;
// Contains an entry for each child view included in the layout.
std::vector<ChildLayout> child_layouts;
};
~LayoutManagerBase() override; ~LayoutManagerBase() override;
View* host_view() { return host_view_; } View* host_view() { return host_view_; }
...@@ -87,13 +51,6 @@ class VIEWS_EXPORT LayoutManagerBase : public LayoutManager { ...@@ -87,13 +51,6 @@ class VIEWS_EXPORT LayoutManagerBase : public LayoutManager {
int GetPreferredHeightForWidth(const View* host, int width) const override; int GetPreferredHeightForWidth(const View* host, int width) const override;
void Layout(View* host) override; void Layout(View* host) override;
// Returns a layout that's linearly interpolated between |start| and |target|
// by |value|, which should be between 0 and 1. See
// gfx::Tween::LinearIntValueBetween() for the exact math involved.
static ProposedLayout Interpolate(double value,
const ProposedLayout& start,
const ProposedLayout& target);
protected: protected:
LayoutManagerBase(); LayoutManagerBase();
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
namespace views { namespace views {
// Test LayoutManagerBase-specific functionality:
namespace { namespace {
constexpr int kChildViewPadding = 5; constexpr int kChildViewPadding = 5;
...@@ -340,7 +338,7 @@ TEST_F(LayoutManagerBaseManagerTest, ApplyLayout) { ...@@ -340,7 +338,7 @@ TEST_F(LayoutManagerBaseManagerTest, ApplyLayout) {
// doesn't change. // doesn't change.
const gfx::Size old_size = host_view()->size(); const gfx::Size old_size = host_view()->size();
LayoutManagerBase::ProposedLayout layout; ProposedLayout layout;
// This should be ignored. // This should be ignored.
layout.host_size = {123, 456}; layout.host_size = {123, 456};
...@@ -366,7 +364,7 @@ TEST_F(LayoutManagerBaseManagerTest, ApplyLayout_SkipsOmittedViews) { ...@@ -366,7 +364,7 @@ TEST_F(LayoutManagerBaseManagerTest, ApplyLayout_SkipsOmittedViews) {
AddChildView(gfx::Size()); AddChildView(gfx::Size());
AddChildView(gfx::Size()); AddChildView(gfx::Size());
LayoutManagerBase::ProposedLayout layout; ProposedLayout layout;
// Set the child visibility and bounds. // Set the child visibility and bounds.
constexpr gfx::Rect kChild1Bounds(3, 4, 10, 15); constexpr gfx::Rect kChild1Bounds(3, 4, 10, 15);
constexpr gfx::Rect kChild2Bounds(1, 2, 3, 4); constexpr gfx::Rect kChild2Bounds(1, 2, 3, 4);
...@@ -597,7 +595,6 @@ TEST(LayoutManagerBase_ProposedLayoutTest, Equality) { ...@@ -597,7 +595,6 @@ TEST(LayoutManagerBase_ProposedLayoutTest, Equality) {
View* ptr0 = nullptr; View* ptr0 = nullptr;
View* ptr1 = ptr0 + 1; View* ptr1 = ptr0 + 1;
View* ptr2 = ptr0 + 2; View* ptr2 = ptr0 + 2;
using ProposedLayout = LayoutManagerBase::ProposedLayout;
ProposedLayout a; ProposedLayout a;
ProposedLayout b; ProposedLayout b;
EXPECT_TRUE(a == b); EXPECT_TRUE(a == b);
......
...@@ -11,16 +11,6 @@ ...@@ -11,16 +11,6 @@
namespace views { namespace views {
namespace {
std::string OptionalToString(const base::Optional<int>& opt) {
if (!opt.has_value())
return "_";
return base::StringPrintf("%d", opt.value());
}
} // namespace
// SizeBounds ------------------------------------------------------------------ // SizeBounds ------------------------------------------------------------------
SizeBounds::SizeBounds() = default; SizeBounds::SizeBounds() = default;
...@@ -55,8 +45,17 @@ bool SizeBounds::operator<(const SizeBounds& other) const { ...@@ -55,8 +45,17 @@ bool SizeBounds::operator<(const SizeBounds& other) const {
} }
std::string SizeBounds::ToString() const { std::string SizeBounds::ToString() const {
return base::StringPrintf("%s x %s", OptionalToString(width()).c_str(), std::ostringstream oss;
OptionalToString(height()).c_str()); if (width().has_value())
oss << *width();
else
oss << "_";
oss << " x ";
if (height().has_value())
oss << *height();
else
oss << "_";
return oss.str();
} }
} // namespace views } // namespace views
...@@ -16,17 +16,6 @@ ...@@ -16,17 +16,6 @@
#include "ui/views/layout/flex_layout_types.h" #include "ui/views/layout/flex_layout_types.h"
namespace views { namespace views {
namespace layout {
namespace {
std::string OptionalToString(const base::Optional<int>& opt) {
if (!opt.has_value())
return "_";
return base::StringPrintf("%d", opt.value());
}
} // namespace
// NormalizedPoint ------------------------------------------------------------- // NormalizedPoint -------------------------------------------------------------
...@@ -160,8 +149,17 @@ bool NormalizedSizeBounds::operator<(const NormalizedSizeBounds& other) const { ...@@ -160,8 +149,17 @@ bool NormalizedSizeBounds::operator<(const NormalizedSizeBounds& other) const {
} }
std::string NormalizedSizeBounds::ToString() const { std::string NormalizedSizeBounds::ToString() const {
return base::StringPrintf("%s x %s", OptionalToString(main()).c_str(), std::ostringstream oss;
OptionalToString(cross()).c_str()); if (main().has_value())
oss << *main();
else
oss << "_";
oss << " x ";
if (cross().has_value())
oss << *cross();
else
oss << "_";
return oss.str();
} }
// NormalizedRect -------------------------------------------------------------- // NormalizedRect --------------------------------------------------------------
...@@ -379,5 +377,4 @@ gfx::Rect Denormalize(LayoutOrientation orientation, ...@@ -379,5 +377,4 @@ gfx::Rect Denormalize(LayoutOrientation orientation,
Denormalize(orientation, bounds.size())); Denormalize(orientation, bounds.size()));
} }
} // namespace layout
} // namespace views } // namespace views
...@@ -22,13 +22,6 @@ namespace views { ...@@ -22,13 +22,6 @@ namespace views {
class SizeBounds; class SizeBounds;
// NOTE: Types in this file are intended for use by layout managers, preferably
// only in ui/views/layout. They are marked as VIEWS_EXPORT for use in unit
// tests as well as experimental layout managers currently not in the Views
// library.
namespace layout {
// Represents a point in layout space - that is, a point on the main and cross // Represents a point in layout space - that is, a point on the main and cross
// axes of the layout (regardless of whether it is vertically or horizontally // axes of the layout (regardless of whether it is vertically or horizontally
// oriented. // oriented.
...@@ -285,7 +278,6 @@ NormalizedRect VIEWS_EXPORT Normalize(LayoutOrientation orientation, ...@@ -285,7 +278,6 @@ NormalizedRect VIEWS_EXPORT Normalize(LayoutOrientation orientation,
gfx::Rect VIEWS_EXPORT Denormalize(LayoutOrientation orientation, gfx::Rect VIEWS_EXPORT Denormalize(LayoutOrientation orientation,
const NormalizedRect& rect); const NormalizedRect& rect);
} // namespace layout
} // namespace views } // namespace views
#endif // UI_VIEWS_LAYOUT_NORMALIZED_GEOMETRY_H_ #endif // UI_VIEWS_LAYOUT_NORMALIZED_GEOMETRY_H_
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace views { namespace views {
namespace layout {
TEST(NormalizedRectTest, Inset_NormalizedInsets) { TEST(NormalizedRectTest, Inset_NormalizedInsets) {
NormalizedRect rect(1, 2, 10, 11); NormalizedRect rect(1, 2, 10, 11);
...@@ -46,5 +45,4 @@ TEST(NormalizedRectTest, Inset_Negative) { ...@@ -46,5 +45,4 @@ TEST(NormalizedRectTest, Inset_Negative) {
EXPECT_EQ(17, rect.size_cross()); EXPECT_EQ(17, rect.size_cross());
} }
} // namespace layout
} // namespace views } // namespace views
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/layout/proposed_layout.h"
#include <map>
#include "ui/gfx/animation/tween.h"
namespace views {
bool ChildLayout::operator==(const ChildLayout& other) const {
// Note: if the view is not visible, the bounds do not matter as they will not
// be set.
return child_view == other.child_view && visible == other.visible &&
(!visible || bounds == other.bounds);
}
ProposedLayout::ProposedLayout() = default;
ProposedLayout::ProposedLayout(const ProposedLayout& other) = default;
ProposedLayout::ProposedLayout(ProposedLayout&& other) = default;
ProposedLayout::ProposedLayout(
const gfx::Size& size,
const std::initializer_list<ChildLayout>& children)
: host_size(size), child_layouts(children) {}
ProposedLayout::~ProposedLayout() = default;
ProposedLayout& ProposedLayout::operator=(const ProposedLayout& other) =
default;
ProposedLayout& ProposedLayout::operator=(ProposedLayout&& other) = default;
bool ProposedLayout::operator==(const ProposedLayout& other) const {
return host_size == other.host_size && child_layouts == other.child_layouts;
}
ProposedLayout ProposedLayoutBetween(double value,
const ProposedLayout& start,
const ProposedLayout& target) {
if (value >= 1.0)
return target;
ProposedLayout layout;
// Interpolate the host size.
layout.host_size =
gfx::Tween::SizeValueBetween(value, start.host_size, target.host_size);
// The views may not be listed in the same order and some views might be
// omitted from either the |start| or |target| layout.
std::map<const views::View*, size_t> start_view_to_index;
for (size_t i = 0; i < start.child_layouts.size(); ++i)
start_view_to_index.emplace(start.child_layouts[i].child_view, i);
for (const ChildLayout& target_child : target.child_layouts) {
// Try to match the view from the target with the view from the start.
const auto start_match = start_view_to_index.find(target_child.child_view);
if (start_match == start_view_to_index.end()) {
// If there is no match, make the view present but invisible.
layout.child_layouts.push_back({target_child.child_view, false});
} else {
// Tween the two layouts.
const ChildLayout& start_child = start.child_layouts[start_match->second];
layout.child_layouts.push_back(
{target_child.child_view, start_child.visible && target_child.visible,
gfx::Tween::RectValueBetween(value, start_child.bounds,
target_child.bounds)});
}
}
return layout;
}
} // namespace views
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_LAYOUT_PROPOSED_LAYOUT_H_
#define UI_VIEWS_LAYOUT_PROPOSED_LAYOUT_H_
#include "ui/gfx/geometry/rect.h"
#include "ui/views/views_export.h"
namespace views {
class View;
// Represents layout information for a child view within a host being laid
// out.
struct VIEWS_EXPORT ChildLayout {
bool operator==(const ChildLayout& other) const;
bool operator!=(const ChildLayout& other) const { return !(*this == other); }
View* child_view = nullptr;
bool visible = false;
gfx::Rect bounds;
};
// Contains a full layout specification for the children of the host view.
struct VIEWS_EXPORT ProposedLayout {
ProposedLayout();
~ProposedLayout();
ProposedLayout(const ProposedLayout& other);
ProposedLayout(ProposedLayout&& other);
ProposedLayout(const gfx::Size& size,
const std::initializer_list<ChildLayout>& children);
ProposedLayout& operator=(const ProposedLayout& other);
ProposedLayout& operator=(ProposedLayout&& other);
bool operator==(const ProposedLayout& other) const;
bool operator!=(const ProposedLayout& other) const {
return !(*this == other);
}
// The size of the host view given the size bounds for this layout. If both
// dimensions of the size bounds are specified, this will be the same size.
gfx::Size host_size;
// Contains an entry for each child view included in the layout.
std::vector<ChildLayout> child_layouts;
};
// Returns a layout that's linearly interpolated between |start| and |target|
// by |value|, which should be between 0 and 1. See
// gfx::Tween::LinearIntValueBetween() for the exact math involved.
VIEWS_EXPORT ProposedLayout ProposedLayoutBetween(double value,
const ProposedLayout& start,
const ProposedLayout& target);
} // namespace views
#endif // UI_VIEWS_LAYOUT_PROPOSED_LAYOUT_H_
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