Add a Key class
[chromium-blink-merge.git] / apps / size_constraints.cc
blob4686965f99f5f0963bd025030bc937dcb0a6ae51
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/size_constraints.h"
7 #include <algorithm>
9 #include "ui/gfx/insets.h"
11 namespace apps {
13 SizeConstraints::SizeConstraints()
14 : maximum_size_(kUnboundedSize, kUnboundedSize) {}
16 SizeConstraints::SizeConstraints(const gfx::Size& min_size,
17 const gfx::Size& max_size)
18 : minimum_size_(min_size), maximum_size_(max_size) {}
20 SizeConstraints::~SizeConstraints() {}
22 // static
23 gfx::Size SizeConstraints::AddFrameToConstraints(
24 const gfx::Size& size_constraints,
25 const gfx::Insets& frame_insets) {
26 return gfx::Size(
27 size_constraints.width() == kUnboundedSize
28 ? kUnboundedSize
29 : size_constraints.width() + frame_insets.width(),
30 size_constraints.height() == kUnboundedSize
31 ? kUnboundedSize
32 : size_constraints.height() + frame_insets.height());
35 gfx::Size SizeConstraints::ClampSize(gfx::Size size) const {
36 const gfx::Size max_size = GetMaximumSize();
37 if (max_size.width() != kUnboundedSize)
38 size.set_width(std::min(size.width(), max_size.width()));
39 if (max_size.height() != kUnboundedSize)
40 size.set_height(std::min(size.height(), max_size.height()));
41 size.SetToMax(GetMinimumSize());
42 return size;
45 bool SizeConstraints::HasMinimumSize() const {
46 const gfx::Size min_size = GetMinimumSize();
47 return min_size.width() != kUnboundedSize ||
48 min_size.height() != kUnboundedSize;
51 bool SizeConstraints::HasMaximumSize() const {
52 const gfx::Size max_size = GetMaximumSize();
53 return max_size.width() != kUnboundedSize ||
54 max_size.height() != kUnboundedSize;
57 bool SizeConstraints::HasFixedSize() const {
58 return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize();
61 gfx::Size SizeConstraints::GetMinimumSize() const {
62 return minimum_size_;
65 gfx::Size SizeConstraints::GetMaximumSize() const {
66 return gfx::Size(
67 maximum_size_.width() == kUnboundedSize
68 ? kUnboundedSize
69 : std::max(maximum_size_.width(), minimum_size_.width()),
70 maximum_size_.height() == kUnboundedSize
71 ? kUnboundedSize
72 : std::max(maximum_size_.height(), minimum_size_.height()));
75 void SizeConstraints::set_minimum_size(const gfx::Size& min_size) {
76 minimum_size_ = min_size;
79 void SizeConstraints::set_maximum_size(const gfx::Size& max_size) {
80 maximum_size_ = max_size;
83 } // namespace apps