1 // Copyright (c) 2012 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.
5 #ifndef UI_AURA_WINDOW_TREE_HOST_H_
6 #define UI_AURA_WINDOW_TREE_HOST_H_
10 #include "base/event_types.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ui/aura/aura_export.h"
13 #include "ui/base/cursor/cursor.h"
14 #include "ui/base/ime/input_method_delegate.h"
15 #include "ui/events/event_source.h"
16 #include "ui/gfx/native_widget_types.h"
35 class WindowTreeHostTestApi
;
38 class WindowEventDispatcher
;
39 class WindowTreeHostObserver
;
41 // WindowTreeHost bridges between a native window and the embedded RootWindow.
42 // It provides the accelerated widget and maps events from the native os to
44 class AURA_EXPORT WindowTreeHost
: public ui::internal::InputMethodDelegate
,
45 public ui::EventSource
{
47 ~WindowTreeHost() override
;
49 // Creates a new WindowTreeHost. The caller owns the returned value.
50 static WindowTreeHost
* Create(const gfx::Rect
& bounds
);
52 // Returns the WindowTreeHost for the specified accelerated widget, or NULL
53 // if there is none associated.
54 static WindowTreeHost
* GetForAcceleratedWidget(gfx::AcceleratedWidget widget
);
58 void InitCompositor();
60 void AddObserver(WindowTreeHostObserver
* observer
);
61 void RemoveObserver(WindowTreeHostObserver
* observer
);
63 Window
* window() { return window_
; }
64 const Window
* window() const { return window_
; }
66 ui::EventProcessor
* event_processor();
68 WindowEventDispatcher
* dispatcher() {
69 return const_cast<WindowEventDispatcher
*>(
70 const_cast<const WindowTreeHost
*>(this)->dispatcher());
72 const WindowEventDispatcher
* dispatcher() const { return dispatcher_
.get(); }
74 ui::Compositor
* compositor() { return compositor_
.get(); }
76 // Gets/Sets the root window's transform.
77 virtual gfx::Transform
GetRootTransform() const;
78 virtual void SetRootTransform(const gfx::Transform
& transform
);
79 virtual gfx::Transform
GetInverseRootTransform() const;
81 // Updates the root window's size using |host_size|, current
82 // transform and insets.
83 virtual void UpdateRootWindowSize(const gfx::Size
& host_size
);
85 // Returns the actual size of the screen.
86 // (gfx::Screen only reports on the virtual desktop exposed by Aura.)
87 static gfx::Size
GetNativeScreenSize();
89 // Converts |point| from the root window's coordinate system to native
91 void ConvertPointToNativeScreen(gfx::Point
* point
) const;
93 // Converts |point| from native screen coordinate system to the root window's.
94 void ConvertPointFromNativeScreen(gfx::Point
* point
) const;
96 // Converts |point| from the root window's coordinate system to the
98 void ConvertPointToHost(gfx::Point
* point
) const;
100 // Converts |point| from the host window's coordinate system to the
102 void ConvertPointFromHost(gfx::Point
* point
) const;
105 // Sets the currently-displayed cursor. If the cursor was previously hidden
106 // via ShowCursor(false), it will remain hidden until ShowCursor(true) is
107 // called, at which point the cursor that was last set via SetCursor() will be
109 void SetCursor(gfx::NativeCursor cursor
);
111 // Invoked when the cursor's visibility has changed.
112 void OnCursorVisibilityChanged(bool visible
);
114 // Moves the cursor to the specified location relative to the root window.
115 void MoveCursorTo(const gfx::Point
& location
);
117 // Moves the cursor to the |host_location| given in host coordinates.
118 void MoveCursorToHostLocation(const gfx::Point
& host_location
);
120 gfx::NativeCursor
last_cursor() const { return last_cursor_
; }
122 // Gets the InputMethod instance, if NULL, creates & owns it.
123 ui::InputMethod
* GetInputMethod();
125 // Sets a shared unowned InputMethod. This is used when there is a singleton
126 // InputMethod shared between multiple WindowTreeHost instances.
128 // This is used for Ash only. There are 2 reasons:
129 // 1) ChromeOS virtual keyboard needs to receive ShowImeIfNeeded notification
130 // from InputMethod. Multiple InputMethod instances makes it hard to
131 // register/unregister the observer for that notification.
132 // 2) For Ozone, there is no native focus state for the root window and
133 // WindowTreeHost. See DrmWindowHost::CanDispatchEvent, the key events always
134 // goes to the primary WindowTreeHost. And after InputMethod processed the key
135 // event and continue dispatching it, WindowTargeter::FindTargetForEvent may
136 // re-dispatch it to a different WindowTreeHost. So the singleton InputMethod
137 // can make sure the correct InputMethod instance processes the key event no
138 // matter which WindowTreeHost is the target for event. Please refer to the
139 // test: ExtendedDesktopTest.KeyEventsOnLockScreen.
141 // TODO(shuchen): remove this method after above reasons become invalid.
142 // A possible solution is to make sure DrmWindowHost can find the correct
143 // WindowTreeHost to dispatch events.
144 void SetSharedInputMethod(ui::InputMethod
* input_method
);
146 // Overridden from ui::internal::InputMethodDelegate:
147 bool DispatchKeyEventPostIME(const ui::KeyEvent
& event
) override
;
149 // Returns the EventSource responsible for dispatching events to the window
151 virtual ui::EventSource
* GetEventSource() = 0;
153 // Returns the accelerated widget.
154 virtual gfx::AcceleratedWidget
GetAcceleratedWidget() = 0;
156 // Shows the WindowTreeHost.
159 // Hides the WindowTreeHost.
162 // Gets/Sets the size of the WindowTreeHost (in pixels).
163 virtual gfx::Rect
GetBounds() const = 0;
164 virtual void SetBounds(const gfx::Rect
& bounds_in_pixels
) = 0;
166 // Sets the OS capture to the root window.
167 virtual void SetCapture() = 0;
169 // Releases OS capture of the root window.
170 virtual void ReleaseCapture() = 0;
173 friend class TestScreen
; // TODO(beng): see if we can remove/consolidate.
176 void DestroyCompositor();
177 void DestroyDispatcher();
179 void CreateCompositor(gfx::AcceleratedWidget accelerated_widget
);
181 // Returns the location of the RootWindow on native screen.
182 virtual gfx::Point
GetLocationOnNativeScreen() const = 0;
184 void OnHostMoved(const gfx::Point
& new_location
);
185 void OnHostResized(const gfx::Size
& new_size
);
186 void OnHostCloseRequested();
187 void OnHostActivated();
188 void OnHostLostWindowCapture();
190 // Sets the currently displayed cursor.
191 virtual void SetCursorNative(gfx::NativeCursor cursor
) = 0;
193 // Moves the cursor to the specified location relative to the root window.
194 virtual void MoveCursorToNative(const gfx::Point
& location
) = 0;
196 // kCalled when the cursor visibility has changed.
197 virtual void OnCursorVisibilityChangedNative(bool show
) = 0;
199 // Shows the WindowTreeHost.
200 virtual void ShowImpl() = 0;
202 // Hides the WindowTreeHost.
203 virtual void HideImpl() = 0;
205 // Overridden from ui::EventSource:
206 ui::EventProcessor
* GetEventProcessor() override
;
207 ui::EventDispatchDetails
DeliverEventToProcessor(ui::Event
* event
) override
;
210 friend class test::WindowTreeHostTestApi
;
212 // Moves the cursor to the specified location. This method is internally used
213 // by MoveCursorTo() and MoveCursorToHostLocation().
214 void MoveCursorToInternal(const gfx::Point
& root_location
,
215 const gfx::Point
& host_location
);
217 // We don't use a scoped_ptr for |window_| since we need this ptr to be valid
218 // during its deletion. (Window's dtor notifies observers that may attempt to
219 // reach back up to access this object which will be valid until the end of
221 Window
* window_
; // Owning.
223 base::ObserverList
<WindowTreeHostObserver
> observers_
;
225 scoped_ptr
<WindowEventDispatcher
> dispatcher_
;
227 scoped_ptr
<ui::Compositor
> compositor_
;
229 // Last cursor set. Used for testing.
230 gfx::NativeCursor last_cursor_
;
231 gfx::Point last_cursor_request_position_in_host_
;
233 scoped_ptr
<ui::ViewProp
> prop_
;
235 // The InputMethod instance used to process key events.
236 // If owned it, it is created in GetInputMethod() method;
237 // If not owned it, it is passed in through SetSharedInputMethod() method.
238 ui::InputMethod
* input_method_
;
240 // Whether the InputMethod instance is owned by this WindowTreeHost.
241 bool owned_input_method_
;
243 DISALLOW_COPY_AND_ASSIGN(WindowTreeHost
);
248 #endif // UI_AURA_WINDOW_TREE_HOST_H_