Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: content/browser/web_contents/aura/overscroll_window_animation.cc

Issue 895543005: Refactor GestureNavigation to eliminate code redundancy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Mikhail's comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 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 #include "content/browser/web_contents/aura/overscroll_window_animation.h"
6
7 #include "base/i18n/rtl.h"
8 #include "content/browser/web_contents/aura/overscroll_layer_wrapper.h"
9 #include "content/browser/web_contents/aura/shadow_layer_delegate.h"
10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "ui/aura/window.h"
12 #include "ui/compositor/layer_animation_observer.h"
13 #include "ui/compositor/scoped_layer_animation_settings.h"
14
15 namespace content {
16
17 namespace {
18
19 OverscrollWindowAnimation::Direction GetDirectionForMode(OverscrollMode mode) {
20 LOG(ERROR) << "Getting navigation direction";
21 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_EAST : OVERSCROLL_WEST))
22 return OverscrollWindowAnimation::SLIDE_FRONT;
23 if (mode == (base::i18n::IsRTL() ? OVERSCROLL_WEST : OVERSCROLL_EAST))
24 return OverscrollWindowAnimation::SLIDE_BACK;
25 return OverscrollWindowAnimation::SLIDE_NONE;
26 }
27
28 } // namespace
29
30 OverscrollWindowAnimation::OverscrollWindowAnimation(
31 Delegate* delegate, WebContentsImpl* web_contents)
32 : layer_wrapper_(nullptr),
33 shadow_(nullptr),
34 overlay_window_(nullptr),
35 web_contents_(web_contents),
36 delegate_(delegate),
37 direction_(SLIDE_NONE) {
38 DCHECK(delegate_);
39 }
40
41 OverscrollWindowAnimation::~OverscrollWindowAnimation() {
42 LOG(ERROR) << "OWA destructor";
43 }
44
45 void OverscrollWindowAnimation::CancelOverscroll() {
46 LOG(ERROR) << "OWA: Cancelling overscroll";
47 ui::Layer* layer = GetFrontLayer();
48 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
49 settings.SetPreemptionStrategy(
50 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
51 settings.SetTweenType(gfx::Tween::EASE_OUT);
52 settings.AddObserver(this);
53 layer->SetTransform(gfx::Transform());
54 direction_ = SLIDE_NONE;
55 }
56
57 float OverscrollWindowAnimation::GetTranslationForOverscroll(
58 float delta_x) {
59 DCHECK(direction_ != SLIDE_NONE);
60 const float bounds_width = layer_wrapper_->GetBounds().width();
61 if (direction_ == SLIDE_FRONT)
62 return std::max(-bounds_width, delta_x);
63 else
64 return std::min(bounds_width, delta_x);
65 }
66
67 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const {
68 return GetFrontLayer()->bounds();
69 }
70
71 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x,
72 float delta_y) {
73 if (direction_ == SLIDE_NONE)
74 return false;
75 gfx::Transform transform;
76 transform.Translate(GetTranslationForOverscroll(delta_x), 0);
77 GetFrontLayer()->SetTransform(transform);
78 return true;
79 }
80
81 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() {
82 LOG(ERROR) << "OnImplicitAnimationsCompleted";
83 LOG(ERROR) << "LAYER_WRAPPER_: " << layer_wrapper_;
84 if (direction_ != SLIDE_NONE) {
85 direction_ = SLIDE_NONE;
86 delegate_->OnOverscrollCompleted(layer_wrapper_.Pass());
87 return;
88 }
89 delegate_->OnOverscrollAborted();
90 layer_wrapper_.reset();
91 }
92
93 void OverscrollWindowAnimation::OnOverscrollModeChange(
94 OverscrollMode old_mode,
95 OverscrollMode new_mode) {
96 LOG(ERROR) << "OWA: OnOverscrollModeChange for mode " << new_mode;
97 Direction new_direction = GetDirectionForMode(new_mode);
98
99 if (new_direction == SLIDE_NONE) {
100 // The user cancelled the in progress overscroll gesture.
101 // TODO(nsatragno): in this case, show a feedback animation.
102 LOG(ERROR) << "Direction: SLIDE_NONE";
103 if (is_active())
104 CancelOverscroll();
105 return;
106 }
107 LOG(ERROR) << "Preparing layer wrapper";
108 if (layer_wrapper_) {
109 LOG(ERROR) << "ALERT: MODE CHANGE WITH LAYER WRAPPER ACTIVE! "
110 << "Aborting animations.";
111 GetFrontLayer()->GetAnimator()->AbortAllAnimations();
112 }
113 layer_wrapper_ =
114 new_direction == SLIDE_FRONT ? delegate_->CreateFrontLayerWrapper()
115 : delegate_->CreateBackLayerWrapper();
116 if (!layer_wrapper_) {
117 LOG(ERROR) << "Cannot navigate, setting direction to SLIDE_NONE";
118 direction_ = SLIDE_NONE;
119 return;
120 }
121 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size());
122 if (new_direction == OverscrollWindowAnimation::SLIDE_FRONT)
123 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0);
124 // TODO maybe we want to set the window bounds here, too.
125 layer_wrapper_->layer()->SetBounds(bounds);
126 LOG(ERROR) << "Setting shadow delegate.";
127 direction_ = new_direction;
128 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer()));
129 }
130
131 void OverscrollWindowAnimation::OnOverscrollComplete(
132 OverscrollMode overscroll_mode) {
133 LOG(ERROR) << "OWA: OnOverscrollComplete";
134 if (!is_active())
135 return;
136 delegate_->OnOverscrollCompleting();
137 int content_width = layer_wrapper_->GetBounds().width();
138 float translate_x;
139 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) ||
140 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) {
141 translate_x = content_width;
142 } else {
143 translate_x = -content_width;
144 }
145
146 ui::Layer* layer = GetFrontLayer();
147 gfx::Transform transform;
148 transform.Translate(translate_x, 0);
149 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
150 settings.SetPreemptionStrategy(
151 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
152 settings.SetTweenType(gfx::Tween::EASE_OUT);
153 settings.AddObserver(this);
154 layer->SetTransform(transform);
155 }
156
157 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const {
158 if (direction_ == SLIDE_FRONT) {
159 DCHECK(layer_wrapper_);
160 return layer_wrapper_->layer();
161 }
162 if (web_contents_)
mfomitchev 2015/03/10 19:26:00 OWA shouldn't know about web_contents
Nina 2015/03/12 22:21:29 Modified so it doesn't have to.
163 return web_contents_->GetContentNativeView()->layer();
164 DCHECK(overlay_window_);
165 return overlay_window_->layer();
166 }
167
168 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698