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

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: Last round of comments. 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_->OnOverscrollAborted();
mfomitchev 2015/03/31 21:04:07 Set overscroll_cancelled_ to false here?
Nina 2015/04/01 18:10:01 Done.
78 } else {
79 delegate_->OnOverscrollCompleted(slide_window_.Pass());
80 }
81 direction_ = SLIDE_NONE;
82 }
83
84 void OverscrollWindowAnimation::OnOverscrollModeChange(
85 OverscrollMode old_mode,
86 OverscrollMode new_mode) {
87 Direction new_direction = GetDirectionForMode(new_mode);
88 if (new_direction == SLIDE_NONE) {
89 // The user cancelled the in progress animation.
90 if (is_active())
91 CancelAnimation();
92 return;
93 }
94 if (slide_window_)
95 GetFrontLayer()->GetAnimator()->StopAnimating();
96 slide_window_ = new_direction == SLIDE_FRONT ? delegate_->CreateFrontWindow()
97 : delegate_->CreateBackWindow();
98 if (!slide_window_) {
99 // Cannot navigate, do not start the animation.
100 direction_ = SLIDE_NONE;
101 return;
102 }
103 overscroll_cancelled_ = false;
104 gfx::Rect bounds = gfx::Rect(GetVisibleBounds().size());
105
106 // Make sure the live window is in its default position.
107 delegate_->GetMainWindow()->SetTransform(gfx::Transform());
108 delegate_->GetMainWindow()->SetBounds(bounds);
109
110 if (new_direction == SLIDE_FRONT)
111 bounds.Offset(base::i18n::IsRTL() ? -bounds.width() : bounds.width(), 0);
112 slide_window_->SetBounds(bounds);
113 direction_ = new_direction;
114 shadow_.reset(new ShadowLayerDelegate(GetFrontLayer()));
115 }
116
117 void OverscrollWindowAnimation::OnOverscrollComplete(
118 OverscrollMode overscroll_mode) {
119 if (!is_active())
120 return;
121 delegate_->OnOverscrollCompleting();
122 int content_width = GetVisibleBounds().width();
123 float translate_x;
124 if ((base::i18n::IsRTL() && direction_ == SLIDE_FRONT) ||
125 (!base::i18n::IsRTL() && direction_ == SLIDE_BACK)) {
126 translate_x = content_width;
127 } else {
128 translate_x = -content_width;
129 }
130 ui::Layer* layer = GetFrontLayer();
131 gfx::Transform transform;
132 transform.Translate(translate_x, 0);
133 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
134 settings.SetPreemptionStrategy(
135 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
136 settings.SetTweenType(gfx::Tween::EASE_OUT);
137 settings.AddObserver(this);
138 layer->SetTransform(transform);
139 }
140
141 ui::Layer* OverscrollWindowAnimation::GetFrontLayer() const {
142 DCHECK(direction_ != SLIDE_NONE);
143 if (direction_ == SLIDE_FRONT) {
144 DCHECK(slide_window_);
145 return slide_window_->layer();
146 }
147 return delegate_->GetMainWindow()->layer();
148 }
149
150 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698