18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ash/frame/frame_border_hit_test_controller.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ash/ash_constants.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/wm/resize_handle_window_targeter.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/wm/window_state_observer.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/aura/env.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/aura/window.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window_observer.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window_targeter.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/base/hit_test.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/views/widget/widget.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/views/widget/widget_delegate.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/views/window/non_client_view.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace ash {
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)FrameBorderHitTestController::FrameBorderHitTestController(views::Widget* frame)
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : frame_window_(frame->GetNativeWindow()) {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  frame_window_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      new ResizeHandleWindowTargeter(frame_window_, NULL)));
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)FrameBorderHitTestController::~FrameBorderHitTestController() {
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// static
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int FrameBorderHitTestController::NonClientHitTest(
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    views::NonClientFrameView* view,
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    FrameCaptionButtonContainerView* caption_button_container,
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const gfx::Point& point_in_widget) {
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  gfx::Rect expanded_bounds = view->bounds();
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int outside_bounds = kResizeOutsideBoundsSize;
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (aura::Env::GetInstance()->is_touch_down())
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    outside_bounds *= kResizeOutsideBoundsScaleForTouch;
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  expanded_bounds.Inset(-outside_bounds, -outside_bounds);
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!expanded_bounds.Contains(point_in_widget))
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return HTNOWHERE;
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Check the frame first, as we allow a small area overlapping the contents
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // to be used for resize handles.
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  views::Widget* frame = view->GetWidget();
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool can_ever_resize = frame->widget_delegate()->CanResize();
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Don't allow overlapping resize handles when the window is maximized or
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // fullscreen, as it can't be resized in those states.
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int resize_border =
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      frame->IsMaximized() || frame->IsFullscreen() ? 0 :
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      kResizeInsideBoundsSize;
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int frame_component = view->GetHTComponentForFrame(point_in_widget,
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     resize_border,
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     resize_border,
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     kResizeAreaCornerSize,
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     kResizeAreaCornerSize,
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     can_ever_resize);
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (frame_component != HTNOWHERE)
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return frame_component;
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int client_component = frame->client_view()->NonClientHitTest(
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      point_in_widget);
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (client_component != HTNOWHERE)
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return client_component;
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (caption_button_container->visible()) {
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    gfx::Point point_in_caption_button_container(point_in_widget);
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    views::View::ConvertPointFromWidget(caption_button_container,
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        &point_in_caption_button_container);
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int caption_button_component = caption_button_container->NonClientHitTest(
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        point_in_caption_button_container);
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (caption_button_component != HTNOWHERE)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return caption_button_component;
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Caption is a safe default.
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return HTCAPTION;
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace ash
84