OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "chrome/browser/ui/views/toolbar/app_menu_animation.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "cc/paint/paint_flags.h" | |
9 #include "chrome/browser/ui/views/toolbar/app_menu_button.h" | |
10 #include "ui/gfx/animation/tween.h" | |
11 #include "ui/gfx/canvas.h" | |
12 #include "ui/gfx/color_palette.h" | |
13 #include "ui/gfx/color_utils.h" | |
14 #include "ui/gfx/skia_util.h" | |
15 | |
16 namespace { | |
17 | |
18 // Duration of the open and close animations in ms. | |
19 constexpr float kOpenDuration = 733.0f; | |
msw
2017/04/13 17:23:12
nit: be consistent about the 'Ms' post-fix for dur
spqchan
2017/04/13 22:52:42
Done.
| |
20 constexpr float kCloseDuration = 283.0f; | |
21 | |
22 // Duration of the color animation in ms. | |
23 constexpr float kColorDurationMs = 100.0f; | |
24 | |
25 // The % the top and bottom dots need to be offset from the middle. | |
26 constexpr float kDotYOffset = 0.32f; | |
27 | |
28 // Value of the stroke when the icon is opened or closed. | |
29 constexpr float kCloseStroke = 0.204f; | |
30 constexpr float kOpenStroke = 0.136f; | |
31 | |
32 // Value of the width when the animation is fully opened. | |
33 constexpr float kOpenWidth = 0.52f; | |
34 | |
35 // The delay of the color and dot animations in ms. | |
36 constexpr float kColorDelayMs = 33.33f; | |
37 constexpr float kDotDelayMs = 66.67f; | |
38 | |
39 // The % of time it takes for each dot to animate to its full width. | |
40 constexpr float kTopWidthOpenInterval = 533.3f / kOpenDuration; | |
41 constexpr float kMiddleWidthOpenInterval = 383.3f / kOpenDuration; | |
42 constexpr float kBottomWidthOpenInterval = 400.0f / kOpenDuration; | |
43 | |
44 // The % of time it takes for each dot to animate to its final stroke. | |
45 constexpr float kTopStrokeOpenInterval = 400.0f / kOpenDuration; | |
46 constexpr float kMiddleStrokeOpenInterval = 283.3f / kOpenDuration; | |
47 constexpr float kBottomStrokeOpenInterval = 266.7f / kOpenDuration; | |
48 | |
49 // The % of time it takes for each dot to animate its width and stroke. | |
50 constexpr float kWidthStrokeCloseInterval = 150.0f / kCloseDuration; | |
51 | |
52 } // namespace | |
53 | |
54 AppMenuAnimation::AppMenuDot::AppMenuDot(base::TimeDelta delay, | |
55 float width_open_interval, | |
56 float stroke_open_interval) | |
57 : delay_(delay), | |
58 width_open_interval_(width_open_interval), | |
59 stroke_open_interval_(stroke_open_interval) {} | |
60 | |
61 void AppMenuAnimation::AppMenuDot::Paint(const gfx::PointF& center_point, | |
62 SkColor start_color, | |
63 SkColor severity_color, | |
64 gfx::Canvas* canvas, | |
65 const gfx::Rect& bounds, | |
66 const gfx::SlideAnimation* animation) { | |
67 bool is_opening = animation->IsShowing(); | |
68 float total_duration = is_opening ? kOpenDuration : kCloseDuration; | |
69 float width_duration = | |
70 is_opening ? width_open_interval_ : kWidthStrokeCloseInterval; | |
71 float stroke_duration = | |
72 is_opening ? stroke_open_interval_ : kWidthStrokeCloseInterval; | |
73 | |
74 // Reverse the delay if the animation is closing. | |
msw
2017/04/13 17:23:12
nit: rephrase this for a bit of clarity, 'reverse
spqchan
2017/04/13 22:52:42
I...got nothing. Hope you don't mind me using your
| |
75 base::TimeDelta delay = | |
76 is_opening ? delay_ | |
77 : base::TimeDelta::FromMicroseconds(kDotDelayMs * 2) - delay_; | |
msw
2017/04/13 17:23:12
Should this be FromMilliseconds?
spqchan
2017/04/13 22:52:42
Done.
| |
78 float progress = | |
79 animation->GetCurrentValue() - (delay.InMillisecondsF() / total_duration); | |
80 | |
81 float width_progress = 0.0; | |
82 float stroke_progress = 0.0; | |
83 float color_progress = 0.0; | |
84 | |
85 if (progress > 0) { | |
86 width_progress = std::min(1.0f, progress / width_duration); | |
87 stroke_progress = std::min(1.0f, progress / stroke_duration); | |
88 | |
89 if (is_opening) { | |
90 float color_delay = kColorDelayMs / total_duration; | |
msw
2017/04/13 17:23:12
nit: should these two values use the 'interval' po
spqchan
2017/04/13 22:52:42
Done.
| |
91 float color_duration = kColorDurationMs / total_duration; | |
92 if (progress > color_delay) { | |
93 color_progress = | |
94 std::min(1.0f, (progress - color_delay) / color_duration); | |
95 } | |
96 } | |
97 } | |
98 | |
99 float dot_height = | |
100 gfx::Tween::FloatValueBetween(stroke_progress, kCloseStroke, kOpenStroke); | |
101 dot_height *= bounds.height(); | |
102 | |
103 float dot_width = | |
104 gfx::Tween::FloatValueBetween(width_progress, kCloseStroke, kOpenWidth); | |
105 dot_width *= bounds.width(); | |
106 | |
107 gfx::PointF point = center_point; | |
108 point.Offset(-dot_width / 2, -dot_height / 2); | |
109 | |
110 SkColor color = is_opening ? gfx::Tween::ColorValueBetween( | |
111 color_progress, start_color, severity_color) | |
112 : severity_color; | |
113 | |
114 cc::PaintFlags flags; | |
115 flags.setColor(color); | |
116 flags.setStrokeWidth(bounds.height() * kCloseStroke); | |
117 flags.setStrokeCap(cc::PaintFlags::kRound_Cap); | |
118 flags.setStyle(cc::PaintFlags::kFill_Style); | |
119 flags.setAntiAlias(true); | |
120 | |
121 gfx::SizeF dot_size = gfx::SizeF(dot_width, dot_height); | |
122 canvas->DrawRoundRect(gfx::RectF(point, dot_size), 2.0, flags); | |
123 } | |
124 | |
125 AppMenuAnimation::AppMenuAnimation(AppMenuButton* owner, | |
126 bool should_animate_closed) | |
127 : owner_(owner), | |
128 should_animate_closed_(should_animate_closed), | |
129 animation_(base::MakeUnique<gfx::SlideAnimation>(this)), | |
130 bottom_dot_(base::TimeDelta(), | |
131 kBottomWidthOpenInterval, | |
132 kBottomStrokeOpenInterval), | |
133 middle_dot_(base::TimeDelta::FromMicroseconds(kDotDelayMs), | |
msw
2017/04/13 17:23:12
Should this be FromMilliseconds?
spqchan
2017/04/13 22:52:42
Done.
| |
134 kMiddleWidthOpenInterval, | |
135 kMiddleStrokeOpenInterval), | |
136 top_dot_(base::TimeDelta::FromMicroseconds(kDotDelayMs * 2), | |
msw
2017/04/13 17:23:12
Should this be FromMilliseconds?
spqchan
2017/04/13 22:52:42
Done.
| |
137 kTopWidthOpenInterval, | |
138 kTopStrokeOpenInterval), | |
139 start_color_(gfx::kPlaceholderColor), | |
140 severity_color_(gfx::kPlaceholderColor) { | |
141 animation_->SetSlideDuration(kOpenDuration); | |
142 animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN); | |
143 } | |
144 | |
145 AppMenuAnimation::~AppMenuAnimation() {} | |
146 | |
147 void AppMenuAnimation::PaintAppMenu(gfx::Canvas* canvas, | |
148 const gfx::Rect& bounds) { | |
149 gfx::PointF middle_point = gfx::PointF(bounds.CenterPoint()); | |
150 float y_offset = kDotYOffset * bounds.height(); | |
151 gfx::PointF top_point = middle_point; | |
152 top_point.Offset(0, -y_offset); | |
153 | |
154 gfx::PointF bottom_point = middle_point; | |
155 bottom_point.Offset(0, y_offset); | |
156 | |
157 middle_dot_.Paint(middle_point, start_color_, severity_color_, canvas, bounds, | |
158 animation_.get()); | |
159 top_dot_.Paint(top_point, start_color_, severity_color_, canvas, bounds, | |
160 animation_.get()); | |
161 bottom_dot_.Paint(bottom_point, start_color_, severity_color_, canvas, bounds, | |
162 animation_.get()); | |
163 } | |
164 | |
165 void AppMenuAnimation::SetIconColors(SkColor start_color, | |
166 SkColor severity_color) { | |
167 start_color_ = start_color; | |
168 severity_color_ = severity_color; | |
169 } | |
170 | |
171 void AppMenuAnimation::StartAnimation() { | |
172 if (!animation_->is_animating()) { | |
173 animation_->SetSlideDuration(kOpenDuration); | |
174 animation_->Show(); | |
175 owner_->AppMenuAnimationStarted(); | |
176 } | |
177 } | |
178 | |
179 void AppMenuAnimation::AnimationEnded(const gfx::Animation* animation) { | |
180 if (animation_->IsShowing() && should_animate_closed_) { | |
181 animation_->SetSlideDuration(kCloseDuration); | |
182 animation_->Hide(); | |
183 return; | |
184 } | |
185 | |
186 if (!animation_->IsShowing()) | |
187 start_color_ = severity_color_; | |
188 | |
189 owner_->AppMenuAnimationEnded(); | |
190 } | |
191 | |
192 void AppMenuAnimation::AnimationProgressed(const gfx::Animation* animation) { | |
193 owner_->SchedulePaint(); | |
194 } | |
OLD | NEW |