window_slider.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#ifndef CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
6#define CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
7
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"
15
16namespace ui {
17class Layer;
18}
19
20namespace content {
21
22class ShadowLayerDelegate;
23
24// A class for sliding the layer in a Window on top of other layers.
25class CONTENT_EXPORT WindowSlider : public ui::EventHandler,
26                                    public aura::WindowObserver {
27 public:
28  class CONTENT_EXPORT Delegate {
29   public:
30    virtual ~Delegate() {}
31
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;
36
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;
41
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;
45
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;
53
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;
59
60    // Called when the slider is destroyed.
61    virtual void OnWindowSliderDestroyed() = 0;
62  };
63
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
68  // destroys itself.
69  WindowSlider(Delegate* delegate,
70               aura::Window* event_window,
71               aura::Window* owner);
72
73  virtual ~WindowSlider();
74
75  // Changes the owner of the slider.
76  void ChangeOwner(aura::Window* new_owner);
77
78  bool IsSlideInProgress() const;
79
80 private:
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();
84
85  void UpdateForScroll(float x_offset, float y_offset);
86
87  // Completes or resets the slide depending on whether the sliding layer
88  // passed the "complete slide threshold".
89  void CompleteOrResetSlide();
90
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();
95
96  // Resets in-progress slide if any, and starts the animation of the slidden
97  // window to its original position.
98  void ResetSlide();
99
100  // The following callbacks are triggered after an animation.
101  void SlideAnimationCompleted(scoped_ptr<ui::Layer> layer,
102                               scoped_ptr<ShadowLayerDelegate> shadow);
103
104  void ResetSlideAnimationCompleted(scoped_ptr<ui::Layer> layer,
105                                    scoped_ptr<ShadowLayerDelegate> shadow);
106
107  // Overridden from ui::EventHandler:
108  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
109  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
110  virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
111  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
112
113  // Overridden from aura::WindowObserver:
114  virtual void OnWindowRemovingFromRootWindow(aura::Window* window,
115                                              aura::Window* new_root) OVERRIDE;
116
117  Delegate* delegate_;
118
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_;
123
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
127  // destroy |owner_|.
128  aura::Window* owner_;
129
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_;
133
134  // The accumulated amount of horizontal scroll.
135  float delta_x_;
136
137  // This keeps track of the layer created by the delegate.
138  scoped_ptr<ui::Layer> slider_;
139
140  // This manages the shadow for the layers.
141  scoped_ptr<ShadowLayerDelegate> shadow_;
142
143  base::WeakPtrFactory<WindowSlider> weak_factory_;
144
145  float active_start_threshold_;
146
147  const float start_threshold_touchscreen_;
148  const float start_threshold_touchpad_;
149  const float complete_threshold_;
150
151  DISALLOW_COPY_AND_ASSIGN(WindowSlider);
152};
153
154}  // namespace content
155
156#endif  // CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
157