OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/views/mus/window_manager_connection.h" | 5 #include "ui/views/mus/window_manager_connection.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
11 #include "services/shell/public/cpp/connection.h" | 11 #include "services/shell/public/cpp/connection.h" |
12 #include "services/shell/public/cpp/connector.h" | 12 #include "services/shell/public/cpp/connector.h" |
13 #include "services/ui/common/gpu_service.h" | 13 #include "services/ui/common/gpu_service.h" |
14 #include "services/ui/public/cpp/property_type_converters.h" | 14 #include "services/ui/public/cpp/property_type_converters.h" |
15 #include "services/ui/public/cpp/window.h" | 15 #include "services/ui/public/cpp/window.h" |
16 #include "services/ui/public/cpp/window_property.h" | 16 #include "services/ui/public/cpp/window_property.h" |
17 #include "services/ui/public/cpp/window_tree_client.h" | 17 #include "services/ui/public/cpp/window_tree_client.h" |
18 #include "services/ui/public/interfaces/event_matcher.mojom.h" | 18 #include "services/ui/public/interfaces/event_matcher.mojom.h" |
19 #include "services/ui/public/interfaces/window_tree.mojom.h" | 19 #include "services/ui/public/interfaces/window_tree.mojom.h" |
20 #include "ui/views/mus/clipboard_mus.h" | 20 #include "ui/views/mus/clipboard_mus.h" |
21 #include "ui/views/mus/native_widget_mus.h" | 21 #include "ui/views/mus/native_widget_mus.h" |
22 #include "ui/views/mus/screen_mus.h" | 22 #include "ui/views/mus/screen_mus.h" |
23 #include "ui/views/pointer_watcher.h" | 23 #include "ui/views/pointer_watcher.h" |
24 #include "ui/views/touch_event_watcher.h" | |
25 #include "ui/views/views_delegate.h" | 24 #include "ui/views/views_delegate.h" |
26 | 25 |
27 namespace views { | 26 namespace views { |
28 namespace { | 27 namespace { |
29 | 28 |
30 using WindowManagerConnectionPtr = | 29 using WindowManagerConnectionPtr = |
31 base::ThreadLocalPointer<views::WindowManagerConnection>; | 30 base::ThreadLocalPointer<views::WindowManagerConnection>; |
32 | 31 |
33 // Env is thread local so that aura may be used on multiple threads. | 32 // Env is thread local so that aura may be used on multiple threads. |
34 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = | 33 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 if (init_params.type == Widget::InitParams::TYPE_CONTROL) | 87 if (init_params.type == Widget::InitParams::TYPE_CONTROL) |
89 return nullptr; | 88 return nullptr; |
90 std::map<std::string, std::vector<uint8_t>> properties = props; | 89 std::map<std::string, std::vector<uint8_t>> properties = props; |
91 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); | 90 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); |
92 properties[ui::mojom::WindowManager::kAppID_Property] = | 91 properties[ui::mojom::WindowManager::kAppID_Property] = |
93 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); | 92 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); |
94 return new NativeWidgetMus(delegate, connector_, NewWindow(properties), | 93 return new NativeWidgetMus(delegate, connector_, NewWindow(properties), |
95 ui::mojom::SurfaceType::DEFAULT); | 94 ui::mojom::SurfaceType::DEFAULT); |
96 } | 95 } |
97 | 96 |
98 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher) { | 97 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher, |
98 ui::EventPointerType type, | |
99 bool want_moves) { | |
99 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | 100 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). |
100 DCHECK(!HasTouchEventWatcher()); | 101 DCHECK(type != ui::EventPointerType::POINTER_TYPE_ANY || !want_moves); |
102 DCHECK(event_pointer_type_ == ui::EventPointerType::POINTER_TYPE_UNKNOWN || | |
103 event_pointer_type_ == type); | |
101 bool had_watcher = HasPointerWatcher(); | 104 bool had_watcher = HasPointerWatcher(); |
102 pointer_watchers_.AddObserver(watcher); | 105 pointer_watchers_.AddObserver(watcher); |
103 if (!had_watcher) { | 106 if (!had_watcher) { |
104 // Start a watcher for pointer down. | 107 event_pointer_type_ = type; |
105 // TODO(jamescook): Extend event observers to handle multiple event types. | |
106 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | 108 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); |
107 matcher->type_matcher = ui::mojom::EventTypeMatcher::New(); | 109 if (want_moves) { |
108 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | 110 matcher->pointer_kind_matcher = ui::mojom::PointerKindMatcher::New(); |
111 if (type == ui::EventPointerType::POINTER_TYPE_TOUCH) { | |
112 matcher->pointer_kind_matcher->pointer_kind = | |
113 ui::mojom::PointerKind::TOUCH; | |
114 } else { | |
115 matcher->pointer_kind_matcher->pointer_kind = | |
116 ui::mojom::PointerKind::MOUSE; | |
117 } | |
118 } else { | |
119 // Start a watcher for pointer down. | |
120 // TODO(jamescook): Extend event observers to handle multiple event types. | |
121 matcher->type_matcher = ui::mojom::EventTypeMatcher::New(); | |
122 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | |
James Cook
2016/07/22 22:06:26
Likewise, it's very odd that want_moves false impl
| |
123 } | |
109 client_->SetEventObserver(std::move(matcher)); | 124 client_->SetEventObserver(std::move(matcher)); |
110 } | 125 } |
111 } | 126 } |
112 | 127 |
113 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { | 128 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { |
114 pointer_watchers_.RemoveObserver(watcher); | 129 pointer_watchers_.RemoveObserver(watcher); |
115 if (!HasPointerWatcher()) { | 130 if (!HasPointerWatcher()) { |
116 // Last PointerWatcher removed, stop the event observer. | 131 // Last PointerWatcher removed, stop the event observer. |
117 client_->SetEventObserver(nullptr); | 132 client_->SetEventObserver(nullptr); |
133 event_pointer_type_ = ui::EventPointerType::POINTER_TYPE_UNKNOWN; | |
118 } | 134 } |
119 } | 135 } |
120 | 136 |
121 void WindowManagerConnection::AddTouchEventWatcher(TouchEventWatcher* watcher) { | |
122 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | |
123 DCHECK(!HasPointerWatcher()); | |
124 bool had_watcher = HasTouchEventWatcher(); | |
125 touch_event_watchers_.AddObserver(watcher); | |
126 if (!had_watcher) { | |
127 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | |
128 matcher->pointer_kind_matcher = ui::mojom::PointerKindMatcher::New(); | |
129 matcher->pointer_kind_matcher->pointer_kind = ui::mojom::PointerKind::TOUCH; | |
130 client_->SetEventObserver(std::move(matcher)); | |
131 } | |
132 } | |
133 | |
134 void WindowManagerConnection::RemoveTouchEventWatcher( | |
135 TouchEventWatcher* watcher) { | |
136 touch_event_watchers_.RemoveObserver(watcher); | |
137 if (!HasTouchEventWatcher()) { | |
138 // Last TouchEventWatcher removed, stop the event observer. | |
139 client_->SetEventObserver(nullptr); | |
140 } | |
141 } | |
142 | |
143 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { | 137 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { |
144 return client_->GetRoots(); | 138 return client_->GetRoots(); |
145 } | 139 } |
146 | 140 |
147 WindowManagerConnection::WindowManagerConnection( | 141 WindowManagerConnection::WindowManagerConnection( |
148 shell::Connector* connector, | 142 shell::Connector* connector, |
149 const shell::Identity& identity) | 143 const shell::Identity& identity) |
150 : connector_(connector), identity_(identity) { | 144 : connector_(connector), |
145 identity_(identity), | |
146 event_pointer_type_(ui::EventPointerType::POINTER_TYPE_UNKNOWN) { | |
151 lazy_tls_ptr.Pointer()->Set(this); | 147 lazy_tls_ptr.Pointer()->Set(this); |
152 | 148 |
153 ui::GpuService::Initialize(connector); | 149 ui::GpuService::Initialize(connector); |
154 | 150 |
155 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); | 151 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); |
156 client_->ConnectViaWindowTreeFactory(connector_); | 152 client_->ConnectViaWindowTreeFactory(connector_); |
157 | 153 |
158 screen_.reset(new ScreenMus(this)); | 154 screen_.reset(new ScreenMus(this)); |
159 screen_->Init(connector); | 155 screen_->Init(connector); |
160 | 156 |
161 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); | 157 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); |
162 clipboard->Init(connector); | 158 clipboard->Init(connector); |
163 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); | 159 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); |
164 | 160 |
165 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( | 161 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( |
166 &WindowManagerConnection::CreateNativeWidgetMus, | 162 &WindowManagerConnection::CreateNativeWidgetMus, |
167 base::Unretained(this), | 163 base::Unretained(this), |
168 std::map<std::string, std::vector<uint8_t>>())); | 164 std::map<std::string, std::vector<uint8_t>>())); |
169 } | 165 } |
170 | 166 |
171 bool WindowManagerConnection::HasPointerWatcher() { | 167 bool WindowManagerConnection::HasPointerWatcher() { |
172 // Check to see if we really have any observers left. This doesn't use | 168 // Check to see if we really have any observers left. This doesn't use |
173 // base::ObserverList<>::might_have_observers() because that returns true | 169 // base::ObserverList<>::might_have_observers() because that returns true |
174 // during iteration over the list even when the last observer is removed. | 170 // during iteration over the list even when the last observer is removed. |
175 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); | 171 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); |
176 return !!iterator.GetNext(); | 172 return !!iterator.GetNext(); |
177 } | 173 } |
178 | 174 |
179 bool WindowManagerConnection::HasTouchEventWatcher() { | |
180 // Check to see if we really have any observers left. This doesn't use | |
181 // base::ObserverList<>::might_have_observers() because that returns true | |
182 // during iteration over the list even when the last observer is removed. | |
183 base::ObserverList<TouchEventWatcher>::Iterator iterator( | |
184 &touch_event_watchers_); | |
185 return !!iterator.GetNext(); | |
186 } | |
187 | |
188 void WindowManagerConnection::OnEmbed(ui::Window* root) {} | 175 void WindowManagerConnection::OnEmbed(ui::Window* root) {} |
189 | 176 |
190 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { | 177 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { |
191 if (client_.get() == client) { | 178 if (client_.get() == client) { |
192 client_.release(); | 179 client_.release(); |
193 } else { | 180 } else { |
194 DCHECK(!client_); | 181 DCHECK(!client_); |
195 } | 182 } |
196 } | 183 } |
197 | 184 |
198 void WindowManagerConnection::OnEventObserved(const ui::Event& event, | 185 void WindowManagerConnection::OnEventObserved(const ui::Event& event, |
199 ui::Window* target) { | 186 ui::Window* target) { |
200 if (!event.IsLocatedEvent()) | 187 if (!event.IsLocatedEvent()) |
201 return; | 188 return; |
202 Widget* target_widget = nullptr; | 189 Widget* target_widget = nullptr; |
203 if (target) { | 190 if (target) { |
204 ui::Window* root = target->GetRoot(); | 191 ui::Window* root = target->GetRoot(); |
205 target_widget = NativeWidgetMus::GetWidgetForWindow(root); | 192 target_widget = NativeWidgetMus::GetWidgetForWindow(root); |
206 } | 193 } |
207 | 194 |
208 // The mojo input events type converter uses the event root_location field | 195 // The mojo input events type converter uses the event root_location field |
209 // to store screen coordinates. Screen coordinates really should be returned | 196 // to store screen coordinates. Screen coordinates really should be returned |
210 // separately. See http://crbug.com/608547 | 197 // separately. See http://crbug.com/608547 |
211 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); | 198 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); |
212 if (HasPointerWatcher()) { | 199 bool event_match = false; |
213 if (event.type() == ui::ET_MOUSE_PRESSED) { | 200 switch (event_pointer_type_) { |
214 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 201 case ui::EventPointerType::POINTER_TYPE_TOUCH: |
215 OnMousePressed(*event.AsMouseEvent(), | 202 event_match = event.IsTouchEvent() || event.IsTouchPointerEvent(); |
216 location_in_screen, target_widget)); | 203 break; |
217 } else if (event.type() == ui::ET_TOUCH_PRESSED) { | 204 case ui::EventPointerType::POINTER_TYPE_MOUSE: |
218 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 205 event_match = event.IsMouseEvent() || event.IsMousePointerEvent(); |
219 OnTouchPressed(*event.AsTouchEvent(), | 206 break; |
220 location_in_screen, target_widget)); | 207 case ui::EventPointerType::POINTER_TYPE_ANY: |
221 } | 208 event_match = event.type() == ui::ET_MOUSE_PRESSED || |
222 } else if (HasTouchEventWatcher()) { | 209 event.type() == ui::ET_TOUCH_PRESSED; |
223 if (event.IsTouchEvent() || event.IsTouchPointerEvent()) { | 210 break; |
224 FOR_EACH_OBSERVER( | 211 default: |
225 TouchEventWatcher, touch_event_watchers_, | 212 NOTREACHED(); |
226 OnTouchEventObserved(*event.AsLocatedEvent(), target_widget)); | |
227 } | |
228 } | 213 } |
214 if (event_match) | |
215 FOR_EACH_OBSERVER( | |
216 PointerWatcher, pointer_watchers_, | |
217 OnPointerEventObserved(*event.AsLocatedEvent(), location_in_screen, | |
218 target_widget)); | |
229 } | 219 } |
230 | 220 |
231 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { | 221 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { |
232 if (client_) | 222 if (client_) |
233 NativeWidgetMus::NotifyFrameChanged(client_.get()); | 223 NativeWidgetMus::NotifyFrameChanged(client_.get()); |
234 } | 224 } |
235 | 225 |
236 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { | 226 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { |
237 return client_->GetCursorScreenPoint(); | 227 return client_->GetCursorScreenPoint(); |
238 } | 228 } |
239 | 229 |
240 } // namespace views | 230 } // namespace views |
OLD | NEW |