| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/mus/drop_target_mus.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 #include <vector> |
| 11 |
| 12 #include "services/ui/public/interfaces/window_tree_constants.mojom.h" |
| 13 #include "ui/aura/window.h" |
| 14 #include "ui/aura/window_tree_host.h" |
| 15 #include "ui/base/dragdrop/drag_drop_types.h" |
| 16 #include "ui/base/dragdrop/drop_target_event.h" |
| 17 #include "ui/views/mus/os_exchange_data_provider_mus.h" |
| 18 #include "ui/wm/public/drag_drop_client.h" |
| 19 #include "ui/wm/public/drag_drop_delegate.h" |
| 20 |
| 21 namespace views { |
| 22 |
| 23 static_assert(ui::DragDropTypes::DRAG_NONE == ui::mojom::kDropEffectNone, |
| 24 "Drag constants must be the same"); |
| 25 static_assert(ui::DragDropTypes::DRAG_MOVE == ui::mojom::kDropEffectMove, |
| 26 "Drag constants must be the same"); |
| 27 static_assert(ui::DragDropTypes::DRAG_COPY == ui::mojom::kDropEffectCopy, |
| 28 "Drag constants must be the same"); |
| 29 static_assert(ui::DragDropTypes::DRAG_LINK == ui::mojom::kDropEffectLink, |
| 30 "Drag constants must be the same"); |
| 31 |
| 32 DropTargetMus::DropTargetMus(aura::Window* root_window) |
| 33 : root_window_(root_window), target_window_(nullptr) {} |
| 34 |
| 35 DropTargetMus::~DropTargetMus() {} |
| 36 |
| 37 // TODO(erg): |mime_data| could be arbitrarily large. Maybe try to elide |
| 38 // sending it when target window == source window? |
| 39 |
| 40 void DropTargetMus::Translate(uint32_t key_state, |
| 41 const gfx::Point& screen_location, |
| 42 uint32_t effect, |
| 43 std::unique_ptr<ui::DropTargetEvent>* event, |
| 44 aura::client::DragDropDelegate** delegate) { |
| 45 gfx::Point location = screen_location; |
| 46 gfx::Point root_location = location; |
| 47 root_window_->GetHost()->ConvertPointFromNativeScreen(&root_location); |
| 48 aura::Window* target_window = |
| 49 root_window_->GetEventHandlerForPoint(root_location); |
| 50 bool target_window_changed = false; |
| 51 if (target_window != target_window_) { |
| 52 if (target_window_) |
| 53 NotifyDragExited(); |
| 54 target_window_ = target_window; |
| 55 if (target_window_) |
| 56 target_window_->AddObserver(this); |
| 57 target_window_changed = true; |
| 58 } |
| 59 *delegate = nullptr; |
| 60 if (!target_window_) |
| 61 return; |
| 62 *delegate = aura::client::GetDragDropDelegate(target_window_); |
| 63 if (!*delegate) |
| 64 return; |
| 65 |
| 66 location = root_location; |
| 67 aura::Window::ConvertPointToTarget(root_window_, target_window_, &location); |
| 68 *event = base::MakeUnique<ui::DropTargetEvent>( |
| 69 *(os_exchange_data_.get()), location, root_location, effect); |
| 70 (*event)->set_flags(key_state); |
| 71 if (target_window_changed) |
| 72 (*delegate)->OnDragEntered(*event->get()); |
| 73 } |
| 74 |
| 75 void DropTargetMus::NotifyDragExited() { |
| 76 if (!target_window_) |
| 77 return; |
| 78 |
| 79 aura::client::DragDropDelegate* delegate = |
| 80 aura::client::GetDragDropDelegate(target_window_); |
| 81 if (delegate) |
| 82 delegate->OnDragExited(); |
| 83 |
| 84 target_window_->RemoveObserver(this); |
| 85 target_window_ = nullptr; |
| 86 } |
| 87 |
| 88 void DropTargetMus::OnDragDropStart( |
| 89 std::map<std::string, std::vector<uint8_t>> mime_data) { |
| 90 // We store the mime data here because we need to access it during each phase |
| 91 // of the drag, but we also don't move the data cross-process multiple times. |
| 92 os_exchange_data_ = base::MakeUnique<ui::OSExchangeData>( |
| 93 base::MakeUnique<OSExchangeDataProviderMus>(std::move(mime_data))); |
| 94 } |
| 95 |
| 96 uint32_t DropTargetMus::OnDragEnter(uint32_t key_state, |
| 97 const gfx::Point& position, |
| 98 uint32_t effect_bitmask) { |
| 99 std::unique_ptr<ui::DropTargetEvent> event; |
| 100 aura::client::DragDropDelegate* delegate = nullptr; |
| 101 // Translate will call OnDragEntered. |
| 102 Translate(key_state, position, effect_bitmask, &event, &delegate); |
| 103 return ui::mojom::kDropEffectNone; |
| 104 } |
| 105 |
| 106 uint32_t DropTargetMus::OnDragOver(uint32_t key_state, |
| 107 const gfx::Point& position, |
| 108 uint32_t effect) { |
| 109 int drag_operation = ui::DragDropTypes::DRAG_NONE; |
| 110 std::unique_ptr<ui::DropTargetEvent> event; |
| 111 aura::client::DragDropDelegate* delegate = nullptr; |
| 112 |
| 113 Translate(key_state, position, effect, &event, &delegate); |
| 114 if (delegate) |
| 115 drag_operation = delegate->OnDragUpdated(*event); |
| 116 return drag_operation; |
| 117 } |
| 118 |
| 119 void DropTargetMus::OnDragLeave() { |
| 120 NotifyDragExited(); |
| 121 } |
| 122 |
| 123 uint32_t DropTargetMus::OnCompleteDrop(uint32_t key_state, |
| 124 const gfx::Point& position, |
| 125 uint32_t effect) { |
| 126 int drag_operation = ui::DragDropTypes::DRAG_NONE; |
| 127 std::unique_ptr<ui::DropTargetEvent> event; |
| 128 aura::client::DragDropDelegate* delegate = nullptr; |
| 129 Translate(key_state, position, effect, &event, &delegate); |
| 130 if (delegate) |
| 131 drag_operation = delegate->OnPerformDrop(*event); |
| 132 if (target_window_) { |
| 133 target_window_->RemoveObserver(this); |
| 134 target_window_ = nullptr; |
| 135 } |
| 136 |
| 137 return drag_operation; |
| 138 } |
| 139 |
| 140 void DropTargetMus::OnDragDropDone() { |
| 141 os_exchange_data_.reset(); |
| 142 } |
| 143 |
| 144 void DropTargetMus::OnWindowDestroyed(aura::Window* window) { |
| 145 DCHECK_EQ(window, target_window_); |
| 146 target_window_ = nullptr; |
| 147 } |
| 148 |
| 149 } // namespace views |
| OLD | NEW |