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 void DropTargetMus::Translate(uint32_t key_state, |
| 38 const gfx::Point& screen_location, |
| 39 uint32_t effect, |
| 40 std::unique_ptr<ui::DropTargetEvent>* event, |
| 41 aura::client::DragDropDelegate** delegate) { |
| 42 gfx::Point location = screen_location; |
| 43 gfx::Point root_location = location; |
| 44 root_window_->GetHost()->ConvertPointFromNativeScreen(&root_location); |
| 45 aura::Window* target_window = |
| 46 root_window_->GetEventHandlerForPoint(root_location); |
| 47 bool target_window_changed = false; |
| 48 if (target_window != target_window_) { |
| 49 if (target_window_) |
| 50 NotifyDragExited(); |
| 51 target_window_ = target_window; |
| 52 if (target_window_) |
| 53 target_window_->AddObserver(this); |
| 54 target_window_changed = true; |
| 55 } |
| 56 *delegate = nullptr; |
| 57 if (!target_window_) |
| 58 return; |
| 59 *delegate = aura::client::GetDragDropDelegate(target_window_); |
| 60 if (!*delegate) |
| 61 return; |
| 62 |
| 63 location = root_location; |
| 64 aura::Window::ConvertPointToTarget(root_window_, target_window_, &location); |
| 65 *event = base::MakeUnique<ui::DropTargetEvent>( |
| 66 *(os_exchange_data_.get()), location, root_location, effect); |
| 67 (*event)->set_flags(key_state); |
| 68 if (target_window_changed) |
| 69 (*delegate)->OnDragEntered(*event->get()); |
| 70 } |
| 71 |
| 72 void DropTargetMus::NotifyDragExited() { |
| 73 if (!target_window_) |
| 74 return; |
| 75 |
| 76 aura::client::DragDropDelegate* delegate = |
| 77 aura::client::GetDragDropDelegate(target_window_); |
| 78 if (delegate) |
| 79 delegate->OnDragExited(); |
| 80 |
| 81 target_window_->RemoveObserver(this); |
| 82 target_window_ = nullptr; |
| 83 } |
| 84 |
| 85 void DropTargetMus::OnDragDropStart( |
| 86 std::map<std::string, std::vector<uint8_t>> mime_data) { |
| 87 // We store the mime data here because we need to access it during each phase |
| 88 // of the drag, but we also don't move the data cross-process multiple times. |
| 89 os_exchange_data_ = base::MakeUnique<ui::OSExchangeData>( |
| 90 base::MakeUnique<OSExchangeDataProviderMus>(std::move(mime_data))); |
| 91 } |
| 92 |
| 93 uint32_t DropTargetMus::OnDragEnter(uint32_t key_state, |
| 94 const gfx::Point& position, |
| 95 uint32_t effect_bitmask) { |
| 96 std::unique_ptr<ui::DropTargetEvent> event; |
| 97 aura::client::DragDropDelegate* delegate = nullptr; |
| 98 // Translate will call OnDragEntered. |
| 99 Translate(key_state, position, effect_bitmask, &event, &delegate); |
| 100 return ui::mojom::kDropEffectNone; |
| 101 } |
| 102 |
| 103 uint32_t DropTargetMus::OnDragOver(uint32_t key_state, |
| 104 const gfx::Point& position, |
| 105 uint32_t effect) { |
| 106 int drag_operation = ui::DragDropTypes::DRAG_NONE; |
| 107 std::unique_ptr<ui::DropTargetEvent> event; |
| 108 aura::client::DragDropDelegate* delegate = nullptr; |
| 109 |
| 110 Translate(key_state, position, effect, &event, &delegate); |
| 111 if (delegate) |
| 112 drag_operation = delegate->OnDragUpdated(*event); |
| 113 return drag_operation; |
| 114 } |
| 115 |
| 116 void DropTargetMus::OnDragLeave() { |
| 117 NotifyDragExited(); |
| 118 } |
| 119 |
| 120 uint32_t DropTargetMus::OnCompleteDrop(uint32_t key_state, |
| 121 const gfx::Point& position, |
| 122 uint32_t effect) { |
| 123 int drag_operation = ui::DragDropTypes::DRAG_NONE; |
| 124 std::unique_ptr<ui::DropTargetEvent> event; |
| 125 aura::client::DragDropDelegate* delegate = nullptr; |
| 126 Translate(key_state, position, effect, &event, &delegate); |
| 127 if (delegate) |
| 128 drag_operation = delegate->OnPerformDrop(*event); |
| 129 if (target_window_) { |
| 130 target_window_->RemoveObserver(this); |
| 131 target_window_ = nullptr; |
| 132 } |
| 133 |
| 134 return drag_operation; |
| 135 } |
| 136 |
| 137 void DropTargetMus::OnDragDropDone() { |
| 138 os_exchange_data_.reset(); |
| 139 } |
| 140 |
| 141 void DropTargetMus::OnWindowDestroyed(aura::Window* window) { |
| 142 DCHECK_EQ(window, target_window_); |
| 143 target_window_ = nullptr; |
| 144 } |
| 145 |
| 146 } // namespace views |
OLD | NEW |