event_generator_delegate_aura.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
3// found in the LICENSE file.
4
5#include "ui/aura/test/event_generator_delegate_aura.h"
6
7#include "ui/aura/client/screen_position_client.h"
8#include "ui/aura/window_event_dispatcher.h"
9#include "ui/aura/window_tree_host.h"
10
11namespace aura {
12namespace test {
13namespace {
14
15class DefaultEventGeneratorDelegate : public EventGeneratorDelegateAura {
16 public:
17  explicit DefaultEventGeneratorDelegate(Window* root_window)
18      : root_window_(root_window) {}
19  virtual ~DefaultEventGeneratorDelegate() {}
20
21  // EventGeneratorDelegateAura overrides:
22  virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const OVERRIDE {
23    return root_window_->GetHost();
24  }
25
26  virtual client::ScreenPositionClient* GetScreenPositionClient(
27      const aura::Window* window) const OVERRIDE {
28    return NULL;
29  }
30
31 private:
32  Window* root_window_;
33
34  DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate);
35};
36
37const Window* WindowFromTarget(const ui::EventTarget* event_target) {
38  return static_cast<const Window*>(event_target);
39}
40
41}  // namespace
42
43EventGeneratorDelegateAura::EventGeneratorDelegateAura() {
44}
45
46EventGeneratorDelegateAura::~EventGeneratorDelegateAura() {
47}
48
49ui::EventTarget* EventGeneratorDelegateAura::GetTargetAt(
50    const gfx::Point& location) {
51  return GetHostAt(location)->window();
52}
53
54ui::EventSource* EventGeneratorDelegateAura::GetEventSource(
55    ui::EventTarget* target) {
56  return static_cast<Window*>(target)->GetHost()->GetEventSource();
57}
58
59gfx::Point EventGeneratorDelegateAura::CenterOfTarget(
60    const ui::EventTarget* target) const {
61  gfx::Point center =
62      gfx::Rect(WindowFromTarget(target)->bounds().size()).CenterPoint();
63  ConvertPointFromTarget(target, &center);
64  return center;
65}
66
67gfx::Point EventGeneratorDelegateAura::CenterOfWindow(
68    gfx::NativeWindow window) const {
69  return CenterOfTarget(window);
70}
71
72void EventGeneratorDelegateAura::ConvertPointFromTarget(
73    const ui::EventTarget* event_target,
74    gfx::Point* point) const {
75  DCHECK(point);
76  const Window* target = WindowFromTarget(event_target);
77  aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
78  if (client)
79    client->ConvertPointToScreen(target, point);
80  else
81    aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point);
82}
83
84void EventGeneratorDelegateAura::ConvertPointToTarget(
85    const ui::EventTarget* event_target,
86    gfx::Point* point) const {
87  DCHECK(point);
88  const Window* target = WindowFromTarget(event_target);
89  aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
90  if (client)
91    client->ConvertPointFromScreen(target, point);
92  else
93    aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point);
94}
95
96void EventGeneratorDelegateAura::ConvertPointFromHost(
97    const ui::EventTarget* hosted_target,
98    gfx::Point* point) const {
99  const Window* window = WindowFromTarget(hosted_target);
100  window->GetHost()->ConvertPointFromHost(point);
101}
102
103}  // namespace test
104}  // namespace aura
105
106namespace ui {
107namespace test {
108
109// static
110EventGeneratorDelegate* EventGenerator::CreateDefaultPlatformDelegate(
111    EventGenerator* owner,
112    gfx::NativeWindow root_window,
113    gfx::NativeWindow window) {
114  return new aura::test::DefaultEventGeneratorDelegate(root_window);
115}
116
117}  // namespace test
118}  // namespace ui
119