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

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 nits Created 5 years, 8 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 : delegate_(delegate),
32 direction_(SLIDE_NONE),
33 overscroll_cancelled_(false) {
34 DCHECK(delegate_);
35 }
36
37 OverscrollWindowAnimation::~OverscrollWindowAnimation() {
38 }
39
40 void OverscrollWindowAnimation::CancelAnimation() {
41 ui::Layer* layer = GetFrontLayer();
42 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
43 settings.SetPreemptionStrategy(
44 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
45 settings.SetTweenType(gfx::Tween::EASE_OUT);
46 settings.AddObserver(this);
47 layer->SetTransform(gfx::Transform());
48 overscroll_cancelled_ = true;
49 }
50
51 float OverscrollWindowAnimation::GetTranslationForOverscroll(float delta_x) {
52 DCHECK(direction_ != SLIDE_NONE);
53 const float bounds_width = GetVisibleBounds().width();
54 if (direction_ == SLIDE_FRONT)
55 return std::max(-bounds_width, delta_x);
56 else
57 return std::min(bounds_width, delta_x);
58 }
59
60 gfx::Rect OverscrollWindowAnimation::GetVisibleBounds() const {
61 return delegate_->GetMainWindow()->bounds();
62 }
63
64 bool OverscrollWindowAnimation::OnOverscrollUpdate(float delta_x,
65 float delta_y) {
66 if (direction_ == SLIDE_NONE)
67 return false;
68 gfx::Transform transform;
69 transform.Translate(GetTranslationForOverscroll(delta_x), 0);
70 GetFrontLayer()->SetTransform(transform);
71 return true;
72 }
73
74 void OverscrollWindowAnimation::OnImplicitAnimationsCompleted() {
75 if (overscroll_cancelled_) {
76 slide_window_.reset();
77 delegate_->OnOverscrollCancelled();
78 overscroll_cancelled_ = false;
79 } else {
80 delegate_->OnOverscrollCompleted(slide_window_.Pass());
81 }
82 direction_ = SLIDE_NONE;
83 }
84
85 void OverscrollWindowAnimation::OnOverscrollModeChange(
86 OverscrollMode old_mode,
87 OverscrollMode new_mode) {
88 Direction new_direction = GetDirectionForMode(new_mode);
89 if (new_direction == SLIDE_NONE) {
90 // The user cancelled the in progress animation.
91 if (is_active())
92 CancelAnimation();
mfomitchev 2015/04/07 21:07:38 I think this method name is a bit confusing - we a
Nina 2015/04/08 13:35:20 Renamed to CancelSlide.
93 return;
94 }
95 if (slide_window_)
mfomitchev 2015/04/07 21:07:38 Use is_active() instead?
Nina 2015/04/08 13:35:20 Done.
96 GetFrontLayer()->GetAnimator()->StopAnimating();
97 slide_window_ = new_direction == SLIDE_FRONT ? delegate_->CreateFrontWindow()
98 : delegate_->CreateBackWindow();
99 if (!slide_window_) {
100 // Cannot navigate, do not start the animation.
101 direction_ = SLIDE_NONE;
102 return;
103 }
104 overscroll_cancelled_ = false;
105 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size());
106
107 // Make sure the live window is in its default position.
108 delegate_->GetMainWindow()->SetTransform(gfx::Transform());
109 delegate_->GetMainWindow()->SetBounds(bounds);
110
111 if (new_direction == SLIDE_FRONT)
112 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0);
113 slide_window_->SetBounds(bounds);
mfomitchev 2015/04/07 21:07:38 Ugh, just noticed this. Why are we setting the bou
Nina 2015/04/08 13:35:20 To simplify the transform logic - especially for p
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 DCHECK(direction_ != SLIDE_NONE);
144 if (direction_ == SLIDE_FRONT) {
145 DCHECK(slide_window_);
146 return slide_window_->layer();
147 }
148 return delegate_->GetMainWindow()->layer();
149 }
150
151 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698