blob: 896af448ae4f4ebf658c1421a094fe1be1221cec [file] [log] [blame]
[email protected]e0d22e82012-01-04 00:46:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]81585f32011-07-29 19:32:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]99f07e02011-12-07 00:02:595#ifndef UI_AURA_ROOT_WINDOW_H_
6#define UI_AURA_ROOT_WINDOW_H_
[email protected]81585f32011-07-29 19:32:067#pragma once
8
[email protected]81585f32011-07-29 19:32:069#include "base/basictypes.h"
[email protected]8d8953c2012-03-23 23:19:1010#include "base/memory/ref_counted.h"
[email protected]82a4e9092012-02-12 18:09:3711#include "base/memory/scoped_ptr.h"
[email protected]f296be72011-10-11 15:40:0012#include "base/memory/weak_ptr.h"
[email protected]35e9b66a2011-10-06 18:19:2113#include "base/message_loop.h"
[email protected]f94f0f12011-09-14 21:14:0114#include "ui/aura/aura_export.h"
[email protected]114bfbd2011-10-18 21:20:2415#include "ui/aura/focus_manager.h"
[email protected]4aeda5792012-01-26 20:38:4216#include "ui/aura/gestures/gesture_recognizer.h"
[email protected]114bfbd2011-10-18 21:20:2417#include "ui/aura/window.h"
[email protected]c7839552012-04-03 21:14:3618#include "ui/base/cursor/cursor.h"
[email protected]7a05bdc2011-10-04 23:38:3919#include "ui/base/events.h"
[email protected]905e19a2011-09-07 02:17:5320#include "ui/gfx/compositor/compositor.h"
[email protected]894e8fc2012-02-24 13:29:5021#include "ui/gfx/compositor/compositor_observer.h"
[email protected]e876c272011-11-02 16:42:4522#include "ui/gfx/compositor/layer_animation_observer.h"
[email protected]81585f32011-07-29 19:32:0623#include "ui/gfx/native_widget_types.h"
[email protected]896728e2011-10-14 00:41:2624#include "ui/gfx/point.h"
[email protected]81585f32011-07-29 19:32:0625
26namespace gfx {
27class Size;
28}
29
[email protected]593ddfa2011-10-20 21:51:4330namespace ui {
[email protected]fe7074c62011-10-28 15:22:5431class LayerAnimationSequence;
[email protected]593ddfa2011-10-20 21:51:4332class Transform;
33}
34
[email protected]81585f32011-07-29 19:32:0635namespace aura {
36
[email protected]8d8953c2012-03-23 23:19:1037class RootWindow;
[email protected]99f07e02011-12-07 00:02:5938class RootWindowHost;
39class RootWindowObserver;
[email protected]5951806f2011-10-17 16:06:3540class KeyEvent;
[email protected]a83f0f22011-08-23 15:39:1541class MouseEvent;
[email protected]aca200e2011-12-15 17:52:0242class StackingClient;
43class ScrollEvent;
[email protected]5951806f2011-10-17 16:06:3544class TouchEvent;
[email protected]538f9562012-01-17 17:46:0445class GestureEvent;
[email protected]b1b155512011-08-18 22:47:5046
[email protected]8d8953c2012-03-23 23:19:1047// This class represents a lock on the compositor, that can be used to prevent a
48// compositing pass from happening while we're waiting for an asynchronous
49// event. The typical use case is when waiting for a renderer to produce a frame
50// at the right size. The caller keeps a reference on this object, and drops the
51// reference once it desires to release the lock.
52// Note however that the lock is canceled after a short timeout to ensure
53// responsiveness of the UI, so the compositor tree should be kept in a
54// "reasonable" state while the lock is held.
55// Don't instantiate this class directly, use RootWindow::GetCompositorLock.
56class AURA_EXPORT CompositorLock :
57 public base::RefCounted<CompositorLock>,
58 public base::SupportsWeakPtr<CompositorLock> {
59 public:
60 ~CompositorLock();
61
62 private:
63 friend class RootWindow;
64
65 CompositorLock(RootWindow* root_window);
66 void CancelLock();
67
68 RootWindow* root_window_;
69 DISALLOW_COPY_AND_ASSIGN(CompositorLock);
70};
71
[email protected]99f07e02011-12-07 00:02:5972// RootWindow is responsible for hosting a set of windows.
73class AURA_EXPORT RootWindow : public ui::CompositorDelegate,
[email protected]894e8fc2012-02-24 13:29:5074 public ui::CompositorObserver,
[email protected]99f07e02011-12-07 00:02:5975 public Window,
76 public internal::FocusManager,
77 public ui::LayerAnimationObserver {
[email protected]81585f32011-07-29 19:32:0678 public:
[email protected]e050ef142012-03-21 01:04:2479 explicit RootWindow(const gfx::Rect& initial_bounds);
[email protected]58482fa2012-03-02 14:57:3980 virtual ~RootWindow();
[email protected]9ed496e2011-10-18 23:47:3981
[email protected]627a62f2012-02-14 22:59:1482 static void set_hide_host_cursor(bool hide) {
83 hide_host_cursor_ = hide;
84 }
85 static bool hide_host_cursor() {
86 return hide_host_cursor_;
87 }
88
[email protected]9ed496e2011-10-18 23:47:3989 ui::Compositor* compositor() { return compositor_.get(); }
[email protected]896728e2011-10-14 00:41:2690 gfx::Point last_mouse_location() const { return last_mouse_location_; }
[email protected]912be0e2011-11-09 19:05:2491 gfx::NativeCursor last_cursor() const { return last_cursor_; }
[email protected]627a62f2012-02-14 22:59:1492 bool cursor_shown() const { return cursor_shown_; }
[email protected]9ed496e2011-10-18 23:47:3993 Window* mouse_pressed_handler() { return mouse_pressed_handler_; }
94 Window* capture_window() { return capture_window_; }
[email protected]09c69432012-03-16 16:23:2895 Window* focused_window() { return focused_window_; }
[email protected]87b0d82e2011-10-07 21:02:5996
[email protected]99f07e02011-12-07 00:02:5997 // Shows the root window host.
98 void ShowRootWindow();
[email protected]970aa362011-08-30 20:03:3499
[email protected]99f07e02011-12-07 00:02:59100 // Sets the size of the root window.
[email protected]114bfbd2011-10-18 21:20:24101 void SetHostSize(const gfx::Size& size);
102 gfx::Size GetHostSize() const;
[email protected]970aa362011-08-30 20:03:34103
[email protected]e050ef142012-03-21 01:04:24104 // Sets the bounds of the host window.
105 void SetHostBounds(const gfx::Rect& size);
106
[email protected]627a62f2012-02-14 22:59:14107 // Sets the currently-displayed cursor. If the cursor was previously hidden
108 // via ShowCursor(false), it will remain hidden until ShowCursor(true) is
109 // called, at which point the cursor that was last set via SetCursor() will be
110 // used.
[email protected]0207c92d2011-10-04 14:45:07111 void SetCursor(gfx::NativeCursor cursor);
[email protected]70ccf702011-09-22 18:15:58112
[email protected]627a62f2012-02-14 22:59:14113 // Shows or hides the cursor.
[email protected]09a02fff2011-12-20 22:01:43114 void ShowCursor(bool show);
115
[email protected]5922cb22012-01-17 21:53:12116 // Moves the cursor to the specified location relative to the root window.
117 void MoveCursorTo(const gfx::Point& location);
118
[email protected]e4243262012-01-19 18:59:25119 // Clips the cursor movement to |capture_window_|. Should be invoked only
120 // after SetCapture(). ReleaseCapture() implicitly removes any confines set
121 // using this function. Returns true if successful.
122 bool ConfineCursorToWindow();
123
[email protected]81585f32011-07-29 19:32:06124 // Draws the necessary set of windows.
125 void Draw();
126
[email protected]7df588fbd2012-02-10 14:15:56127 // Draw the whole screen.
128 void ScheduleFullDraw();
129
[email protected]a83f0f22011-08-23 15:39:15130 // Handles a mouse event. Returns true if handled.
[email protected]593ddfa2011-10-20 21:51:43131 bool DispatchMouseEvent(MouseEvent* event);
[email protected]a83f0f22011-08-23 15:39:15132
[email protected]c94f8592011-09-02 20:12:13133 // Handles a key event. Returns true if handled.
[email protected]593ddfa2011-10-20 21:51:43134 bool DispatchKeyEvent(KeyEvent* event);
[email protected]c94f8592011-09-02 20:12:13135
[email protected]aca200e2011-12-15 17:52:02136 // Handles a scroll event. Returns true if handled.
137 bool DispatchScrollEvent(ScrollEvent* event);
138
[email protected]5951806f2011-10-17 16:06:35139 // Handles a touch event. Returns true if handled.
[email protected]593ddfa2011-10-20 21:51:43140 bool DispatchTouchEvent(TouchEvent* event);
[email protected]5951806f2011-10-17 16:06:35141
[email protected]538f9562012-01-17 17:46:04142 // Handles a gesture event. Returns true if handled. Unlike the other
143 // event-dispatching function (e.g. for touch/mouse/keyboard events), gesture
144 // events are dispatched from GestureRecognizer instead of RootWindowHost.
145 bool DispatchGestureEvent(GestureEvent* event);
146
[email protected]7a9f8912011-09-12 21:31:31147 // Called when the host changes size.
148 void OnHostResized(const gfx::Size& size);
149
[email protected]114bfbd2011-10-18 21:20:24150 // Invoked when |window| is being destroyed.
[email protected]539a8af2012-01-27 20:12:14151 void OnWindowDestroying(Window* window);
152
[email protected]c4866112012-03-31 00:36:29153 // Invoked when |window|'s bounds have changed. |contained_mouse| indicates if
[email protected]539a8af2012-01-27 20:12:14154 // the bounds before change contained the |last_moust_location()|.
155 void OnWindowBoundsChanged(Window* window, bool contained_mouse);
156
[email protected]c4866112012-03-31 00:36:29157 // Invoked when |window|'s visibility is changed.
[email protected]539a8af2012-01-27 20:12:14158 void OnWindowVisibilityChanged(Window* window, bool is_visible);
159
[email protected]c4866112012-03-31 00:36:29160 // Invoked when |window|'s tranfrom has changed. |contained_mouse|
[email protected]539a8af2012-01-27 20:12:14161 // indicates if the bounds before change contained the
162 // |last_moust_location()|.
163 void OnWindowTransformed(Window* window, bool contained_mouse);
[email protected]b8b456ce2011-09-27 00:19:13164
[email protected]c4866112012-03-31 00:36:29165 // Invoked when the keyboard mapping (in X11 terms: the mapping between
166 // keycodes and keysyms) has changed.
167 void OnKeyboardMappingChanged();
168
[email protected]6eb10822012-03-27 03:34:10169 // Add/remove observer. There is no need to remove the observer if
170 // the root window is being deleted. In particular, you SHOULD NOT remove
171 // in |WindowObserver::OnWindowDestroying| of the observer observing
172 // the root window because it is too late to remove it.
[email protected]9fc206d2011-12-13 00:05:33173 void AddRootWindowObserver(RootWindowObserver* observer);
174 void RemoveRootWindowObserver(RootWindowObserver* observer);
[email protected]e717ffd2011-10-13 17:15:19175
[email protected]b5a18642011-11-18 22:45:36176 // Posts |native_event| to the platform's event queue.
177 void PostNativeEvent(const base::NativeEvent& native_event);
178
[email protected]99f07e02011-12-07 00:02:59179 // Converts |point| from the root window's coordinate system to native
180 // screen's.
[email protected]ace7ca02011-11-22 21:34:17181 void ConvertPointToNativeScreen(gfx::Point* point) const;
182
[email protected]4def0e692011-10-19 14:11:09183 // Capture -------------------------------------------------------------------
184
[email protected]114bfbd2011-10-18 21:20:24185 // Sets capture to the specified window.
186 void SetCapture(Window* window);
187
188 // If |window| has mouse capture, the current capture window is set to NULL.
189 void ReleaseCapture(Window* window);
190
[email protected]4aeda5792012-01-26 20:38:42191 // Gesture Recognition -------------------------------------------------------
192
193 // When a touch event is dispatched to a Window, it can notify the RootWindow
194 // to queue the touch event for asynchronous gesture recognition. These are
195 // the entry points for the asynchronous processing of the queued touch
196 // events.
197 // Process the next touch event for gesture recognition. |processed| indicates
198 // whether the queued event was processed by the window or not.
199 void AdvanceQueuedTouchEvent(Window* window, bool processed);
200
201 GestureRecognizer* gesture_recognizer() const {
202 return gesture_recognizer_.get();
203 }
204
205 // Provided only for testing:
206 void SetGestureRecognizerForTesting(GestureRecognizer* gr);
207
[email protected]3ae258d02012-03-05 23:50:13208 // Returns the accelerated widget from the RootWindowHost.
209 gfx::AcceleratedWidget GetAcceleratedWidget();
210
[email protected]745816be2011-11-22 05:08:30211#if !defined(NDEBUG)
212 // Toggles the host's full screen state.
213 void ToggleFullScreen();
214#endif
215
[email protected]308aaa32012-03-12 13:14:50216 // These methods are used to defer the processing of mouse events related
217 // to resize. A client (typically a RenderWidgetHostViewAura) can call
218 // HoldMouseMoves when an resize is initiated and then ReleaseMouseMoves
219 // once the resize is completed.
220 //
221 // More than one hold can be invoked and each hold must be cancelled by a
222 // release before we resume normal operation.
223 void HoldMouseMoves();
224 void ReleaseMouseMoves();
225
[email protected]8d8953c2012-03-23 23:19:10226 // Creates a compositor lock.
227 scoped_refptr<CompositorLock> GetCompositorLock();
228
[email protected]b635b722012-03-23 17:15:00229 // Sets if the window should be focused when shown.
230 void SetFocusWhenShown(bool focus_when_shown);
231
[email protected]54231f72012-01-31 21:07:26232 // Overridden from Window:
233 virtual RootWindow* GetRootWindow() OVERRIDE;
[email protected]e050ef142012-03-21 01:04:24234 virtual const RootWindow* GetRootWindow() const OVERRIDE;
[email protected]54231f72012-01-31 21:07:26235 virtual void SetTransform(const ui::Transform& transform) OVERRIDE;
236
[email protected]6e456882011-12-06 07:42:51237 // Overridden from ui::CompositorDelegate:
[email protected]e0d22e82012-01-04 00:46:57238 virtual void ScheduleDraw() OVERRIDE;
[email protected]6e456882011-12-06 07:42:51239
[email protected]894e8fc2012-02-24 13:29:50240 // Overridden from ui::CompositorObserver:
[email protected]a8f677c2012-03-23 01:36:06241 virtual void OnCompositingStarted(ui::Compositor*) OVERRIDE;
[email protected]894e8fc2012-02-24 13:29:50242 virtual void OnCompositingEnded(ui::Compositor*) OVERRIDE;
243
[email protected]72eb1cf2012-01-31 23:31:11244 private:
[email protected]16690b02012-02-01 04:06:35245 friend class Window;
[email protected]8d8953c2012-03-23 23:19:10246 friend class CompositorLock;
[email protected]16690b02012-02-01 04:06:35247
[email protected]114bfbd2011-10-18 21:20:24248 // Called whenever the mouse moves, tracks the current |mouse_moved_handler_|,
249 // sending exited and entered events as its value changes.
250 void HandleMouseMoved(const MouseEvent& event, Window* target);
251
[email protected]759017e02012-02-11 20:46:11252 // Called whenever the |capture_window_| changes.
253 // Sends capture changed events to event filters for old capture window.
254 void HandleMouseCaptureChanged(Window* old_capture_window);
255
[email protected]ea2a867a2011-10-26 17:41:33256 bool ProcessMouseEvent(Window* target, MouseEvent* event);
257 bool ProcessKeyEvent(Window* target, KeyEvent* event);
258 ui::TouchStatus ProcessTouchEvent(Window* target, TouchEvent* event);
[email protected]538f9562012-01-17 17:46:04259 ui::GestureStatus ProcessGestureEvent(Window* target, GestureEvent* event);
[email protected]4aeda5792012-01-26 20:38:42260 bool ProcessGestures(GestureRecognizer::Gestures* gestures);
[email protected]ea2a867a2011-10-26 17:41:33261
[email protected]16690b02012-02-01 04:06:35262 // Called when a Window is attached or detached from the RootWindow.
263 void OnWindowAddedToRootWindow(Window* window);
264 void OnWindowRemovedFromRootWindow(Window* window);
265
[email protected]a278e2602012-02-16 16:46:35266 // Called when a window becomes invisible, either by being removed
267 // from root window hierachy, via SetVisible(false) or being destroyed.
268 // |destroyed| is set to true when the window is being destroyed.
269 void OnWindowHidden(Window* invisible, bool destroyed);
270
[email protected]114bfbd2011-10-18 21:20:24271 // Overridden from Window:
272 virtual bool CanFocus() const OVERRIDE;
[email protected]1128681f2012-01-13 03:15:40273 virtual bool CanReceiveEvents() const OVERRIDE;
[email protected]114bfbd2011-10-18 21:20:24274 virtual internal::FocusManager* GetFocusManager() OVERRIDE;
[email protected]114bfbd2011-10-18 21:20:24275
[email protected]e876c272011-11-02 16:42:45276 // Overridden from ui::LayerAnimationObserver:
[email protected]fe7074c62011-10-28 15:22:54277 virtual void OnLayerAnimationEnded(
[email protected]306768162012-02-02 02:04:41278 ui::LayerAnimationSequence* animation) OVERRIDE;
[email protected]e876c272011-11-02 16:42:45279 virtual void OnLayerAnimationScheduled(
[email protected]306768162012-02-02 02:04:41280 ui::LayerAnimationSequence* animation) OVERRIDE;
[email protected]e876c272011-11-02 16:42:45281 virtual void OnLayerAnimationAborted(
[email protected]306768162012-02-02 02:04:41282 ui::LayerAnimationSequence* animation) OVERRIDE;
[email protected]326828b2011-10-21 02:01:10283
[email protected]114bfbd2011-10-18 21:20:24284 // Overridden from FocusManager:
[email protected]09c69432012-03-16 16:23:28285 virtual void SetFocusedWindow(Window* window,
286 const aura::Event* event) OVERRIDE;
[email protected]114bfbd2011-10-18 21:20:24287 virtual Window* GetFocusedWindow() OVERRIDE;
288 virtual bool IsFocusedWindow(const Window* window) const OVERRIDE;
289
[email protected]99f07e02011-12-07 00:02:59290 // Initializes the root window.
[email protected]9ed496e2011-10-18 23:47:39291 void Init();
[email protected]87b0d82e2011-10-07 21:02:59292
[email protected]8bbbb40d2012-03-23 01:49:23293 // We hold and aggregate mouse drags as a way of throttling resizes when
294 // HoldMouseMoves() is called. The following methods are used to dispatch held
295 // and newly incoming mouse events, typically when an event other than a mouse
296 // drag needs dispatching or a matching ReleaseMouseMoves() is called.
[email protected]308aaa32012-03-12 13:14:50297 bool DispatchMouseEventImpl(MouseEvent* event);
298 void DispatchHeldMouseMove();
299
[email protected]4a14bca92011-10-21 00:09:22300 // Parses the switch describing the initial size for the host window and
301 // returns bounds for the window.
302 gfx::Rect GetInitialHostWindowBounds() const;
303
[email protected]539a8af2012-01-27 20:12:14304 // Posts a task to send synthesized mouse move event if there
305 // is no a pending task.
306 void PostMouseMoveEventAfterWindowChange();
307
308 // Creates and dispatches synthesized mouse move event using the
309 // current mouse location.
310 void SynthesizeMouseMoveEvent();
311
[email protected]8d8953c2012-03-23 23:19:10312 // Called by CompositorLock.
313 void UnlockCompositor();
314
[email protected]82a4e9092012-02-12 18:09:37315 scoped_ptr<ui::Compositor> compositor_;
[email protected]81585f32011-07-29 19:32:06316
[email protected]99f07e02011-12-07 00:02:59317 scoped_ptr<RootWindowHost> host_;
[email protected]8d8c7732011-08-25 22:35:13318
[email protected]627a62f2012-02-14 22:59:14319 // If set before the RootWindow is created, the cursor will be drawn within
320 // the Aura root window but hidden outside of it, and it'll remain hidden
321 // after the Aura window is closed.
322 static bool hide_host_cursor_;
323
[email protected]5978af5e2011-10-24 22:17:18324 // Used to schedule painting.
[email protected]99f07e02011-12-07 00:02:59325 base::WeakPtrFactory<RootWindow> schedule_paint_factory_;
[email protected]5978af5e2011-10-24 22:17:18326
[email protected]539a8af2012-01-27 20:12:14327 // Use to post mouse move event.
328 base::WeakPtrFactory<RootWindow> event_factory_;
329
[email protected]896728e2011-10-14 00:41:26330 // Last location seen in a mouse event.
331 gfx::Point last_mouse_location_;
332
[email protected]bf74dd82011-11-11 21:36:41333 // ui::EventFlags containing the current state of the mouse buttons.
334 int mouse_button_flags_;
335
[email protected]912be0e2011-11-09 19:05:24336 // Last cursor set. Used for testing.
337 gfx::NativeCursor last_cursor_;
338
[email protected]627a62f2012-02-14 22:59:14339 // Is the cursor currently shown? Used for testing.
340 bool cursor_shown_;
341
[email protected]99f07e02011-12-07 00:02:59342 ObserverList<RootWindowObserver> observers_;
[email protected]e717ffd2011-10-13 17:15:19343
[email protected]114bfbd2011-10-18 21:20:24344 // The capture window. When not-null, this window receives all the mouse and
345 // touch events.
346 Window* capture_window_;
347
348 Window* mouse_pressed_handler_;
349 Window* mouse_moved_handler_;
350 Window* focused_window_;
[email protected]912b6f32012-01-19 00:48:01351
352 // The gesture_recognizer_ for this.
[email protected]b9457352012-01-19 16:31:06353 scoped_ptr<GestureRecognizer> gesture_recognizer_;
[email protected]114bfbd2011-10-18 21:20:24354
[email protected]539a8af2012-01-27 20:12:14355 bool synthesize_mouse_move_;
[email protected]894e8fc2012-02-24 13:29:50356 bool waiting_on_compositing_end_;
357 bool draw_on_compositing_end_;
[email protected]539a8af2012-01-27 20:12:14358
[email protected]308aaa32012-03-12 13:14:50359 bool defer_draw_scheduling_;
360
361 // How many holds are outstanding. We try to defer dispatching mouse moves
362 // while the count is > 0.
363 int mouse_move_hold_count_;
364 bool should_hold_mouse_moves_;
[email protected]308aaa32012-03-12 13:14:50365 scoped_ptr<MouseEvent> held_mouse_move_;
366
[email protected]8d8953c2012-03-23 23:19:10367 CompositorLock* compositor_lock_;
368 bool draw_on_compositor_unlock_;
369
[email protected]d6794882012-03-29 03:19:58370 int draw_trace_count_;
371
[email protected]99f07e02011-12-07 00:02:59372 DISALLOW_COPY_AND_ASSIGN(RootWindow);
[email protected]81585f32011-07-29 19:32:06373};
374
375} // namespace aura
376
[email protected]99f07e02011-12-07 00:02:59377#endif // UI_AURA_ROOT_WINDOW_H_