| 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 "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 Loading... |
| 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_( |
| 50 base::MakeUnique<EventTargeter>(delegate_, |
| 51 &modal_window_controller_)), |
| 49 mouse_button_down_(false), | 52 mouse_button_down_(false), |
| 50 mouse_cursor_source_window_(nullptr), | 53 mouse_cursor_source_window_(nullptr), |
| 51 mouse_cursor_in_non_client_area_(false) {} | 54 mouse_cursor_in_non_client_area_(false) {} |
| 52 | 55 |
| 53 EventDispatcher::~EventDispatcher() { | 56 EventDispatcher::~EventDispatcher() { |
| 54 SetMouseCursorSourceWindow(nullptr); | 57 SetMouseCursorSourceWindow(nullptr); |
| 55 if (capture_window_) { | 58 if (capture_window_) { |
| 56 UnobserveWindow(capture_window_); | 59 UnobserveWindow(capture_window_); |
| 57 capture_window_ = nullptr; | 60 capture_window_ = nullptr; |
| 58 capture_window_client_id_ = kInvalidClientId; | 61 capture_window_client_id_ = kInvalidClientId; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 const ClientSpecificId target_client_id = delegate_->GetEventTargetClientId( | 217 const ClientSpecificId target_client_id = delegate_->GetEventTargetClientId( |
| 215 mouse_cursor_source_window_, mouse_cursor_in_non_client_area_); | 218 mouse_cursor_source_window_, mouse_cursor_in_non_client_area_); |
| 216 const ServerWindow* window = mouse_cursor_source_window_; | 219 const ServerWindow* window = mouse_cursor_source_window_; |
| 217 while (window && window->id().client_id == target_client_id) | 220 while (window && window->id().client_id == target_client_id) |
| 218 window = window->parent(); | 221 window = window->parent(); |
| 219 return window; | 222 return window; |
| 220 } | 223 } |
| 221 | 224 |
| 222 void EventDispatcher::UpdateNonClientAreaForCurrentWindow() { | 225 void EventDispatcher::UpdateNonClientAreaForCurrentWindow() { |
| 223 if (mouse_cursor_source_window_) { | 226 if (mouse_cursor_source_window_) { |
| 224 DeepestWindow deepest_window = FindDeepestVisibleWindowForEvents( | 227 DeepestWindow deepest_window = |
| 225 &mouse_pointer_last_location_, &mouse_pointer_display_id_); | 228 event_targeter_->FindDeepestVisibleWindowForEvents( |
| 229 &mouse_pointer_last_location_, &mouse_pointer_display_id_); |
| 226 if (deepest_window.window == mouse_cursor_source_window_) { | 230 if (deepest_window.window == mouse_cursor_source_window_) { |
| 227 mouse_cursor_in_non_client_area_ = mouse_cursor_source_window_ | 231 mouse_cursor_in_non_client_area_ = mouse_cursor_source_window_ |
| 228 ? deepest_window.in_non_client_area | 232 ? deepest_window.in_non_client_area |
| 229 : false; | 233 : false; |
| 230 } | 234 } |
| 231 } | 235 } |
| 232 } | 236 } |
| 233 | 237 |
| 234 void EventDispatcher::UpdateCursorProviderByLastKnownLocation() { | 238 void EventDispatcher::UpdateCursorProviderByLastKnownLocation() { |
| 235 if (!mouse_button_down_) { | 239 if (!mouse_button_down_) { |
| 236 DeepestWindow deepest_window = FindDeepestVisibleWindowForEvents( | 240 DeepestWindow deepest_window = |
| 237 &mouse_pointer_last_location_, &mouse_pointer_display_id_); | 241 event_targeter_->FindDeepestVisibleWindowForEvents( |
| 242 &mouse_pointer_last_location_, &mouse_pointer_display_id_); |
| 238 SetMouseCursorSourceWindow(deepest_window.window); | 243 SetMouseCursorSourceWindow(deepest_window.window); |
| 239 if (mouse_cursor_source_window_) { | 244 if (mouse_cursor_source_window_) { |
| 240 mouse_cursor_in_non_client_area_ = deepest_window.in_non_client_area; | 245 mouse_cursor_in_non_client_area_ = deepest_window.in_non_client_area; |
| 241 } else { | 246 } else { |
| 242 SetMouseCursorSourceWindow(delegate_->GetRootWindowContaining( | 247 SetMouseCursorSourceWindow(delegate_->GetRootWindowContaining( |
| 243 &mouse_pointer_last_location_, &mouse_pointer_display_id_)); | 248 &mouse_pointer_last_location_, &mouse_pointer_display_id_)); |
| 244 mouse_cursor_in_non_client_area_ = true; | 249 mouse_cursor_in_non_client_area_ = true; |
| 245 } | 250 } |
| 246 } | 251 } |
| 247 } | 252 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 374 |
| 370 // Update mouse down state upon events which change it. | 375 // Update mouse down state upon events which change it. |
| 371 if (is_mouse_event) { | 376 if (is_mouse_event) { |
| 372 if (event.type() == ui::ET_POINTER_DOWN) | 377 if (event.type() == ui::ET_POINTER_DOWN) |
| 373 mouse_button_down_ = true; | 378 mouse_button_down_ = true; |
| 374 else if (is_pointer_going_up) | 379 else if (is_pointer_going_up) |
| 375 mouse_button_down_ = false; | 380 mouse_button_down_ = false; |
| 376 } | 381 } |
| 377 | 382 |
| 378 if (drag_controller_) { | 383 if (drag_controller_) { |
| 379 const PointerTarget target = PointerTargetForEvent(event); | 384 const PointerTarget target = |
| 385 event_targeter_->PointerTargetForEvent(event, &event_display_id_); |
| 380 if (drag_controller_->DispatchPointerEvent(event, target.window)) | 386 if (drag_controller_->DispatchPointerEvent(event, target.window)) |
| 381 return; | 387 return; |
| 382 } | 388 } |
| 383 | 389 |
| 384 if (capture_window_) { | 390 if (capture_window_) { |
| 385 SetMouseCursorSourceWindow(capture_window_); | 391 SetMouseCursorSourceWindow(capture_window_); |
| 386 DispatchToClient(capture_window_, capture_window_client_id_, event); | 392 DispatchToClient(capture_window_, capture_window_client_id_, event); |
| 387 return; | 393 return; |
| 388 } | 394 } |
| 389 | 395 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 DCHECK(IsTrackingPointer(pointer_id)); | 453 DCHECK(IsTrackingPointer(pointer_id)); |
| 448 ServerWindow* window = pointer_targets_[pointer_id].window; | 454 ServerWindow* window = pointer_targets_[pointer_id].window; |
| 449 pointer_targets_.erase(pointer_id); | 455 pointer_targets_.erase(pointer_id); |
| 450 if (window) | 456 if (window) |
| 451 UnobserveWindow(window); | 457 UnobserveWindow(window); |
| 452 } | 458 } |
| 453 | 459 |
| 454 void EventDispatcher::UpdateTargetForPointer(int32_t pointer_id, | 460 void EventDispatcher::UpdateTargetForPointer(int32_t pointer_id, |
| 455 const ui::LocatedEvent& event) { | 461 const ui::LocatedEvent& event) { |
| 456 if (!IsTrackingPointer(pointer_id)) { | 462 if (!IsTrackingPointer(pointer_id)) { |
| 457 StartTrackingPointer(pointer_id, PointerTargetForEvent(event)); | 463 StartTrackingPointer(pointer_id, event_targeter_->PointerTargetForEvent( |
| 464 event, &event_display_id_)); |
| 458 return; | 465 return; |
| 459 } | 466 } |
| 460 | 467 |
| 461 const PointerTarget pointer_target = PointerTargetForEvent(event); | 468 const PointerTarget pointer_target = |
| 469 event_targeter_->PointerTargetForEvent(event, &event_display_id_); |
| 462 if (pointer_target.window == pointer_targets_[pointer_id].window && | 470 if (pointer_target.window == pointer_targets_[pointer_id].window && |
| 463 pointer_target.in_nonclient_area == | 471 pointer_target.in_nonclient_area == |
| 464 pointer_targets_[pointer_id].in_nonclient_area) { | 472 pointer_targets_[pointer_id].in_nonclient_area) { |
| 465 // The targets are the same, only set the down state to true if necessary. | 473 // The targets are the same, only set the down state to true if necessary. |
| 466 // Down going to up is handled by ProcessLocatedEvent(). | 474 // Down going to up is handled by ProcessLocatedEvent(). |
| 467 if (pointer_target.is_pointer_down) | 475 if (pointer_target.is_pointer_down) |
| 468 pointer_targets_[pointer_id].is_pointer_down = true; | 476 pointer_targets_[pointer_id].is_pointer_down = true; |
| 469 return; | 477 return; |
| 470 } | 478 } |
| 471 | 479 |
| 472 // The targets are changing. Send an exit if appropriate. | 480 // The targets are changing. Send an exit if appropriate. |
| 473 if (event.IsMousePointerEvent()) { | 481 if (event.IsMousePointerEvent()) { |
| 474 ui::PointerEvent exit_event( | 482 ui::PointerEvent exit_event( |
| 475 ui::ET_POINTER_EXITED, event.location(), event.root_location(), | 483 ui::ET_POINTER_EXITED, event.location(), event.root_location(), |
| 476 event.flags(), 0 /* changed_button_flags */, | 484 event.flags(), 0 /* changed_button_flags */, |
| 477 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE, | 485 ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_MOUSE, |
| 478 ui::MouseEvent::kMousePointerId), | 486 ui::MouseEvent::kMousePointerId), |
| 479 event.time_stamp()); | 487 event.time_stamp()); |
| 480 DispatchToPointerTarget(pointer_targets_[pointer_id], exit_event); | 488 DispatchToPointerTarget(pointer_targets_[pointer_id], exit_event); |
| 481 } | 489 } |
| 482 | 490 |
| 483 // Technically we're updating in place, but calling start then stop makes for | 491 // Technically we're updating in place, but calling start then stop makes for |
| 484 // simpler code. | 492 // simpler code. |
| 485 StopTrackingPointer(pointer_id); | 493 StopTrackingPointer(pointer_id); |
| 486 StartTrackingPointer(pointer_id, pointer_target); | 494 StartTrackingPointer(pointer_id, pointer_target); |
| 487 } | 495 } |
| 488 | 496 |
| 489 EventDispatcher::PointerTarget EventDispatcher::PointerTargetForEvent( | |
| 490 const ui::LocatedEvent& event) { | |
| 491 PointerTarget pointer_target; | |
| 492 gfx::Point event_root_location(event.root_location()); | |
| 493 DeepestWindow deepest_window = FindDeepestVisibleWindowForEvents( | |
| 494 &event_root_location, &event_display_id_); | |
| 495 pointer_target.window = | |
| 496 modal_window_controller_.GetTargetForWindow(deepest_window.window); | |
| 497 pointer_target.is_mouse_event = event.IsMousePointerEvent(); | |
| 498 pointer_target.in_nonclient_area = | |
| 499 deepest_window.window != pointer_target.window || | |
| 500 !pointer_target.window || deepest_window.in_non_client_area; | |
| 501 pointer_target.is_pointer_down = event.type() == ui::ET_POINTER_DOWN; | |
| 502 return pointer_target; | |
| 503 } | |
| 504 | |
| 505 bool EventDispatcher::AreAnyPointersDown() const { | 497 bool EventDispatcher::AreAnyPointersDown() const { |
| 506 for (const auto& pair : pointer_targets_) { | 498 for (const auto& pair : pointer_targets_) { |
| 507 if (pair.second.is_pointer_down) | 499 if (pair.second.is_pointer_down) |
| 508 return true; | 500 return true; |
| 509 } | 501 } |
| 510 return false; | 502 return false; |
| 511 } | 503 } |
| 512 | 504 |
| 513 void EventDispatcher::DispatchToPointerTarget(const PointerTarget& target, | 505 void EventDispatcher::DispatchToPointerTarget(const PointerTarget& target, |
| 514 const ui::LocatedEvent& event) { | 506 const ui::LocatedEvent& event) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 Accelerator* EventDispatcher::FindAccelerator( | 575 Accelerator* EventDispatcher::FindAccelerator( |
| 584 const ui::KeyEvent& event, | 576 const ui::KeyEvent& event, |
| 585 const ui::mojom::AcceleratorPhase phase) { | 577 const ui::mojom::AcceleratorPhase phase) { |
| 586 for (const auto& pair : accelerators_) { | 578 for (const auto& pair : accelerators_) { |
| 587 if (pair.second->MatchesEvent(event, phase)) | 579 if (pair.second->MatchesEvent(event, phase)) |
| 588 return pair.second.get(); | 580 return pair.second.get(); |
| 589 } | 581 } |
| 590 return nullptr; | 582 return nullptr; |
| 591 } | 583 } |
| 592 | 584 |
| 593 DeepestWindow EventDispatcher::FindDeepestVisibleWindowForEvents( | |
| 594 gfx::Point* location, | |
| 595 int64_t* display_id) { | |
| 596 ServerWindow* root = delegate_->GetRootWindowContaining(location, display_id); | |
| 597 return root ? ui::ws::FindDeepestVisibleWindowForEvents(root, *location) | |
| 598 : DeepestWindow(); | |
| 599 } | |
| 600 | |
| 601 void EventDispatcher::CancelImplicitCaptureExcept(ServerWindow* window, | 585 void EventDispatcher::CancelImplicitCaptureExcept(ServerWindow* window, |
| 602 ClientSpecificId client_id) { | 586 ClientSpecificId client_id) { |
| 603 for (const auto& pair : pointer_targets_) { | 587 for (const auto& pair : pointer_targets_) { |
| 604 ServerWindow* target = pair.second.window; | 588 ServerWindow* target = pair.second.window; |
| 605 if (!target) | 589 if (!target) |
| 606 continue; | 590 continue; |
| 607 UnobserveWindow(target); | 591 UnobserveWindow(target); |
| 608 if (target == window) | 592 if (target == window) |
| 609 continue; | 593 continue; |
| 610 | 594 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 if (mouse_cursor_source_window_ == window) | 645 if (mouse_cursor_source_window_ == window) |
| 662 SetMouseCursorSourceWindow(nullptr); | 646 SetMouseCursorSourceWindow(nullptr); |
| 663 } | 647 } |
| 664 | 648 |
| 665 void EventDispatcher::OnDragCursorUpdated() { | 649 void EventDispatcher::OnDragCursorUpdated() { |
| 666 delegate_->UpdateNativeCursorFromDispatcher(); | 650 delegate_->UpdateNativeCursorFromDispatcher(); |
| 667 } | 651 } |
| 668 | 652 |
| 669 } // namespace ws | 653 } // namespace ws |
| 670 } // namespace ui | 654 } // namespace ui |
| OLD | NEW |