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 "ui/wm/core/easy_resize_window_targeter.h"
6
7#include "ui/aura/window.h"
8#include "ui/gfx/geometry/insets_f.h"
9#include "ui/gfx/geometry/rect.h"
10#include "ui/wm/public/transient_window_client.h"
11
12namespace wm {
13
14EasyResizeWindowTargeter::EasyResizeWindowTargeter(
15    aura::Window* container,
16    const gfx::Insets& mouse_extend,
17    const gfx::Insets& touch_extend)
18    : container_(container),
19      mouse_extend_(mouse_extend),
20      touch_extend_(touch_extend) {
21}
22
23EasyResizeWindowTargeter::~EasyResizeWindowTargeter() {
24}
25
26bool EasyResizeWindowTargeter::EventLocationInsideBounds(
27    ui::EventTarget* target,
28    const ui::LocatedEvent& event) const {
29  aura::Window* window = static_cast<aura::Window*>(target);
30  if (ShouldUseExtendedBounds(window)) {
31    // Note that |event|'s location is in |window|'s parent's coordinate system,
32    // so convert it to |window|'s coordinate system first.
33    gfx::Point point = event.location();
34    if (window->parent())
35      aura::Window::ConvertPointToTarget(window->parent(), window, &point);
36
37    gfx::Rect bounds(window->bounds().size());
38    if (event.IsTouchEvent() || event.IsGestureEvent())
39      bounds.Inset(touch_extend_);
40    else
41      bounds.Inset(mouse_extend_);
42
43    return bounds.Contains(point);
44  }
45  return WindowTargeter::EventLocationInsideBounds(window, event);
46}
47
48bool EasyResizeWindowTargeter::ShouldUseExtendedBounds(
49    const aura::Window* window) const {
50  // Use the extended bounds only for immediate child windows of |container_|.
51  // Use the default targetter otherwise.
52  if (window->parent() != container_)
53    return false;
54
55  aura::client::TransientWindowClient* transient_window_client =
56      aura::client::GetTransientWindowClient();
57  return !transient_window_client ||
58      !transient_window_client->GetTransientParent(window) ||
59      transient_window_client->GetTransientParent(window) == container_;
60}
61
62}  // namespace wm
63