| 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/toolbar/app_menu_animation.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "cc/paint/paint_flags.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 kOpenDurationMs = 733.0f; | |
| 19 constexpr float kCloseDurationMs = 283.0f; | |
| 20 | |
| 21 // Duration of the color animation in ms. | |
| 22 constexpr float kColorDurationMs = 100.0f; | |
| 23 | |
| 24 // The radius of each dot in the icon. | |
| 25 constexpr float kDotRadius = 2.0f; | |
| 26 | |
| 27 // The % the top and bottom dots need to be offset from the middle. | |
| 28 constexpr float kDotYOffset = 0.32f; | |
| 29 | |
| 30 // Value of the stroke when the icon is opened or closed. | |
| 31 constexpr float kCloseStroke = 0.204f; | |
| 32 constexpr float kOpenStroke = 0.136f; | |
| 33 | |
| 34 // Value of the width when the animation is fully opened. | |
| 35 constexpr float kOpenWidth = 0.52f; | |
| 36 | |
| 37 // The delay of the color and dot animations in ms. | |
| 38 constexpr float kColorDelayMs = 33.33f; | |
| 39 constexpr float kDotDelayMs = 66.67f; | |
| 40 | |
| 41 // The % of time it takes for each dot to animate to its full width. | |
| 42 constexpr float kTopWidthOpenInterval = 533.3f / kOpenDurationMs; | |
| 43 constexpr float kMiddleWidthOpenInterval = 383.3f / kOpenDurationMs; | |
| 44 constexpr float kBottomWidthOpenInterval = 400.0f / kOpenDurationMs; | |
| 45 | |
| 46 // The % of time it takes for each dot to animate to its final stroke. | |
| 47 constexpr float kTopStrokeOpenInterval = 400.0f / kOpenDurationMs; | |
| 48 constexpr float kMiddleStrokeOpenInterval = 283.3f / kOpenDurationMs; | |
| 49 constexpr float kBottomStrokeOpenInterval = 266.7f / kOpenDurationMs; | |
| 50 | |
| 51 // The % of time it takes for each dot to animate its width and stroke. | |
| 52 constexpr float kWidthStrokeCloseInterval = 150.0f / kCloseDurationMs; | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 AppMenuAnimation::AppMenuDot::AppMenuDot(base::TimeDelta delay, | |
| 57 float width_open_interval, | |
| 58 float stroke_open_interval) | |
| 59 : delay_(delay), | |
| 60 width_open_interval_(width_open_interval), | |
| 61 stroke_open_interval_(stroke_open_interval) {} | |
| 62 | |
| 63 void AppMenuAnimation::AppMenuDot::Paint(const gfx::PointF& center_point, | |
| 64 SkColor start_color, | |
| 65 SkColor target_color, | |
| 66 gfx::Canvas* canvas, | |
| 67 const gfx::Rect& bounds, | |
| 68 const gfx::SlideAnimation* animation, | |
| 69 AppMenuAnimationDelegate* delegate) { | |
| 70 bool is_opening = animation->IsShowing(); | |
| 71 float total_duration = is_opening ? kOpenDurationMs : kCloseDurationMs; | |
| 72 float width_duration = | |
| 73 is_opening ? width_open_interval_ : kWidthStrokeCloseInterval; | |
| 74 float stroke_duration = | |
| 75 is_opening ? stroke_open_interval_ : kWidthStrokeCloseInterval; | |
| 76 | |
| 77 // When the animation is closing, each dot uses the remainder of the full | |
| 78 // delay period (2 * kDotDelayMs). The results should be (0->2x, 1x->1x, | |
| 79 // 2x->0). | |
| 80 base::TimeDelta delay = | |
| 81 is_opening ? delay_ | |
| 82 : base::TimeDelta::FromMilliseconds(kDotDelayMs * 2) - delay_; | |
| 83 float progress = | |
| 84 animation->GetCurrentValue() - (delay.InMillisecondsF() / total_duration); | |
| 85 | |
| 86 float width_progress = 0.0; | |
| 87 float stroke_progress = 0.0; | |
| 88 float color_progress = 0.0; | |
| 89 | |
| 90 if (progress > 0) { | |
| 91 width_progress = std::min(1.0f, progress / width_duration); | |
| 92 stroke_progress = std::min(1.0f, progress / stroke_duration); | |
| 93 | |
| 94 if (is_opening) { | |
| 95 float color_delay_interval = kColorDelayMs / total_duration; | |
| 96 float color_duration_interval = kColorDurationMs / total_duration; | |
| 97 if (progress > color_delay_interval) { | |
| 98 color_progress = std::min( | |
| 99 1.0f, (progress - color_delay_interval) / color_duration_interval); | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 float dot_height = | |
| 105 gfx::Tween::FloatValueBetween(stroke_progress, kCloseStroke, kOpenStroke); | |
| 106 dot_height *= bounds.height(); | |
| 107 | |
| 108 float dot_width = | |
| 109 gfx::Tween::FloatValueBetween(width_progress, kCloseStroke, kOpenWidth); | |
| 110 dot_width *= bounds.width(); | |
| 111 | |
| 112 gfx::PointF point = center_point; | |
| 113 point.Offset(-dot_width / 2, -dot_height / 2); | |
| 114 | |
| 115 SkColor color = is_opening ? gfx::Tween::ColorValueBetween( | |
| 116 color_progress, start_color, target_color) | |
| 117 : target_color; | |
| 118 | |
| 119 cc::PaintFlags flags; | |
| 120 flags.setColor(color); | |
| 121 flags.setStrokeWidth(dot_height); | |
| 122 flags.setStrokeCap(cc::PaintFlags::kRound_Cap); | |
| 123 flags.setStyle(cc::PaintFlags::kFill_Style); | |
| 124 flags.setAntiAlias(true); | |
| 125 canvas->DrawRoundRect(gfx::RectF(point, gfx::SizeF(dot_width, dot_height)), | |
| 126 kDotRadius, flags); | |
| 127 } | |
| 128 | |
| 129 AppMenuAnimation::AppMenuAnimation(AppMenuAnimationDelegate* delegate, | |
| 130 SkColor initial_color) | |
| 131 : delegate_(delegate), | |
| 132 animation_(base::MakeUnique<gfx::SlideAnimation>(this)), | |
| 133 bottom_dot_(base::TimeDelta(), | |
| 134 kBottomWidthOpenInterval, | |
| 135 kBottomStrokeOpenInterval), | |
| 136 middle_dot_(base::TimeDelta::FromMilliseconds(kDotDelayMs), | |
| 137 kMiddleWidthOpenInterval, | |
| 138 kMiddleStrokeOpenInterval), | |
| 139 top_dot_(base::TimeDelta::FromMilliseconds(kDotDelayMs * 2), | |
| 140 kTopWidthOpenInterval, | |
| 141 kTopStrokeOpenInterval), | |
| 142 start_color_(initial_color), | |
| 143 target_color_(initial_color) { | |
| 144 animation_->SetSlideDuration(kOpenDurationMs); | |
| 145 animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN); | |
| 146 } | |
| 147 | |
| 148 AppMenuAnimation::~AppMenuAnimation() {} | |
| 149 | |
| 150 void AppMenuAnimation::PaintAppMenu(gfx::Canvas* canvas, | |
| 151 const gfx::Rect& bounds) { | |
| 152 gfx::PointF middle_point = gfx::PointF(bounds.CenterPoint()); | |
| 153 float y_offset = kDotYOffset * bounds.height(); | |
| 154 gfx::PointF top_point = middle_point; | |
| 155 top_point.Offset(0, -y_offset); | |
| 156 | |
| 157 gfx::PointF bottom_point = middle_point; | |
| 158 bottom_point.Offset(0, y_offset); | |
| 159 | |
| 160 middle_dot_.Paint(middle_point, start_color_, target_color_, canvas, bounds, | |
| 161 animation_.get(), delegate_); | |
| 162 top_dot_.Paint(top_point, start_color_, target_color_, canvas, bounds, | |
| 163 animation_.get(), delegate_); | |
| 164 bottom_dot_.Paint(bottom_point, start_color_, target_color_, canvas, bounds, | |
| 165 animation_.get(), delegate_); | |
| 166 } | |
| 167 | |
| 168 void AppMenuAnimation::StartAnimation() { | |
| 169 if (!animation_->is_animating()) { | |
| 170 animation_->SetSlideDuration(kOpenDurationMs); | |
| 171 animation_->Show(); | |
| 172 delegate_->AppMenuAnimationStarted(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void AppMenuAnimation::AnimationEnded(const gfx::Animation* animation) { | |
| 177 if (animation_->IsShowing()) { | |
| 178 animation_->SetSlideDuration(kCloseDurationMs); | |
| 179 animation_->Hide(); | |
| 180 } else { | |
| 181 start_color_ = target_color_; | |
| 182 } | |
| 183 | |
| 184 delegate_->AppMenuAnimationEnded(); | |
| 185 } | |
| 186 | |
| 187 void AppMenuAnimation::AnimationProgressed(const gfx::Animation* animation) { | |
| 188 delegate_->InvalidateIcon(); | |
| 189 } | |
| OLD | NEW |