Chromium Code Reviews| 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/desktop_event_filter.h" | |
| 13 #include "ui/aura_shell/drag_image_view.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 static_cast<DesktopEventFilter*>(Desktop::GetInstance()->event_filter())-> | |
|
Ben Goodger (Google)
2011/11/16 20:39:49
Sync, and you can now just do:
Shell::GetInstance
varunjain
2011/11/16 22:55:20
Done.
| |
| 51 AddFilter(this); | |
| 52 } | |
| 53 | |
| 54 DragDropController::~DragDropController() { | |
| 55 static_cast<DesktopEventFilter*>(Desktop::GetInstance()->event_filter())-> | |
| 56 RemoveFilter(this); | |
|
Ben Goodger (Google)
2011/11/16 20:39:49
ditto:
Shell::GetInstance()->RemoveDesktopEventFi
varunjain
2011/11/16 22:55:20
Done.
| |
| 57 Cleanup(); | |
| 58 } | |
| 59 | |
| 60 void DragDropController::StartDragAndDrop(const ui::OSExchangeData& data, | |
| 61 int operation) { | |
| 62 DCHECK(!drag_drop_in_progress_); | |
| 63 aura::Window* capture_window = Desktop::GetInstance()->capture_window(); | |
| 64 if (capture_window) | |
| 65 Desktop::GetInstance()->ReleaseCapture(capture_window); | |
| 66 drag_drop_in_progress_ = true; | |
| 67 | |
| 68 drag_data_ = &data; | |
| 69 drag_operation_ = operation; | |
| 70 gfx::Point location = Desktop::GetInstance()->last_mouse_location(); | |
| 71 const ui::OSExchangeDataProviderAura& provider = | |
| 72 static_cast<const ui::OSExchangeDataProviderAura&>(data.provider()); | |
| 73 | |
| 74 drag_image_.reset(new DragImageView); | |
| 75 drag_image_->SetImage(provider.drag_image()); | |
| 76 drag_image_->SetScreenBounds(gfx::Rect(location.Add(kDragDropWidgetOffset), | |
| 77 drag_image_->GetPreferredSize())); | |
| 78 drag_image_->SetWidgetVisible(true); | |
| 79 | |
| 80 dragged_window_ = Desktop::GetInstance()->GetEventHandlerForPoint(location); | |
| 81 | |
| 82 if (should_block_during_drag_drop_) { | |
| 83 MessageLoopForUI::current()->RunWithDispatcher( | |
| 84 Desktop::GetInstance()->GetDispatcher()); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void DragDropController::DragUpdate(aura::Window* target, | |
| 89 const aura::MouseEvent& event) { | |
| 90 aura::WindowDragDropDelegate* delegate = NULL; | |
| 91 if (target != dragged_window_) { | |
| 92 if ((delegate = GetDragDropDelegate(dragged_window_))) | |
| 93 delegate->OnDragExited(); | |
| 94 dragged_window_ = target; | |
| 95 if ((delegate = GetDragDropDelegate(dragged_window_))) { | |
| 96 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 97 if (delegate->CanDrop(e)) | |
| 98 delegate->OnDragEntered(e); | |
| 99 } | |
| 100 } else { | |
| 101 if ((delegate = GetDragDropDelegate(dragged_window_))) { | |
| 102 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 103 delegate->OnDragUpdated(e); | |
| 104 // TODO(varunjain): uncomment the following lines when cursor issue with | |
| 105 // X for tests is fixed. | |
| 106 // gfx::NativeCursor cursor = (op == ui::DragDropTypes::DRAG_NONE)? | |
| 107 // aura::kCursorMove : aura::kCursorHand; | |
| 108 // Desktop::GetInstance()->SetCursor(cursor); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 DCHECK(drag_image_.get()); | |
| 113 if (drag_image_->IsVisible()) { | |
| 114 drag_image_->SetScreenPosition(Desktop::GetInstance()-> | |
| 115 last_mouse_location().Add(kDragDropWidgetOffset)); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void DragDropController::Drop(aura::Window* target, | |
| 120 const aura::MouseEvent& event) { | |
| 121 aura::WindowDragDropDelegate* delegate = NULL; | |
| 122 DCHECK(target == dragged_window_); | |
| 123 if ((delegate = GetDragDropDelegate(dragged_window_))) { | |
| 124 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_); | |
| 125 if (delegate->CanDrop(e)) | |
| 126 delegate->OnPerformDrop(e); | |
| 127 // TODO(varunjain): else Do drag widget flying back animation | |
| 128 } | |
| 129 | |
| 130 Cleanup(); | |
| 131 if (should_block_during_drag_drop_) | |
| 132 MessageLoop::current()->Quit(); | |
| 133 } | |
| 134 | |
| 135 void DragDropController::DragCancel() { | |
| 136 // TODO(varunjain): Do drag widget flying back animation | |
| 137 Cleanup(); | |
| 138 if (should_block_during_drag_drop_) | |
| 139 MessageLoop::current()->Quit(); | |
| 140 } | |
| 141 | |
| 142 bool DragDropController::PreHandleKeyEvent(aura::Window* target, | |
| 143 aura::KeyEvent* event) { | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 bool DragDropController::PreHandleMouseEvent(aura::Window* target, | |
| 148 aura::MouseEvent* event) { | |
| 149 if (!drag_drop_in_progress_) | |
| 150 return false; | |
| 151 switch (event->type()) { | |
| 152 case ui::ET_MOUSE_DRAGGED: | |
| 153 DragUpdate(target, *event); | |
| 154 break; | |
| 155 case ui::ET_MOUSE_RELEASED: | |
| 156 Drop(target, *event); | |
| 157 break; | |
| 158 case ui::ET_MOUSE_EXITED: | |
| 159 DragCancel(); | |
| 160 break; | |
| 161 default: | |
| 162 NOTREACHED(); | |
| 163 break; | |
| 164 } | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 ui::TouchStatus DragDropController::PreHandleTouchEvent( | |
| 169 aura::Window* target, | |
| 170 aura::TouchEvent* event) { | |
| 171 return ui::TOUCH_STATUS_UNKNOWN; | |
| 172 } | |
| 173 | |
| 174 //////////////////////////////////////////////////////////////////////////////// | |
| 175 // DragDropController, private: | |
| 176 | |
| 177 void DragDropController::Cleanup() { | |
| 178 drag_image_.reset(); | |
| 179 drag_data_ = NULL; | |
| 180 drag_operation_ = 0; | |
| 181 drag_drop_in_progress_ = false; | |
| 182 } | |
| 183 | |
| 184 } // namespace internal | |
| 185 } // namespace aura_shell | |
| OLD | NEW |