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, root_windows_
.count(root
));
18 root_windows_
.insert(root
);
19 aura::client::SetCaptureClient(root
, this);
22 void CaptureController::Detach(aura::Window
* root
) {
23 root_windows_
.erase(root
);
24 aura::client::SetCaptureClient(root
, NULL
);
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::Window
* old_capture_root
= old_capture_window
?
40 old_capture_window
->GetRootWindow() : NULL
;
42 // Copy the list in case it's modified out from under us.
43 RootWindows
root_windows(root_windows_
);
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
;
58 for (RootWindows::const_iterator i
= root_windows
.begin();
59 i
!= root_windows
.end(); ++i
) {
60 aura::client::CaptureDelegate
* delegate
= (*i
)->GetHost()->dispatcher();
61 delegate
->UpdateCapture(old_capture_window
, new_capture_window
);
64 aura::Window
* capture_root
=
65 capture_window_
? capture_window_
->GetRootWindow() : NULL
;
66 if (capture_root
!= old_capture_root
) {
67 if (old_capture_root
) {
68 aura::client::CaptureDelegate
* delegate
=
69 old_capture_root
->GetHost()->dispatcher();
70 delegate
->ReleaseNativeCapture();
73 aura::client::CaptureDelegate
* delegate
=
74 capture_root
->GetHost()->dispatcher();
75 delegate
->SetNativeCapture();
80 void CaptureController::ReleaseCapture(aura::Window
* window
) {
81 if (capture_window_
!= window
)
86 aura::Window
* CaptureController::GetCaptureWindow() {
87 return capture_window_
;
90 aura::Window
* CaptureController::GetGlobalCaptureWindow() {
91 return capture_window_
;
94 ////////////////////////////////////////////////////////////////////////////////
95 // CaptureController, private:
97 CaptureController::CaptureController()
98 : capture_window_(NULL
) {
101 CaptureController::~CaptureController() {
104 ////////////////////////////////////////////////////////////////////////////////
105 // ScopedCaptureClient:
108 CaptureController
* ScopedCaptureClient::capture_controller_
= NULL
;
110 ScopedCaptureClient::ScopedCaptureClient(aura::Window
* root
)
111 : root_window_(root
) {
112 root
->AddObserver(this);
113 if (!capture_controller_
)
114 capture_controller_
= new CaptureController
;
115 capture_controller_
->Attach(root
);
118 ScopedCaptureClient::~ScopedCaptureClient() {
123 bool ScopedCaptureClient::IsActive() {
124 return capture_controller_
&& capture_controller_
->is_active();
127 void ScopedCaptureClient::OnWindowDestroyed(aura::Window
* window
) {
128 DCHECK_EQ(window
, root_window_
);
132 void ScopedCaptureClient::Shutdown() {
136 root_window_
->RemoveObserver(this);
137 capture_controller_
->Detach(root_window_
);
138 if (!capture_controller_
->is_active()) {
139 delete capture_controller_
;
140 capture_controller_
= NULL
;