window_slider.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
18bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek// Copyright (c) 2013 The Chromium Authors. All rights reserved.
28bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek// Use of this source code is governed by a BSD-style license that can be
38bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek// found in the LICENSE file.
48bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek
58bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#ifndef CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
68bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#define CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
78bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek
88bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#include "base/compiler_specific.h"
98bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#include "base/memory/scoped_ptr.h"
108bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#include "base/memory/weak_ptr.h"
118bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#include "content/common/content_export.h"
128bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek#include "ui/aura/window_observer.h"
13a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis#include "ui/base/events/event_handler.h"
14a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis
158bef8238181a30e52dea380789a7e2d760eac532Ted Kremeneknamespace ui {
168bef8238181a30e52dea380789a7e2d760eac532Ted Kremenekclass Layer;
178bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek}
188bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek
19a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidisnamespace content {
20a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis
21a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidisclass ShadowLayerDelegate;
22a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis
23a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis// A class for sliding the layer in a Window on top of other layers.
24a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidisclass CONTENT_EXPORT WindowSlider : public ui::EventHandler,
25a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis                                    public aura::WindowObserver {
26a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis public:
27a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis  class Delegate {
28a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis   public:
29a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    virtual ~Delegate() {}
30a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis
31a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    // Creates a layer to show in the background, as the window-layer slides
32a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    // with the scroll gesture.
33a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    // The WindowSlider takes ownership of the created layer.
34a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    virtual ui::Layer* CreateBackLayer() = 0;
35a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis
36a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    // Creates a layer to slide on top of the window-layer with the scroll
37a5888f61be9f8d76e9b48a453dbced50523bd2e0Argyrios Kyrtzidis    // gesture.
388bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek    // The WindowSlider takes ownership of the created layer.
398bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek    virtual ui::Layer* CreateFrontLayer() = 0;
408bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek
418bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek    // Called when the slide is complete. Note that at the end of a completed
428bef8238181a30e52dea380789a7e2d760eac532Ted Kremenek    // slide, the window-layer may have been transformed. The callback here
43    // should reset the transform if necessary.
44    virtual void OnWindowSlideComplete() = 0;
45
46    // Called when the slide is aborted. Note that when the slide is aborted,
47    // the WindowSlider resets any transform it applied on the window-layer.
48    virtual void OnWindowSlideAborted() = 0;
49
50    // Called when the slider is destroyed.
51    virtual void OnWindowSliderDestroyed() = 0;
52  };
53
54  // The WindowSlider slides the layers in the |owner| window. It starts
55  // intercepting scroll events on |event_window|, and uses those events to
56  // control the layer-slide. The lifetime of the slider is managed by the
57  // lifetime of |owner|, i.e. if |owner| is destroyed, then the slider also
58  // destroys itself.
59  WindowSlider(Delegate* delegate,
60               aura::Window* event_window,
61               aura::Window* owner);
62
63  virtual ~WindowSlider();
64
65  // Changes the owner of the slider.
66  void ChangeOwner(aura::Window* new_owner);
67
68 private:
69  // Sets up the slider layer correctly (sets the correct bounds of the layer,
70  // parents it to the right layer, and sets up the correct stacking order).
71  void SetupSliderLayer();
72
73  void UpdateForScroll(float x_offset, float y_offset);
74
75  void UpdateForFling(float x_velocity, float y_velocity);
76
77  // Resets any in-progress slide.
78  void ResetScroll();
79
80  // Cancels any scroll/animation in progress.
81  void CancelScroll();
82
83  // The following callbacks are triggered after an animation.
84  // The |Complete| callback destroys the |WindowSlider| itself.
85  void CompleteWindowSlideAfterAnimation();
86
87  void AbortWindowSlideAfterAnimation();
88
89  // Overridden from ui::EventHandler:
90  virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
91  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
92  virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
93  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
94
95  // Overridden from aura::WindowObserver:
96  virtual void OnWindowRemovingFromRootWindow(aura::Window* window) OVERRIDE;
97
98  Delegate* delegate_;
99
100  // The slider intercepts scroll events from this window. The slider does not
101  // own |event_window_|. If |event_window_| is destroyed, then the slider stops
102  // listening for events, but it doesn't destroy itself.
103  aura::Window* event_window_;
104
105  // The window the slider operates on. The lifetime of the slider is bound to
106  // this window (i.e. if |owner_| does, the slider destroys itself). The slider
107  // can also delete itself when a slide gesture is completed. This does not
108  // destroy |owner_|.
109  aura::Window* owner_;
110
111  // The accumulated amount of horizontal scroll.
112  float delta_x_;
113
114  // This keeps track of the layer created by the delegate.
115  scoped_ptr<ui::Layer> slider_;
116
117  // This manages the shadow for the layers.
118  scoped_ptr<ShadowLayerDelegate> shadow_;
119
120  base::WeakPtrFactory<WindowSlider> weak_factory_;
121
122  const float min_start_threshold_;
123  const float complete_threshold_;
124
125  DISALLOW_COPY_AND_ASSIGN(WindowSlider);
126};
127
128}  // namespace content
129
130#endif  // CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
131