Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: ui/aura_shell/drag_drop_controller.cc

Issue 8450018: First shot at implementing drag&drop for Aura (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: minor changes Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/desktop.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_drag_drop_delegate.h"
12 #include "ui/aura_shell/drag_image_view.h"
13 #include "ui/base/dragdrop/drag_drop_types.h"
14 #include "ui/base/dragdrop/os_exchange_data_provider_aura.h"
15 #include "ui/gfx/point.h"
16 #include "ui/gfx/rect.h"
17 #include "views/widget/native_widget_aura.h"
18
19 namespace aura_shell {
20 namespace internal {
21
22 using aura::Desktop;
23
24 namespace {
25 aura::WindowDragDropDelegate* GetDragDropDelegate(aura::Window* window) {
26 if (!window)
27 return NULL;
28 void* prop = window->GetProperty(aura::kDragDropDelegateKey);
29 if (!prop)
30 return NULL;
31 return static_cast<aura::WindowDragDropDelegate*>(prop);
32 }
33
34 const gfx::Point kDragDropWidgetOffset(0, 0);
35
36 }
37
38 ////////////////////////////////////////////////////////////////////////////////
39 // DragDropController, public:
40
41 DragDropController::DragDropController()
42 : drag_image_(NULL),
43 drag_data_(NULL),
44 drag_operation_(0),
45 dragged_window_(NULL),
46 drag_drop_in_progress_(false),
47 should_block_during_drag_drop_(true) {
48 }
49
50 DragDropController::~DragDropController() {
51 Cleanup();
52 }
53
54 void DragDropController::StartDragAndDrop(const ui::OSExchangeData& data,
55 int operation) {
56 DCHECK(!drag_drop_in_progress_);
57 aura::Window* capture_window = Desktop::GetInstance()->capture_window();
58 if (capture_window)
59 Desktop::GetInstance()->ReleaseCapture(capture_window);
60 drag_drop_in_progress_ = true;
61
62 drag_data_ = &data;
63 drag_operation_ = operation;
64 gfx::Point location = Desktop::GetInstance()->last_mouse_location();
65 const ui::OSExchangeDataProviderAura& provider =
66 static_cast<const ui::OSExchangeDataProviderAura&>(data.provider());
67
68 drag_image_.reset(new DragImageView);
69 drag_image_->SetImage(provider.drag_image());
70 drag_image_->SetScreenBounds(gfx::Rect(location.Add(kDragDropWidgetOffset),
71 drag_image_->GetPreferredSize()));
72 drag_image_->SetVisible(true);
73
74 dragged_window_ = Desktop::GetInstance()->GetEventHandlerForPoint(location);
75
76 if (should_block_during_drag_drop_) {
77 MessageLoopForUI::current()->RunWithDispatcher(
78 Desktop::GetInstance()->GetDispatcher());
79 }
80 }
81
82 void DragDropController::DragUpdate(aura::Window* target,
83 const aura::MouseEvent& event) {
84 aura::WindowDragDropDelegate* delegate = NULL;
85 if (target != dragged_window_) {
86 if ((delegate = GetDragDropDelegate(dragged_window_)))
87 delegate->OnDragExited();
88 dragged_window_ = target;
89 if ((delegate = GetDragDropDelegate(dragged_window_))) {
90 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_);
91 if (delegate->CanDrop(e))
92 delegate->OnDragEntered(e);
93 }
94 } else {
95 if ((delegate = GetDragDropDelegate(dragged_window_))) {
96 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_);
97 delegate->OnDragUpdated(e);
98 // TODO(varunjain): uncomment the following lines when cursor issue with
99 // X for tests is fixed.
100 // gfx::NativeCursor cursor = (op == ui::DragDropTypes::DRAG_NONE)?
101 // aura::kCursorMove : aura::kCursorHand;
102 // Desktop::GetInstance()->SetCursor(cursor);
103 }
104 }
105
106 DCHECK(drag_image_.get());
107 if (drag_image_->IsVisible()) {
108 drag_image_->SetScreenPosition(Desktop::GetInstance()->
109 last_mouse_location().Add(kDragDropWidgetOffset));
110 }
111 }
112
113 void DragDropController::Drop(aura::Window* target,
114 const aura::MouseEvent& event) {
115 aura::WindowDragDropDelegate* delegate = NULL;
116 DCHECK(target == dragged_window_);
117 if ((delegate = GetDragDropDelegate(dragged_window_))) {
118 aura::DropTargetEvent e(*drag_data_, event.location(), drag_operation_);
119 if (delegate->CanDrop(e))
120 delegate->OnPerformDrop(e);
121 // TODO(varunjain): else Do drag widget flying back animation
122 }
123
124 Cleanup();
125 if (should_block_during_drag_drop_)
126 MessageLoop::current()->Quit();
127 }
128
129 void DragDropController::DragCancel() {
130 // TODO(varunjain): Do drag widget flying back animation
131 Cleanup();
132 if (should_block_during_drag_drop_)
133 MessageLoop::current()->Quit();
134 }
135
136 bool DragDropController::IsDragAndDropInProgress() {
137 return drag_drop_in_progress_;
138 }
139
140 ////////////////////////////////////////////////////////////////////////////////
141 // DragDropController, private:
142
143 void DragDropController::Cleanup() {
144 drag_image_.reset();
145 drag_data_ = NULL;
146 drag_operation_ = 0;
147 drag_drop_in_progress_ = false;
148 }
149
150 } // namespace internal
151 } // namespace aura_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698