1// Copyright 2013 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 "ash/frame/frame_border_hit_test_controller.h"
6
7#include "ash/ash_constants.h"
8#include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
9#include "ash/wm/resize_handle_window_targeter.h"
10#include "ash/wm/window_state_observer.h"
11#include "ui/aura/env.h"
12#include "ui/aura/window.h"
13#include "ui/aura/window_observer.h"
14#include "ui/aura/window_targeter.h"
15#include "ui/base/hit_test.h"
16#include "ui/views/widget/widget.h"
17#include "ui/views/widget/widget_delegate.h"
18#include "ui/views/window/non_client_view.h"
19
20namespace ash {
21
22FrameBorderHitTestController::FrameBorderHitTestController(views::Widget* frame)
23    : frame_window_(frame->GetNativeWindow()) {
24  frame_window_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
25      new ResizeHandleWindowTargeter(frame_window_, NULL)));
26}
27
28FrameBorderHitTestController::~FrameBorderHitTestController() {
29}
30
31// static
32int FrameBorderHitTestController::NonClientHitTest(
33    views::NonClientFrameView* view,
34    FrameCaptionButtonContainerView* caption_button_container,
35    const gfx::Point& point_in_widget) {
36  gfx::Rect expanded_bounds = view->bounds();
37  int outside_bounds = kResizeOutsideBoundsSize;
38
39  if (aura::Env::GetInstance()->is_touch_down())
40    outside_bounds *= kResizeOutsideBoundsScaleForTouch;
41  expanded_bounds.Inset(-outside_bounds, -outside_bounds);
42
43  if (!expanded_bounds.Contains(point_in_widget))
44    return HTNOWHERE;
45
46  // Check the frame first, as we allow a small area overlapping the contents
47  // to be used for resize handles.
48  views::Widget* frame = view->GetWidget();
49  bool can_ever_resize = frame->widget_delegate()->CanResize();
50  // Don't allow overlapping resize handles when the window is maximized or
51  // fullscreen, as it can't be resized in those states.
52  int resize_border =
53      frame->IsMaximized() || frame->IsFullscreen() ? 0 :
54      kResizeInsideBoundsSize;
55  int frame_component = view->GetHTComponentForFrame(point_in_widget,
56                                                     resize_border,
57                                                     resize_border,
58                                                     kResizeAreaCornerSize,
59                                                     kResizeAreaCornerSize,
60                                                     can_ever_resize);
61  if (frame_component != HTNOWHERE)
62    return frame_component;
63
64  int client_component = frame->client_view()->NonClientHitTest(
65      point_in_widget);
66  if (client_component != HTNOWHERE)
67    return client_component;
68
69  if (caption_button_container->visible()) {
70    gfx::Point point_in_caption_button_container(point_in_widget);
71    views::View::ConvertPointFromWidget(caption_button_container,
72        &point_in_caption_button_container);
73    int caption_button_component = caption_button_container->NonClientHitTest(
74        point_in_caption_button_container);
75    if (caption_button_component != HTNOWHERE)
76      return caption_button_component;
77  }
78
79  // Caption is a safe default.
80  return HTCAPTION;
81}
82
83}  // namespace ash
84