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

Side by Side Diff: ui/aura/window_targeter.cc

Issue 1119423003: Refactors away method implementations in ui::EventTargeter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactors ui::EventTargeter (rebased) Created 5 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 | « ui/aura/window_targeter.h ('k') | ui/aura/window_targeter_unittest.cc » ('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 (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
20 bool IsLocatedEvent(const ui::Event& event) {
21 return event.IsMouseEvent() || event.IsTouchEvent() ||
22 event.IsScrollEvent() || event.IsGestureEvent();
23 }
24
25 } // namespace
26
27 WindowTargeter::WindowTargeter() {} 19 WindowTargeter::WindowTargeter() {}
28 WindowTargeter::~WindowTargeter() {} 20 WindowTargeter::~WindowTargeter() {}
29 21
30 ui::EventTarget* WindowTargeter::FindTargetForEvent(ui::EventTarget* root, 22 Window* WindowTargeter::FindTargetForLocatedEvent(Window* window,
31 ui::Event* event) { 23 ui::LocatedEvent* event) {
32 Window* window = static_cast<Window*>(root); 24 if (!window->parent()) {
33 Window* target = event->IsKeyEvent() ? 25 Window* target = FindTargetInRootWindow(window, *event);
34 FindTargetForKeyEvent(window, *static_cast<ui::KeyEvent*>(event)) : 26 if (target) {
35 static_cast<Window*>(EventTargeter::FindTargetForEvent(root, event)); 27 window->ConvertEventToTarget(target, event);
36 if (target && !window->parent() && !window->Contains(target)) { 28 return target;
37 // |window| is the root window, but |target| is not a descendent of
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 } 29 }
52 ignore_result(
53 new_root->GetHost()->event_processor()->OnEventFromSource(event));
54
55 target = NULL;
56 } 30 }
57 return target; 31 return FindTargetForLocatedEventRecursively(window, event);
58 } 32 }
59 33
60 bool WindowTargeter::SubtreeCanAcceptEvent( 34 bool WindowTargeter::SubtreeCanAcceptEvent(
61 ui::EventTarget* target, 35 Window* window,
62 const ui::LocatedEvent& event) const { 36 const ui::LocatedEvent& event) const {
63 aura::Window* window = static_cast<aura::Window*>(target);
64 if (!window->IsVisible()) 37 if (!window->IsVisible())
65 return false; 38 return false;
66 if (window->ignore_events()) 39 if (window->ignore_events())
67 return false; 40 return false;
68 client::EventClient* client = client::GetEventClient(window->GetRootWindow()); 41 client::EventClient* client = client::GetEventClient(window->GetRootWindow());
69 if (client && !client->CanProcessEventsWithinSubtree(window)) 42 if (client && !client->CanProcessEventsWithinSubtree(window))
70 return false; 43 return false;
71 44
72 Window* parent = window->parent(); 45 Window* parent = window->parent();
73 if (parent && parent->delegate_ && !parent->delegate_-> 46 if (parent && parent->delegate_ && !parent->delegate_->
74 ShouldDescendIntoChildForEventHandling(window, event.location())) { 47 ShouldDescendIntoChildForEventHandling(window, event.location())) {
75 return false; 48 return false;
76 } 49 }
77 return true; 50 return true;
78 } 51 }
79 52
80 bool WindowTargeter::EventLocationInsideBounds( 53 bool WindowTargeter::EventLocationInsideBounds(
81 ui::EventTarget* target, 54 Window* window,
82 const ui::LocatedEvent& event) const { 55 const ui::LocatedEvent& event) const {
83 aura::Window* window = static_cast<aura::Window*>(target);
84 gfx::Point point = event.location(); 56 gfx::Point point = event.location();
85 if (window->parent()) 57 if (window->parent())
86 aura::Window::ConvertPointToTarget(window->parent(), window, &point); 58 Window::ConvertPointToTarget(window->parent(), window, &point);
87 return gfx::Rect(window->bounds().size()).Contains(point); 59 return gfx::Rect(window->bounds().size()).Contains(point);
88 } 60 }
89 61
90 ui::EventTarget* WindowTargeter::FindTargetForLocatedEvent( 62 ui::EventTarget* WindowTargeter::FindTargetForEvent(ui::EventTarget* root,
91 ui::EventTarget* root, 63 ui::Event* event) {
92 ui::LocatedEvent* event) {
93 Window* window = static_cast<Window*>(root); 64 Window* window = static_cast<Window*>(root);
94 if (!window->parent()) { 65 Window* target =
95 Window* target = FindTargetInRootWindow(window, *event); 66 event->IsKeyEvent()
96 if (target) { 67 ? FindTargetForKeyEvent(window, *static_cast<ui::KeyEvent*>(event))
97 window->ConvertEventToTarget(target, event); 68 : FindTargetForNonKeyEvent(window, event);
98 return target; 69 if (target && !window->parent() && !window->Contains(target)) {
70 // |window| is the root window, but |target| is not a descendent of
71 // |window|. So do not allow dispatching from here. Instead, dispatch the
72 // event through the WindowEventDispatcher that owns |target|.
73 Window* new_root = target->GetRootWindow();
74 if (event->IsLocatedEvent()) {
75 // The event has been transformed to be in |target|'s coordinate system.
76 // But dispatching the event through the EventProcessor requires the event
77 // to be in the host's coordinate system. So, convert the event to be in
78 // the root's coordinate space, and then to the host's coordinate space by
79 // applying the host's transform.
80 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
81 located_event->ConvertLocationToTarget(target, new_root);
82 located_event->UpdateForRootTransform(
83 new_root->GetHost()->GetRootTransform());
99 } 84 }
85 ignore_result(
86 new_root->GetHost()->event_processor()->OnEventFromSource(event));
87
88 target = nullptr;
100 } 89 }
101 return EventTargeter::FindTargetForLocatedEvent(root, event); 90 return target;
91 }
92
93 ui::EventTarget* WindowTargeter::FindNextBestTarget(
94 ui::EventTarget* previous_target,
95 ui::Event* event) {
96 return nullptr;
97 }
98
99 bool WindowTargeter::SubtreeShouldBeExploredForEvent(
100 Window* window,
101 const ui::LocatedEvent& event) {
102 return SubtreeCanAcceptEvent(window, event) &&
103 EventLocationInsideBounds(window, event);
102 } 104 }
103 105
104 Window* WindowTargeter::FindTargetForKeyEvent(Window* window, 106 Window* WindowTargeter::FindTargetForKeyEvent(Window* window,
105 const ui::KeyEvent& key) { 107 const ui::KeyEvent& key) {
106 Window* root_window = window->GetRootWindow(); 108 Window* root_window = window->GetRootWindow();
107 client::FocusClient* focus_client = client::GetFocusClient(root_window); 109 client::FocusClient* focus_client = client::GetFocusClient(root_window);
108 Window* focused_window = focus_client->GetFocusedWindow(); 110 Window* focused_window = focus_client->GetFocusedWindow();
109 if (!focused_window) 111 if (!focused_window)
110 return window; 112 return window;
111 113
112 client::EventClient* event_client = client::GetEventClient(root_window); 114 client::EventClient* event_client = client::GetEventClient(root_window);
113 if (event_client && 115 if (event_client &&
114 !event_client->CanProcessEventsWithinSubtree(focused_window)) { 116 !event_client->CanProcessEventsWithinSubtree(focused_window)) {
115 focus_client->FocusWindow(NULL); 117 focus_client->FocusWindow(nullptr);
116 return NULL; 118 return nullptr;
117 } 119 }
118 return focused_window ? focused_window : window; 120 return focused_window ? focused_window : window;
119 } 121 }
120 122
123 Window* WindowTargeter::FindTargetForNonKeyEvent(Window* root_window,
124 ui::Event* event) {
125 if (!event->IsLocatedEvent())
126 return root_window;
127 return FindTargetForLocatedEvent(root_window,
128 static_cast<ui::LocatedEvent*>(event));
129 }
130
121 Window* WindowTargeter::FindTargetInRootWindow(Window* root_window, 131 Window* WindowTargeter::FindTargetInRootWindow(Window* root_window,
122 const ui::LocatedEvent& event) { 132 const ui::LocatedEvent& event) {
123 DCHECK_EQ(root_window, root_window->GetRootWindow()); 133 DCHECK_EQ(root_window, root_window->GetRootWindow());
124 134
125 // Mouse events should be dispatched to the window that processed the 135 // Mouse events should be dispatched to the window that processed the
126 // mouse-press events (if any). 136 // mouse-press events (if any).
127 if (event.IsScrollEvent() || event.IsMouseEvent()) { 137 if (event.IsScrollEvent() || event.IsMouseEvent()) {
128 WindowEventDispatcher* dispatcher = root_window->GetHost()->dispatcher(); 138 WindowEventDispatcher* dispatcher = root_window->GetHost()->dispatcher();
129 if (dispatcher->mouse_pressed_handler()) 139 if (dispatcher->mouse_pressed_handler())
130 return dispatcher->mouse_pressed_handler(); 140 return dispatcher->mouse_pressed_handler();
(...skipping 15 matching lines...) Expand all
146 ui::GestureRecognizer::Get()->GetTargetForLocation( 156 ui::GestureRecognizer::Get()->GetTargetForLocation(
147 event.location(), touch.source_device_id()); 157 event.location(), touch.source_device_id());
148 if (consumer) 158 if (consumer)
149 return static_cast<Window*>(consumer); 159 return static_cast<Window*>(consumer);
150 160
151 // If the initial touch is outside the root window, target the root. 161 // If the initial touch is outside the root window, target the root.
152 if (!root_window->bounds().Contains(event.location())) 162 if (!root_window->bounds().Contains(event.location()))
153 return root_window; 163 return root_window;
154 } 164 }
155 165
156 return NULL; 166 return nullptr;
167 }
168
169 Window* WindowTargeter::FindTargetForLocatedEventRecursively(
170 Window* root_window,
171 ui::LocatedEvent* event) {
172 scoped_ptr<ui::EventTargetIterator> iter = root_window->GetChildIterator();
173 if (iter) {
174 ui::EventTarget* target = root_window;
175 for (ui::EventTarget* child = iter->GetNextTarget(); child;
176 child = iter->GetNextTarget()) {
177 WindowTargeter* targeter =
178 static_cast<WindowTargeter*>(child->GetEventTargeter());
179 if (!targeter)
180 targeter = this;
181 if (!targeter->SubtreeShouldBeExploredForEvent(
182 static_cast<Window*>(child), *event)) {
183 continue;
184 }
185 target->ConvertEventToTarget(child, event);
186 target = child;
187 Window* child_target_window =
188 static_cast<Window*>(targeter->FindTargetForEvent(child, event));
189 if (child_target_window)
190 return child_target_window;
191 }
192 target->ConvertEventToTarget(root_window, event);
193 }
194 return root_window->CanAcceptEvent(*event) ? root_window : nullptr;
157 } 195 }
158 196
159 } // namespace aura 197 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/window_targeter.h ('k') | ui/aura/window_targeter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698