1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/layout/fill_layout.h"
#include <algorithm>
#include "ui/views/view_class_properties.h"
namespace views {
FillLayout::FillLayout() = default;
FillLayout::~FillLayout() = default;
FillLayout& FillLayout::SetMinimumSizeEnabled(bool minimum_size_enabled) {
if (minimum_size_enabled != minimum_size_enabled_) {
minimum_size_enabled_ = minimum_size_enabled;
InvalidateHost(true);
}
return *this;
}
FillLayout& FillLayout::SetIncludeInsets(bool include_insets) {
if (include_insets != include_insets_) {
include_insets_ = include_insets;
InvalidateHost(true);
}
return *this;
}
ProposedLayout FillLayout::CalculateProposedLayout(
const SizeBounds& size_bounds) const {
// Because we explicitly override GetPreferredSize and
// GetPreferredHeightForWidth(), we should always call this method with well-
// defined bounds.
DCHECK(size_bounds.is_fully_bounded());
ProposedLayout layout;
layout.host_size = host_view()->size();
const gfx::Rect contents_bounds = host_view()->GetContentsBounds();
for (View* child : host_view()->children()) {
if (!child->GetProperty(kViewIgnoredByLayoutKey)) {
layout.child_layouts.push_back(
ChildLayout{child, child->GetVisible(), contents_bounds,
SizeBounds(contents_bounds.size())});
}
}
return layout;
}
gfx::Size FillLayout::GetPreferredSize(const View* host) const {
DCHECK_EQ(host_view(), host);
gfx::Size result;
bool has_child = false;
for (const View* child : host->children()) {
if (!child->GetProperty(kViewIgnoredByLayoutKey)) {
has_child = true;
result.SetToMax(child->GetPreferredSize(GetContentsSizeBounds(host)));
}
}
// For backwards compatibility, do not include insets if there are no
// children.
if (has_child && include_insets_) {
const gfx::Insets insets = host->GetInsets();
result.Enlarge(insets.width(), insets.height());
}
return result;
}
gfx::Size FillLayout::GetPreferredSize(const View* host,
const SizeBounds& available_size) const {
DCHECK_EQ(host_view(), host);
gfx::Size result;
bool has_child = false;
const SizeBounds new_available_size =
include_insets_ ? available_size.Inset(host->GetInsets())
: available_size;
for (const View* child : host->children()) {
if (!child->GetProperty(kViewIgnoredByLayoutKey)) {
has_child = true;
result.SetToMax(child->GetPreferredSize(new_available_size));
}
}
// For backwards compatibility, do not include insets if there are no
// children.
if (has_child && include_insets_) {
const gfx::Insets insets = host->GetInsets();
result.Enlarge(insets.width(), insets.height());
}
return result;
}
gfx::Size FillLayout::GetMinimumSize(const View* host) const {
DCHECK_EQ(host_view(), host);
if (!minimum_size_enabled_) {
return host->GetPreferredSize(GetContentsSizeBounds(host));
}
gfx::Size result;
bool has_child = false;
for (const View* child : host->children()) {
if (!child->GetProperty(kViewIgnoredByLayoutKey)) {
has_child = true;
result.SetToMax(child->GetMinimumSize());
}
}
// For backwards compatibility, do not include insets if there are no
// children.
if (has_child && include_insets_) {
const gfx::Insets insets = host->GetInsets();
result.Enlarge(insets.width(), insets.height());
}
return result;
}
int FillLayout::GetPreferredHeightForWidth(const View* host, int width) const {
DCHECK_EQ(host_view(), host);
const gfx::Insets insets = host->GetInsets();
width -= insets.width();
int height = 0;
for (const View* child : host->children()) {
if (!child->GetProperty(kViewIgnoredByLayoutKey)) {
height =
std::max(height, insets.height() + child->GetHeightForWidth(width));
}
}
return height;
}
SizeBounds FillLayout::GetContentsSizeBounds(const View* host) const {
return host->bounds().IsEmpty()
? SizeBounds()
: SizeBounds(host->GetContentsBounds().size());
}
} // namespace views
|