15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/devtools/devtools_contents_resizing_strategy.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <algorithm>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy()
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    : hide_inspected_contents_(false) {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const gfx::Rect& bounds)
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    : bounds_(bounds),
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      hide_inspected_contents_(bounds_.IsEmpty() && !bounds_.x() &&
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          !bounds_.y()) {
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void DevToolsContentsResizingStrategy::CopyFrom(
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const DevToolsContentsResizingStrategy& strategy) {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bounds_ = strategy.bounds();
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  hide_inspected_contents_ = strategy.hide_inspected_contents();
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool DevToolsContentsResizingStrategy::Equals(
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const DevToolsContentsResizingStrategy& strategy) {
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return bounds_ == strategy.bounds() &&
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      hide_inspected_contents_ == strategy.hide_inspected_contents();
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ApplyDevToolsContentsResizingStrategy(
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const DevToolsContentsResizingStrategy& strategy,
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const gfx::Size& container_size,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::Rect* new_devtools_bounds,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    gfx::Rect* new_contents_bounds) {
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new_devtools_bounds->SetRect(
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      0, 0, container_size.width(), container_size.height());
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const gfx::Rect& bounds = strategy.bounds();
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (bounds.size().IsEmpty() && !strategy.hide_inspected_contents()) {
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    new_contents_bounds->SetRect(
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        0, 0, container_size.width(), container_size.height());
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int left = std::min(bounds.x(), container_size.width());
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int top = std::min(bounds.y(), container_size.height());
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int width = std::min(bounds.width(), container_size.width() - left);
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  int height = std::min(bounds.height(), container_size.height() - top);
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new_contents_bounds->SetRect(left, top, width, height);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
54