Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sammie Quon | c5976c9 | 2023-04-28 23:54:58 | [diff] [blame] | 5 | #include "ash/system/phonehub/phone_hub_app_loading_icon.h" |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 6 | |
| 7 | #include "ash/style/ash_color_provider.h" |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 8 | #include "ui/base/metadata/metadata_impl_macros.h" |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 9 | #include "ui/compositor/layer.h" |
| 10 | #include "ui/gfx/canvas.h" |
| 11 | #include "ui/gfx/image/canvas_image_source.h" |
Curt Clemens | 533b501 | 2023-03-21 21:46:15 | [diff] [blame] | 12 | #include "ui/views/animation/animation_builder.h" |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 13 | |
| 14 | namespace ash { |
Curt Clemens | 971477fd | 2023-03-17 00:15:07 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 18 | // Constants for loading animation |
| 19 | constexpr float kAnimationOpacityHigh = 1.0f; |
| 20 | constexpr float kAnimationOpacityLow = 0.5f; |
| 21 | constexpr int kAnimationFadeDownDurationInMs = 500; |
| 22 | constexpr int kAnimationFadeUpDurationInMs = 500; |
| 23 | |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 24 | class LoadingCircle : public gfx::CanvasImageSource { |
| 25 | public: |
Curt Clemens | 971477fd | 2023-03-17 00:15:07 | [diff] [blame] | 26 | explicit LoadingCircle(int size) |
| 27 | : CanvasImageSource(AppIcon::GetRecommendedImageSize(size)) {} |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 28 | |
| 29 | LoadingCircle(const LoadingCircle&) = delete; |
| 30 | LoadingCircle& operator=(const LoadingCircle&) = delete; |
| 31 | |
| 32 | void Draw(gfx::Canvas* canvas) override { |
| 33 | float radius = size().width() / 2.0f; |
| 34 | cc::PaintFlags flags; |
| 35 | flags.setStyle(cc::PaintFlags::kFill_Style); |
| 36 | flags.setAntiAlias(true); |
| 37 | flags.setColor(AshColorProvider::Get()->GetControlsLayerColor( |
| 38 | AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive)); |
| 39 | canvas->DrawCircle(gfx::PointF(radius, radius), radius, flags); |
| 40 | } |
| 41 | }; |
| 42 | |
Curt Clemens | 971477fd | 2023-03-17 00:15:07 | [diff] [blame] | 43 | } // namespace |
| 44 | |
| 45 | AppLoadingIcon::AppLoadingIcon(int size) |
| 46 | : AppIcon(gfx::Image( |
| 47 | gfx::CanvasImageSource::MakeImageSkia<LoadingCircle>(size)), |
| 48 | size) { |
Joe Antonetti | 92e3f7b | 2023-01-20 18:28:23 | [diff] [blame] | 49 | SetPaintToLayer(); |
| 50 | layer()->SetFillsBoundsOpaquely(false); |
| 51 | layer()->SetFillsBoundsCompletely(false); |
| 52 | } |
Curt Clemens | 971477fd | 2023-03-17 00:15:07 | [diff] [blame] | 53 | |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 54 | AppLoadingIcon::~AppLoadingIcon() = default; |
| 55 | |
| 56 | void AppLoadingIcon::StartLoadingAnimation( |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 57 | std::optional<base::TimeDelta> initial_delay) { |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 58 | if (initial_delay) { |
| 59 | animation_initial_delay_timer_.Start( |
| 60 | FROM_HERE, *initial_delay, |
| 61 | base::BindOnce(&AppLoadingIcon::StartLoadingAnimation, |
| 62 | base::Unretained(this), |
Arthur Sonzogni | a98e443 | 2023-11-28 18:15:01 | [diff] [blame] | 63 | /*initial_delay=*/std::nullopt)); |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
| 67 | views::AnimationBuilder builder; |
Curt Clemens | 1861e55 | 2023-03-21 21:00:15 | [diff] [blame] | 68 | animation_abort_handle_ = builder.GetAbortHandle(); |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 69 | builder.Repeatedly() |
| 70 | .SetDuration(base::Milliseconds(kAnimationFadeDownDurationInMs)) |
| 71 | .SetOpacity(this, kAnimationOpacityLow, gfx::Tween::ACCEL_30_DECEL_20_85) |
| 72 | .Then() |
| 73 | .SetDuration(base::Milliseconds(kAnimationFadeUpDurationInMs)) |
| 74 | .SetOpacity(this, kAnimationOpacityHigh, gfx::Tween::LINEAR); |
Curt Clemens | 6dc633cf8 | 2023-03-17 22:21:18 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void AppLoadingIcon::StopLoadingAnimation() { |
| 78 | animation_abort_handle_.reset(); |
| 79 | animation_initial_delay_timer_.Stop(); |
| 80 | } |
| 81 | |
Allen Bauer | 912e8ff3 | 2023-11-08 21:13:11 | [diff] [blame] | 82 | BEGIN_METADATA(AppLoadingIcon) |
| 83 | END_METADATA |
| 84 | |
Curt Clemens | 971477fd | 2023-03-17 00:15:07 | [diff] [blame] | 85 | } // namespace ash |