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
3 // found in the LICENSE file.
4
5 #include "ui/aura/window_targeter.h"
6
7 #include "ui/aura/client/capture_client.h"
8 #include "ui/aura/client/event_client.h"
9 #include "ui/aura/client/focus_client.h"
10 #include "ui/aura/root_window.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_delegate.h"
13 #include "ui/events/event_target.h"
14
15 namespace aura {
16
WindowTargeter()17 WindowTargeter::WindowTargeter() {}
~WindowTargeter()18 WindowTargeter::~WindowTargeter() {}
19
FindTargetForEvent(ui::EventTarget * root,ui::Event * event)20 ui::EventTarget* WindowTargeter::FindTargetForEvent(ui::EventTarget* root,
21 ui::Event* event) {
22 if (event->IsKeyEvent()) {
23 Window* window = static_cast<Window*>(root);
24 Window* root_window = window->GetRootWindow();
25 const ui::KeyEvent& key = static_cast<const ui::KeyEvent&>(*event);
26 if (key.key_code() == ui::VKEY_UNKNOWN)
27 return NULL;
28 client::EventClient* event_client = client::GetEventClient(root_window);
29 client::FocusClient* focus_client = client::GetFocusClient(root_window);
30 Window* focused_window = focus_client->GetFocusedWindow();
31 if (event_client &&
32 !event_client->CanProcessEventsWithinSubtree(focused_window)) {
33 focus_client->FocusWindow(NULL);
34 return NULL;
35 }
36 return focused_window ? focused_window : window;
37 }
38 return EventTargeter::FindTargetForEvent(root, event);
39 }
40
SubtreeShouldBeExploredForEvent(ui::EventTarget * root,const ui::LocatedEvent & event)41 bool WindowTargeter::SubtreeShouldBeExploredForEvent(
42 ui::EventTarget* root,
43 const ui::LocatedEvent& event) {
44 Window* window = static_cast<Window*>(root);
45 if (!window->IsVisible())
46 return false;
47 if (window->ignore_events())
48 return false;
49 client::EventClient* client = client::GetEventClient(window->GetRootWindow());
50 if (client && !client->CanProcessEventsWithinSubtree(window))
51 return false;
52
53 Window* parent = window->parent();
54 if (parent && parent->delegate_ && !parent->delegate_->
55 ShouldDescendIntoChildForEventHandling(window, event.location())) {
56 return false;
57 }
58 return window->bounds().Contains(event.location());
59 }
60
FindTargetForLocatedEvent(ui::EventTarget * root,ui::LocatedEvent * event)61 ui::EventTarget* WindowTargeter::FindTargetForLocatedEvent(
62 ui::EventTarget* root,
63 ui::LocatedEvent* event) {
64 Window* window = static_cast<Window*>(root);
65 if (!window->parent()) {
66 Window* target = FindTargetInRootWindow(window, *event);
67 if (target) {
68 window->ConvertEventToTarget(target, event);
69 return target;
70 }
71 }
72 return EventTargeter::FindTargetForLocatedEvent(root, event);
73 }
74
FindTargetInRootWindow(Window * root_window,const ui::LocatedEvent & event)75 Window* WindowTargeter::FindTargetInRootWindow(Window* root_window,
76 const ui::LocatedEvent& event) {
77 DCHECK_EQ(root_window, root_window->GetRootWindow());
78
79 // Mouse events should be dispatched to the window that processed the
80 // mouse-press events (if any).
81 if (event.IsScrollEvent() || event.IsMouseEvent()) {
82 WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
83 if (dispatcher->mouse_pressed_handler())
84 return dispatcher->mouse_pressed_handler();
85 }
86
87 // All events should be directed towards the capture window (if any).
88 Window* capture_window = client::GetCaptureWindow(root_window);
89 if (capture_window)
90 return capture_window;
91
92 return NULL;
93 }
94
95 } // namespace aura
96