diff options
Diffstat (limited to 'chromium/ui/gfx/paint_throbber.cc')
-rw-r--r-- | chromium/ui/gfx/paint_throbber.cc | 87 |
1 files changed, 43 insertions, 44 deletions
diff --git a/chromium/ui/gfx/paint_throbber.cc b/chromium/ui/gfx/paint_throbber.cc index 83a31693365..e59b25bf820 100644 --- a/chromium/ui/gfx/paint_throbber.cc +++ b/chromium/ui/gfx/paint_throbber.cc @@ -4,6 +4,8 @@ #include "ui/gfx/paint_throbber.h" +#include <algorithm> + #include "base/time/time.h" #include "cc/paint/paint_flags.h" #include "third_party/skia/include/core/SkPath.h" @@ -18,14 +20,14 @@ namespace gfx { namespace { // The maximum size of the "spinning" state arc, in degrees. -const int64_t kMaxArcSize = 270; +constexpr int64_t kMaxArcSize = 270; // The amount of time it takes to grow the "spinning" arc from 0 to 270 degrees. -const int64_t kArcTimeMs = 666; +constexpr auto kArcTime = base::TimeDelta::FromSecondsD(2.0 / 3.0); // The amount of time it takes for the "spinning" throbber to make a full // rotation. -const int64_t kRotationTimeMs = 1568; +constexpr auto kRotationTime = base::TimeDelta::FromMilliseconds(1568); void PaintArc(Canvas* canvas, const Rect& bounds, @@ -44,7 +46,7 @@ void PaintArc(Canvas* canvas, Rect oval = bounds; // Inset by half the stroke width to make sure the whole arc is inside // the visible rect. - int inset = SkScalarCeilToInt(*stroke_width / 2.0); + const int inset = SkScalarCeilToInt(*stroke_width / 2.0); oval.Inset(inset, inset); SkPath path; @@ -66,10 +68,10 @@ void CalculateWaitingAngles(const base::TimeDelta& elapsed_time, // the throbber spins counter-clockwise. The finish angle starts at 12 o'clock // (90 degrees) and rotates steadily. The start angle trails 180 degrees // behind, except for the first half revolution, when it stays at 12 o'clock. - base::TimeDelta revolution_time = base::TimeDelta::FromMilliseconds(1320); + constexpr auto kRevolutionTime = base::TimeDelta::FromMilliseconds(1320); int64_t twelve_oclock = 90; int64_t finish_angle_cc = - twelve_oclock + 360 * elapsed_time / revolution_time; + twelve_oclock + 360 * elapsed_time / kRevolutionTime; int64_t start_angle_cc = std::max(finish_angle_cc - 180, twelve_oclock); // Negate the angles to convert to the clockwise numbers Skia expects. @@ -91,31 +93,29 @@ void PaintThrobberSpinningWithStartAngle( // The sweep angle ranges from -270 to 270 over 1333ms. CSS // animation timing functions apply in between key frames, so we have to // break up the 1333ms into two keyframes (-270 to 0, then 0 to 270). - base::TimeDelta arc_time = base::TimeDelta::FromMilliseconds(kArcTimeMs); - double arc_size_progress = static_cast<double>(elapsed_time.InMicroseconds() % - arc_time.InMicroseconds()) / - arc_time.InMicroseconds(); + const double arc_progress = + (elapsed_time % kArcTime).InMicrosecondsF() / kArcTime.InMicrosecondsF(); // This tween is equivalent to cubic-bezier(0.4, 0.0, 0.2, 1). - double sweep = kMaxArcSize * Tween::CalculateValue(Tween::FAST_OUT_SLOW_IN, - arc_size_progress); - int64_t sweep_keyframe = (elapsed_time / arc_time) % 2; + double sweep = kMaxArcSize * + Tween::CalculateValue(Tween::FAST_OUT_SLOW_IN, arc_progress); + const int64_t sweep_keyframe = (elapsed_time / kArcTime) % 2; if (sweep_keyframe == 0) sweep -= kMaxArcSize; // This part makes sure the sweep is at least 5 degrees long. Roughly // equivalent to the "magic constants" in SVG's fillunfill animation. - const double min_sweep_length = 5.0; - if (sweep >= 0.0 && sweep < min_sweep_length) { - start_angle -= (min_sweep_length - sweep); - sweep = min_sweep_length; - } else if (sweep <= 0.0 && sweep > -min_sweep_length) { - start_angle += (-min_sweep_length - sweep); - sweep = -min_sweep_length; + constexpr double kMinSweepLength = 5.0; + if (sweep >= 0.0 && sweep < kMinSweepLength) { + start_angle -= (kMinSweepLength - sweep); + sweep = kMinSweepLength; + } else if (sweep <= 0.0 && sweep > -kMinSweepLength) { + start_angle += (-kMinSweepLength - sweep); + sweep = -kMinSweepLength; } // To keep the sweep smooth, we have an additional rotation after each - // |arc_time| period has elapsed. See SVG's 'rot' animation. - int64_t rot_keyframe = (elapsed_time / (arc_time * 2)) % 4; + // arc period has elapsed. See SVG's 'rot' animation. + const int64_t rot_keyframe = (elapsed_time / (kArcTime * 2)) % 4; PaintArc(canvas, bounds, color, start_angle + rot_keyframe * kMaxArcSize, sweep, stroke_width); } @@ -127,9 +127,7 @@ void PaintThrobberSpinning(Canvas* canvas, SkColor color, const base::TimeDelta& elapsed_time, base::Optional<SkScalar> stroke_width) { - base::TimeDelta rotation_time = - base::TimeDelta::FromMilliseconds(kRotationTimeMs); - int64_t start_angle = 270 + 360 * elapsed_time / rotation_time; + const int64_t start_angle = 270 + 360 * elapsed_time / kRotationTime; PaintThrobberSpinningWithStartAngle(canvas, bounds, color, elapsed_time, start_angle, stroke_width); } @@ -157,35 +155,34 @@ void PaintThrobberSpinningAfterWaiting(Canvas* canvas, // |arc_time_offset| is the effective amount of time one would have to wait // for the "spinning" sweep to match |waiting_sweep|. Brute force calculation. if (waiting_state->arc_time_offset.is_zero()) { - for (int64_t arc_time_it = 0; arc_time_it <= kArcTimeMs; ++arc_time_it) { - double arc_size_progress = static_cast<double>(arc_time_it) / kArcTimeMs; + for (int64_t arc_ms = 0; arc_ms <= kArcTime.InMillisecondsRoundedUp(); + ++arc_ms) { + double arc_size_progress = + std::min(1.0, arc_ms / kArcTime.InMillisecondsF()); if (kMaxArcSize * Tween::CalculateValue(Tween::FAST_OUT_SLOW_IN, arc_size_progress) >= waiting_sweep) { - // Add kArcTimeMs to sidestep the |sweep_keyframe == 0| offset below. - waiting_state->arc_time_offset = - base::TimeDelta::FromMilliseconds(arc_time_it + kArcTimeMs); + // Add kArcTime to sidestep the |sweep_keyframe == 0| offset below. + waiting_state->arc_time_offset = kArcTime * (arc_size_progress + 1); break; } } } // Blend the color between "waiting" and "spinning" states. - base::TimeDelta color_fade_time = base::TimeDelta::FromMilliseconds(900); + constexpr auto kColorFadeTime = base::TimeDelta::FromMilliseconds(900); float color_progress = 1.0f; - if (elapsed_time < color_fade_time) { - color_progress = static_cast<float>(Tween::CalculateValue( + if (elapsed_time < kColorFadeTime) { + color_progress = float{Tween::CalculateValue( Tween::LINEAR_OUT_SLOW_IN, - static_cast<double>(elapsed_time.InMicroseconds()) / - color_fade_time.InMicroseconds())); + elapsed_time.InMicrosecondsF() / kColorFadeTime.InMicrosecondsF())}; } - SkColor blend_color = + const SkColor blend_color = color_utils::AlphaBlend(color, waiting_state->color, color_progress); - int64_t start_angle = - waiting_start_angle + - 360 * elapsed_time / base::TimeDelta::FromMilliseconds(kRotationTimeMs); - base::TimeDelta effective_elapsed_time = + const int64_t start_angle = + waiting_start_angle + 360 * elapsed_time / kRotationTime; + const base::TimeDelta effective_elapsed_time = elapsed_time + waiting_state->arc_time_offset; PaintThrobberSpinningWithStartAngle(canvas, bounds, blend_color, @@ -197,13 +194,15 @@ GFX_EXPORT void PaintNewThrobberWaiting(Canvas* canvas, const RectF& throbber_container_bounds, SkColor color, const base::TimeDelta& elapsed_time) { + // Cycle time for the waiting throbber. + constexpr auto kNewThrobberWaitingCycleTime = base::TimeDelta::FromSeconds(1); + // The throbber bounces back and forth. We map the elapsed time to 0->2. Time // 0->1 represents when the throbber moves left to right, time 1->2 represents // right to left. - float time = - 2.0f * - (elapsed_time.InMilliseconds() % kNewThrobberWaitingAnimationCycleMs) / - kNewThrobberWaitingAnimationCycleMs; + float time = 2.0f * + (elapsed_time % kNewThrobberWaitingCycleTime).InMicrosecondsF() / + kNewThrobberWaitingCycleTime.InMicrosecondsF(); // 1 -> 2 values mirror back to 1 -> 0 values to represent right-to-left. const bool going_back = time > 1.0f; if (going_back) |