blob: 072b9eb769e7a0a5feee38675e77ca4e3ea08d38 [file] [log] [blame]
[email protected]bb612db2012-01-09 17:31:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]084b6bb2011-11-17 05:18:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2f744282011-12-23 22:40:525#ifndef ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_
6#define ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_
[email protected]084b6bb2011-11-17 05:18:167
dchenga94547472016-04-08 08:41:118#include <memory>
9
[email protected]b65bdda2011-12-23 23:35:3110#include "ash/ash_export.h"
Scott Violet9838c932017-06-19 20:54:5111#include "ash/display/window_tree_host_manager.h"
Jonah55ab85e82021-12-07 17:57:4012#include "ash/drag_drop/drag_drop_capture_delegate.h"
Collin Bakerc6fae372020-05-01 23:37:4713#include "ash/drag_drop/tab_drag_drop_delegate.h"
[email protected]8e937c1e2012-06-28 22:57:3014#include "base/callback.h"
[email protected]ffa4e4482012-11-28 12:11:4915#include "base/memory/weak_ptr.h"
Daichi Hironoe66b4152017-08-01 06:20:3316#include "base/observer_list.h"
Evan Stade55a443f2017-07-20 19:41:0217#include "base/time/time.h"
Anton Bikineev43cee28e2021-05-14 23:35:3318#include "third_party/abseil-cpp/absl/types/optional.h"
sky92d41b242016-10-27 20:10:1619#include "ui/aura/client/drag_drop_client.h"
Aya ElAttar2e554e52021-01-11 13:03:4320#include "ui/aura/client/drag_drop_delegate.h"
[email protected]341ad582012-02-03 18:31:0921#include "ui/aura/window_observer.h"
Henrique Ferreiro1748fd12020-08-04 12:51:4622#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
[email protected]084b6bb2011-11-17 05:18:1623#include "ui/base/dragdrop/os_exchange_data.h"
[email protected]86ccbd42013-09-18 18:11:5424#include "ui/events/event_constants.h"
25#include "ui/events/event_handler.h"
[email protected]ffb15d12013-09-15 17:29:3026#include "ui/gfx/animation/animation_delegate.h"
Avi Drissmanfefc2f82014-12-22 19:25:2927#include "ui/gfx/geometry/rect.h"
Allen Bauer35d4e722020-06-16 22:47:5628#include "ui/views/widget/unique_widget_ptr.h"
[email protected]084b6bb2011-11-17 05:18:1629
[email protected]ffb15d12013-09-15 17:29:3030namespace gfx {
[email protected]96fe25bf2012-11-27 20:30:2531class LinearAnimation;
[email protected]bb612db2012-01-09 17:31:1332}
33
sky4f4d2592016-10-28 17:14:4734namespace ui {
35class LocatedEvent;
36}
37
[email protected]55f593352011-12-24 05:42:4638namespace ash {
Nick Diego Yamanef258eb442020-10-28 04:57:1639class ToplevelWindowDragDelegate;
[email protected]084b6bb2011-11-17 05:18:1640
jamescookb8dcef522016-06-25 14:42:5541class ASH_EXPORT DragDropController : public aura::client::DragDropClient,
42 public ui::EventHandler,
43 public gfx::AnimationDelegate,
jamescook035dc062016-08-05 21:13:5244 public aura::WindowObserver,
Scott Violet9838c932017-06-19 20:54:5145 public WindowTreeHostManager::Observer {
[email protected]828a6dfa2012-05-10 20:03:0246 public:
[email protected]084b6bb2011-11-17 05:18:1647 DragDropController();
Peter Boströmec31a042021-09-16 23:37:3448
49 DragDropController(const DragDropController&) = delete;
50 DragDropController& operator=(const DragDropController&) = delete;
51
dcheng34dfcbe2014-10-29 18:58:1952 ~DragDropController() override;
[email protected]084b6bb2011-11-17 05:18:1653
54 void set_should_block_during_drag_drop(bool should_block_during_drag_drop) {
55 should_block_during_drag_drop_ = should_block_during_drag_drop;
56 }
57
James Cookeb426b52018-09-17 16:45:1258 void set_enabled(bool enabled) { enabled_ = enabled; }
59
Nick Diego Yamanef258eb442020-10-28 04:57:1660 void set_toplevel_window_drag_delegate(ToplevelWindowDragDelegate* delegate) {
61 toplevel_window_drag_delegate_ = delegate;
62 }
63
[email protected]458b365c2011-12-15 05:04:0964 // Overridden from aura::client::DragDropClient:
Henrique Ferreirod7b970da2021-04-16 19:36:4465 ui::mojom::DragOperation StartDragAndDrop(
66 std::unique_ptr<ui::OSExchangeData> data,
67 aura::Window* root_window,
68 aura::Window* source_window,
69 const gfx::Point& screen_location,
70 int allowed_operations,
71 ui::mojom::DragEventSource source) override;
dcheng34dfcbe2014-10-29 18:58:1972 void DragCancel() override;
73 bool IsDragDropInProgress() override;
Daichi Hironoe66b4152017-08-01 06:20:3374 void AddObserver(aura::client::DragDropClientObserver* observer) override;
75 void RemoveObserver(aura::client::DragDropClientObserver* observer) override;
[email protected]084b6bb2011-11-17 05:18:1676
[email protected]304594c2012-11-13 16:35:2777 // Overridden from ui::EventHandler:
dcheng34dfcbe2014-10-29 18:58:1978 void OnKeyEvent(ui::KeyEvent* event) override;
79 void OnMouseEvent(ui::MouseEvent* event) override;
80 void OnTouchEvent(ui::TouchEvent* event) override;
81 void OnGestureEvent(ui::GestureEvent* event) override;
[email protected]084b6bb2011-11-17 05:18:1682
[email protected]341ad582012-02-03 18:31:0983 // Overridden from aura::WindowObserver.
dcheng34dfcbe2014-10-29 18:58:1984 void OnWindowDestroyed(aura::Window* window) override;
[email protected]341ad582012-02-03 18:31:0985
Fergus Dalla4293852019-07-26 07:13:4786 void SetDragImage(const gfx::ImageSkia& image,
87 const gfx::Vector2d& image_offset);
88
[email protected]ffa4e4482012-11-28 12:11:4989 protected:
[email protected]c08610452012-11-28 21:48:3690 // Helper method to create a LinearAnimation object that will run the drag
91 // cancel animation. Caller take ownership of the returned object. Protected
92 // for testing.
[email protected]ffb15d12013-09-15 17:29:3093 virtual gfx::LinearAnimation* CreateCancelAnimation(
Evan Stade55a443f2017-07-20 19:41:0294 base::TimeDelta duration,
[email protected]c08610452012-11-28 21:48:3695 int frame_rate,
[email protected]ffb15d12013-09-15 17:29:3096 gfx::AnimationDelegate* delegate);
[email protected]c08610452012-11-28 21:48:3697
sky4f4d2592016-10-28 17:14:4798 // Exposed for tests to override.
99 virtual void DragUpdate(aura::Window* target, const ui::LocatedEvent& event);
100 virtual void Drop(aura::Window* target, const ui::LocatedEvent& event);
101
[email protected]ffa4e4482012-11-28 12:11:49102 // Actual implementation of |DragCancel()|. protected for testing.
Evan Stade55a443f2017-07-20 19:41:02103 virtual void DoDragCancel(base::TimeDelta drag_cancel_animation_duration);
[email protected]ffa4e4482012-11-28 12:11:49104
Jonah55ab85e82021-12-07 17:57:40105 // Exposed for test assertions.
106 DragDropCaptureDelegate* get_capture_delegate() { return capture_delegate_; }
107
[email protected]084b6bb2011-11-17 05:18:16108 private:
James Cook317781a2017-07-18 02:08:06109 friend class DragDropControllerTest;
James Cookeb426b52018-09-17 16:45:12110 friend class DragDropControllerTestApi;
[email protected]084b6bb2011-11-17 05:18:16111
[email protected]ffb15d12013-09-15 17:29:30112 // Overridden from gfx::AnimationDelegate:
dcheng34dfcbe2014-10-29 18:58:19113 void AnimationEnded(const gfx::Animation* animation) override;
114 void AnimationProgressed(const gfx::Animation* animation) override;
115 void AnimationCanceled(const gfx::Animation* animation) override;
[email protected]bb612db2012-01-09 17:31:13116
Scott Violet9838c932017-06-19 20:54:51117 // WindowTreeHostManager::Observer:
jamescook035dc062016-08-05 21:13:52118 void OnDisplayConfigurationChanging() override;
119
[email protected]bb612db2012-01-09 17:31:13120 // Helper method to start drag widget flying back animation.
Evan Stade55a443f2017-07-20 19:41:02121 void StartCanceledAnimation(base::TimeDelta animation_duration);
[email protected]ffa4e4482012-11-28 12:11:49122
123 // Helper method to forward |pending_log_tap_| event to |drag_source_window_|.
124 void ForwardPendingLongTap();
[email protected]bb612db2012-01-09 17:31:13125
[email protected]084b6bb2011-11-17 05:18:16126 // Helper method to reset everything.
127 void Cleanup();
128
Aya ElAttarfd5ce86f2021-09-23 18:20:00129 // Helper method to perform the drop if allowed by
130 // DataTransferPolicyController. If it's run, `drag_cancel` will be replaced.
131 // Otherwise `drag_cancel` will run to cancel the drag.
132 void PerformDrop(const gfx::Point drop_location_in_screen,
133 ui::DropTargetEvent event,
134 std::unique_ptr<ui::OSExchangeData> drag_data,
135 aura::client::DragDropDelegate::DropCallback drop_cb,
136 std::unique_ptr<TabDragDropDelegate> tab_drag_drop_delegate,
137 base::ScopedClosureRunner drag_cancel);
138
Jonah4b609892021-11-09 22:51:07139 void CancelIfInProgress();
140
James Cookeb426b52018-09-17 16:45:12141 bool enabled_ = false;
Allen Bauer35d4e722020-06-16 22:47:56142 views::UniqueWidgetPtr drag_image_widget_;
[email protected]ceb36f7d2012-10-31 18:33:24143 gfx::Vector2d drag_image_offset_;
Navid Zolghadr0beb8292019-07-08 18:54:04144 std::unique_ptr<ui::OSExchangeData> drag_data_;
Henrique Ferreirod7b970da2021-04-16 19:36:44145 int allowed_operations_ = 0;
146 ui::mojom::DragOperation operation_ = ui::mojom::DragOperation::kNone;
Aya ElAttar2e554e52021-01-11 13:03:43147 aura::client::DragUpdateInfo current_drag_info_;
[email protected]6a5fc422012-03-06 04:43:26148
Collin Bakerc6fae372020-05-01 23:37:47149 // Used when processing a Chrome tab drag from a WebUI tab strip.
Aya ElAttarfd5ce86f2021-09-23 18:20:00150 std::unique_ptr<TabDragDropDelegate> tab_drag_drop_delegate_;
Collin Baker7f9a1652020-04-28 20:18:16151
[email protected]6a5fc422012-03-06 04:43:26152 // Window that is currently under the drag cursor.
Allen Bauer35d4e722020-06-16 22:47:56153 aura::Window* drag_window_ = nullptr;
[email protected]ffa4e4482012-11-28 12:11:49154
155 // Starting and final bounds for the drag image for the drag cancel animation.
156 gfx::Rect drag_image_initial_bounds_for_cancel_animation_;
157 gfx::Rect drag_image_final_bounds_for_cancel_animation_;
[email protected]96fe25bf2012-11-27 20:30:25158
dchenga94547472016-04-08 08:41:11159 std::unique_ptr<gfx::LinearAnimation> cancel_animation_;
sangwoo843c6cda2019-12-06 00:15:51160 std::unique_ptr<gfx::AnimationDelegate> cancel_animation_notifier_;
[email protected]084b6bb2011-11-17 05:18:16161
[email protected]ffa4e4482012-11-28 12:11:49162 // Window that started the drag.
Allen Bauer35d4e722020-06-16 22:47:56163 aura::Window* drag_source_window_ = nullptr;
[email protected]ffa4e4482012-11-28 12:11:49164
[email protected]084b6bb2011-11-17 05:18:16165 // Indicates whether the caller should be blocked on a drag/drop session.
166 // Only be used for tests.
Allen Bauer35d4e722020-06-16 22:47:56167 bool should_block_during_drag_drop_ = true;
[email protected]084b6bb2011-11-17 05:18:16168
gab2998ee72017-05-05 16:23:50169 // Closure for quitting nested run loop.
Reilly Grantb6702232019-11-26 22:46:04170 base::OnceClosure quit_closure_;
[email protected]8e937c1e2012-06-28 22:57:30171
Jonah55ab85e82021-12-07 17:57:40172 // If non-null, a drag is active which required a capture window.
173 DragDropCaptureDelegate* capture_delegate_;
Jun Mukai14fc3002021-06-22 17:45:38174
Henrique Ferreiro1748fd12020-08-04 12:51:46175 ui::mojom::DragEventSource current_drag_event_source_ =
176 ui::mojom::DragEventSource::kMouse;
[email protected]ffa4e4482012-11-28 12:11:49177
178 // Holds a synthetic long tap event to be sent to the |drag_source_window_|.
179 // See comment in OnGestureEvent() on why we need this.
Jonah4b609892021-11-09 22:51:07180 std::unique_ptr<ui::Event> pending_long_tap_;
[email protected]ffa4e4482012-11-28 12:11:49181
Fergus Dalla4293852019-07-26 07:13:47182 gfx::Point start_location_;
183 gfx::Point current_location_;
184
Trent Apteda250ec3ab2018-08-19 08:52:19185 base::ObserverList<aura::client::DragDropClientObserver>::Unchecked
186 observers_;
Daichi Hironoe66b4152017-08-01 06:20:33187
Nick Diego Yamanef258eb442020-10-28 04:57:16188 ToplevelWindowDragDelegate* toplevel_window_drag_delegate_ = nullptr;
189
Aya ElAttarfd5ce86f2021-09-23 18:20:00190 // Weak ptr for async drop callbacks to be invalidated if a new drag starts.
191 base::WeakPtrFactory<DragDropController> drop_weak_factory_{this};
192
Jeremy Roman47d432e2019-08-20 14:24:00193 base::WeakPtrFactory<DragDropController> weak_factory_{this};
[email protected]084b6bb2011-11-17 05:18:16194};
195
[email protected]55f593352011-12-24 05:42:46196} // namespace ash
[email protected]084b6bb2011-11-17 05:18:16197
[email protected]2f744282011-12-23 22:40:52198#endif // ASH_DRAG_DROP_DRAG_DROP_CONTROLLER_H_