paint_throbber.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2015 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. #ifndef UI_GFX_PAINT_THROBBER_H_
  5. #define UI_GFX_PAINT_THROBBER_H_
  6. #include <stdint.h>
  7. #include "base/time/time.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/gfx/gfx_export.h"
  11. namespace gfx {
  12. class Canvas;
  13. class Rect;
  14. class RectF;
  15. // This struct describes the "spinning" state of a throb animation.
  16. struct GFX_EXPORT ThrobberSpinningState {
  17. // The start angle of the arc in degrees.
  18. SkScalar start_angle = 0.f;
  19. // The sweep angle of the arc in degrees. Positive is clockwise.
  20. SkScalar sweep_angle = 0.f;
  21. };
  22. // This struct describes the "waiting" mode of a throb animation. It's useful
  23. // for building a "spinning" state animation on top of a previous "waiting"
  24. // mode animation. See PaintThrobberSpinningAfterWaiting.
  25. struct GFX_EXPORT ThrobberWaitingState {
  26. // The amount of time that was spent in the waiting state.
  27. base::TimeDelta elapsed_time;
  28. // The color of the arc in the waiting state.
  29. SkColor color = SK_ColorTRANSPARENT;
  30. // An opaque value used to cache calculations made by
  31. // PaintThrobberSpinningAfterWaiting.
  32. base::TimeDelta arc_time_offset;
  33. };
  34. // Returns the calculated state for a single frame of the throbber in the
  35. // "spinning", aka Material, state. Note that animation duration is a hardcoded
  36. // value in line with Material design specifications but is cyclic, so the
  37. // specified `elapsed_time` may exceed animation duration.
  38. GFX_EXPORT ThrobberSpinningState
  39. CalculateThrobberSpinningState(base::TimeDelta elapsed_time);
  40. // Paints a single frame of the throbber in the "spinning", aka Material, state.
  41. // Note that animation duration is a hardcoded value in line with Material
  42. // design specifications but is cyclic, so the specified `elapsed_time` may
  43. // exceed animation duration.
  44. GFX_EXPORT void PaintThrobberSpinning(
  45. Canvas* canvas,
  46. const Rect& bounds,
  47. SkColor color,
  48. const base::TimeDelta& elapsed_time,
  49. absl::optional<SkScalar> stroke_width = absl::nullopt);
  50. // Paints a throbber in the "waiting" state. Used when waiting on a network
  51. // response, for example.
  52. GFX_EXPORT void PaintThrobberWaiting(
  53. Canvas* canvas,
  54. const Rect& bounds,
  55. SkColor color,
  56. const base::TimeDelta& elapsed_time,
  57. absl::optional<SkScalar> stroke_width = absl::nullopt);
  58. // Paint a throbber in the "spinning" state, smoothly transitioning from a
  59. // previous "waiting" state described by |waiting_state|, which is an in-out
  60. // param.
  61. GFX_EXPORT void PaintThrobberSpinningAfterWaiting(
  62. Canvas* canvas,
  63. const Rect& bounds,
  64. SkColor color,
  65. const base::TimeDelta& elapsed_time,
  66. ThrobberWaitingState* waiting_state,
  67. absl::optional<SkScalar> stroke_width = absl::nullopt);
  68. // Paints a throbber in the "waiting" state (bouncing back and forth). Used when
  69. // waiting on a network response, for example.
  70. GFX_EXPORT void PaintNewThrobberWaiting(Canvas* canvas,
  71. const RectF& throbber_container_bounds,
  72. SkColor color,
  73. const base::TimeDelta& elapsed_time);
  74. } // namespace gfx
  75. #endif // UI_GFX_PAINT_THROBBER_H_