OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/drag_drop_controller.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "ui/aura/client/aura_constants.h" |
| 9 #include "ui/aura/client/window_drag_drop_delegate.h" |
| 10 #include "ui/aura/desktop.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura_shell/drag_image_view.h" |
| 13 #include "ui/aura_shell/shell.h" |
| 14 #include "ui/base/dragdrop/drag_drop_types.h" |
| 15 #include "ui/base/dragdrop/os_exchange_data_provider_aura.h" |
| 16 #include "ui/gfx/point.h" |
| 17 #include "ui/gfx/rect.h" |
| 18 #include "views/widget/native_widget_aura.h" |
| 19 |
| 20 namespace aura_shell { |
| 21 namespace internal { |
| 22 |
| 23 using aura::Desktop; |
| 24 |
| 25 namespace { |
| 26 aura::WindowDragDropDelegate* GetDragDropDelegate(aura::Window* window) { |
| 27 if (!window) |
| 28 return NULL; |
| 29 void* prop = window->GetProperty(aura::kDragDropDelegateKey); |
| 30 if (!prop) |
| 31 return NULL; |
| 32 return static_cast<aura::WindowDragDropDelegate*>(prop); |
| 33 } |
| 34 |
| 35 const gfx::Point kDragDropWidgetOffset(0, 0); |
| 36 |
| 37 } |
| 38 |
| 39 //////////////////////////////////////////////////////////////////////////////// |
| 40 // DragDropController, public: |
| 41 |
| 42 DragDropController::DragDropController() |
| 43 : aura::EventFilter(Desktop::GetInstance()), |
| 44 drag_image_(NULL), |
| 45 drag_data_(NULL), |
| 46 drag_operation_(0), |
| 47 dragged_window_(NULL), |
| 48 drag_drop_in_progress_(false), |
| 49 should_block_during_drag_drop_(true) { |
| 50 Shell::GetInstance()->AddDesktopEventFilter(this); |
| 51 } |
| 52 |
| 53 DragDropController::~DragDropController() { |
| 54 Shell::GetInstance()->RemoveDesktopEventFilter(this); |
| 55 Cleanup(); |
| 56 } |
| 57 |
| 58 void DragDropController::StartDragAndDrop(const ui::OSExchangeData& data, |
| 59 int operation) { |
| 60 DCHECK(!drag_drop_in_progress_); |
| 61 aura::Window* capture_window = Desktop::GetInstance()->capture_window(); |
| 62 if (capture_window) |
| 63 Desktop::GetInstance()->ReleaseCapture(capture_window); |
| 64 drag_drop_in_progress_ = true; |
| 65 |
| 66 drag_data_ = &data; |
| 67 drag_operation_ = operation; |
| 68 gfx::Point location = Desktop::GetInstance()->last_mouse_location(); |
| 69 const ui::OSExchangeDataProviderAura& provider = |
| 70 static_cast<const ui::OSExchangeDataProviderAura&>(data.provider()); |
| 71 |
| 72 drag_image_.reset(new DragImageView); |
| 73 drag_image_->SetImage(provider.drag_image()); |
| 74 drag_image_->SetScreenBounds(gfx::Rect(location.Add(kDragDropWidgetOffset), |
| 75 drag_image_->GetPreferredSize())); |
| 76 drag_image_->SetWidgetVisible(true); |
| 77 |
| 78 dragged_window_ = Desktop::GetInstance()->GetEventHandlerForPoint(location); |
| 79 |
| 80 if (should_block_during_drag_drop_) { |
| 81 MessageLoopForUI::current()->RunWithDispatcher( |
| 82 Desktop::GetInstance()->GetDispatcher()); |
| 83 } |
| 84 } |
| 85 |
| 86 void DragDropController::DragUpdate(aura::Window* target, |
| 87 const aura::MouseEvent& event) { |
| 88 aura::WindowDragDropDelegate* delegate = NULL; |
| 89 if (target != dragged_window_) { |
| 90 if ((delegate = GetDragDropDelegate(dragged_window_))) |
| 91 delegate->OnDragExited(); |
| 92 dragged_window_ = target; |
| 93 if ((delegate = GetDragDropDelegate(dragged_window_))) { |
| 94 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); |
| 95 if (delegate->CanDrop(e)) |
| 96 delegate->OnDragEntered(e); |
| 97 } |
| 98 } else { |
| 99 if ((delegate = GetDragDropDelegate(dragged_window_))) { |
| 100 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); |
| 101 delegate->OnDragUpdated(e); |
| 102 // TODO(varunjain): uncomment the following lines when cursor issue with |
| 103 // X for tests is fixed. |
| 104 // gfx::NativeCursor cursor = (op == ui::DragDropTypes::DRAG_NONE)? |
| 105 // aura::kCursorMove : aura::kCursorHand; |
| 106 // Desktop::GetInstance()->SetCursor(cursor); |
| 107 } |
| 108 } |
| 109 |
| 110 DCHECK(drag_image_.get()); |
| 111 if (drag_image_->IsVisible()) { |
| 112 drag_image_->SetScreenPosition(Desktop::GetInstance()-> |
| 113 last_mouse_location().Add(kDragDropWidgetOffset)); |
| 114 } |
| 115 } |
| 116 |
| 117 void DragDropController::Drop(aura::Window* target, |
| 118 const aura::MouseEvent& event) { |
| 119 aura::WindowDragDropDelegate* delegate = NULL; |
| 120 DCHECK(target == dragged_window_); |
| 121 if ((delegate = GetDragDropDelegate(dragged_window_))) { |
| 122 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); |
| 123 if (delegate->CanDrop(e)) |
| 124 delegate->OnPerformDrop(e); |
| 125 // TODO(varunjain): else Do drag widget flying back animation |
| 126 } |
| 127 |
| 128 Cleanup(); |
| 129 if (should_block_during_drag_drop_) |
| 130 MessageLoop::current()->Quit(); |
| 131 } |
| 132 |
| 133 void DragDropController::DragCancel() { |
| 134 // TODO(varunjain): Do drag widget flying back animation |
| 135 Cleanup(); |
| 136 if (should_block_during_drag_drop_) |
| 137 MessageLoop::current()->Quit(); |
| 138 } |
| 139 |
| 140 bool DragDropController::PreHandleKeyEvent(aura::Window* target, |
| 141 aura::KeyEvent* event) { |
| 142 return false; |
| 143 } |
| 144 |
| 145 bool DragDropController::PreHandleMouseEvent(aura::Window* target, |
| 146 aura::MouseEvent* event) { |
| 147 if (!drag_drop_in_progress_) |
| 148 return false; |
| 149 switch (event->type()) { |
| 150 case ui::ET_MOUSE_DRAGGED: |
| 151 DragUpdate(target, *event); |
| 152 break; |
| 153 case ui::ET_MOUSE_RELEASED: |
| 154 Drop(target, *event); |
| 155 break; |
| 156 case ui::ET_MOUSE_EXITED: |
| 157 DragCancel(); |
| 158 break; |
| 159 default: |
| 160 NOTREACHED(); |
| 161 break; |
| 162 } |
| 163 return true; |
| 164 } |
| 165 |
| 166 ui::TouchStatus DragDropController::PreHandleTouchEvent( |
| 167 aura::Window* target, |
| 168 aura::TouchEvent* event) { |
| 169 return ui::TOUCH_STATUS_UNKNOWN; |
| 170 } |
| 171 |
| 172 //////////////////////////////////////////////////////////////////////////////// |
| 173 // DragDropController, private: |
| 174 |
| 175 void DragDropController::Cleanup() { |
| 176 drag_image_.reset(); |
| 177 drag_data_ = NULL; |
| 178 drag_operation_ = 0; |
| 179 drag_drop_in_progress_ = false; |
| 180 } |
| 181 |
| 182 } // namespace internal |
| 183 } // namespace aura_shell |
OLD | NEW |