1// Copyright (c) 2012 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/wm/gestures/shelf_gesture_handler.h"
6
7#include "ash/root_window_controller.h"
8#include "ash/session/session_state_delegate.h"
9#include "ash/shelf/shelf_layout_manager.h"
10#include "ash/shelf/shelf_types.h"
11#include "ash/shelf/shelf_widget.h"
12#include "ash/shell.h"
13#include "ash/system/status_area_widget.h"
14#include "ash/wm/gestures/tray_gesture_handler.h"
15#include "ash/wm/window_state.h"
16#include "ash/wm/window_util.h"
17#include "ui/aura/window.h"
18#include "ui/compositor/layer.h"
19#include "ui/compositor/scoped_layer_animation_settings.h"
20#include "ui/gfx/transform.h"
21#include "ui/views/widget/widget.h"
22
23namespace ash {
24
25ShelfGestureHandler::ShelfGestureHandler()
26    : drag_in_progress_(false) {
27}
28
29ShelfGestureHandler::~ShelfGestureHandler() {
30}
31
32bool ShelfGestureHandler::ProcessGestureEvent(const ui::GestureEvent& event) {
33  Shell* shell = Shell::GetInstance();
34  if (!shell->session_state_delegate()->NumberOfLoggedInUsers() ||
35      shell->session_state_delegate()->IsScreenLocked()) {
36    // The gestures are disabled in the lock/login screen.
37    return false;
38  }
39
40  // TODO(oshima): Find the root window controller from event's location.
41  RootWindowController* controller = Shell::GetPrimaryRootWindowController();
42
43  ShelfLayoutManager* shelf = controller->GetShelfLayoutManager();
44
45  if (event.type() == ui::ET_GESTURE_WIN8_EDGE_SWIPE) {
46    shelf->OnGestureEdgeSwipe(event);
47    return true;
48  }
49
50  const aura::Window* fullscreen = controller->GetWindowForFullscreenMode();
51  if (fullscreen &&
52      ash::wm::GetWindowState(fullscreen)->hide_shelf_when_fullscreen()) {
53    return false;
54  }
55
56  if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN) {
57    drag_in_progress_ = true;
58    shelf->StartGestureDrag(event);
59    return true;
60  }
61
62  if (!drag_in_progress_)
63    return false;
64
65  if (event.type() == ui::ET_GESTURE_SCROLL_UPDATE) {
66    if (tray_handler_) {
67      if (!tray_handler_->UpdateGestureDrag(event))
68        tray_handler_.reset();
69    } else if (shelf->UpdateGestureDrag(event) ==
70        ShelfLayoutManager::DRAG_TRAY) {
71      tray_handler_.reset(new TrayGestureHandler());
72    }
73
74    return true;
75  }
76
77  drag_in_progress_ = false;
78
79  if (event.type() == ui::ET_GESTURE_SCROLL_END ||
80      event.type() == ui::ET_SCROLL_FLING_START) {
81    if (tray_handler_) {
82      tray_handler_->CompleteGestureDrag(event);
83      tray_handler_.reset();
84    }
85
86    shelf->CompleteGestureDrag(event);
87    return true;
88  }
89
90  // Unexpected event. Reset the state and let the event fall through.
91  shelf->CancelGestureDrag();
92  return false;
93}
94
95}  // namespace ash
96