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 #ifndef SERVICES_UI_WS_EVENT_DISPATCHER_H_ | 5 #ifndef SERVICES_UI_WS_EVENT_DISPATCHER_H_ |
6 #define SERVICES_UI_WS_EVENT_DISPATCHER_H_ | 6 #define SERVICES_UI_WS_EVENT_DISPATCHER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | |
15 #include "services/ui/common/types.h" | 16 #include "services/ui/common/types.h" |
16 #include "services/ui/public/interfaces/cursor/cursor.mojom.h" | 17 #include "services/ui/public/interfaces/cursor/cursor.mojom.h" |
17 #include "services/ui/public/interfaces/window_manager.mojom.h" | 18 #include "services/ui/public/interfaces/window_manager.mojom.h" |
18 #include "services/ui/ws/drag_cursor_updater.h" | 19 #include "services/ui/ws/drag_cursor_updater.h" |
19 #include "services/ui/ws/modal_window_controller.h" | 20 #include "services/ui/ws/modal_window_controller.h" |
20 #include "services/ui/ws/server_window_observer.h" | 21 #include "services/ui/ws/server_window_observer.h" |
21 #include "ui/gfx/geometry/rect_f.h" | 22 #include "ui/gfx/geometry/rect_f.h" |
22 | 23 |
23 namespace ui { | 24 namespace ui { |
24 class Event; | 25 class Event; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 // is not added. Returns whether adding the accelerator was successful or not. | 136 // is not added. Returns whether adding the accelerator was successful or not. |
136 bool AddAccelerator(uint32_t id, mojom::EventMatcherPtr event_matcher); | 137 bool AddAccelerator(uint32_t id, mojom::EventMatcherPtr event_matcher); |
137 | 138 |
138 void RemoveAccelerator(uint32_t id); | 139 void RemoveAccelerator(uint32_t id); |
139 | 140 |
140 // Processes the supplied event, informing the delegate as approriate. This | 141 // Processes the supplied event, informing the delegate as approriate. This |
141 // may result in generating any number of events. If |match_phase| is | 142 // may result in generating any number of events. If |match_phase| is |
142 // ANY and there is a matching accelerator with PRE_TARGET found, than only | 143 // ANY and there is a matching accelerator with PRE_TARGET found, than only |
143 // OnAccelerator() is called. The expectation is after the PRE_TARGET has been | 144 // OnAccelerator() is called. The expectation is after the PRE_TARGET has been |
144 // handled this is again called with an AcceleratorMatchPhase of POST_ONLY. | 145 // handled this is again called with an AcceleratorMatchPhase of POST_ONLY. |
145 void ProcessEvent(const ui::Event& event, AcceleratorMatchPhase match_phase); | 146 void ProcessEvent(const ui::Event& event, AcceleratorMatchPhase match_phase); |
sky
2017/05/22 16:06:56
Update the description.
riajiang
2017/05/29 23:38:07
Added that it may be asynchronous.
| |
146 | 147 |
148 bool IsHitTestInFlight() const; | |
149 | |
147 private: | 150 private: |
148 friend class test::EventDispatcherTestApi; | 151 friend class test::EventDispatcherTestApi; |
149 | 152 |
150 // Keeps track of state associated with an active pointer. | 153 // Keeps track of state associated with an active pointer. |
151 struct PointerTarget { | 154 struct PointerTarget { |
152 PointerTarget() | 155 PointerTarget() |
153 : window(nullptr), | 156 : window(nullptr), |
154 is_mouse_event(false), | 157 is_mouse_event(false), |
155 in_nonclient_area(false), | 158 in_nonclient_area(false), |
156 is_pointer_down(false) {} | 159 is_pointer_down(false) {} |
157 | 160 |
158 // The target window, which may be null. null is used in two situations: | 161 // The target window, which may be null. null is used in two situations: |
159 // when there is no valid window target, or there was a target but the | 162 // when there is no valid window target, or there was a target but the |
160 // window is destroyed before a corresponding release/cancel. | 163 // window is destroyed before a corresponding release/cancel. |
161 ServerWindow* window; | 164 ServerWindow* window; |
162 | 165 |
163 bool is_mouse_event; | 166 bool is_mouse_event; |
164 | 167 |
165 // Did the pointer event start in the non-client area. | 168 // Did the pointer event start in the non-client area. |
166 bool in_nonclient_area; | 169 bool in_nonclient_area; |
167 | 170 |
168 bool is_pointer_down; | 171 bool is_pointer_down; |
169 }; | 172 }; |
170 | 173 |
174 using PointerTargetForEventCallback = base::OnceCallback<void(PointerTarget)>; | |
175 using HitTestCallback = base::OnceCallback<void(DeepestWindow)>; | |
176 | |
177 struct HitTestRequest { | |
178 HitTestRequest(const gfx::Point& location, | |
179 HitTestCallback hittest_callback); | |
180 ~HitTestRequest(); | |
181 | |
182 gfx::Point hittest_location; | |
183 HitTestCallback hittest_callback; | |
184 }; | |
185 | |
171 void SetMouseCursorSourceWindow(ServerWindow* window); | 186 void SetMouseCursorSourceWindow(ServerWindow* window); |
172 | 187 |
173 void ProcessKeyEvent(const ui::KeyEvent& event, | 188 void ProcessKeyEvent(const ui::KeyEvent& event, |
174 AcceleratorMatchPhase match_phase); | 189 AcceleratorMatchPhase match_phase); |
175 | 190 |
176 bool IsTrackingPointer(int32_t pointer_id) const { | 191 bool IsTrackingPointer(int32_t pointer_id) const { |
177 return pointer_targets_.count(pointer_id) > 0; | 192 return pointer_targets_.count(pointer_id) > 0; |
178 } | 193 } |
179 | 194 |
180 // EventDispatcher provides the following logic for pointer events: | 195 // EventDispatcher provides the following logic for pointer events: |
181 // . wheel events go to the current target of the associated pointer. If | 196 // . wheel events go to the current target of the associated pointer. If |
182 // there is no target, they go to the deepest window. | 197 // there is no target, they go to the deepest window. |
183 // . move (not drag) events go to the deepest window. | 198 // . move (not drag) events go to the deepest window. |
184 // . when a pointer goes down all events until the corresponding up or | 199 // . when a pointer goes down all events until the corresponding up or |
185 // cancel go to the deepest target. For mouse events the up only occurs | 200 // cancel go to the deepest target. For mouse events the up only occurs |
186 // when no buttons on the mouse are down. | 201 // when no buttons on the mouse are down. |
187 // This also generates exit events as appropriate. For example, if the mouse | 202 // This also generates exit events as appropriate. For example, if the mouse |
188 // moves between one window to another an exit is generated on the first. | 203 // moves between one window to another an exit is generated on the first. |
189 void ProcessPointerEvent(const ui::PointerEvent& event); | 204 void ProcessPointerEvent(const ui::PointerEvent& event); |
205 void ProcessPointerEventOnFoundTarget(const ui::PointerEvent& event, | |
206 PointerTarget pointer_target_found); | |
190 | 207 |
191 // Adds |pointer_target| to |pointer_targets_|. | 208 // Adds |pointer_target| to |pointer_targets_|. |
192 void StartTrackingPointer(int32_t pointer_id, | 209 void StartTrackingPointer(int32_t pointer_id, |
193 const PointerTarget& pointer_target); | 210 const PointerTarget& pointer_target); |
194 | 211 |
195 // Removes a PointerTarget from |pointer_targets_|. | 212 // Removes a PointerTarget from |pointer_targets_|. |
196 void StopTrackingPointer(int32_t pointer_id); | 213 void StopTrackingPointer(int32_t pointer_id); |
197 | 214 |
198 // Starts tracking the pointer for |event|, or if already tracking the | 215 // Starts tracking the pointer for |event|, or if already tracking the |
199 // pointer sends the appropriate event to the delegate and updates the | 216 // pointer sends the appropriate event to the delegate and updates the |
200 // currently tracked PointerTarget appropriately. | 217 // currently tracked PointerTarget appropriately. |
201 void UpdateTargetForPointer(int32_t pointer_id, | 218 void UpdateTargetForPointer(int32_t pointer_id, |
202 const ui::LocatedEvent& event); | 219 const ui::PointerEvent& event, |
220 const PointerTarget& pointer_target_found); | |
203 | 221 |
204 // Returns a PointerTarget for the supplied event. If there is no valid | 222 // |callback| is called with a PointerTarget for the supplied event. If there |
205 // event target for the specified location |window| in the returned value is | 223 // is no valid event target for the specified location |window| in the |
206 // null. | 224 // returned value is null. |
207 PointerTarget PointerTargetForEvent(const ui::LocatedEvent& event); | 225 void PointerTargetForEvent(const ui::PointerEvent& event, |
226 PointerTargetForEventCallback callback); | |
227 void PointerTargetForEventOnFoundWindow( | |
228 const ui::PointerEvent& event, | |
229 PointerTargetForEventCallback callback, | |
230 DeepestWindow deepest_window); | |
208 | 231 |
209 // Returns true if any pointers are in the pressed/down state. | 232 // Returns true if any pointers are in the pressed/down state. |
210 bool AreAnyPointersDown() const; | 233 bool AreAnyPointersDown() const; |
211 | 234 |
212 // If |target->window| is valid, then passes the event to the delegate. | 235 // If |target->window| is valid, then passes the event to the delegate. |
213 void DispatchToPointerTarget(const PointerTarget& target, | 236 void DispatchToPointerTarget(const PointerTarget& target, |
214 const ui::LocatedEvent& event); | 237 const ui::LocatedEvent& event); |
215 | 238 |
216 // Dispatch |event| to the delegate. | 239 // Dispatch |event| to the delegate. |
217 void DispatchToClient(ServerWindow* window, | 240 void DispatchToClient(ServerWindow* window, |
218 ClientSpecificId client_id, | 241 ClientSpecificId client_id, |
219 const ui::LocatedEvent& event); | 242 const ui::LocatedEvent& event); |
220 | 243 |
221 // Stops sending pointer events to |window|. This does not remove the entry | 244 // Stops sending pointer events to |window|. This does not remove the entry |
222 // for |window| from |pointer_targets_|, rather it nulls out the window. This | 245 // for |window| from |pointer_targets_|, rather it nulls out the window. This |
223 // way we continue to eat events until the up/cancel is received. | 246 // way we continue to eat events until the up/cancel is received. |
224 void CancelPointerEventsToTarget(ServerWindow* window); | 247 void CancelPointerEventsToTarget(ServerWindow* window); |
225 | 248 |
226 // Used to observe a window. Can be called multiple times on a window. To | 249 // Used to observe a window. Can be called multiple times on a window. To |
227 // unobserve a window, UnobserveWindow() should be called the same number of | 250 // unobserve a window, UnobserveWindow() should be called the same number of |
228 // times. | 251 // times. |
229 void ObserveWindow(ServerWindow* winodw); | 252 void ObserveWindow(ServerWindow* winodw); |
230 void UnobserveWindow(ServerWindow* winodw); | 253 void UnobserveWindow(ServerWindow* winodw); |
231 | 254 |
232 // Returns an Accelerator bound to the specified code/flags, and of the | 255 // Returns an Accelerator bound to the specified code/flags, and of the |
233 // matching |phase|. Otherwise returns null. | 256 // matching |phase|. Otherwise returns null. |
234 Accelerator* FindAccelerator(const ui::KeyEvent& event, | 257 Accelerator* FindAccelerator(const ui::KeyEvent& event, |
235 const ui::mojom::AcceleratorPhase phase); | 258 const ui::mojom::AcceleratorPhase phase); |
236 | 259 |
237 DeepestWindow FindDeepestVisibleWindowForEvents(const gfx::Point& location); | 260 // Call WindowFinder to find the deepest visible window for |location|. |
261 // |callback| is called with the window found. | |
262 void FindDeepestVisibleWindowForEvents(const gfx::Point& location, | |
263 HitTestCallback callback); | |
sky
2017/05/22 16:06:56
I understand you're doing this as an initial step
riajiang
2017/05/29 23:38:07
As discussed, changed to use the new EventTargeter
| |
264 void FindDeepestVisibleWindowForEventsImpl(const gfx::Point& location, | |
265 HitTestCallback callback); | |
266 void FindDeepestVisibleWindowForEventsAsync(const gfx::Point& location, | |
267 HitTestCallback callback); | |
268 | |
269 void UpdateNonClientAreaForCurrentWindowOnFoundWindow( | |
270 DeepestWindow deepest_window); | |
271 void UpdateCursorProviderByLastKnownLocationOnFoundWindow( | |
272 DeepestWindow deepest_window); | |
273 | |
274 void ProcessNextHittesetRequestFromQueue(); | |
238 | 275 |
239 // Clears the implicit captures in |pointer_targets_|, with the exception of | 276 // Clears the implicit captures in |pointer_targets_|, with the exception of |
240 // |window|. |window| may be null. |client_id| is the target client of | 277 // |window|. |window| may be null. |client_id| is the target client of |
241 // |window|. | 278 // |window|. |
242 void CancelImplicitCaptureExcept(ServerWindow* window, | 279 void CancelImplicitCaptureExcept(ServerWindow* window, |
243 ClientSpecificId client_id); | 280 ClientSpecificId client_id); |
244 | 281 |
245 // ServerWindowObserver: | 282 // ServerWindowObserver: |
246 void OnWillChangeWindowHierarchy(ServerWindow* window, | 283 void OnWillChangeWindowHierarchy(ServerWindow* window, |
247 ServerWindow* new_parent, | 284 ServerWindow* new_parent, |
(...skipping 16 matching lines...) Expand all Loading... | |
264 bool mouse_button_down_; | 301 bool mouse_button_down_; |
265 ServerWindow* mouse_cursor_source_window_; | 302 ServerWindow* mouse_cursor_source_window_; |
266 bool mouse_cursor_in_non_client_area_; | 303 bool mouse_cursor_in_non_client_area_; |
267 | 304 |
268 // The on screen location of the mouse pointer. This can be outside the | 305 // The on screen location of the mouse pointer. This can be outside the |
269 // bounds of |mouse_cursor_source_window_|, which can capture the cursor. | 306 // bounds of |mouse_cursor_source_window_|, which can capture the cursor. |
270 gfx::Point mouse_pointer_last_location_; | 307 gfx::Point mouse_pointer_last_location_; |
271 | 308 |
272 std::map<uint32_t, std::unique_ptr<Accelerator>> accelerators_; | 309 std::map<uint32_t, std::unique_ptr<Accelerator>> accelerators_; |
273 | 310 |
311 // True if we are waiting for the result of a hit-test. False otherwise. | |
312 bool hittest_in_flight_; | |
313 | |
314 // Request would go into this queue if it's coming from EventDispatcher:: | |
315 // UpdateNonClientAreaForCurrentWindow() or EventDispatcher:: | |
316 // UpdateCursorProviderByLastKnownLocation(). If it's an event that needs | |
317 // targeting, it would only go to WindowManagerState::event_queue_ when it's | |
318 // waiting for an active hit-test. | |
319 std::queue<std::unique_ptr<HitTestRequest>> hittest_request_queue_; | |
320 | |
274 using PointerIdToTargetMap = std::map<int32_t, PointerTarget>; | 321 using PointerIdToTargetMap = std::map<int32_t, PointerTarget>; |
275 // |pointer_targets_| contains the active pointers. For a mouse based pointer | 322 // |pointer_targets_| contains the active pointers. For a mouse based pointer |
276 // a PointerTarget is always active (and present in |pointer_targets_|). For | 323 // a PointerTarget is always active (and present in |pointer_targets_|). For |
277 // touch based pointers the pointer is active while down and removed on | 324 // touch based pointers the pointer is active while down and removed on |
278 // cancel or up. | 325 // cancel or up. |
279 PointerIdToTargetMap pointer_targets_; | 326 PointerIdToTargetMap pointer_targets_; |
280 | 327 |
281 // Keeps track of number of observe requests for each observed window. | 328 // Keeps track of number of observe requests for each observed window. |
282 std::map<const ServerWindow*, uint8_t> observed_windows_; | 329 std::map<const ServerWindow*, uint8_t> observed_windows_; |
283 | 330 |
284 #if !defined(NDEBUG) | 331 #if !defined(NDEBUG) |
285 std::unique_ptr<ui::Event> previous_event_; | 332 std::unique_ptr<ui::Event> previous_event_; |
286 AcceleratorMatchPhase previous_accelerator_match_phase_ = | 333 AcceleratorMatchPhase previous_accelerator_match_phase_ = |
287 AcceleratorMatchPhase::ANY; | 334 AcceleratorMatchPhase::ANY; |
288 #endif | 335 #endif |
289 | 336 |
337 base::WeakPtrFactory<EventDispatcher> weak_ptr_factory_; | |
338 | |
290 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); | 339 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); |
291 }; | 340 }; |
292 | 341 |
293 } // namespace ws | 342 } // namespace ws |
294 } // namespace ui | 343 } // namespace ui |
295 | 344 |
296 #endif // SERVICES_UI_WS_EVENT_DISPATCHER_H_ | 345 #endif // SERVICES_UI_WS_EVENT_DISPATCHER_H_ |
OLD | NEW |