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