summaryrefslogtreecommitdiffstats
path: root/chrome/browser/devtools/devtools_contents_resizing_strategy.cc
blob: 519278b6590c7a4e870b3b5daa36bd14733bdab7 (plain)
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
// Copyright 2014 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 "chrome/browser/devtools/devtools_contents_resizing_strategy.h"

#include <algorithm>

DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy() {
}

DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
    const gfx::Insets& insets, const gfx::Size& min_size)
    : insets_(insets),
      min_size_(min_size) {
}

void DevToolsContentsResizingStrategy::CopyFrom(
    const DevToolsContentsResizingStrategy& strategy) {
  insets_ = strategy.insets();
  min_size_ = strategy.min_size();
}

bool DevToolsContentsResizingStrategy::Equals(
    const DevToolsContentsResizingStrategy& strategy) {
  return insets_ == strategy.insets() && min_size_ == strategy.min_size();
}

void 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) {
  new_devtools_bounds->SetRect(
      0, 0, container_size.width(), container_size.height());

  const gfx::Insets& insets = strategy.insets();
  const gfx::Size& min_size = strategy.min_size();

  int width = std::max(0, container_size.width() - insets.width());
  int left = insets.left();
  if (width < min_size.width() && insets.width() > 0) {
    int min_width = std::min(min_size.width(), container_size.width());
    int insets_width = container_size.width() - min_width;
    int insets_decrease = insets.width() - insets_width;
    // Decrease both left and right insets proportionally.
    left -= insets_decrease * insets.left() / insets.width();
    width = min_width;
  }
  left = std::max(0, std::min(container_size.width(), left));

  int height = std::max(0, container_size.height() - insets.height());
  int top = insets.top();
  if (height < min_size.height() && insets.height() > 0) {
    int min_height = std::min(min_size.height(), container_size.height());
    int insets_height = container_size.height() - min_height;
    int insets_decrease = insets.height() - insets_height;
    // Decrease both top and bottom insets proportionally.
    top -= insets_decrease * insets.top() / insets.height();
    height = min_height;
  }
  top = std::max(0, std::min(container_size.height(), top));

  new_contents_bounds->SetRect(left, top, width, height);
}