Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_PAINT_THROBBER_H_ |
| 6 | #define UI_GFX_PAINT_THROBBER_H_ |
| 7 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Arthur Sonzogni | 3eb9fd51 | 2024-02-09 12:20:43 | [diff] [blame^] | 10 | #include <optional> |
| 11 | |
estade | 618db46 | 2015-05-28 00:52:30 | [diff] [blame] | 12 | #include "base/time/time.h" |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkColor.h" |
| 14 | #include "ui/gfx/gfx_export.h" |
| 15 | |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 16 | namespace gfx { |
| 17 | |
| 18 | class Canvas; |
| 19 | class Rect; |
Peter Boström | 18a40fa | 2018-10-31 19:03:50 | [diff] [blame] | 20 | class RectF; |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 21 | |
David Black | 869a906b | 2021-07-23 05:38:04 | [diff] [blame] | 22 | // This struct describes the "spinning" state of a throb animation. |
| 23 | struct GFX_EXPORT ThrobberSpinningState { |
| 24 | // The start angle of the arc in degrees. |
| 25 | SkScalar start_angle = 0.f; |
| 26 | |
| 27 | // The sweep angle of the arc in degrees. Positive is clockwise. |
| 28 | SkScalar sweep_angle = 0.f; |
| 29 | }; |
| 30 | |
estade | 618db46 | 2015-05-28 00:52:30 | [diff] [blame] | 31 | // This struct describes the "waiting" mode of a throb animation. It's useful |
| 32 | // for building a "spinning" state animation on top of a previous "waiting" |
| 33 | // mode animation. See PaintThrobberSpinningAfterWaiting. |
| 34 | struct GFX_EXPORT ThrobberWaitingState { |
| 35 | // The amount of time that was spent in the waiting state. |
| 36 | base::TimeDelta elapsed_time; |
Brett Wilson | 0e161fe5 | 2017-12-01 00:05:29 | [diff] [blame] | 37 | |
estade | 618db46 | 2015-05-28 00:52:30 | [diff] [blame] | 38 | // The color of the arc in the waiting state. |
Brett Wilson | 0e161fe5 | 2017-12-01 00:05:29 | [diff] [blame] | 39 | SkColor color = SK_ColorTRANSPARENT; |
| 40 | |
estade | 618db46 | 2015-05-28 00:52:30 | [diff] [blame] | 41 | // An opaque value used to cache calculations made by |
| 42 | // PaintThrobberSpinningAfterWaiting. |
| 43 | base::TimeDelta arc_time_offset; |
| 44 | }; |
| 45 | |
David Black | 869a906b | 2021-07-23 05:38:04 | [diff] [blame] | 46 | // Returns the calculated state for a single frame of the throbber in the |
| 47 | // "spinning", aka Material, state. Note that animation duration is a hardcoded |
| 48 | // value in line with Material design specifications but is cyclic, so the |
| 49 | // specified `elapsed_time` may exceed animation duration. |
| 50 | GFX_EXPORT ThrobberSpinningState |
| 51 | CalculateThrobberSpinningState(base::TimeDelta elapsed_time); |
| 52 | |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 53 | // Paints a single frame of the throbber in the "spinning", aka Material, state. |
David Black | 869a906b | 2021-07-23 05:38:04 | [diff] [blame] | 54 | // Note that animation duration is a hardcoded value in line with Material |
| 55 | // design specifications but is cyclic, so the specified `elapsed_time` may |
| 56 | // exceed animation duration. |
Peter Boström | 7c316e2d | 2019-01-19 03:26:38 | [diff] [blame] | 57 | GFX_EXPORT void PaintThrobberSpinning( |
| 58 | Canvas* canvas, |
| 59 | const Rect& bounds, |
| 60 | SkColor color, |
| 61 | const base::TimeDelta& elapsed_time, |
Arthur Sonzogni | 3eb9fd51 | 2024-02-09 12:20:43 | [diff] [blame^] | 62 | std::optional<SkScalar> stroke_width = std::nullopt); |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 63 | |
| 64 | // Paints a throbber in the "waiting" state. Used when waiting on a network |
| 65 | // response, for example. |
Peter Boström | 7c316e2d | 2019-01-19 03:26:38 | [diff] [blame] | 66 | GFX_EXPORT void PaintThrobberWaiting( |
| 67 | Canvas* canvas, |
| 68 | const Rect& bounds, |
| 69 | SkColor color, |
| 70 | const base::TimeDelta& elapsed_time, |
Arthur Sonzogni | 3eb9fd51 | 2024-02-09 12:20:43 | [diff] [blame^] | 71 | std::optional<SkScalar> stroke_width = std::nullopt); |
estade | 618db46 | 2015-05-28 00:52:30 | [diff] [blame] | 72 | |
| 73 | // Paint a throbber in the "spinning" state, smoothly transitioning from a |
| 74 | // previous "waiting" state described by |waiting_state|, which is an in-out |
| 75 | // param. |
| 76 | GFX_EXPORT void PaintThrobberSpinningAfterWaiting( |
| 77 | Canvas* canvas, |
| 78 | const Rect& bounds, |
| 79 | SkColor color, |
| 80 | const base::TimeDelta& elapsed_time, |
Peter Boström | 7c316e2d | 2019-01-19 03:26:38 | [diff] [blame] | 81 | ThrobberWaitingState* waiting_state, |
Arthur Sonzogni | 3eb9fd51 | 2024-02-09 12:20:43 | [diff] [blame^] | 82 | std::optional<SkScalar> stroke_width = std::nullopt); |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 83 | |
Peter Boström | 18a40fa | 2018-10-31 19:03:50 | [diff] [blame] | 84 | // Paints a throbber in the "waiting" state (bouncing back and forth). Used when |
| 85 | // waiting on a network response, for example. |
| 86 | GFX_EXPORT void PaintNewThrobberWaiting(Canvas* canvas, |
| 87 | const RectF& throbber_container_bounds, |
| 88 | SkColor color, |
| 89 | const base::TimeDelta& elapsed_time); |
estade | b597175 | 2015-05-27 23:30:57 | [diff] [blame] | 90 | |
estade | 4b7d20a | 2015-05-03 20:21:26 | [diff] [blame] | 91 | } // namespace gfx |
| 92 | |
| 93 | #endif // UI_GFX_PAINT_THROBBER_H_ |