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

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: Fixed some bugs related to back and forth 2nd case navigation 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 <algorithm>
8
9 #include "base/i18n/rtl.h"
10 #include "content/browser/web_contents/aura/shadow_layer_delegate.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "ui/aura/window.h"
13 #include "ui/compositor/layer_animation_observer.h"
14 #include "ui/compositor/scoped_layer_animation_settings.h"
15
16 namespace content {
17
18 namespace {
19
20 OverscrollWindowAnimation::Direction GetDirectionForMode(OverscrollMode mode) {
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(Delegate* delegate)
31 : wrapper_(nullptr),
32 shadow_(nullptr),
33 delegate_(delegate),
34 direction_(SLIDE_NONE) {
35 DCHECK(delegate_);
36 }
37
38 OverscrollWindowAnimation::~OverscrollWindowAnimation() {
39 }
40
41 void OverscrollWindowAnimation::CancelOverscroll() {
42 ui::Layer* layer = GetFrontLayer();
43 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
44 settings.SetPreemptionStrategy(
45 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
46 settings.SetTweenType(gfx::Tween::EASE_OUT);
47 settings.AddObserver(this);
48 layer->SetTransform(gfx::Transform());
49 direction_ = SLIDE_NONE;
50 }
51
52 float OverscrollWindowAnimation::GetTranslationForOverscroll(
53 float delta_x) {
54 DCHECK(direction_ != SLIDE_NONE);
55 const float bounds_width = GetVisibleBounds().width();
56 if (direction_ == SLIDE_FRONT)
57 return std::max(-bounds_width, delta_x);
58 else
59 return std::min(bounds_width, delta_x);
60 }
61
62 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const {
63 return delegate_->GetLiveWindow()->bounds();
64 }
65
66 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x,
67 float delta_y) {
68 if (direction_ == SLIDE_NONE)
69 return false;
70 gfx::Transform transform;
71 transform.Translate(GetTranslationForOverscroll(delta_x), 0);
72 GetFrontLayer()->SetTransform(transform);
73 return true;
74 }
75
76 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() {
77 if (direction_ != SLIDE_NONE) {
78 direction_ = SLIDE_NONE;
79 delegate_->OnOverscrollCompleted(wrapper_.Pass());
80 return;
81 }
82 wrapper_.reset();
83 delegate_->OnOverscrollAborted();
84 }
85
86 void OverscrollWindowAnimation::OnOverscrollModeChange(
87 OverscrollMode old_mode,
88 OverscrollMode new_mode) {
89 Direction new_direction = GetDirectionForMode(new_mode);
90 if (new_direction == SLIDE_NONE) {
91 // The user cancelled the in progress overscroll gesture.
92 // TODO(nsatragno): in this case, show a feedback animation.
mfomitchev 2015/03/16 22:28:02 I think we can get rid of this TODO?
Nina 2015/03/17 14:27:07 Done.
93 if (direction_ != SLIDE_NONE)
94 CancelOverscroll();
95 return;
96 }
97 if (wrapper_)
98 GetFrontLayer()->GetAnimator()->AbortAllAnimations();
mfomitchev 2015/03/16 22:28:02 So I think we need to do StopAnimating() instead o
Nina 2015/03/17 14:27:07 Done.
99 wrapper_ =
100 new_direction == SLIDE_FRONT ? delegate_->CreateFrontWrapper()
101 : delegate_->CreateBackWrapper();
102 if (!wrapper_) {
103 // Cannot navigate, do not start an overscroll gesture.
104 direction_ = SLIDE_NONE;
105 return;
106 }
107 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size());
108 // Make sure the live window is in its default position.
mfomitchev 2015/03/16 22:28:02 Don't you also need to abort all animations on it?
Nina 2015/03/17 14:27:07 Ah, I guess yes - if something else transforms the
109 delegate_->GetLiveWindow()->SetBounds(bounds);
110 delegate_->GetLiveWindow()->SetTransform(gfx::Transform());
111 if (new_direction == OverscrollWindowAnimation::SLIDE_FRONT)
112 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0);
113 wrapper_->SetBounds(bounds);
114 direction_ = new_direction;
115 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer()));
116 }
117
118 void OverscrollWindowAnimation::OnOverscrollComplete(
119 OverscrollMode overscroll_mode) {
120 if (!is_active())
121 return;
122 delegate_->OnOverscrollCompleting();
123 int content_width = GetVisibleBounds().width();
124 float translate_x;
125 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) ||
126 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) {
127 translate_x = content_width;
128 } else {
129 translate_x = -content_width;
130 }
131 ui::Layer* layer = GetFrontLayer();
132 gfx::Transform transform;
133 transform.Translate(translate_x, 0);
134 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
135 settings.SetPreemptionStrategy(
136 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
137 settings.SetTweenType(gfx::Tween::EASE_OUT);
138 settings.AddObserver(this);
139 layer->SetTransform(transform);
140 }
141
142 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const {
143 if (direction_ == SLIDE_FRONT) {
144 DCHECK(wrapper_);
145 return wrapper_->GetLayer();
146 }
147 return delegate_->GetLiveWindow()->layer();
148 }
149
150 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698