drag_drop_controller.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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#ifndef ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_ 6#define ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_ 7 8#include "ash/ash_export.h" 9#include "base/callback.h" 10#include "base/memory/weak_ptr.h" 11#include "ui/aura/client/drag_drop_client.h" 12#include "ui/aura/window_observer.h" 13#include "ui/base/dragdrop/os_exchange_data.h" 14#include "ui/events/event_constants.h" 15#include "ui/events/event_handler.h" 16#include "ui/gfx/animation/animation_delegate.h" 17#include "ui/gfx/rect.h" 18 19namespace aura { 20class RootWindow; 21class Window; 22} 23 24namespace gfx { 25class LinearAnimation; 26} 27 28namespace ash { 29 30namespace test { 31class DragDropControllerTest; 32} 33 34namespace internal { 35 36class DragDropTracker; 37class DragDropTrackerDelegate; 38class DragImageView; 39 40class ASH_EXPORT DragDropController 41 : public aura::client::DragDropClient, 42 public ui::EventHandler, 43 public gfx::AnimationDelegate, 44 public aura::WindowObserver { 45 public: 46 DragDropController(); 47 virtual ~DragDropController(); 48 49 void set_should_block_during_drag_drop(bool should_block_during_drag_drop) { 50 should_block_during_drag_drop_ = should_block_during_drag_drop; 51 } 52 53 // Overridden from aura::client::DragDropClient: 54 virtual int StartDragAndDrop( 55 const ui::OSExchangeData& data, 56 aura::RootWindow* root_window, 57 aura::Window* source_window, 58 const gfx::Point& root_location, 59 int operation, 60 ui::DragDropTypes::DragEventSource source) OVERRIDE; 61 virtual void DragUpdate(aura::Window* target, 62 const ui::LocatedEvent& event) OVERRIDE; 63 virtual void Drop(aura::Window* target, 64 const ui::LocatedEvent& event) OVERRIDE; 65 virtual void DragCancel() OVERRIDE; 66 virtual bool IsDragDropInProgress() OVERRIDE; 67 68 // Overridden from ui::EventHandler: 69 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; 70 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; 71 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE; 72 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 73 74 // Overridden from aura::WindowObserver. 75 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE; 76 77 protected: 78 // Helper method to create a LinearAnimation object that will run the drag 79 // cancel animation. Caller take ownership of the returned object. Protected 80 // for testing. 81 virtual gfx::LinearAnimation* CreateCancelAnimation( 82 int duration, 83 int frame_rate, 84 gfx::AnimationDelegate* delegate); 85 86 // Actual implementation of |DragCancel()|. protected for testing. 87 virtual void DoDragCancel(int drag_cancel_animation_duration_ms); 88 89 private: 90 friend class ash::test::DragDropControllerTest; 91 92 // Overridden from gfx::AnimationDelegate: 93 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 94 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 95 virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE; 96 97 // Helper method to start drag widget flying back animation. 98 void StartCanceledAnimation(int animation_duration_ms); 99 100 // Helper method to forward |pending_log_tap_| event to |drag_source_window_|. 101 void ForwardPendingLongTap(); 102 103 // Helper method to reset everything. 104 void Cleanup(); 105 106 scoped_ptr<DragImageView> drag_image_; 107 gfx::Vector2d drag_image_offset_; 108 const ui::OSExchangeData* drag_data_; 109 int drag_operation_; 110 111 // Window that is currently under the drag cursor. 112 aura::Window* drag_window_; 113 114 // Starting and final bounds for the drag image for the drag cancel animation. 115 gfx::Rect drag_image_initial_bounds_for_cancel_animation_; 116 gfx::Rect drag_image_final_bounds_for_cancel_animation_; 117 118 scoped_ptr<gfx::LinearAnimation> cancel_animation_; 119 120 // Window that started the drag. 121 aura::Window* drag_source_window_; 122 123 // Indicates whether the caller should be blocked on a drag/drop session. 124 // Only be used for tests. 125 bool should_block_during_drag_drop_; 126 127 // Closure for quitting nested message loop. 128 base::Closure quit_closure_; 129 130 scoped_ptr<ash::internal::DragDropTracker> drag_drop_tracker_; 131 scoped_ptr<DragDropTrackerDelegate> drag_drop_window_delegate_; 132 133 ui::DragDropTypes::DragEventSource current_drag_event_source_; 134 135 // Holds a synthetic long tap event to be sent to the |drag_source_window_|. 136 // See comment in OnGestureEvent() on why we need this. 137 scoped_ptr<ui::GestureEvent> pending_long_tap_; 138 139 base::WeakPtrFactory<DragDropController> weak_factory_; 140 141 DISALLOW_COPY_AND_ASSIGN(DragDropController); 142}; 143 144} // namespace internal 145} // namespace ash 146 147#endif // ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_ 148