1 // Copyright (c) 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.
5 #ifndef CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
6 #define CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/common/content_export.h"
12 #include "ui/aura/window_observer.h"
13 #include "ui/compositor/layer_animator.h"
14 #include "ui/events/event_handler.h"
22 class ShadowLayerDelegate
;
24 // A class for sliding the layer in a Window on top of other layers.
25 class CONTENT_EXPORT WindowSlider
: public ui::EventHandler
,
26 public aura::WindowObserver
{
28 class CONTENT_EXPORT Delegate
{
30 virtual ~Delegate() {}
32 // Creates a layer to show behind the window-layer. Called when the
33 // window-layer starts sliding out to reveal the layer underneath.
34 // The WindowSlider takes ownership of the created layer.
35 virtual ui::Layer
* CreateBackLayer() = 0;
37 // Creates a layer to show on top of the window-layer. Called when the new
38 // layer needs to start sliding in on top of the window-layer.
39 // The WindowSlider takes ownership of the created layer.
40 virtual ui::Layer
* CreateFrontLayer() = 0;
42 // Called when the slide is aborted. Note that when the slide is aborted,
43 // the WindowSlider resets any transform it applied on the window-layer.
44 virtual void OnWindowSlideAborted() = 0;
46 // Called when the slide is about to be complete. The delegate can take
47 // action with the assumption that slide will complete soon (within the
48 // duration of the final transition animation effect).
49 // This callback is always preceeded by CreateBackLayer() or by
50 // CreateFrontLayer() callback, and is guaranteed to be followed by the
51 // OnWindowSlideCompleted() callback.
52 virtual void OnWindowSlideCompleting() = 0;
54 // Called when the window slide completes. Note that at the end the
55 // window-layer may have been transformed. The callback here should reset
56 // the transform if necessary. |layer| is the slider's layer (previously
57 // created via CreateBackLayer() or CreateFrontLayer()).
58 virtual void OnWindowSlideCompleted(scoped_ptr
<ui::Layer
> layer
) = 0;
60 // Called when the slider is destroyed.
61 virtual void OnWindowSliderDestroyed() = 0;
64 // The WindowSlider slides the layers in the |owner| window. It starts
65 // intercepting scroll events on |event_window|, and uses those events to
66 // control the layer-slide. The lifetime of the slider is managed by the
67 // lifetime of |owner|, i.e. if |owner| is destroyed, then the slider also
69 WindowSlider(Delegate
* delegate
,
70 aura::Window
* event_window
,
73 ~WindowSlider() override
;
75 // Changes the owner of the slider.
76 void ChangeOwner(aura::Window
* new_owner
);
78 bool IsSlideInProgress() const;
81 // Sets up the slider layer correctly (sets the correct bounds of the layer,
82 // parents it to the right layer, and sets up the correct stacking order).
83 void SetupSliderLayer();
85 void UpdateForScroll(float x_offset
, float y_offset
);
87 // Completes or resets the slide depending on whether the sliding layer
88 // passed the "complete slide threshold".
89 void CompleteOrResetSlide();
91 // Stops all slider-owned animations, progressing them to their end-points.
92 // Note that depending on the sate of the Delegate and the WindowSlider, this
93 // may destroy the WindowSlider through animation callbacks.
94 void CompleteActiveAnimations();
96 // Resets in-progress slide if any, and starts the animation of the slidden
97 // window to its original position.
100 // The following callbacks are triggered after an animation.
101 void SlideAnimationCompleted(scoped_ptr
<ui::Layer
> layer
,
102 scoped_ptr
<ShadowLayerDelegate
> shadow
);
104 void ResetSlideAnimationCompleted(scoped_ptr
<ui::Layer
> layer
,
105 scoped_ptr
<ShadowLayerDelegate
> shadow
);
107 // Overridden from ui::EventHandler:
108 void OnKeyEvent(ui::KeyEvent
* event
) override
;
109 void OnMouseEvent(ui::MouseEvent
* event
) override
;
110 void OnScrollEvent(ui::ScrollEvent
* event
) override
;
111 void OnGestureEvent(ui::GestureEvent
* event
) override
;
113 // Overridden from aura::WindowObserver:
114 void OnWindowRemovingFromRootWindow(aura::Window
* window
,
115 aura::Window
* new_root
) override
;
119 // The slider intercepts scroll events from this window. The slider does not
120 // own |event_window_|. If |event_window_| is destroyed, then the slider stops
121 // listening for events, but it doesn't destroy itself.
122 aura::Window
* event_window_
;
124 // The window the slider operates on. The lifetime of the slider is bound to
125 // this window (i.e. if |owner_| does, the slider destroys itself). The slider
126 // can also delete itself when a slide gesture is completed. This does not
128 aura::Window
* owner_
;
130 // Set to the Animator of the currently active animation. If no animation is
131 // active, this is set to NULL.
132 ui::LayerAnimator
* active_animator_
;
134 // The accumulated amount of horizontal scroll.
137 // This keeps track of the layer created by the delegate.
138 scoped_ptr
<ui::Layer
> slider_
;
140 // This manages the shadow for the layers.
141 scoped_ptr
<ShadowLayerDelegate
> shadow_
;
143 float active_start_threshold_
;
145 const float start_threshold_touchscreen_
;
146 const float start_threshold_touchpad_
;
147 const float complete_threshold_
;
149 base::WeakPtrFactory
<WindowSlider
> weak_factory_
;
151 DISALLOW_COPY_AND_ASSIGN(WindowSlider
);
154 } // namespace content
156 #endif // CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_