| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_targeter.h" | 5 #include "ui/aura/window_targeter.h" |
| 6 | 6 |
| 7 #include "ui/aura/client/capture_client.h" | 7 #include "ui/aura/client/capture_client.h" |
| 8 #include "ui/aura/client/event_client.h" | 8 #include "ui/aura/client/event_client.h" |
| 9 #include "ui/aura/client/focus_client.h" | 9 #include "ui/aura/client/focus_client.h" |
| 10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
| 11 #include "ui/aura/window_delegate.h" | 11 #include "ui/aura/window_delegate.h" |
| 12 #include "ui/aura/window_event_dispatcher.h" | 12 #include "ui/aura/window_event_dispatcher.h" |
| 13 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/events/event_target.h" | 14 #include "ui/events/event_target.h" |
| 15 #include "ui/events/event_target_iterator.h" |
| 15 | 16 |
| 16 namespace aura { | 17 namespace aura { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 bool IsLocatedEvent(const ui::Event& event) { | 21 bool IsLocatedEvent(const ui::Event& event) { |
| 21 return event.IsMouseEvent() || event.IsTouchEvent() || | 22 return event.IsMouseEvent() || event.IsTouchEvent() || |
| 22 event.IsScrollEvent() || event.IsGestureEvent(); | 23 event.IsScrollEvent() || event.IsGestureEvent(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 WindowTargeter::WindowTargeter() {} | 28 WindowTargeter::WindowTargeter() {} |
| 28 WindowTargeter::~WindowTargeter() {} | 29 WindowTargeter::~WindowTargeter() {} |
| 29 | 30 |
| 30 ui::EventTarget* WindowTargeter::FindTargetForEvent(ui::EventTarget* root, | 31 ui::EventTarget* WindowTargeter::FindTargetForLocatedEvent( |
| 31 ui::Event* event) { | 32 ui::EventTarget* root, |
| 33 ui::LocatedEvent* event) { |
| 32 Window* window = static_cast<Window*>(root); | 34 Window* window = static_cast<Window*>(root); |
| 33 Window* target = event->IsKeyEvent() ? | 35 if (!window->parent()) { |
| 34 FindTargetForKeyEvent(window, *static_cast<ui::KeyEvent*>(event)) : | 36 Window* target = FindTargetInRootWindow(window, *event); |
| 35 static_cast<Window*>(EventTargeter::FindTargetForEvent(root, event)); | 37 if (target) { |
| 36 if (target && !window->parent() && !window->Contains(target)) { | 38 window->ConvertEventToTarget(target, event); |
| 37 // |window| is the root window, but |target| is not a descendent of | 39 return target; |
| 38 // |window|. So do not allow dispatching from here. Instead, dispatch the | |
| 39 // event through the WindowEventDispatcher that owns |target|. | |
| 40 aura::Window* new_root = target->GetRootWindow(); | |
| 41 if (IsLocatedEvent(*event)) { | |
| 42 // The event has been transformed to be in |target|'s coordinate system. | |
| 43 // But dispatching the event through the EventProcessor requires the event | |
| 44 // to be in the host's coordinate system. So, convert the event to be in | |
| 45 // the root's coordinate space, and then to the host's coordinate space by | |
| 46 // applying the host's transform. | |
| 47 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); | |
| 48 located_event->ConvertLocationToTarget(target, new_root); | |
| 49 located_event->UpdateForRootTransform( | |
| 50 new_root->GetHost()->GetRootTransform()); | |
| 51 } | 40 } |
| 52 ignore_result( | 41 } |
| 53 new_root->GetHost()->event_processor()->OnEventFromSource(event)); | |
| 54 | 42 |
| 55 target = NULL; | 43 return EventTargeter::FindTargetForEvent(root, event); |
| 56 } | |
| 57 return target; | |
| 58 } | 44 } |
| 59 | 45 |
| 60 bool WindowTargeter::SubtreeCanAcceptEvent( | 46 bool WindowTargeter::SubtreeCanAcceptEvent( |
| 61 ui::EventTarget* target, | 47 ui::EventTarget* target, |
| 62 const ui::LocatedEvent& event) const { | 48 const ui::LocatedEvent& event) const { |
| 63 aura::Window* window = static_cast<aura::Window*>(target); | 49 aura::Window* window = static_cast<aura::Window*>(target); |
| 64 if (!window->IsVisible()) | 50 if (!window->IsVisible()) |
| 65 return false; | 51 return false; |
| 66 if (window->ignore_events()) | 52 if (window->ignore_events()) |
| 67 return false; | 53 return false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 80 bool WindowTargeter::EventLocationInsideBounds( | 66 bool WindowTargeter::EventLocationInsideBounds( |
| 81 ui::EventTarget* target, | 67 ui::EventTarget* target, |
| 82 const ui::LocatedEvent& event) const { | 68 const ui::LocatedEvent& event) const { |
| 83 aura::Window* window = static_cast<aura::Window*>(target); | 69 aura::Window* window = static_cast<aura::Window*>(target); |
| 84 gfx::Point point = event.location(); | 70 gfx::Point point = event.location(); |
| 85 if (window->parent()) | 71 if (window->parent()) |
| 86 aura::Window::ConvertPointToTarget(window->parent(), window, &point); | 72 aura::Window::ConvertPointToTarget(window->parent(), window, &point); |
| 87 return gfx::Rect(window->bounds().size()).Contains(point); | 73 return gfx::Rect(window->bounds().size()).Contains(point); |
| 88 } | 74 } |
| 89 | 75 |
| 90 ui::EventTarget* WindowTargeter::FindTargetForLocatedEvent( | 76 ui::EventTarget* WindowTargeter::FindTargetForEvent(ui::EventTarget* root, |
| 91 ui::EventTarget* root, | 77 ui::Event* event) { |
| 92 ui::LocatedEvent* event) { | |
| 93 Window* window = static_cast<Window*>(root); | 78 Window* window = static_cast<Window*>(root); |
| 94 if (!window->parent()) { | 79 Window* target = |
| 95 Window* target = FindTargetInRootWindow(window, *event); | 80 event->IsKeyEvent() |
| 96 if (target) { | 81 ? FindTargetForKeyEvent(window, *static_cast<ui::KeyEvent*>(event)) |
| 97 window->ConvertEventToTarget(target, event); | 82 : FindTargetForNonKeyEvent(window, event); |
| 98 return target; | 83 if (target && !window->parent() && !window->Contains(target)) { |
| 84 // |window| is the root window, but |target| is not a descendent of |
| 85 // |window|. So do not allow dispatching from here. Instead, dispatch the |
| 86 // event through the WindowEventDispatcher that owns |target|. |
| 87 aura::Window* new_root = target->GetRootWindow(); |
| 88 if (IsLocatedEvent(*event)) { |
| 89 // The event has been transformed to be in |target|'s coordinate system. |
| 90 // But dispatching the event through the EventProcessor requires the event |
| 91 // to be in the host's coordinate system. So, convert the event to be in |
| 92 // the root's coordinate space, and then to the host's coordinate space by |
| 93 // applying the host's transform. |
| 94 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event); |
| 95 located_event->ConvertLocationToTarget(target, new_root); |
| 96 located_event->UpdateForRootTransform( |
| 97 new_root->GetHost()->GetRootTransform()); |
| 99 } | 98 } |
| 99 ignore_result( |
| 100 new_root->GetHost()->event_processor()->OnEventFromSource(event)); |
| 101 |
| 102 target = NULL; |
| 100 } | 103 } |
| 101 return EventTargeter::FindTargetForLocatedEvent(root, event); | 104 return target; |
| 105 } |
| 106 |
| 107 bool WindowTargeter::SubtreeShouldBeExploredForEvent( |
| 108 ui::EventTarget* target, |
| 109 const ui::LocatedEvent& event) { |
| 110 return SubtreeCanAcceptEvent(target, event) && |
| 111 EventLocationInsideBounds(target, event); |
| 102 } | 112 } |
| 103 | 113 |
| 104 Window* WindowTargeter::FindTargetForKeyEvent(Window* window, | 114 Window* WindowTargeter::FindTargetForKeyEvent(Window* window, |
| 105 const ui::KeyEvent& key) { | 115 const ui::KeyEvent& key) { |
| 106 Window* root_window = window->GetRootWindow(); | 116 Window* root_window = window->GetRootWindow(); |
| 107 client::FocusClient* focus_client = client::GetFocusClient(root_window); | 117 client::FocusClient* focus_client = client::GetFocusClient(root_window); |
| 108 Window* focused_window = focus_client->GetFocusedWindow(); | 118 Window* focused_window = focus_client->GetFocusedWindow(); |
| 109 if (!focused_window) | 119 if (!focused_window) |
| 110 return window; | 120 return window; |
| 111 | 121 |
| 112 client::EventClient* event_client = client::GetEventClient(root_window); | 122 client::EventClient* event_client = client::GetEventClient(root_window); |
| 113 if (event_client && | 123 if (event_client && |
| 114 !event_client->CanProcessEventsWithinSubtree(focused_window)) { | 124 !event_client->CanProcessEventsWithinSubtree(focused_window)) { |
| 115 focus_client->FocusWindow(NULL); | 125 focus_client->FocusWindow(NULL); |
| 116 return NULL; | 126 return NULL; |
| 117 } | 127 } |
| 118 return focused_window ? focused_window : window; | 128 return focused_window ? focused_window : window; |
| 119 } | 129 } |
| 120 | 130 |
| 131 Window* WindowTargeter::FindTargetForNonKeyEvent(Window* root_window, |
| 132 ui::Event* event) { |
| 133 ui::EventTarget* target = NULL; |
| 134 if (IsLocatedEvent(*event)) { |
| 135 target = FindTargetForLocatedEvent(root_window, |
| 136 static_cast<ui::LocatedEvent*>(event)); |
| 137 } else { |
| 138 target = EventTargeter::FindTargetForEvent(root_window, event); |
| 139 } |
| 140 return static_cast<Window*>(target); |
| 141 } |
| 142 |
| 121 Window* WindowTargeter::FindTargetInRootWindow(Window* root_window, | 143 Window* WindowTargeter::FindTargetInRootWindow(Window* root_window, |
| 122 const ui::LocatedEvent& event) { | 144 const ui::LocatedEvent& event) { |
| 123 DCHECK_EQ(root_window, root_window->GetRootWindow()); | 145 DCHECK_EQ(root_window, root_window->GetRootWindow()); |
| 124 | 146 |
| 125 // Mouse events should be dispatched to the window that processed the | 147 // Mouse events should be dispatched to the window that processed the |
| 126 // mouse-press events (if any). | 148 // mouse-press events (if any). |
| 127 if (event.IsScrollEvent() || event.IsMouseEvent()) { | 149 if (event.IsScrollEvent() || event.IsMouseEvent()) { |
| 128 WindowEventDispatcher* dispatcher = root_window->GetHost()->dispatcher(); | 150 WindowEventDispatcher* dispatcher = root_window->GetHost()->dispatcher(); |
| 129 if (dispatcher->mouse_pressed_handler()) | 151 if (dispatcher->mouse_pressed_handler()) |
| 130 return dispatcher->mouse_pressed_handler(); | 152 return dispatcher->mouse_pressed_handler(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 150 | 172 |
| 151 // If the initial touch is outside the root window, target the root. | 173 // If the initial touch is outside the root window, target the root. |
| 152 if (!root_window->bounds().Contains(event.location())) | 174 if (!root_window->bounds().Contains(event.location())) |
| 153 return root_window; | 175 return root_window; |
| 154 } | 176 } |
| 155 | 177 |
| 156 return NULL; | 178 return NULL; |
| 157 } | 179 } |
| 158 | 180 |
| 159 } // namespace aura | 181 } // namespace aura |
| OLD | NEW |