Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(994)

Side by Side Diff: services/ui/ws/event_dispatcher.cc

Issue 2905333002: Separate out event-targeting logic in EventDispatcher to EventTargeter. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/ui/ws/event_dispatcher.h ('k') | services/ui/ws/event_targeter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "services/ui/ws/event_dispatcher.h" 5 #include "services/ui/ws/event_dispatcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 28 matching lines...) Expand all
39 39
40 } // namespace 40 } // namespace
41 41
42 //////////////////////////////////////////////////////////////////////////////// 42 ////////////////////////////////////////////////////////////////////////////////
43 43
44 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) 44 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate)
45 : delegate_(delegate), 45 : delegate_(delegate),
46 capture_window_(nullptr), 46 capture_window_(nullptr),
47 capture_window_client_id_(kInvalidClientId), 47 capture_window_client_id_(kInvalidClientId),
48 modal_window_controller_(this), 48 modal_window_controller_(this),
49 event_targeter_(new EventTargeter(delegate_, &modal_window_controller_)),
sky 2017/05/26 19:53:43 MakeUnique
riajiang 2017/05/26 21:06:49 Done.
49 mouse_button_down_(false), 50 mouse_button_down_(false),
50 mouse_cursor_source_window_(nullptr), 51 mouse_cursor_source_window_(nullptr),
51 mouse_cursor_in_non_client_area_(false) {} 52 mouse_cursor_in_non_client_area_(false) {}
52 53
53 EventDispatcher::~EventDispatcher() { 54 EventDispatcher::~EventDispatcher() {
54 SetMouseCursorSourceWindow(nullptr); 55 SetMouseCursorSourceWindow(nullptr);
55 if (capture_window_) { 56 if (capture_window_) {
56 UnobserveWindow(capture_window_); 57 UnobserveWindow(capture_window_);
57 capture_window_ = nullptr; 58 capture_window_ = nullptr;
58 capture_window_client_id_ = kInvalidClientId; 59 capture_window_client_id_ = kInvalidClientId;
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 mouse_cursor_source_window_, mouse_cursor_in_non_client_area_); 214 mouse_cursor_source_window_, mouse_cursor_in_non_client_area_);
214 const ServerWindow* window = mouse_cursor_source_window_; 215 const ServerWindow* window = mouse_cursor_source_window_;
215 while (window && window->id().client_id == target_client_id) 216 while (window && window->id().client_id == target_client_id)
216 window = window->parent(); 217 window = window->parent();
217 return window; 218 return window;
218 } 219 }
219 220
220 void EventDispatcher::UpdateNonClientAreaForCurrentWindow() { 221 void EventDispatcher::UpdateNonClientAreaForCurrentWindow() {
221 if (mouse_cursor_source_window_) { 222 if (mouse_cursor_source_window_) {
222 DeepestWindow deepest_window = 223 DeepestWindow deepest_window =
223 FindDeepestVisibleWindowForEvents(mouse_pointer_last_location_); 224 event_targeter_->FindDeepestVisibleWindowForEvents(
225 mouse_pointer_last_location_);
224 if (deepest_window.window == mouse_cursor_source_window_) { 226 if (deepest_window.window == mouse_cursor_source_window_) {
225 mouse_cursor_in_non_client_area_ = mouse_cursor_source_window_ 227 mouse_cursor_in_non_client_area_ = mouse_cursor_source_window_
226 ? deepest_window.in_non_client_area 228 ? deepest_window.in_non_client_area
227 : false; 229 : false;
228 } 230 }
229 } 231 }
230 } 232 }
231 233
232 void EventDispatcher::UpdateCursorProviderByLastKnownLocation() { 234 void EventDispatcher::UpdateCursorProviderByLastKnownLocation() {
233 if (!mouse_button_down_) { 235 if (!mouse_button_down_) {
234 DeepestWindow deepest_window = 236 DeepestWindow deepest_window =
235 FindDeepestVisibleWindowForEvents(mouse_pointer_last_location_); 237 event_targeter_->FindDeepestVisibleWindowForEvents(
238 mouse_pointer_last_location_);
236 SetMouseCursorSourceWindow(deepest_window.window); 239 SetMouseCursorSourceWindow(deepest_window.window);
237 if (mouse_cursor_source_window_) { 240 if (mouse_cursor_source_window_) {
238 mouse_cursor_in_non_client_area_ = deepest_window.in_non_client_area; 241 mouse_cursor_in_non_client_area_ = deepest_window.in_non_client_area;
239 } else { 242 } else {
240 gfx::Point location = mouse_pointer_last_location_; 243 gfx::Point location = mouse_pointer_last_location_;
241 SetMouseCursorSourceWindow(delegate_->GetRootWindowContaining(&location)); 244 SetMouseCursorSourceWindow(delegate_->GetRootWindowContaining(&location));
242 mouse_cursor_in_non_client_area_ = true; 245 mouse_cursor_in_non_client_area_ = true;
243 } 246 }
244 } 247 }
245 } 248 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 366
364 // Update mouse down state upon events which change it. 367 // Update mouse down state upon events which change it.
365 if (is_mouse_event) { 368 if (is_mouse_event) {
366 if (event.type() == ui::ET_POINTER_DOWN) 369 if (event.type() == ui::ET_POINTER_DOWN)
367 mouse_button_down_ = true; 370 mouse_button_down_ = true;
368 else if (is_pointer_going_up) 371 else if (is_pointer_going_up)
369 mouse_button_down_ = false; 372 mouse_button_down_ = false;
370 } 373 }
371 374
372 if (drag_controller_) { 375 if (drag_controller_) {
373 const PointerTarget target = PointerTargetForEvent(event); 376 const PointerTarget target = event_targeter_->PointerTargetForEvent(event);
374 if (drag_controller_->DispatchPointerEvent(event, target.window)) 377 if (drag_controller_->DispatchPointerEvent(event, target.window))
375 return; 378 return;
376 } 379 }
377 380
378 if (capture_window_) { 381 if (capture_window_) {
379 SetMouseCursorSourceWindow(capture_window_); 382 SetMouseCursorSourceWindow(capture_window_);
380 DispatchToClient(capture_window_, capture_window_client_id_, event); 383 DispatchToClient(capture_window_, capture_window_client_id_, event);
381 return; 384 return;
382 } 385 }
383 386
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 DCHECK(IsTrackingPointer(pointer_id)); 442 DCHECK(IsTrackingPointer(pointer_id));
440 ServerWindow* window = pointer_targets_[pointer_id].window; 443 ServerWindow* window = pointer_targets_[pointer_id].window;
441 pointer_targets_.erase(pointer_id); 444 pointer_targets_.erase(pointer_id);
442 if (window) 445 if (window)
443 UnobserveWindow(window); 446 UnobserveWindow(window);
444 } 447 }
445 448
446 void EventDispatcher::UpdateTargetForPointer(int32_t pointer_id, 449 void EventDispatcher::UpdateTargetForPointer(int32_t pointer_id,
447 const ui::LocatedEvent& event) { 450 const ui::LocatedEvent& event) {
448 if (!IsTrackingPointer(pointer_id)) { 451 if (!IsTrackingPointer(pointer_id)) {
449 StartTrackingPointer(pointer_id, PointerTargetForEvent(event)); 452 StartTrackingPointer(pointer_id,
453 event_targeter_->PointerTargetForEvent(event));
450 return; 454 return;
451 } 455 }
452 456
453 const PointerTarget pointer_target = PointerTargetForEvent(event); 457 const PointerTarget pointer_target =
458 event_targeter_->PointerTargetForEvent(event);
454 if (pointer_target.window == pointer_targets_[pointer_id].window && 459 if (pointer_target.window == pointer_targets_[pointer_id].window &&
455 pointer_target.in_nonclient_area == 460 pointer_target.in_nonclient_area ==
456 pointer_targets_[pointer_id].in_nonclient_area) { 461 pointer_targets_[pointer_id].in_nonclient_area) {
457 // The targets are the same, only set the down state to true if necessary. 462 // The targets are the same, only set the down state to true if necessary.
458 // Down going to up is handled by ProcessLocatedEvent(). 463 // Down going to up is handled by ProcessLocatedEvent().
459 if (pointer_target.is_pointer_down) 464 if (pointer_target.is_pointer_down)
460 pointer_targets_[pointer_id].is_pointer_down = true; 465 pointer_targets_[pointer_id].is_pointer_down = true;
461 return; 466 return;
462 } 467 }
463 468
464 // The targets are changing. Send an exit if appropriate. 469 // The targets are changing. Send an exit if appropriate.
465 if (event.IsMousePointerEvent()) { 470 if (event.IsMousePointerEvent()) {
466 ui::PointerEvent exit_event( 471 ui::PointerEvent exit_event(
467 ui::ET_POINTER_EXITED, event.location(), event.root_location(), 472 ui::ET_POINTER_EXITED, event.location(), event.root_location(),
468 event.flags(), 0 /* changed_button_flags */, 473 event.flags(), 0 /* changed_button_flags */,
469 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE, 474 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE,
470 ui::MouseEvent::kMousePointerId), 475 ui::MouseEvent::kMousePointerId),
471 event.time_stamp()); 476 event.time_stamp());
472 DispatchToPointerTarget(pointer_targets_[pointer_id], exit_event); 477 DispatchToPointerTarget(pointer_targets_[pointer_id], exit_event);
473 } 478 }
474 479
475 // Technically we're updating in place, but calling start then stop makes for 480 // Technically we're updating in place, but calling start then stop makes for
476 // simpler code. 481 // simpler code.
477 StopTrackingPointer(pointer_id); 482 StopTrackingPointer(pointer_id);
478 StartTrackingPointer(pointer_id, pointer_target); 483 StartTrackingPointer(pointer_id, pointer_target);
479 } 484 }
480 485
481 EventDispatcher::PointerTarget EventDispatcher::PointerTargetForEvent(
482 const ui::LocatedEvent& event) {
483 PointerTarget pointer_target;
484 DeepestWindow deepest_window =
485 FindDeepestVisibleWindowForEvents(event.root_location());
486 pointer_target.window =
487 modal_window_controller_.GetTargetForWindow(deepest_window.window);
488 pointer_target.is_mouse_event = event.IsMousePointerEvent();
489 pointer_target.in_nonclient_area =
490 deepest_window.window != pointer_target.window ||
491 !pointer_target.window || deepest_window.in_non_client_area;
492 pointer_target.is_pointer_down = event.type() == ui::ET_POINTER_DOWN;
493 return pointer_target;
494 }
495
496 bool EventDispatcher::AreAnyPointersDown() const { 486 bool EventDispatcher::AreAnyPointersDown() const {
497 for (const auto& pair : pointer_targets_) { 487 for (const auto& pair : pointer_targets_) {
498 if (pair.second.is_pointer_down) 488 if (pair.second.is_pointer_down)
499 return true; 489 return true;
500 } 490 }
501 return false; 491 return false;
502 } 492 }
503 493
504 void EventDispatcher::DispatchToPointerTarget(const PointerTarget& target, 494 void EventDispatcher::DispatchToPointerTarget(const PointerTarget& target,
505 const ui::LocatedEvent& event) { 495 const ui::LocatedEvent& event) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 Accelerator* EventDispatcher::FindAccelerator( 563 Accelerator* EventDispatcher::FindAccelerator(
574 const ui::KeyEvent& event, 564 const ui::KeyEvent& event,
575 const ui::mojom::AcceleratorPhase phase) { 565 const ui::mojom::AcceleratorPhase phase) {
576 for (const auto& pair : accelerators_) { 566 for (const auto& pair : accelerators_) {
577 if (pair.second->MatchesEvent(event, phase)) 567 if (pair.second->MatchesEvent(event, phase))
578 return pair.second.get(); 568 return pair.second.get();
579 } 569 }
580 return nullptr; 570 return nullptr;
581 } 571 }
582 572
583 DeepestWindow EventDispatcher::FindDeepestVisibleWindowForEvents(
584 const gfx::Point& location) {
585 gfx::Point relative_location(location);
586 // For the case of no root.
587 ServerWindow* root = delegate_->GetRootWindowContaining(&relative_location);
588 return root ? ui::ws::FindDeepestVisibleWindowForEvents(root,
589 relative_location)
590 : DeepestWindow();
591 }
592
593 void EventDispatcher::CancelImplicitCaptureExcept(ServerWindow* window, 573 void EventDispatcher::CancelImplicitCaptureExcept(ServerWindow* window,
594 ClientSpecificId client_id) { 574 ClientSpecificId client_id) {
595 for (const auto& pair : pointer_targets_) { 575 for (const auto& pair : pointer_targets_) {
596 ServerWindow* target = pair.second.window; 576 ServerWindow* target = pair.second.window;
597 if (!target) 577 if (!target)
598 continue; 578 continue;
599 UnobserveWindow(target); 579 UnobserveWindow(target);
600 if (target == window) 580 if (target == window)
601 continue; 581 continue;
602 582
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 if (mouse_cursor_source_window_ == window) 633 if (mouse_cursor_source_window_ == window)
654 SetMouseCursorSourceWindow(nullptr); 634 SetMouseCursorSourceWindow(nullptr);
655 } 635 }
656 636
657 void EventDispatcher::OnDragCursorUpdated() { 637 void EventDispatcher::OnDragCursorUpdated() {
658 delegate_->UpdateNativeCursorFromDispatcher(); 638 delegate_->UpdateNativeCursorFromDispatcher();
659 } 639 }
660 640
661 } // namespace ws 641 } // namespace ws
662 } // namespace ui 642 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/ws/event_dispatcher.h ('k') | services/ui/ws/event_targeter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698