• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/devtools/devtools_contents_resizing_strategy.h"
6 
7 #include <algorithm>
8 
DevToolsContentsResizingStrategy()9 DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy() {
10 }
11 
DevToolsContentsResizingStrategy(const gfx::Insets & insets,const gfx::Size & min_size)12 DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
13     const gfx::Insets& insets, const gfx::Size& min_size)
14     : insets_(insets),
15       min_size_(min_size) {
16 }
17 
DevToolsContentsResizingStrategy(const gfx::Rect & bounds)18 DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
19     const gfx::Rect& bounds)
20     : bounds_(bounds) {
21 }
22 
23 
CopyFrom(const DevToolsContentsResizingStrategy & strategy)24 void DevToolsContentsResizingStrategy::CopyFrom(
25     const DevToolsContentsResizingStrategy& strategy) {
26   insets_ = strategy.insets();
27   min_size_ = strategy.min_size();
28   bounds_ = strategy.bounds();
29 }
30 
Equals(const DevToolsContentsResizingStrategy & strategy)31 bool DevToolsContentsResizingStrategy::Equals(
32     const DevToolsContentsResizingStrategy& strategy) {
33   return insets_ == strategy.insets() && min_size_ == strategy.min_size() &&
34       bounds_ == strategy.bounds();
35 }
36 
ApplyDevToolsContentsResizingStrategy(const DevToolsContentsResizingStrategy & strategy,const gfx::Size & container_size,const gfx::Rect & old_devtools_bounds,const gfx::Rect & old_contents_bounds,gfx::Rect * new_devtools_bounds,gfx::Rect * new_contents_bounds)37 void ApplyDevToolsContentsResizingStrategy(
38     const DevToolsContentsResizingStrategy& strategy,
39     const gfx::Size& container_size,
40     const gfx::Rect& old_devtools_bounds,
41     const gfx::Rect& old_contents_bounds,
42     gfx::Rect* new_devtools_bounds,
43     gfx::Rect* new_contents_bounds) {
44   new_devtools_bounds->SetRect(
45       0, 0, container_size.width(), container_size.height());
46 
47   const gfx::Insets& insets = strategy.insets();
48   const gfx::Size& min_size = strategy.min_size();
49   const gfx::Rect& bounds = strategy.bounds();
50 
51   if (!bounds.size().IsEmpty()) {
52     int left = std::min(bounds.x(), container_size.width());
53     int top = std::min(bounds.y(), container_size.height());
54     int width = std::min(bounds.width(), container_size.width() - left);
55     int height = std::min(bounds.height(), container_size.height() - top);
56     new_contents_bounds->SetRect(left, top, width, height);
57     return;
58   }
59 
60   int width = std::max(0, container_size.width() - insets.width());
61   int left = insets.left();
62   if (width < min_size.width() && insets.width() > 0) {
63     int min_width = std::min(min_size.width(), container_size.width());
64     int insets_width = container_size.width() - min_width;
65     int insets_decrease = insets.width() - insets_width;
66     // Decrease both left and right insets proportionally.
67     left -= insets_decrease * insets.left() / insets.width();
68     width = min_width;
69   }
70   left = std::max(0, std::min(container_size.width(), left));
71 
72   int height = std::max(0, container_size.height() - insets.height());
73   int top = insets.top();
74   if (height < min_size.height() && insets.height() > 0) {
75     int min_height = std::min(min_size.height(), container_size.height());
76     int insets_height = container_size.height() - min_height;
77     int insets_decrease = insets.height() - insets_height;
78     // Decrease both top and bottom insets proportionally.
79     top -= insets_decrease * insets.top() / insets.height();
80     height = min_height;
81   }
82   top = std::max(0, std::min(container_size.height(), top));
83 
84   new_contents_bounds->SetRect(left, top, width, height);
85 }
86