OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/wm/core/capture_controller.h" | 5 #include "ui/wm/core/capture_controller.h" |
6 | 6 |
7 #include "ui/aura/window.h" | 7 #include "ui/aura/window.h" |
8 #include "ui/aura/window_event_dispatcher.h" | 8 #include "ui/aura/window_event_dispatcher.h" |
9 #include "ui/aura/window_tree_host.h" | 9 #include "ui/aura/window_tree_host.h" |
10 | 10 |
11 namespace wm { | 11 namespace wm { |
12 | 12 |
13 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
14 // CaptureController, public: | 14 // CaptureController, public: |
15 | 15 |
16 void CaptureController::Attach(aura::Window* root) { | 16 void CaptureController::Attach(aura::Window* root) { |
17 DCHECK_EQ(0u, root_windows_.count(root)); | 17 DCHECK_EQ(0u, delegates_.count(root)); |
18 root_windows_.insert(root); | 18 delegates_[root] = root->GetHost()->dispatcher(); |
19 aura::client::SetCaptureClient(root, this); | 19 aura::client::SetCaptureClient(root, this); |
20 } | 20 } |
21 | 21 |
22 void CaptureController::Detach(aura::Window* root) { | 22 void CaptureController::Detach(aura::Window* root) { |
23 root_windows_.erase(root); | 23 delegates_.erase(root); |
24 aura::client::SetCaptureClient(root, NULL); | 24 aura::client::SetCaptureClient(root, nullptr); |
25 } | 25 } |
26 | 26 |
27 //////////////////////////////////////////////////////////////////////////////// | 27 //////////////////////////////////////////////////////////////////////////////// |
28 // CaptureController, aura::client::CaptureClient implementation: | 28 // CaptureController, aura::client::CaptureClient implementation: |
29 | 29 |
30 void CaptureController::SetCapture(aura::Window* new_capture_window) { | 30 void CaptureController::SetCapture(aura::Window* new_capture_window) { |
31 if (capture_window_ == new_capture_window) | 31 if (capture_window_ == new_capture_window) |
32 return; | 32 return; |
33 | 33 |
34 // Make sure window has a root window. | 34 // Make sure window has a root window. |
35 DCHECK(!new_capture_window || new_capture_window->GetRootWindow()); | 35 DCHECK(!new_capture_window || new_capture_window->GetRootWindow()); |
36 DCHECK(!capture_window_ || capture_window_->GetRootWindow()); | 36 DCHECK(!capture_window_ || capture_window_->GetRootWindow()); |
37 | 37 |
38 aura::Window* old_capture_window = capture_window_; | 38 aura::Window* old_capture_window = capture_window_; |
39 aura::Window* old_capture_root = old_capture_window ? | 39 aura::client::CaptureDelegate* old_capture_delegate = capture_delegate_; |
40 old_capture_window->GetRootWindow() : NULL; | |
41 | 40 |
42 // Copy the list in case it's modified out from under us. | 41 // Copy the map in case it's modified out from under us. |
43 RootWindows root_windows(root_windows_); | 42 std::map<aura::Window*, aura::client::CaptureDelegate*> delegates = |
43 delegates_; | |
44 | 44 |
45 // If we're actually starting capture, then cancel any touches/gestures | 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 | 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 | 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 | 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 | 49 // along (and so shouldn't be canceled) and those that got moved, so |
50 // just leave them all where they are. | 50 // just leave them all where they are. |
51 if (new_capture_window) { | 51 if (new_capture_window) { |
52 ui::GestureRecognizer::Get()->TransferEventsTo(old_capture_window, | 52 ui::GestureRecognizer::Get()->TransferEventsTo(old_capture_window, |
53 new_capture_window); | 53 new_capture_window); |
54 } | 54 } |
55 | 55 |
56 capture_window_ = new_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() | |
60 ? nullptr | |
61 : delegates_[capture_root_window]; | |
57 | 62 |
58 for (RootWindows::const_iterator i = root_windows.begin(); | 63 for (std::map<aura::Window*, aura::client::CaptureDelegate*>::const_iterator |
59 i != root_windows.end(); ++i) { | 64 it = delegates.begin(); |
60 aura::client::CaptureDelegate* delegate = (*i)->GetHost()->dispatcher(); | 65 it != delegates.end(); ++it) { |
sadrul
2014/12/16 15:24:13
Consider using const auto&
pkotwicz
2014/12/16 18:04:09
Much shorter with const auto&
| |
61 delegate->UpdateCapture(old_capture_window, new_capture_window); | 66 it->second->UpdateCapture(old_capture_window, new_capture_window); |
62 } | 67 } |
63 | 68 |
64 aura::Window* capture_root = | 69 if (capture_delegate_ != old_capture_delegate) { |
65 capture_window_ ? capture_window_->GetRootWindow() : NULL; | 70 if (old_capture_delegate) |
66 if (capture_root != old_capture_root) { | 71 old_capture_delegate->ReleaseNativeCapture(); |
67 if (old_capture_root) { | 72 if (capture_delegate_) |
68 aura::client::CaptureDelegate* delegate = | 73 capture_delegate_->SetNativeCapture(); |
69 old_capture_root->GetHost()->dispatcher(); | |
70 delegate->ReleaseNativeCapture(); | |
71 } | |
72 if (capture_root) { | |
73 aura::client::CaptureDelegate* delegate = | |
74 capture_root->GetHost()->dispatcher(); | |
75 delegate->SetNativeCapture(); | |
76 } | |
77 } | 74 } |
78 } | 75 } |
79 | 76 |
80 void CaptureController::ReleaseCapture(aura::Window* window) { | 77 void CaptureController::ReleaseCapture(aura::Window* window) { |
81 if (capture_window_ != window) | 78 if (capture_window_ != window) |
82 return; | 79 return; |
83 SetCapture(NULL); | 80 SetCapture(nullptr); |
84 } | 81 } |
85 | 82 |
86 aura::Window* CaptureController::GetCaptureWindow() { | 83 aura::Window* CaptureController::GetCaptureWindow() { |
87 return capture_window_; | 84 return capture_window_; |
88 } | 85 } |
89 | 86 |
90 aura::Window* CaptureController::GetGlobalCaptureWindow() { | 87 aura::Window* CaptureController::GetGlobalCaptureWindow() { |
91 return capture_window_; | 88 return capture_window_; |
92 } | 89 } |
93 | 90 |
94 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
95 // CaptureController, private: | 92 // CaptureController, private: |
96 | 93 |
97 CaptureController::CaptureController() | 94 CaptureController::CaptureController() |
98 : capture_window_(NULL) { | 95 : capture_window_(nullptr), |
96 capture_delegate_(nullptr) { | |
99 } | 97 } |
100 | 98 |
101 CaptureController::~CaptureController() { | 99 CaptureController::~CaptureController() { |
102 } | 100 } |
103 | 101 |
104 //////////////////////////////////////////////////////////////////////////////// | 102 //////////////////////////////////////////////////////////////////////////////// |
105 // ScopedCaptureClient: | 103 // ScopedCaptureClient: |
106 | 104 |
107 // static | 105 // static |
108 CaptureController* ScopedCaptureClient::capture_controller_ = NULL; | 106 CaptureController* ScopedCaptureClient::capture_controller_ = nullptr; |
109 | 107 |
110 ScopedCaptureClient::ScopedCaptureClient(aura::Window* root) | 108 ScopedCaptureClient::ScopedCaptureClient(aura::Window* root) |
111 : root_window_(root) { | 109 : root_window_(root) { |
112 root->AddObserver(this); | 110 root->AddObserver(this); |
113 if (!capture_controller_) | 111 if (!capture_controller_) |
114 capture_controller_ = new CaptureController; | 112 capture_controller_ = new CaptureController; |
115 capture_controller_->Attach(root); | 113 capture_controller_->Attach(root); |
116 } | 114 } |
117 | 115 |
118 ScopedCaptureClient::~ScopedCaptureClient() { | 116 ScopedCaptureClient::~ScopedCaptureClient() { |
(...skipping 11 matching lines...) Expand all Loading... | |
130 } | 128 } |
131 | 129 |
132 void ScopedCaptureClient::Shutdown() { | 130 void ScopedCaptureClient::Shutdown() { |
133 if (!root_window_) | 131 if (!root_window_) |
134 return; | 132 return; |
135 | 133 |
136 root_window_->RemoveObserver(this); | 134 root_window_->RemoveObserver(this); |
137 capture_controller_->Detach(root_window_); | 135 capture_controller_->Detach(root_window_); |
138 if (!capture_controller_->is_active()) { | 136 if (!capture_controller_->is_active()) { |
139 delete capture_controller_; | 137 delete capture_controller_; |
140 capture_controller_ = NULL; | 138 capture_controller_ = nullptr; |
141 } | 139 } |
142 root_window_ = NULL; | 140 root_window_ = nullptr; |
141 } | |
142 | |
143 /////////////////////////////////////////////////////////////////////////////// | |
144 // CaptureController::TestApi | |
145 | |
146 void ScopedCaptureClient::TestApi::SetDelegate( | |
147 aura::client::CaptureDelegate* delegate) { | |
148 client_->capture_controller_->delegates_[client_->root_window_] = delegate; | |
143 } | 149 } |
144 | 150 |
145 } // namespace wm | 151 } // namespace wm |
OLD | NEW |