window_slider.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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 CreateBackLayerAndSetAsTarget() or 50 // by CreateFrontLayerAndSetAsTarget() callback, and is guaranteed to be 51 // followed by the 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. 57 virtual void OnWindowSlideCompleted() = 0; 58 59 // Called when the slider is destroyed. 60 virtual void OnWindowSliderDestroyed() = 0; 61 }; 62 63 // The WindowSlider slides the layers in the |owner| window. It starts 64 // intercepting scroll events on |event_window|, and uses those events to 65 // control the layer-slide. The lifetime of the slider is managed by the 66 // lifetime of |owner|, i.e. if |owner| is destroyed, then the slider also 67 // destroys itself. 68 WindowSlider(Delegate* delegate, 69 aura::Window* event_window, 70 aura::Window* owner); 71 72 virtual ~WindowSlider(); 73 74 // Changes the owner of the slider. 75 void ChangeOwner(aura::Window* new_owner); 76 77 bool IsSlideInProgress() const; 78 79 private: 80 // Sets up the slider layer correctly (sets the correct bounds of the layer, 81 // parents it to the right layer, and sets up the correct stacking order). 82 void SetupSliderLayer(); 83 84 void UpdateForScroll(float x_offset, float y_offset); 85 86 // Completes or resets the slide depending on whether the sliding layer 87 // passed the "complete slide threshold". 88 void CompleteOrResetSlide(); 89 90 // Stops all slider-owned animations, progressing them to their end-points. 91 // Note that depending on the sate of the Delegate and the WindowSlider, this 92 // may destroy the WindowSlider through animation callbacks. 93 void CompleteActiveAnimations(); 94 95 // Resets in-progress slide if any, and starts the animation of the slidden 96 // window to its original position. 97 void ResetSlide(); 98 99 // The following callbacks are triggered after an animation. 100 void SlideAnimationCompleted(scoped_ptr<ui::Layer> layer, 101 scoped_ptr<ShadowLayerDelegate> shadow); 102 103 void ResetSlideAnimationCompleted(scoped_ptr<ui::Layer> layer, 104 scoped_ptr<ShadowLayerDelegate> shadow); 105 106 // Overridden from ui::EventHandler: 107 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; 108 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; 109 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE; 110 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 111 112 // Overridden from aura::WindowObserver: 113 virtual void OnWindowRemovingFromRootWindow(aura::Window* window, 114 aura::Window* new_root) OVERRIDE; 115 116 Delegate* delegate_; 117 118 // The slider intercepts scroll events from this window. The slider does not 119 // own |event_window_|. If |event_window_| is destroyed, then the slider stops 120 // listening for events, but it doesn't destroy itself. 121 aura::Window* event_window_; 122 123 // The window the slider operates on. The lifetime of the slider is bound to 124 // this window (i.e. if |owner_| does, the slider destroys itself). The slider 125 // can also delete itself when a slide gesture is completed. This does not 126 // destroy |owner_|. 127 aura::Window* owner_; 128 129 // Set to the Animator of the currently active animation. If no animation is 130 // active, this is set to NULL. 131 ui::LayerAnimator* active_animator_; 132 133 // The accumulated amount of horizontal scroll. 134 float delta_x_; 135 136 // This keeps track of the layer created by the delegate. 137 scoped_ptr<ui::Layer> slider_; 138 139 // This manages the shadow for the layers. 140 scoped_ptr<ShadowLayerDelegate> shadow_; 141 142 base::WeakPtrFactory<WindowSlider> weak_factory_; 143 144 float active_start_threshold_; 145 146 const float start_threshold_touchscreen_; 147 const float start_threshold_touchpad_; 148 const float complete_threshold_; 149 150 DISALLOW_COPY_AND_ASSIGN(WindowSlider); 151}; 152 153} // namespace content 154 155#endif // CONTENT_BROWSER_WEB_CONTENTS_AURA_WINDOW_SLIDER_H_ 156