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