Get foreground tab on Android
[chromium-blink-merge.git] / ui / aura / root_window.h
blob3254f8f3abca9e8d4365d08aa6f8f9ad228e8b27
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_ROOT_WINDOW_H_
6 #define UI_AURA_ROOT_WINDOW_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "ui/aura/aura_export.h"
17 #include "ui/aura/client/capture_delegate.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/aura/window_tree_host_delegate.h"
20 #include "ui/base/cursor/cursor.h"
21 #include "ui/compositor/compositor.h"
22 #include "ui/compositor/layer_animation_observer.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/events/event_dispatcher.h"
25 #include "ui/events/gestures/gesture_recognizer.h"
26 #include "ui/events/gestures/gesture_types.h"
27 #include "ui/gfx/native_widget_types.h"
28 #include "ui/gfx/point.h"
29 #include "ui/gfx/transform.h"
31 namespace gfx {
32 class Size;
33 class Transform;
36 namespace ui {
37 class GestureEvent;
38 class GestureRecognizer;
39 class KeyEvent;
40 class LayerAnimationSequence;
41 class MouseEvent;
42 class ScrollEvent;
43 class TouchEvent;
44 class ViewProp;
47 namespace aura {
48 class RootWindow;
49 class RootWindowHost;
50 class RootWindowObserver;
51 class RootWindowTransformer;
52 class TestScreen;
54 // RootWindow is responsible for hosting a set of windows.
55 class AURA_EXPORT RootWindow : public ui::EventDispatcherDelegate,
56 public ui::GestureEventHelper,
57 public ui::LayerAnimationObserver,
58 public aura::client::CaptureDelegate,
59 public aura::RootWindowHostDelegate {
60 public:
61 struct AURA_EXPORT CreateParams {
62 // CreateParams with initial_bounds and default host in pixel.
63 explicit CreateParams(const gfx::Rect& initial_bounds);
64 ~CreateParams() {}
66 gfx::Rect initial_bounds;
68 // A host to use in place of the default one that RootWindow will create.
69 // NULL by default.
70 RootWindowHost* host;
73 explicit RootWindow(const CreateParams& params);
74 virtual ~RootWindow();
76 // Returns the RootWindowHost for the specified accelerated widget, or NULL
77 // if there is none associated.
78 static RootWindow* GetForAcceleratedWidget(gfx::AcceleratedWidget widget);
80 Window* window() {
81 return const_cast<Window*>(const_cast<const RootWindow*>(this)->window());
83 const Window* window() const { return window_.get(); }
84 RootWindowHost* host() {
85 return const_cast<RootWindowHost*>(
86 const_cast<const RootWindow*>(this)->host());
88 const RootWindowHost* host() const { return host_.get(); }
89 ui::Compositor* compositor() { return compositor_.get(); }
90 gfx::NativeCursor last_cursor() const { return last_cursor_; }
91 Window* mouse_pressed_handler() { return mouse_pressed_handler_; }
92 Window* mouse_moved_handler() { return mouse_moved_handler_; }
94 // Initializes the root window.
95 void Init();
97 // Stop listening events in preparation for shutdown.
98 void PrepareForShutdown();
100 // Repost event for re-processing. Used when exiting context menus.
101 // We only support the ET_MOUSE_PRESSED and ET_GESTURE_TAP_DOWN event
102 // types (although the latter is currently a no-op).
103 void RepostEvent(const ui::LocatedEvent& event);
105 RootWindowHostDelegate* AsRootWindowHostDelegate();
107 // Gets/sets the size of the host window.
108 void SetHostSize(const gfx::Size& size_in_pixel);
110 // Sets the bounds of the host window.
111 void SetHostBounds(const gfx::Rect& size_in_pizel);
113 // Sets the currently-displayed cursor. If the cursor was previously hidden
114 // via ShowCursor(false), it will remain hidden until ShowCursor(true) is
115 // called, at which point the cursor that was last set via SetCursor() will be
116 // used.
117 void SetCursor(gfx::NativeCursor cursor);
119 // Invoked when the cursor's visibility has changed.
120 void OnCursorVisibilityChanged(bool visible);
122 // Invoked when the mouse events get enabled or disabled.
123 void OnMouseEventsEnableStateChanged(bool enabled);
125 // Moves the cursor to the specified location relative to the root window.
126 void MoveCursorTo(const gfx::Point& location);
128 // Moves the cursor to the |host_location| given in host coordinates.
129 void MoveCursorToHostLocation(const gfx::Point& host_location);
131 // Draw the damage_rect.
132 void ScheduleRedrawRect(const gfx::Rect& damage_rect);
134 // Returns a target window for the given gesture event.
135 Window* GetGestureTarget(ui::GestureEvent* event);
137 // Handles a gesture event. Returns true if handled. Unlike the other
138 // event-dispatching function (e.g. for touch/mouse/keyboard events), gesture
139 // events are dispatched from GestureRecognizer instead of RootWindowHost.
140 void DispatchGestureEvent(ui::GestureEvent* event);
142 // Invoked when |window| is being destroyed.
143 void OnWindowDestroying(Window* window);
145 // Invoked when |window|'s bounds have changed. |contained_mouse| indicates if
146 // the bounds before change contained the |last_moust_location()|.
147 void OnWindowBoundsChanged(Window* window, bool contained_mouse);
149 // Dispatches OnMouseExited to the |window| which is hiding if nessessary.
150 void DispatchMouseExitToHidingWindow(Window* window);
152 // Dispatches a ui::ET_MOUSE_EXITED event at |point|.
153 void DispatchMouseExitAtPoint(const gfx::Point& point);
155 // Invoked when |window|'s visibility has changed.
156 void OnWindowVisibilityChanged(Window* window, bool is_visible);
158 // Invoked when |window|'s tranfrom has changed. |contained_mouse|
159 // indicates if the bounds before change contained the
160 // |last_moust_location()|.
161 void OnWindowTransformed(Window* window, bool contained_mouse);
163 // Invoked when the keyboard mapping (in X11 terms: the mapping between
164 // keycodes and keysyms) has changed.
165 void OnKeyboardMappingChanged();
167 // The system windowing system has sent a request that we close our window.
168 void OnRootWindowHostCloseRequested();
170 // Add/remove observer. There is no need to remove the observer if
171 // the root window is being deleted. In particular, you SHOULD NOT remove
172 // in |WindowObserver::OnWindowDestroying| of the observer observing
173 // the root window because it is too late to remove it.
174 void AddRootWindowObserver(RootWindowObserver* observer);
175 void RemoveRootWindowObserver(RootWindowObserver* observer);
177 // Converts |point| from the root window's coordinate system to the
178 // host window's.
179 void ConvertPointToHost(gfx::Point* point) const;
181 // Converts |point| from the host window's coordinate system to the
182 // root window's.
183 void ConvertPointFromHost(gfx::Point* point) const;
185 // Gesture Recognition -------------------------------------------------------
187 // When a touch event is dispatched to a Window, it may want to process the
188 // touch event asynchronously. In such cases, the window should consume the
189 // event during the event dispatch. Once the event is properly processed, the
190 // window should let the RootWindow know about the result of the event
191 // processing, so that gesture events can be properly created and dispatched.
192 void ProcessedTouchEvent(ui::TouchEvent* event,
193 Window* window,
194 ui::EventResult result);
196 // These methods are used to defer the processing of mouse/touch events
197 // related to resize. A client (typically a RenderWidgetHostViewAura) can call
198 // HoldPointerMoves when an resize is initiated and then ReleasePointerMoves
199 // once the resize is completed.
201 // More than one hold can be invoked and each hold must be cancelled by a
202 // release before we resume normal operation.
203 void HoldPointerMoves();
204 void ReleasePointerMoves();
206 // Gets the last location seen in a mouse event in this root window's
207 // coordinates. This may return a point outside the root window's bounds.
208 gfx::Point GetLastMouseLocationInRoot() const;
210 void SetRootWindowTransformer(scoped_ptr<RootWindowTransformer> transformer);
211 gfx::Transform GetRootTransform() const;
213 void SetTransform(const gfx::Transform& transform);
215 private:
216 FRIEND_TEST_ALL_PREFIXES(RootWindowTest, KeepTranslatedEventInRoot);
218 friend class Window;
219 friend class TestScreen;
221 // The parameter for OnWindowHidden() to specify why window is hidden.
222 enum WindowHiddenReason {
223 WINDOW_DESTROYED, // Window is destroyed.
224 WINDOW_HIDDEN, // Window is hidden.
225 WINDOW_MOVING, // Window is temporarily marked as hidden due to move
226 // across root windows.
229 // Updates the event with the appropriate transform for the device scale
230 // factor. The RootWindowHostDelegate dispatches events in the physical pixel
231 // coordinate. But the event processing from RootWindow onwards happen in
232 // device-independent pixel coordinate. So it is necessary to update the event
233 // received from the host.
234 void TransformEventForDeviceScaleFactor(ui::LocatedEvent* event);
236 // Moves the cursor to the specified location. This method is internally used
237 // by MoveCursorTo() and MoveCursorToHostLocation().
238 void MoveCursorToInternal(const gfx::Point& root_location,
239 const gfx::Point& host_location);
241 // Dispatches the specified event type (intended for enter/exit) to the
242 // |mouse_moved_handler_|.
243 ui::EventDispatchDetails DispatchMouseEnterOrExit(
244 const ui::MouseEvent& event,
245 ui::EventType type) WARN_UNUSED_RESULT;
246 ui::EventDispatchDetails ProcessEvent(Window* target,
247 ui::Event* event) WARN_UNUSED_RESULT;
248 ui::EventDispatchDetails ProcessGestures(
249 ui::GestureRecognizer::Gestures* gestures) WARN_UNUSED_RESULT;
251 // Called when a Window is attached or detached from the RootWindow.
252 void OnWindowAddedToRootWindow(Window* window);
253 void OnWindowRemovedFromRootWindow(Window* window, Window* new_root);
255 // Called when a window becomes invisible, either by being removed
256 // from root window hierarchy, via SetVisible(false) or being destroyed.
257 // |reason| specifies what triggered the hiding.
258 void OnWindowHidden(Window* invisible, WindowHiddenReason reason);
260 // Cleans up the gesture recognizer for all windows in |window| (including
261 // |window| itself).
262 void CleanupGestureRecognizerState(Window* window);
264 // Updates the root window's size using |host_size|, current
265 // transform and insets.
266 void UpdateRootWindowSize(const gfx::Size& host_size);
268 // Overridden from aura::client::CaptureDelegate:
269 virtual void UpdateCapture(Window* old_capture, Window* new_capture) OVERRIDE;
270 virtual void OnOtherRootGotCapture() OVERRIDE;
271 virtual void SetNativeCapture() OVERRIDE;
272 virtual void ReleaseNativeCapture() OVERRIDE;
274 // Overridden from ui::EventDispatcherDelegate.
275 virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE;
277 // Overridden from ui::GestureEventHelper.
278 virtual bool CanDispatchToConsumer(ui::GestureConsumer* consumer) OVERRIDE;
279 virtual void DispatchPostponedGestureEvent(ui::GestureEvent* event) OVERRIDE;
280 virtual void DispatchCancelTouchEvent(ui::TouchEvent* event) OVERRIDE;
282 // Overridden from ui::LayerAnimationObserver:
283 virtual void OnLayerAnimationEnded(
284 ui::LayerAnimationSequence* animation) OVERRIDE;
285 virtual void OnLayerAnimationScheduled(
286 ui::LayerAnimationSequence* animation) OVERRIDE;
287 virtual void OnLayerAnimationAborted(
288 ui::LayerAnimationSequence* animation) OVERRIDE;
290 // Overridden from aura::RootWindowHostDelegate:
291 virtual bool OnHostKeyEvent(ui::KeyEvent* event) OVERRIDE;
292 virtual bool OnHostMouseEvent(ui::MouseEvent* event) OVERRIDE;
293 virtual bool OnHostScrollEvent(ui::ScrollEvent* event) OVERRIDE;
294 virtual bool OnHostTouchEvent(ui::TouchEvent* event) OVERRIDE;
295 virtual void OnHostCancelMode() OVERRIDE;
296 virtual void OnHostActivated() OVERRIDE;
297 virtual void OnHostLostWindowCapture() OVERRIDE;
298 virtual void OnHostLostMouseGrab() OVERRIDE;
299 virtual void OnHostPaint(const gfx::Rect& damage_rect) OVERRIDE;
300 virtual void OnHostMoved(const gfx::Point& origin) OVERRIDE;
301 virtual void OnHostResized(const gfx::Size& size) OVERRIDE;
302 virtual float GetDeviceScaleFactor() OVERRIDE;
303 virtual RootWindow* AsRootWindow() OVERRIDE;
304 virtual const RootWindow* AsRootWindow() const OVERRIDE;
306 ui::EventDispatchDetails OnHostMouseEventImpl(ui::MouseEvent* event)
307 WARN_UNUSED_RESULT;
309 // We hold and aggregate mouse drags and touch moves as a way of throttling
310 // resizes when HoldMouseMoves() is called. The following methods are used to
311 // dispatch held and newly incoming mouse and touch events, typically when an
312 // event other than one of these needs dispatching or a matching
313 // ReleaseMouseMoves()/ReleaseTouchMoves() is called. NOTE: because these
314 // methods dispatch events from RootWindowHost the coordinates are in terms of
315 // the root.
316 ui::EventDispatchDetails DispatchMouseEventImpl(ui::MouseEvent* event)
317 WARN_UNUSED_RESULT;
318 ui::EventDispatchDetails DispatchMouseEventRepost(ui::MouseEvent* event)
319 WARN_UNUSED_RESULT;
320 ui::EventDispatchDetails DispatchMouseEventToTarget(ui::MouseEvent* event,
321 Window* target)
322 WARN_UNUSED_RESULT;
323 ui::EventDispatchDetails DispatchTouchEventImpl(ui::TouchEvent* event)
324 WARN_UNUSED_RESULT;
325 ui::EventDispatchDetails DispatchHeldEvents() WARN_UNUSED_RESULT;
326 // Creates and dispatches synthesized mouse move event using the
327 // current mouse location.
328 ui::EventDispatchDetails SynthesizeMouseMoveEvent() WARN_UNUSED_RESULT;
330 void DispatchHeldEventsAsync();
331 void SynthesizeMouseMoveEventAsync();
333 // Posts a task to send synthesized mouse move event if there
334 // is no a pending task.
335 void PostMouseMoveEventAfterWindowChange();
337 gfx::Transform GetInverseRootTransform() const;
339 // TODO(beng): evaluate the ideal ownership model.
340 scoped_ptr<Window> window_;
342 scoped_ptr<ui::Compositor> compositor_;
344 scoped_ptr<RootWindowHost> host_;
346 // Touch ids that are currently down.
347 uint32 touch_ids_down_;
349 // Last cursor set. Used for testing.
350 gfx::NativeCursor last_cursor_;
352 ObserverList<RootWindowObserver> observers_;
354 Window* mouse_pressed_handler_;
355 Window* mouse_moved_handler_;
356 Window* event_dispatch_target_;
358 bool synthesize_mouse_move_;
359 bool waiting_on_compositing_end_;
360 bool draw_on_compositing_end_;
362 bool defer_draw_scheduling_;
364 // How many move holds are outstanding. We try to defer dispatching
365 // touch/mouse moves while the count is > 0.
366 int move_hold_count_;
367 scoped_ptr<ui::LocatedEvent> held_move_event_;
369 // Allowing for reposting of events. Used when exiting context menus.
370 scoped_ptr<ui::LocatedEvent> held_repostable_event_;
372 scoped_ptr<ui::ViewProp> prop_;
374 scoped_ptr<RootWindowTransformer> transformer_;
376 // Used to schedule reposting an event.
377 base::WeakPtrFactory<RootWindow> repost_event_factory_;
379 // Used to schedule DispatchHeldEvents() when |move_hold_count_| goes to 0.
380 base::WeakPtrFactory<RootWindow> held_event_factory_;
382 DISALLOW_COPY_AND_ASSIGN(RootWindow);
385 } // namespace aura
387 #endif // UI_AURA_ROOT_WINDOW_H_