Home | History | Annotate | Download | only in aura
      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/base/events/event_handler.h"
     14 
     15 namespace ui {
     16 class Layer;
     17 }
     18 
     19 namespace content {
     20 
     21 class ShadowLayerDelegate;
     22 
     23 // A class for sliding the layer in a Window on top of other layers.
     24 class CONTENT_EXPORT WindowSlider : public ui::EventHandler,
     25                                     public aura::WindowObserver {
     26  public:
     27   class Delegate {
     28    public:
     29     virtual ~Delegate() {}
     30 
     31     // Creates a layer to show in the background, as the window-layer slides
     32     // with the scroll gesture.
     33     // The WindowSlider takes ownership of the created layer.
     34     virtual ui::Layer* CreateBackLayer() = 0;
     35 
     36     // Creates a layer to slide on top of the window-layer with the scroll
     37     // gesture.
     38     // The WindowSlider takes ownership of the created layer.
     39     virtual ui::Layer* CreateFrontLayer() = 0;
     40 
     41     // Called when the slide is complete. Note that at the end of a completed
     42     // 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   bool IsSlideInProgress() const;
     69 
     70  private:
     71   // Sets up the slider layer correctly (sets the correct bounds of the layer,
     72   // parents it to the right layer, and sets up the correct stacking order).
     73   void SetupSliderLayer();
     74 
     75   void UpdateForScroll(float x_offset, float y_offset);
     76 
     77   void UpdateForFling(float x_velocity, float y_velocity);
     78 
     79   // Resets any in-progress slide.
     80   void ResetScroll();
     81 
     82   // Cancels any scroll/animation in progress.
     83   void CancelScroll();
     84 
     85   // The following callbacks are triggered after an animation.
     86   void CompleteWindowSlideAfterAnimation();
     87 
     88   void AbortWindowSlideAfterAnimation();
     89 
     90   // Overridden from ui::EventHandler:
     91   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
     92   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
     93   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
     94   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
     95 
     96   // Overridden from aura::WindowObserver:
     97   virtual void OnWindowRemovingFromRootWindow(aura::Window* window) OVERRIDE;
     98 
     99   Delegate* delegate_;
    100 
    101   // The slider intercepts scroll events from this window. The slider does not
    102   // own |event_window_|. If |event_window_| is destroyed, then the slider stops
    103   // listening for events, but it doesn't destroy itself.
    104   aura::Window* event_window_;
    105 
    106   // The window the slider operates on. The lifetime of the slider is bound to
    107   // this window (i.e. if |owner_| does, the slider destroys itself). The slider
    108   // can also delete itself when a slide gesture is completed. This does not
    109   // destroy |owner_|.
    110   aura::Window* owner_;
    111 
    112   // The accumulated amount of horizontal scroll.
    113   float delta_x_;
    114 
    115   // This keeps track of the layer created by the delegate.
    116   scoped_ptr<ui::Layer> slider_;
    117 
    118   // This manages the shadow for the layers.
    119   scoped_ptr<ShadowLayerDelegate> shadow_;
    120 
    121   base::WeakPtrFactory<WindowSlider> weak_factory_;
    122 
    123   const float horiz_start_threshold_;
    124   const float complete_threshold_;
    125 
    126   DISALLOW_COPY_AND_ASSIGN(WindowSlider);
    127 };
    128 
    129 }  // namespace content
    130 
    131 #endif  // CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_
    132