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_
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_processor.h"
25 #include "ui/events/event_targeter.h"
26 #include "ui/events/gestures/gesture_recognizer.h"
27 #include "ui/events/gestures/gesture_types.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/point.h"
38 class GestureRecognizer;
40 class LayerAnimationSequence;
50 class RootWindowObserver;
54 // RootWindow is responsible for hosting a set of windows.
55 class AURA_EXPORT RootWindow : public ui::EventProcessor,
56 public ui::GestureEventHelper,
57 public ui::LayerAnimationObserver,
58 public client::CaptureDelegate,
59 public WindowTreeHostDelegate {
61 struct AURA_EXPORT CreateParams {
62 // CreateParams with initial_bounds and default host in pixel.
63 explicit CreateParams(const gfx::Rect& initial_bounds);
66 gfx::Rect initial_bounds;
68 // A host to use in place of the default one that RootWindow will create.
73 explicit RootWindow(const CreateParams& params);
74 virtual ~RootWindow();
76 // Returns the WindowTreeHost for the specified accelerated widget, or NULL
77 // if there is none associated.
78 static RootWindow* GetForAcceleratedWidget(gfx::AcceleratedWidget widget);
81 return const_cast<Window*>(const_cast<const RootWindow*>(this)->window());
83 const Window* window() const { return window_.get(); }
84 WindowTreeHost* host() {
85 return const_cast<WindowTreeHost*>(
86 const_cast<const RootWindow*>(this)->host());
88 const WindowTreeHost* host() const { return host_.get(); }
89 Window* mouse_pressed_handler() { return mouse_pressed_handler_; }
90 Window* mouse_moved_handler() { return mouse_moved_handler_; }
92 // Stop listening events in preparation for shutdown.
93 void PrepareForShutdown();
95 // Repost event for re-processing. Used when exiting context menus.
96 // We only support the ET_MOUSE_PRESSED and ET_GESTURE_TAP_DOWN event
97 // types (although the latter is currently a no-op).
98 void RepostEvent(const ui::LocatedEvent& event);
100 WindowTreeHostDelegate* AsWindowTreeHostDelegate();
102 // Invoked when the mouse events get enabled or disabled.
103 void OnMouseEventsEnableStateChanged(bool enabled);
105 // Returns a target window for the given gesture event.
106 Window* GetGestureTarget(ui::GestureEvent* event);
108 // Handles a gesture event. Returns true if handled. Unlike the other
109 // event-dispatching function (e.g. for touch/mouse/keyboard events), gesture
110 // events are dispatched from GestureRecognizer instead of WindowTreeHost.
111 void DispatchGestureEvent(ui::GestureEvent* event);
113 // Invoked when |window| is being destroyed.
114 void OnWindowDestroying(Window* window);
116 // Invoked when |window|'s bounds have changed. |contained_mouse| indicates if
117 // the bounds before change contained the |last_moust_location()|.
118 void OnWindowBoundsChanged(Window* window, bool contained_mouse);
120 // Dispatches OnMouseExited to the |window| which is hiding if nessessary.
121 void DispatchMouseExitToHidingWindow(Window* window);
123 // Dispatches a ui::ET_MOUSE_EXITED event at |point|.
124 void DispatchMouseExitAtPoint(const gfx::Point& point);
126 // Invoked when |window|'s visibility has changed.
127 void OnWindowVisibilityChanged(Window* window, bool is_visible);
129 // Invoked when |window|'s tranfrom has changed. |contained_mouse|
130 // indicates if the bounds before change contained the
131 // |last_moust_location()|.
132 void OnWindowTransformed(Window* window, bool contained_mouse);
134 // Invoked when the keyboard mapping (in X11 terms: the mapping between
135 // keycodes and keysyms) has changed.
136 void OnKeyboardMappingChanged();
138 // The system windowing system has sent a request that we close our window.
139 void OnWindowTreeHostCloseRequested();
141 // Add/remove observer. There is no need to remove the observer if
142 // the root window is being deleted. In particular, you SHOULD NOT remove
143 // in |WindowObserver::OnWindowDestroying| of the observer observing
144 // the root window because it is too late to remove it.
145 void AddRootWindowObserver(RootWindowObserver* observer);
146 void RemoveRootWindowObserver(RootWindowObserver* observer);
148 // Gesture Recognition -------------------------------------------------------
150 // When a touch event is dispatched to a Window, it may want to process the
151 // touch event asynchronously. In such cases, the window should consume the
152 // event during the event dispatch. Once the event is properly processed, the
153 // window should let the RootWindow know about the result of the event
154 // processing, so that gesture events can be properly created and dispatched.
155 void ProcessedTouchEvent(ui::TouchEvent* event,
157 ui::EventResult result);
159 // These methods are used to defer the processing of mouse/touch events
160 // related to resize. A client (typically a RenderWidgetHostViewAura) can call
161 // HoldPointerMoves when an resize is initiated and then ReleasePointerMoves
162 // once the resize is completed.
164 // More than one hold can be invoked and each hold must be cancelled by a
165 // release before we resume normal operation.
166 void HoldPointerMoves();
167 void ReleasePointerMoves();
169 // Gets the last location seen in a mouse event in this root window's
170 // coordinates. This may return a point outside the root window's bounds.
171 gfx::Point GetLastMouseLocationInRoot() const;
174 FRIEND_TEST_ALL_PREFIXES(RootWindowTest, KeepTranslatedEventInRoot);
177 friend class TestScreen;
179 // The parameter for OnWindowHidden() to specify why window is hidden.
180 enum WindowHiddenReason {
181 WINDOW_DESTROYED, // Window is destroyed.
182 WINDOW_HIDDEN, // Window is hidden.
183 WINDOW_MOVING, // Window is temporarily marked as hidden due to move
184 // across root windows.
187 // Updates the event with the appropriate transform for the device scale
188 // factor. The WindowTreeHostDelegate dispatches events in the physical pixel
189 // coordinate. But the event processing from RootWindow onwards happen in
190 // device-independent pixel coordinate. So it is necessary to update the event
191 // received from the host.
192 void TransformEventForDeviceScaleFactor(ui::LocatedEvent* event);
194 // Dispatches the specified event type (intended for enter/exit) to the
195 // |mouse_moved_handler_|.
196 ui::EventDispatchDetails DispatchMouseEnterOrExit(
197 const ui::MouseEvent& event,
198 ui::EventType type) WARN_UNUSED_RESULT;
199 ui::EventDispatchDetails ProcessGestures(
200 ui::GestureRecognizer::Gestures* gestures) WARN_UNUSED_RESULT;
202 // Called when a Window is attached or detached from the RootWindow.
203 void OnWindowAddedToRootWindow(Window* window);
204 void OnWindowRemovedFromRootWindow(Window* window, Window* new_root);
206 // Called when a window becomes invisible, either by being removed
207 // from root window hierarchy, via SetVisible(false) or being destroyed.
208 // |reason| specifies what triggered the hiding. Note that becoming invisible
209 // will cause a window to lose capture and some windows may destroy themselves
210 // on capture (like DragDropTracker).
211 void OnWindowHidden(Window* invisible, WindowHiddenReason reason);
213 // Cleans up the state of gestures for all windows in |window| (including
214 // |window| itself). This includes cancelling active touch points.
215 void CleanupGestureState(Window* window);
217 // Overridden from aura::client::CaptureDelegate:
218 virtual void UpdateCapture(Window* old_capture, Window* new_capture) OVERRIDE;
219 virtual void OnOtherRootGotCapture() OVERRIDE;
220 virtual void SetNativeCapture() OVERRIDE;
221 virtual void ReleaseNativeCapture() OVERRIDE;
223 // Overridden from ui::EventProcessor:
224 virtual ui::EventTarget* GetRootTarget() OVERRIDE;
225 virtual void PrepareEventForDispatch(ui::Event* event) OVERRIDE;
227 // Overridden from ui::EventDispatcherDelegate.
228 virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE;
229 virtual ui::EventDispatchDetails PreDispatchEvent(ui::EventTarget* target,
230 ui::Event* event) OVERRIDE;
231 virtual ui::EventDispatchDetails PostDispatchEvent(
232 ui::EventTarget* target, const ui::Event& event) OVERRIDE;
234 // Overridden from ui::GestureEventHelper.
235 virtual bool CanDispatchToConsumer(ui::GestureConsumer* consumer) OVERRIDE;
236 virtual void DispatchPostponedGestureEvent(ui::GestureEvent* event) OVERRIDE;
237 virtual void DispatchCancelTouchEvent(ui::TouchEvent* event) OVERRIDE;
239 // Overridden from ui::LayerAnimationObserver:
240 virtual void OnLayerAnimationEnded(
241 ui::LayerAnimationSequence* animation) OVERRIDE;
242 virtual void OnLayerAnimationScheduled(
243 ui::LayerAnimationSequence* animation) OVERRIDE;
244 virtual void OnLayerAnimationAborted(
245 ui::LayerAnimationSequence* animation) OVERRIDE;
247 // Overridden from aura::WindowTreeHostDelegate:
248 virtual void OnHostCancelMode() OVERRIDE;
249 virtual void OnHostActivated() OVERRIDE;
250 virtual void OnHostLostWindowCapture() OVERRIDE;
251 virtual void OnHostLostMouseGrab() OVERRIDE;
252 virtual void OnHostMoved(const gfx::Point& origin) OVERRIDE;
253 virtual void OnHostResized(const gfx::Size& size) OVERRIDE;
254 virtual void OnCursorMovedToRootLocation(
255 const gfx::Point& root_location) OVERRIDE;
256 virtual RootWindow* AsRootWindow() OVERRIDE;
257 virtual const RootWindow* AsRootWindow() const OVERRIDE;
258 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
260 // We hold and aggregate mouse drags and touch moves as a way of throttling
261 // resizes when HoldMouseMoves() is called. The following methods are used to
262 // dispatch held and newly incoming mouse and touch events, typically when an
263 // event other than one of these needs dispatching or a matching
264 // ReleaseMouseMoves()/ReleaseTouchMoves() is called. NOTE: because these
265 // methods dispatch events from WindowTreeHost the coordinates are in terms of
268 ui::EventDispatchDetails DispatchHeldEvents() WARN_UNUSED_RESULT;
269 // Creates and dispatches synthesized mouse move event using the
270 // current mouse location.
271 ui::EventDispatchDetails SynthesizeMouseMoveEvent() WARN_UNUSED_RESULT;
273 void SynthesizeMouseMoveEventAsync();
275 // Posts a task to send synthesized mouse move event if there
276 // is no a pending task.
277 void PostMouseMoveEventAfterWindowChange();
279 void PreDispatchLocatedEvent(Window* target, ui::LocatedEvent* event);
280 void PreDispatchMouseEvent(Window* target, ui::MouseEvent* event);
281 void PreDispatchTouchEvent(Window* target, ui::TouchEvent* event);
283 // TODO(beng): evaluate the ideal ownership model.
284 scoped_ptr<Window> window_;
286 scoped_ptr<WindowTreeHost> host_;
288 // Touch ids that are currently down.
289 uint32 touch_ids_down_;
291 ObserverList<RootWindowObserver> observers_;
293 Window* mouse_pressed_handler_;
294 Window* mouse_moved_handler_;
295 Window* event_dispatch_target_;
296 Window* old_dispatch_target_;
298 bool synthesize_mouse_move_;
300 // How many move holds are outstanding. We try to defer dispatching
301 // touch/mouse moves while the count is > 0.
302 int move_hold_count_;
303 // The location of |held_move_event_| is in |window_|'s coordinate.
304 scoped_ptr<ui::LocatedEvent> held_move_event_;
306 // Allowing for reposting of events. Used when exiting context menus.
307 scoped_ptr<ui::LocatedEvent> held_repostable_event_;
309 // Set when dispatching a held event.
310 bool dispatching_held_event_;
312 scoped_ptr<ui::ViewProp> prop_;
314 // Used to schedule reposting an event.
315 base::WeakPtrFactory<RootWindow> repost_event_factory_;
317 // Used to schedule DispatchHeldEvents() when |move_hold_count_| goes to 0.
318 base::WeakPtrFactory<RootWindow> held_event_factory_;
320 DISALLOW_COPY_AND_ASSIGN(RootWindow);
325 #endif // UI_AURA_ROOT_WINDOW_H_