1 // Copyright (c) 2012 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.
5 #include "ui/wm/core/capture_controller.h"
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_event_dispatcher.h"
9 #include "ui/aura/window_tree_host.h"
13 ////////////////////////////////////////////////////////////////////////////////
14 // CaptureController, public:
16 void CaptureController::Attach(aura::Window
* root
) {
17 DCHECK_EQ(0u, delegates_
.count(root
));
18 delegates_
[root
] = root
->GetHost()->dispatcher();
19 aura::client::SetCaptureClient(root
, this);
22 void CaptureController::Detach(aura::Window
* root
) {
23 delegates_
.erase(root
);
24 aura::client::SetCaptureClient(root
, nullptr);
27 ////////////////////////////////////////////////////////////////////////////////
28 // CaptureController, aura::client::CaptureClient implementation:
30 void CaptureController::SetCapture(aura::Window
* new_capture_window
) {
31 if (capture_window_
== new_capture_window
)
34 // Make sure window has a root window.
35 DCHECK(!new_capture_window
|| new_capture_window
->GetRootWindow());
36 DCHECK(!capture_window_
|| capture_window_
->GetRootWindow());
38 aura::Window
* old_capture_window
= capture_window_
;
39 aura::client::CaptureDelegate
* old_capture_delegate
= capture_delegate_
;
41 // Copy the map in case it's modified out from under us.
42 std::map
<aura::Window
*, aura::client::CaptureDelegate
*> delegates
=
45 // If we're actually starting capture, then cancel any touches/gestures
46 // that aren't already locked to the new window, and transfer any on the
47 // old capture window to the new one. When capture is released we have no
48 // distinction between the touches/gestures that were in the window all
49 // along (and so shouldn't be canceled) and those that got moved, so
50 // just leave them all where they are.
51 if (new_capture_window
) {
52 ui::GestureRecognizer::Get()->TransferEventsTo(old_capture_window
,
56 capture_window_
= new_capture_window
;
57 aura::Window
* capture_root_window
=
58 capture_window_
? capture_window_
->GetRootWindow() : nullptr;
59 capture_delegate_
= delegates_
.find(capture_root_window
) == delegates_
.end()
61 : delegates_
[capture_root_window
];
63 for (const auto& it
: delegates
)
64 it
.second
->UpdateCapture(old_capture_window
, new_capture_window
);
66 if (capture_delegate_
!= old_capture_delegate
) {
67 if (old_capture_delegate
)
68 old_capture_delegate
->ReleaseNativeCapture();
69 if (capture_delegate_
)
70 capture_delegate_
->SetNativeCapture();
74 void CaptureController::ReleaseCapture(aura::Window
* window
) {
75 if (capture_window_
!= window
)
80 aura::Window
* CaptureController::GetCaptureWindow() {
81 return capture_window_
;
84 aura::Window
* CaptureController::GetGlobalCaptureWindow() {
85 return capture_window_
;
88 ////////////////////////////////////////////////////////////////////////////////
89 // CaptureController, private:
91 CaptureController::CaptureController()
92 : capture_window_(nullptr),
93 capture_delegate_(nullptr) {
96 CaptureController::~CaptureController() {
99 ////////////////////////////////////////////////////////////////////////////////
100 // ScopedCaptureClient:
103 CaptureController
* ScopedCaptureClient::capture_controller_
= nullptr;
105 ScopedCaptureClient::ScopedCaptureClient(aura::Window
* root
)
106 : root_window_(root
) {
107 root
->AddObserver(this);
108 if (!capture_controller_
)
109 capture_controller_
= new CaptureController
;
110 capture_controller_
->Attach(root
);
113 ScopedCaptureClient::~ScopedCaptureClient() {
118 bool ScopedCaptureClient::IsActive() {
119 return capture_controller_
&& capture_controller_
->is_active();
122 void ScopedCaptureClient::OnWindowDestroyed(aura::Window
* window
) {
123 DCHECK_EQ(window
, root_window_
);
127 void ScopedCaptureClient::Shutdown() {
131 root_window_
->RemoveObserver(this);
132 capture_controller_
->Detach(root_window_
);
133 if (!capture_controller_
->is_active()) {
134 delete capture_controller_
;
135 capture_controller_
= nullptr;
137 root_window_
= nullptr;
140 ///////////////////////////////////////////////////////////////////////////////
141 // CaptureController::TestApi
143 void ScopedCaptureClient::TestApi::SetDelegate(
144 aura::client::CaptureDelegate
* delegate
) {
145 client_
->capture_controller_
->delegates_
[client_
->root_window_
] = delegate
;