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/time/time.h" | |
|
msw
2017/04/11 17:34:13
q: needed?
spqchan
2017/04/12 19:42:09
Removed
| |
| 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 const float kOpenDuration = 733.0f; | |
|
msw
2017/04/11 17:34:13
nit: constexpr for all these
spqchan
2017/04/12 19:42:09
Done.
| |
| 20 const float kCloseDuration = 283.0f; | |
| 21 | |
| 22 // Duration of the color animation in ms. | |
| 23 const float kColorDurationMs = 100.0f; | |
| 24 | |
| 25 // The % the top and bottom dots need to be offset from the middle. | |
| 26 const float kDotYOffset = 0.32f; | |
| 27 | |
| 28 // Value of the stroke when the icon is opened or closed. | |
| 29 const float kCloseStroke = 0.204f; | |
| 30 const float kOpenStroke = 0.136f; | |
| 31 | |
| 32 // Value of the width when the animation is fully opened. | |
| 33 const float kOpenWidth = 0.52f; | |
| 34 | |
| 35 // The delay of the color and dot animations in ms. | |
| 36 const float kColorDelayMs = 33.33f; | |
| 37 const float kDotDelayMs = 66.67f; | |
| 38 | |
| 39 // The % of time it takes for each dot to animate to its full width. | |
| 40 const float kTopWidthOpenInterval = 533.3f / kOpenDuration; | |
| 41 const float kMiddleWidthOpenInterval = 383.3f / kOpenDuration; | |
| 42 const float kBottomWidthOpenInterval = 400.0f / kOpenDuration; | |
| 43 | |
| 44 // The % of time it takes for each dot to animate to its final stroke. | |
| 45 const float kTopStrokeOpenInterval = 400.0f / kOpenDuration; | |
| 46 const float kMiddleStrokeOpenInterval = 283.3f / kOpenDuration; | |
| 47 const float kBottomStrokeOpenInterval = 266.7f / kOpenDuration; | |
| 48 | |
| 49 // The % of time it takes for each dot to animate its width and stroke. | |
| 50 const float kWidthStrokeCloseInterval = 150.0f / kCloseDuration; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 AppMenuAnimation::AppMenuDot::AppMenuDot(float delay, | |
| 55 float widthOpenInterval, | |
|
msw
2017/04/11 17:34:13
Use the |unix_hacker| naming convention for anythi
spqchan
2017/04/12 19:42:09
Done.
| |
| 56 float strokeOpenInterval) | |
| 57 : delay_(delay), | |
| 58 widthOpenInterval_(widthOpenInterval), | |
| 59 strokeOpenInterval_(strokeOpenInterval) {} | |
| 60 | |
| 61 void AppMenuAnimation::AppMenuDot::Paint(gfx::PointF center_pt, | |
|
msw
2017/04/11 17:34:13
nit: |center_point|
spqchan
2017/04/12 19:42:09
Done.
| |
| 62 SkColor start_color, | |
| 63 SkColor severity_color, | |
| 64 gfx::Canvas* canvas, | |
| 65 const gfx::Rect& bounds, | |
| 66 gfx::SlideAnimation* animation) { | |
| 67 bool is_opening = animation->IsShowing(); | |
| 68 float totalDuration = is_opening ? kOpenDuration : kCloseDuration; | |
| 69 float widthDuration = | |
| 70 is_opening ? widthOpenInterval_ : kWidthStrokeCloseInterval; | |
| 71 float strokeDuration = | |
| 72 is_opening ? strokeOpenInterval_ : kWidthStrokeCloseInterval; | |
| 73 | |
| 74 // Reverse the delay if the animation is closing. | |
| 75 float delay = is_opening ? delay_ : kDotDelayMs * 2 - delay_; | |
|
msw
2017/04/11 17:34:13
nit: use parens around (kDotDelayMs * 2 - delay_)
spqchan
2017/04/12 19:42:09
Done.
| |
| 76 | |
| 77 float widthProgress = 0.0; | |
|
msw
2017/04/11 17:34:13
Use the |unix_hacker| naming convention for these:
spqchan
2017/04/12 19:42:09
Done.
| |
| 78 float strokeProgress = 0.0; | |
| 79 float colorProgress = 0.0; | |
| 80 float progress = animation->GetCurrentValue() - (delay / totalDuration); | |
|
msw
2017/04/11 17:34:13
nit: move this up immediately after |delay|, for c
spqchan
2017/04/12 19:42:09
Done.
| |
| 81 | |
| 82 if (progress > 0) { | |
| 83 widthProgress = std::min(1.0f, progress / widthDuration); | |
| 84 strokeProgress = std::min(1.0f, progress / strokeDuration); | |
| 85 | |
| 86 if (is_opening) { | |
| 87 float colorDelay = kColorDelayMs / totalDuration; | |
| 88 float colorDuration = kColorDurationMs / totalDuration; | |
| 89 if (progress > colorDelay) { | |
| 90 colorProgress = std::min(1.0f, (progress - colorDelay) / colorDuration); | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 float dot_height = | |
| 96 gfx::Tween::FloatValueBetween(strokeProgress, kCloseStroke, kOpenStroke); | |
| 97 dot_height *= bounds.height(); | |
| 98 | |
| 99 float dot_width = | |
| 100 gfx::Tween::FloatValueBetween(widthProgress, kCloseStroke, kOpenWidth); | |
| 101 dot_width *= bounds.width(); | |
| 102 | |
| 103 gfx::PointF point = center_pt; | |
| 104 point.Offset(-dot_width / 2, -dot_height / 2); | |
| 105 | |
| 106 SkColor color = is_opening ? gfx::Tween::ColorValueBetween( | |
| 107 colorProgress, 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), | |
| 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 |