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/shelf/shelf_bezel_event_filter.h"
6
7#include "ash/shelf/shelf_layout_manager.h"
8#include "ash/shell.h"
9#include "ui/aura/window.h"
10#include "ui/wm/core/coordinate_conversion.h"
11
12namespace ash {
13
14ShelfBezelEventFilter::ShelfBezelEventFilter(
15    ShelfLayoutManager* shelf)
16    : shelf_(shelf),
17      in_touch_drag_(false) {
18  Shell::GetInstance()->AddPreTargetHandler(this);
19}
20
21ShelfBezelEventFilter::~ShelfBezelEventFilter() {
22  Shell::GetInstance()->RemovePreTargetHandler(this);
23}
24
25void ShelfBezelEventFilter::OnGestureEvent(
26    ui::GestureEvent* event) {
27  gfx::Point point_in_screen(event->location());
28  aura::Window* target = static_cast<aura::Window*>(event->target());
29  ::wm::ConvertPointToScreen(target, &point_in_screen);
30  gfx::Rect screen =
31      Shell::GetScreen()->GetDisplayNearestPoint(point_in_screen).bounds();
32  if ((!screen.Contains(point_in_screen) &&
33       IsShelfOnBezel(screen, point_in_screen)) ||
34      in_touch_drag_) {
35    if (gesture_handler_.ProcessGestureEvent(*event)) {
36      switch (event->type()) {
37        case ui::ET_GESTURE_SCROLL_BEGIN:
38          in_touch_drag_ = true;
39          break;
40        case ui::ET_GESTURE_SCROLL_END:
41        case ui::ET_SCROLL_FLING_START:
42          in_touch_drag_ = false;
43          break;
44        default:
45          break;
46      }
47      event->StopPropagation();
48    }
49  }
50}
51
52bool ShelfBezelEventFilter::IsShelfOnBezel(
53    const gfx::Rect& screen,
54    const gfx::Point& point) const{
55  switch (shelf_->GetAlignment()) {
56    case SHELF_ALIGNMENT_BOTTOM:
57      if (point.y() >= screen.bottom())
58        return true;
59      break;
60    case SHELF_ALIGNMENT_LEFT:
61      if (point.x() <= screen.x())
62        return true;
63      break;
64    case SHELF_ALIGNMENT_TOP:
65      if (point.y() <= screen.y())
66        return true;
67      break;
68    case SHELF_ALIGNMENT_RIGHT:
69      if (point.x() >= screen.right())
70        return true;
71      break;
72  }
73  return false;
74}
75
76}  // namespace ash
77