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