Chromium Code Reviews| 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 kOpenDurationMs = 733.0f; | |
| 20 constexpr float kCloseDurationMs = 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 / kOpenDurationMs; | |
| 41 constexpr float kMiddleWidthOpenInterval = 383.3f / kOpenDurationMs; | |
| 42 constexpr float kBottomWidthOpenInterval = 400.0f / kOpenDurationMs; | |
| 43 | |
| 44 // The % of time it takes for each dot to animate to its final stroke. | |
| 45 constexpr float kTopStrokeOpenInterval = 400.0f / kOpenDurationMs; | |
| 46 constexpr float kMiddleStrokeOpenInterval = 283.3f / kOpenDurationMs; | |
| 47 constexpr float kBottomStrokeOpenInterval = 266.7f / kOpenDurationMs; | |
| 48 | |
| 49 // The % of time it takes for each dot to animate its width and stroke. | |
| 50 constexpr float kWidthStrokeCloseInterval = 150.0f / kCloseDurationMs; | |
| 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 ? kOpenDurationMs : kCloseDurationMs; | |
| 69 float width_duration = | |
| 70 is_opening ? width_open_interval_ : kWidthStrokeCloseInterval; | |
| 71 float stroke_duration = | |
| 72 is_opening ? stroke_open_interval_ : kWidthStrokeCloseInterval; | |
| 73 | |
| 74 // When the animation is closing, the the remainder of the full 2*kDotDelayMs | |
|
msw
2017/04/13 23:05:24
nit: s/the the/each dot uses the/ or similar
spqchan
2017/04/13 23:48:21
Done.
| |
| 75 // delay period (0->2x, 1x->1x, 2x->0). | |
| 76 base::TimeDelta delay = | |
| 77 is_opening ? delay_ | |
| 78 : base::TimeDelta::FromMilliseconds(kDotDelayMs * 2) - delay_; | |
| 79 float progress = | |
| 80 animation->GetCurrentValue() - (delay.InMillisecondsF() / total_duration); | |
| 81 | |
| 82 float width_progress = 0.0; | |
| 83 float stroke_progress = 0.0; | |
| 84 float color_progress = 0.0; | |
| 85 | |
| 86 if (progress > 0) { | |
| 87 width_progress = std::min(1.0f, progress / width_duration); | |
| 88 stroke_progress = std::min(1.0f, progress / stroke_duration); | |
| 89 | |
| 90 if (is_opening) { | |
| 91 float color_delay_interval = kColorDelayMs / total_duration; | |
| 92 float color_duration_interval = kColorDurationMs / total_duration; | |
| 93 if (progress > color_delay_interval) { | |
| 94 color_progress = std::min( | |
| 95 1.0f, (progress - color_delay_interval) / color_duration_interval); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 float dot_height = | |
| 101 gfx::Tween::FloatValueBetween(stroke_progress, kCloseStroke, kOpenStroke); | |
| 102 dot_height *= bounds.height(); | |
| 103 | |
| 104 float dot_width = | |
| 105 gfx::Tween::FloatValueBetween(width_progress, kCloseStroke, kOpenWidth); | |
| 106 dot_width *= bounds.width(); | |
| 107 | |
| 108 gfx::PointF point = center_point; | |
| 109 point.Offset(-dot_width / 2, -dot_height / 2); | |
| 110 | |
| 111 SkColor color = is_opening ? gfx::Tween::ColorValueBetween( | |
| 112 color_progress, start_color, severity_color) | |
| 113 : severity_color; | |
| 114 | |
| 115 cc::PaintFlags flags; | |
| 116 flags.setColor(color); | |
| 117 flags.setStrokeWidth(bounds.height() * kCloseStroke); | |
| 118 flags.setStrokeCap(cc::PaintFlags::kRound_Cap); | |
| 119 flags.setStyle(cc::PaintFlags::kFill_Style); | |
| 120 flags.setAntiAlias(true); | |
| 121 | |
| 122 gfx::SizeF dot_size = gfx::SizeF(dot_width, dot_height); | |
| 123 canvas->DrawRoundRect(gfx::RectF(point, dot_size), 2.0, flags); | |
| 124 } | |
| 125 | |
| 126 AppMenuAnimation::AppMenuAnimation(AppMenuButton* owner, | |
| 127 bool should_animate_closed) | |
| 128 : owner_(owner), | |
| 129 should_animate_closed_(should_animate_closed), | |
| 130 animation_(base::MakeUnique<gfx::SlideAnimation>(this)), | |
| 131 bottom_dot_(base::TimeDelta(), | |
| 132 kBottomWidthOpenInterval, | |
| 133 kBottomStrokeOpenInterval), | |
| 134 middle_dot_(base::TimeDelta::FromMilliseconds(kDotDelayMs), | |
| 135 kMiddleWidthOpenInterval, | |
| 136 kMiddleStrokeOpenInterval), | |
| 137 top_dot_(base::TimeDelta::FromMilliseconds(kDotDelayMs * 2), | |
| 138 kTopWidthOpenInterval, | |
| 139 kTopStrokeOpenInterval), | |
| 140 start_color_(gfx::kPlaceholderColor), | |
| 141 severity_color_(gfx::kPlaceholderColor) { | |
| 142 animation_->SetSlideDuration(kOpenDurationMs); | |
| 143 animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN); | |
| 144 } | |
| 145 | |
| 146 AppMenuAnimation::~AppMenuAnimation() {} | |
| 147 | |
| 148 void AppMenuAnimation::PaintAppMenu(gfx::Canvas* canvas, | |
| 149 const gfx::Rect& bounds) { | |
| 150 gfx::PointF middle_point = gfx::PointF(bounds.CenterPoint()); | |
| 151 float y_offset = kDotYOffset * bounds.height(); | |
| 152 gfx::PointF top_point = middle_point; | |
| 153 top_point.Offset(0, -y_offset); | |
| 154 | |
| 155 gfx::PointF bottom_point = middle_point; | |
| 156 bottom_point.Offset(0, y_offset); | |
| 157 | |
| 158 middle_dot_.Paint(middle_point, start_color_, severity_color_, canvas, bounds, | |
| 159 animation_.get()); | |
| 160 top_dot_.Paint(top_point, start_color_, severity_color_, canvas, bounds, | |
| 161 animation_.get()); | |
| 162 bottom_dot_.Paint(bottom_point, start_color_, severity_color_, canvas, bounds, | |
| 163 animation_.get()); | |
| 164 } | |
| 165 | |
| 166 void AppMenuAnimation::SetIconColors(SkColor start_color, | |
| 167 SkColor severity_color) { | |
| 168 start_color_ = start_color; | |
| 169 severity_color_ = severity_color; | |
| 170 } | |
| 171 | |
| 172 void AppMenuAnimation::StartAnimation() { | |
| 173 if (!animation_->is_animating()) { | |
| 174 animation_->SetSlideDuration(kOpenDurationMs); | |
| 175 animation_->Show(); | |
| 176 owner_->AppMenuAnimationStarted(); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 void AppMenuAnimation::AnimationEnded(const gfx::Animation* animation) { | |
| 181 if (animation_->IsShowing() && should_animate_closed_) { | |
| 182 animation_->SetSlideDuration(kCloseDurationMs); | |
| 183 animation_->Hide(); | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 if (!animation_->IsShowing()) | |
| 188 start_color_ = severity_color_; | |
| 189 | |
| 190 owner_->AppMenuAnimationEnded(); | |
| 191 } | |
| 192 | |
| 193 void AppMenuAnimation::AnimationProgressed(const gfx::Animation* animation) { | |
| 194 owner_->SchedulePaint(); | |
| 195 } | |
| OLD | NEW |