1 // Copyright 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.
6 #include "ash/shell_factory.h"
7 #include "ash/wm/coordinate_conversion.h"
8 #include "ash/wm/window_properties.h"
9 #include "ui/aura/client/capture_client.h"
10 #include "ui/aura/client/screen_position_client.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/test/ui_controls_factory_aura.h"
13 #include "ui/aura/window_property.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/base/test/ui_controls.h"
16 #include "ui/base/test/ui_controls_aura.h"
17 #include "ui/gfx/screen.h"
19 DECLARE_WINDOW_PROPERTY_TYPE(ui_controls::UIControlsAura
*)
25 using ui_controls::UIControlsAura
;
26 using ui_controls::MouseButton
;
28 DEFINE_OWNED_WINDOW_PROPERTY_KEY(UIControlsAura
, kUIControlsKey
, NULL
);
30 // Returns the UIControls object for RootWindow.
31 // kUIControlsKey is owned property and UIControls object
32 // will be deleted when the root window is deleted.
33 UIControlsAura
* GetUIControlsForRootWindow(aura::Window
* root_window
) {
34 UIControlsAura
* native_ui_control
=
35 root_window
->GetProperty(kUIControlsKey
);
36 if (!native_ui_control
) {
38 aura::test::CreateUIControlsAura(root_window
->GetHost());
39 // Pass the ownership to the |root_window|.
40 root_window
->SetProperty(kUIControlsKey
, native_ui_control
);
42 return native_ui_control
;
45 // Returns the UIControls object for the RootWindow at |point_in_screen|.
46 UIControlsAura
* GetUIControlsAt(const gfx::Point
& point_in_screen
) {
47 // TODO(mazda): Support the case passive grab is taken.
48 return GetUIControlsForRootWindow(ash::wm::GetRootWindowAt(point_in_screen
));
53 class UIControlsAsh
: public UIControlsAura
{
57 ~UIControlsAsh() override
{}
59 // UIControslAura overrides:
60 bool SendKeyPress(gfx::NativeWindow window
,
65 bool command
) override
{
66 return SendKeyPressNotifyWhenDone(
67 window
, key
, control
, shift
, alt
, command
, base::Closure());
70 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window
,
76 const base::Closure
& closure
) override
{
78 window
? window
->GetRootWindow() : ash::Shell::GetTargetRootWindow();
79 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(root
);
80 return ui_controls
&& ui_controls
->SendKeyPressNotifyWhenDone(
81 window
, key
, control
, shift
, alt
, command
, closure
);
84 bool SendMouseMove(long x
, long y
) override
{
86 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
87 return ui_controls
&& ui_controls
->SendMouseMove(p
.x(), p
.y());
90 bool SendMouseMoveNotifyWhenDone(long x
,
92 const base::Closure
& closure
) override
{
94 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
96 ui_controls
->SendMouseMoveNotifyWhenDone(p
.x(), p
.y(), closure
);
99 bool SendMouseEvents(MouseButton type
, int state
) override
{
100 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
101 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
102 return ui_controls
&& ui_controls
->SendMouseEvents(type
, state
);
105 bool SendMouseEventsNotifyWhenDone(MouseButton type
,
107 const base::Closure
& closure
) override
{
108 gfx::Point
p(aura::Env::GetInstance()->last_mouse_location());
109 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
110 return ui_controls
&& ui_controls
->SendMouseEventsNotifyWhenDone(
111 type
, state
, closure
);
114 bool SendMouseClick(MouseButton type
) override
{
115 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
116 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
117 return ui_controls
&& ui_controls
->SendMouseClick(type
);
120 void RunClosureAfterAllPendingUIEvents(
121 const base::Closure
& closure
) override
{
122 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(
123 ash::Shell::GetTargetRootWindow());
125 ui_controls
->RunClosureAfterAllPendingUIEvents(closure
);
129 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh
);
132 ui_controls::UIControlsAura
* CreateAshUIControls() {
133 return new ash::test::UIControlsAsh();