Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/ui/ws/drag_controller.h" | 5 #include "services/ui/ws/drag_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "services/ui/public/interfaces/cursor.mojom.h" | |
| 9 #include "services/ui/ws/drag_cursor_updater.h" | |
| 8 #include "services/ui/ws/drag_source.h" | 10 #include "services/ui/ws/drag_source.h" |
| 9 #include "services/ui/ws/drag_target_connection.h" | 11 #include "services/ui/ws/drag_target_connection.h" |
| 10 #include "services/ui/ws/event_dispatcher.h" | 12 #include "services/ui/ws/event_dispatcher.h" |
| 11 #include "services/ui/ws/server_window.h" | 13 #include "services/ui/ws/server_window.h" |
| 12 | 14 |
| 13 namespace ui { | 15 namespace ui { |
| 14 namespace ws { | 16 namespace ws { |
| 15 | 17 |
| 16 struct DragController::Operation { | 18 struct DragController::Operation { |
| 17 OperationType type; | 19 OperationType type; |
| 18 uint32_t event_flags; | 20 uint32_t event_flags; |
| 19 gfx::Point screen_position; | 21 gfx::Point screen_position; |
| 20 }; | 22 }; |
| 21 | 23 |
| 22 struct DragController::WindowState { | 24 struct DragController::WindowState { |
| 23 // Set to true once we've observed the ServerWindow* that is the key to this | 25 // Set to true once we've observed the ServerWindow* that is the key to this |
| 24 // instance in |window_state_|. | 26 // instance in |window_state_|. |
| 25 bool observed; | 27 bool observed = false; |
| 26 | 28 |
| 27 // If we're waiting for a response, this is the type of message. TYPE_NONE | 29 // If we're waiting for a response, this is the type of message. NONE means |
| 28 // means there's no outstanding | 30 // there's no outstanding |
| 29 OperationType waiting_on_reply; | 31 OperationType waiting_on_reply = OperationType::NONE; |
| 30 | 32 |
| 31 // The operation that we'll send off if |waiting_on_reply| isn't TYPE_NONE. | 33 // The operation that we'll send off if |waiting_on_reply| isn't NONE. |
| 32 Operation queued_operation; | 34 Operation queued_operation = {OperationType::NONE, 0, gfx::Point()}; |
| 35 | |
| 36 // The current set of operations that this window accepts. This gets updated | |
| 37 // on each return message. | |
| 38 uint32_t window_operations = 0u; | |
|
sky
2016/09/28 15:38:13
Please be consistent in the naming. You have OnDra
Elliot Glaysher
2016/09/28 18:15:31
Created a DropEffect and DropEffectBitmask type in
| |
| 33 }; | 39 }; |
| 34 | 40 |
| 35 DragController::DragController( | 41 DragController::DragController( |
| 42 DragCursorUpdater* cursor_updater, | |
| 36 DragSource* source, | 43 DragSource* source, |
| 37 ServerWindow* source_window, | 44 ServerWindow* source_window, |
| 38 DragTargetConnection* source_connection, | 45 DragTargetConnection* source_connection, |
| 39 int32_t drag_pointer, | 46 int32_t drag_pointer, |
| 40 mojo::Map<mojo::String, mojo::Array<uint8_t>> mime_data, | 47 mojo::Map<mojo::String, mojo::Array<uint8_t>> mime_data, |
| 41 uint32_t drag_operations) | 48 uint32_t drag_operations) |
| 42 : source_(source), | 49 : source_(source), |
| 50 cursor_updater_(cursor_updater), | |
| 43 drag_operations_(drag_operations), | 51 drag_operations_(drag_operations), |
| 44 drag_pointer_id_(drag_pointer), | 52 drag_pointer_id_(drag_pointer), |
| 53 current_cursor_(static_cast<int32_t>(ui::mojom::Cursor::NO_DROP)), | |
| 45 source_window_(source_window), | 54 source_window_(source_window), |
| 46 source_connection_(source_connection), | 55 source_connection_(source_connection), |
| 47 mime_data_(std::move(mime_data)), | 56 mime_data_(std::move(mime_data)), |
| 48 weak_factory_(this) { | 57 weak_factory_(this) { |
| 58 SetCurrentTargetWindow(nullptr); | |
| 49 EnsureWindowObserved(source_window_); | 59 EnsureWindowObserved(source_window_); |
| 50 } | 60 } |
| 51 | 61 |
| 52 DragController::~DragController() { | 62 DragController::~DragController() { |
| 53 for (auto& pair : window_state_) { | 63 for (auto& pair : window_state_) { |
| 54 if (pair.second.observed) | 64 if (pair.second.observed) |
| 55 pair.first->RemoveObserver(this); | 65 pair.first->RemoveObserver(this); |
| 56 } | 66 } |
| 57 } | 67 } |
| 58 | 68 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 auto it = window_state_.find(window); | 152 auto it = window_state_.find(window); |
| 143 if (it == window_state_.end()) | 153 if (it == window_state_.end()) |
| 144 return 0u; | 154 return 0u; |
| 145 if (it->second.waiting_on_reply == OperationType::NONE) | 155 if (it->second.waiting_on_reply == OperationType::NONE) |
| 146 return 0u; | 156 return 0u; |
| 147 if (it->second.queued_operation.type == OperationType::NONE) | 157 if (it->second.queued_operation.type == OperationType::NONE) |
| 148 return 1u; | 158 return 1u; |
| 149 return 2u; | 159 return 2u; |
| 150 } | 160 } |
| 151 | 161 |
| 162 void DragController::UpdateCursor(ServerWindow* window, | |
| 163 uint32_t effect_bitmask) { | |
| 164 WindowState& state = window_state_[window]; | |
| 165 state.window_operations = effect_bitmask; | |
| 166 | |
| 167 if (current_target_window_ == window) { | |
| 168 current_cursor_ = CursorForWindowOperations(effect_bitmask); | |
| 169 cursor_updater_->OnDragCursorUpdated(); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 int32_t DragController::CursorForWindowOperations(uint32_t window_operations) { | |
| 174 uint32_t combined = window_operations & drag_operations_; | |
| 175 return combined == ui::mojom::kDropEffectNone | |
| 176 ? static_cast<int32_t>(ui::mojom::Cursor::NO_DROP) | |
| 177 : static_cast<int32_t>(ui::mojom::Cursor::COPY); | |
|
sky
2016/09/28 15:38:13
No link?
Elliot Glaysher
2016/09/28 18:15:31
No link. I went through all the cursors and the on
| |
| 178 } | |
| 179 | |
| 152 void DragController::SetCurrentTargetWindow(ServerWindow* current_target) { | 180 void DragController::SetCurrentTargetWindow(ServerWindow* current_target) { |
| 153 current_target_window_ = current_target; | 181 current_target_window_ = current_target; |
| 182 | |
| 183 if (current_target_window_) { | |
| 184 // Immediately set the cursor to the last known set of operations (which | |
| 185 // could be none). | |
| 186 WindowState& state = window_state_[current_target_window_]; | |
| 187 current_cursor_ = CursorForWindowOperations(state.window_operations); | |
| 188 } else { | |
| 189 // Can't drop in empty areas. | |
| 190 current_cursor_ = static_cast<int32_t>(ui::mojom::Cursor::NO_DROP); | |
| 191 } | |
| 192 | |
| 193 cursor_updater_->OnDragCursorUpdated(); | |
| 154 } | 194 } |
| 155 | 195 |
| 156 void DragController::EnsureWindowObserved(ServerWindow* window) { | 196 void DragController::EnsureWindowObserved(ServerWindow* window) { |
| 157 if (!window) | 197 if (!window) |
| 158 return; | 198 return; |
| 159 | 199 |
| 160 WindowState& state = window_state_[window]; | 200 WindowState& state = window_state_[window]; |
| 161 if (!state.observed) { | 201 if (!state.observed) { |
| 162 state.observed = true; | 202 state.observed = true; |
| 163 window->AddObserver(this); | 203 window->AddObserver(this); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 void DragController::OnDragStatusCompleted(const WindowId& id, | 283 void DragController::OnDragStatusCompleted(const WindowId& id, |
| 244 uint32_t bitmask) { | 284 uint32_t bitmask) { |
| 245 ServerWindow* window = source_->GetWindowById(id); | 285 ServerWindow* window = source_->GetWindowById(id); |
| 246 if (!window) { | 286 if (!window) { |
| 247 // The window has been deleted and its queue is empty. | 287 // The window has been deleted and its queue is empty. |
| 248 return; | 288 return; |
| 249 } | 289 } |
| 250 | 290 |
| 251 // We must remove the completed item. | 291 // We must remove the completed item. |
| 252 OnRespondToOperation(window); | 292 OnRespondToOperation(window); |
| 253 | 293 UpdateCursor(window, bitmask); |
| 254 // TODO(erg): |bitmask| is the allowed drag actions at the mouse location. We | |
| 255 // should use this data to change the cursor. | |
| 256 } | 294 } |
| 257 | 295 |
| 258 void DragController::OnDragDropCompleted(const WindowId& id, uint32_t action) { | 296 void DragController::OnDragDropCompleted(const WindowId& id, uint32_t action) { |
| 259 ServerWindow* window = source_->GetWindowById(id); | 297 ServerWindow* window = source_->GetWindowById(id); |
| 260 if (!window) { | 298 if (!window) { |
| 261 // The window has been deleted after we sent the drop message. It's really | 299 // The window has been deleted after we sent the drop message. It's really |
| 262 // hard to recover from this so just signal to the source that our drag | 300 // hard to recover from this so just signal to the source that our drag |
| 263 // failed. | 301 // failed. |
| 264 MessageDragCompleted(false, ui::mojom::kDropEffectNone); | 302 MessageDragCompleted(false, ui::mojom::kDropEffectNone); |
| 265 return; | 303 return; |
| 266 } | 304 } |
| 267 | 305 |
| 268 OnRespondToOperation(window); | 306 OnRespondToOperation(window); |
| 269 MessageDragCompleted(action != 0u, action); | 307 MessageDragCompleted(action != 0u, action); |
| 270 } | 308 } |
| 271 | 309 |
| 272 void DragController::OnWindowDestroying(ServerWindow* window) { | 310 void DragController::OnWindowDestroying(ServerWindow* window) { |
| 273 auto it = window_state_.find(window); | 311 auto it = window_state_.find(window); |
| 274 if (it != window_state_.end()) { | 312 if (it != window_state_.end()) { |
| 275 window->RemoveObserver(this); | 313 window->RemoveObserver(this); |
| 276 window_state_.erase(it); | 314 window_state_.erase(it); |
| 277 } | 315 } |
| 278 | 316 |
| 279 if (current_target_window_ == window) | 317 if (current_target_window_ == window) |
| 280 current_target_window_ = nullptr; | 318 SetCurrentTargetWindow(nullptr); |
| 281 | 319 |
| 282 if (source_window_ == window) { | 320 if (source_window_ == window) { |
| 283 source_window_ = nullptr; | 321 source_window_ = nullptr; |
| 284 // Our source window is being deleted, fail the drag. | 322 // Our source window is being deleted, fail the drag. |
| 285 MessageDragCompleted(false, ui::mojom::kDropEffectNone); | 323 MessageDragCompleted(false, ui::mojom::kDropEffectNone); |
| 286 } | 324 } |
| 287 } | 325 } |
| 288 | 326 |
| 289 } // namespace ws | 327 } // namespace ws |
| 290 } // namespace ui | 328 } // namespace ui |
| OLD | NEW |