OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/aura/window_event_dispatcher.h" | 5 #include "ui/aura/window_event_dispatcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "ui/aura/client/capture_client.h" | 14 #include "ui/aura/client/capture_client.h" |
15 #include "ui/aura/client/cursor_client.h" | 15 #include "ui/aura/client/cursor_client.h" |
16 #include "ui/aura/client/event_client.h" | 16 #include "ui/aura/client/event_client.h" |
17 #include "ui/aura/client/focus_client.h" | 17 #include "ui/aura/client/focus_client.h" |
18 #include "ui/aura/client/screen_position_client.h" | 18 #include "ui/aura/client/screen_position_client.h" |
19 #include "ui/aura/env.h" | 19 #include "ui/aura/env.h" |
20 #include "ui/aura/env_input_state_controller.h" | 20 #include "ui/aura/env_input_state_controller.h" |
21 #include "ui/aura/mus/mus_mouse_location_updater.h" | 21 #include "ui/aura/mus/mus_mouse_location_updater.h" |
22 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
23 #include "ui/aura/window_delegate.h" | 23 #include "ui/aura/window_delegate.h" |
24 #include "ui/aura/window_targeter.h" | 24 #include "ui/aura/window_targeter.h" |
25 #include "ui/aura/window_tracker.h" | 25 #include "ui/aura/window_tracker.h" |
26 #include "ui/aura/window_tree_host.h" | 26 #include "ui/aura/window_tree_host.h" |
27 #include "ui/base/hit_test.h" | 27 #include "ui/base/hit_test.h" |
28 #include "ui/compositor/dip_util.h" | 28 #include "ui/compositor/dip_util.h" |
29 #include "ui/events/event.h" | 29 #include "ui/events/event.h" |
30 #include "ui/events/event_targeter.h" | |
30 #include "ui/events/event_utils.h" | 31 #include "ui/events/event_utils.h" |
31 #include "ui/events/gestures/gesture_recognizer.h" | 32 #include "ui/events/gestures/gesture_recognizer.h" |
32 #include "ui/events/gestures/gesture_types.h" | 33 #include "ui/events/gestures/gesture_types.h" |
33 | 34 |
34 typedef ui::EventDispatchDetails DispatchDetails; | 35 typedef ui::EventDispatchDetails DispatchDetails; |
35 | 36 |
36 namespace aura { | 37 namespace aura { |
37 | 38 |
38 namespace { | 39 namespace { |
39 | 40 |
(...skipping 13 matching lines...) Expand all Loading... | |
53 bool IsEventCandidateForHold(const ui::Event& event) { | 54 bool IsEventCandidateForHold(const ui::Event& event) { |
54 if (event.type() == ui::ET_TOUCH_MOVED) | 55 if (event.type() == ui::ET_TOUCH_MOVED) |
55 return true; | 56 return true; |
56 if (event.type() == ui::ET_MOUSE_DRAGGED) | 57 if (event.type() == ui::ET_MOUSE_DRAGGED) |
57 return true; | 58 return true; |
58 if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED)) | 59 if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED)) |
59 return true; | 60 return true; |
60 return false; | 61 return false; |
61 } | 62 } |
62 | 63 |
64 void ConvertEventLocationToTarget(ui::EventTarget* event_target, | |
65 ui::EventTarget* target, | |
66 ui::Event* event) { | |
67 if (target != event_target && event->IsLocatedEvent()) { | |
68 gfx::Point location = event->AsLocatedEvent()->location(); | |
69 gfx::Point root_location = event->AsLocatedEvent()->root_location(); | |
70 Window::ConvertPointToTarget(static_cast<Window*>(event_target), | |
71 static_cast<Window*>(target), &location); | |
72 Window::ConvertPointToTarget(static_cast<Window*>(event_target), | |
73 static_cast<Window*>(target), &root_location); | |
74 event->AsLocatedEvent()->set_location(location); | |
75 event->AsLocatedEvent()->set_root_location(root_location); | |
76 } | |
77 } | |
78 | |
63 } // namespace | 79 } // namespace |
64 | 80 |
65 //////////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////////// |
66 // WindowEventDispatcher, public: | 82 // WindowEventDispatcher, public: |
67 | 83 |
68 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host) | 84 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host) |
69 : host_(host), | 85 : host_(host), |
70 mouse_pressed_handler_(NULL), | 86 mouse_pressed_handler_(NULL), |
71 mouse_moved_handler_(NULL), | 87 mouse_moved_handler_(NULL), |
72 event_dispatch_target_(NULL), | 88 event_dispatch_target_(NULL), |
73 old_dispatch_target_(NULL), | 89 old_dispatch_target_(NULL), |
74 synthesize_mouse_move_(false), | 90 synthesize_mouse_move_(false), |
75 move_hold_count_(0), | 91 move_hold_count_(0), |
76 dispatching_held_event_(nullptr), | 92 dispatching_held_event_(nullptr), |
77 observer_manager_(this), | 93 observer_manager_(this), |
78 env_controller_(new EnvInputStateController), | 94 env_controller_(new EnvInputStateController), |
95 event_targeter_(new WindowTargeter), | |
79 repost_event_factory_(this), | 96 repost_event_factory_(this), |
80 held_event_factory_(this) { | 97 held_event_factory_(this) { |
81 ui::GestureRecognizer::Get()->AddGestureEventHelper(this); | 98 ui::GestureRecognizer::Get()->AddGestureEventHelper(this); |
82 Env::GetInstance()->AddObserver(this); | 99 Env::GetInstance()->AddObserver(this); |
83 if (Env::GetInstance()->mode() == Env::Mode::MUS) | 100 if (Env::GetInstance()->mode() == Env::Mode::MUS) |
84 mus_mouse_location_updater_ = base::MakeUnique<MusMouseLocationUpdater>(); | 101 mus_mouse_location_updater_ = base::MakeUnique<MusMouseLocationUpdater>(); |
85 } | 102 } |
86 | 103 |
87 WindowEventDispatcher::~WindowEventDispatcher() { | 104 WindowEventDispatcher::~WindowEventDispatcher() { |
88 TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor"); | 105 TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor"); |
89 Env::GetInstance()->RemoveObserver(this); | 106 Env::GetInstance()->RemoveObserver(this); |
90 ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this); | 107 ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this); |
91 } | 108 } |
92 | 109 |
110 ui::EventTargeter* WindowEventDispatcher::GetDefaultEventTargeter() { | |
111 return event_targeter_.get(); | |
112 } | |
113 | |
93 void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent* event) { | 114 void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent* event) { |
94 DCHECK(event->type() == ui::ET_MOUSE_PRESSED || | 115 DCHECK(event->type() == ui::ET_MOUSE_PRESSED || |
95 event->type() == ui::ET_GESTURE_TAP_DOWN || | 116 event->type() == ui::ET_GESTURE_TAP_DOWN || |
96 event->type() == ui::ET_TOUCH_PRESSED); | 117 event->type() == ui::ET_TOUCH_PRESSED); |
97 // We allow for only one outstanding repostable event. This is used | 118 // We allow for only one outstanding repostable event. This is used |
98 // in exiting context menus. A dropped repost request is allowed. | 119 // in exiting context menus. A dropped repost request is allowed. |
99 if (event->type() == ui::ET_MOUSE_PRESSED) { | 120 if (event->type() == ui::ET_MOUSE_PRESSED) { |
100 held_repostable_event_.reset(new ui::MouseEvent( | 121 held_repostable_event_.reset(new ui::MouseEvent( |
101 *event->AsMouseEvent(), static_cast<aura::Window*>(event->target()), | 122 *event->AsMouseEvent(), static_cast<aura::Window*>(event->target()), |
102 window())); | 123 window())); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 void WindowEventDispatcher::SetNativeCapture() { | 430 void WindowEventDispatcher::SetNativeCapture() { |
410 host_->SetCapture(); | 431 host_->SetCapture(); |
411 } | 432 } |
412 | 433 |
413 void WindowEventDispatcher::ReleaseNativeCapture() { | 434 void WindowEventDispatcher::ReleaseNativeCapture() { |
414 host_->ReleaseCapture(); | 435 host_->ReleaseCapture(); |
415 } | 436 } |
416 | 437 |
417 //////////////////////////////////////////////////////////////////////////////// | 438 //////////////////////////////////////////////////////////////////////////////// |
418 // WindowEventDispatcher, ui::EventProcessor implementation: | 439 // WindowEventDispatcher, ui::EventProcessor implementation: |
419 ui::EventTarget* WindowEventDispatcher::GetRootTarget() { | 440 ui::EventTarget* WindowEventDispatcher::GetRootForEvent(ui::Event* event) { |
420 return window(); | 441 if (Env::GetInstance()->mode() == Env::Mode::LOCAL) |
442 return window(); | |
443 | |
444 if (!event->target()) | |
445 return window(); | |
446 | |
447 ui::EventTarget* event_target = event->target(); | |
448 | |
449 // Mouse events should be dispatched to the window that processed the | |
450 // mouse-press events (if any). | |
451 if (event->IsScrollEvent() || event->IsMouseEvent()) { | |
452 if (mouse_pressed_handler()) { | |
453 ConvertEventLocationToTarget(event_target, mouse_pressed_handler(), | |
454 event); | |
455 return mouse_pressed_handler(); | |
456 } | |
457 } | |
458 | |
459 // All events should be directed towards the capture window (if any). | |
sky
2017/02/24 21:22:54
Should this come before the scroll-event/mouse-eve
riajiang
2017/02/27 22:59:30
If there's a mouse_pressed_handler set then that h
| |
460 Window* capture_window = client::GetCaptureWindow(window()); | |
461 if (capture_window) { | |
462 ConvertEventLocationToTarget(event_target, capture_window, event); | |
463 return capture_window; | |
464 } | |
465 | |
466 if (event->IsTouchEvent()) { | |
467 // Query the gesture-recognizer to find targets for touch events. | |
468 const ui::TouchEvent& touch = *event->AsTouchEvent(); | |
469 ui::GestureConsumer* consumer = | |
470 ui::GestureRecognizer::Get()->GetTouchLockedTarget(touch); | |
471 if (consumer) { | |
472 ConvertEventLocationToTarget(event_target, static_cast<Window*>(consumer), | |
473 event); | |
474 return static_cast<Window*>(consumer); | |
475 } | |
476 consumer = ui::GestureRecognizer::Get()->GetTargetForLocation( | |
477 touch.location_f(), touch.source_device_id()); | |
478 if (consumer) { | |
479 ConvertEventLocationToTarget(event_target, static_cast<Window*>(consumer), | |
480 event); | |
481 return static_cast<Window*>(consumer); | |
482 } | |
483 | |
484 // If the initial touch is outside the root window, target the root. | |
485 if (!window()->bounds().Contains(touch.location())) { | |
486 ConvertEventLocationToTarget(event_target, window(), event); | |
487 return window(); | |
488 } | |
489 } | |
490 | |
491 ui::EventTarget* ancestor_with_targeter = event_target; | |
492 for (ui::EventTarget* ancestor = event_target->GetParentTarget(); ancestor; | |
493 ancestor = ancestor->GetParentTarget()) { | |
494 if (ancestor->GetEventTargeter()) | |
495 ancestor_with_targeter = ancestor; | |
496 if (ancestor == window()) | |
497 break; | |
498 } | |
499 ConvertEventLocationToTarget(event_target, ancestor_with_targeter, event); | |
500 return ancestor_with_targeter; | |
421 } | 501 } |
422 | 502 |
423 void WindowEventDispatcher::OnEventProcessingStarted(ui::Event* event) { | 503 void WindowEventDispatcher::OnEventProcessingStarted(ui::Event* event) { |
424 // The held events are already in |window()|'s coordinate system. So it is | 504 // The held events are already in |window()|'s coordinate system. So it is |
425 // not necessary to apply the transform to convert from the host's | 505 // not necessary to apply the transform to convert from the host's |
426 // coordinate system to |window()|'s coordinate system. | 506 // coordinate system to |window()|'s coordinate system. |
427 if (event->IsLocatedEvent() && !is_dispatched_held_event(*event)) | 507 if (event->IsLocatedEvent() && !is_dispatched_held_event(*event)) |
428 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event)); | 508 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event)); |
429 | 509 |
430 if (mus_mouse_location_updater_) | 510 if (mus_mouse_location_updater_) |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
870 } | 950 } |
871 | 951 |
872 // This flag is set depending on the gestures recognized in the call above, | 952 // This flag is set depending on the gestures recognized in the call above, |
873 // and needs to propagate with the forwarded event. | 953 // and needs to propagate with the forwarded event. |
874 event->set_may_cause_scrolling(orig_event.may_cause_scrolling()); | 954 event->set_may_cause_scrolling(orig_event.may_cause_scrolling()); |
875 | 955 |
876 return PreDispatchLocatedEvent(target, event); | 956 return PreDispatchLocatedEvent(target, event); |
877 } | 957 } |
878 | 958 |
879 } // namespace aura | 959 } // namespace aura |
OLD | NEW |