1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206

ash / system / progress_indicator / progress_indicator_animation_registry.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/progress_indicator/progress_indicator_animation_registry.h"

#include <set>

namespace ash {
namespace {

// Type aliases ----------------------------------------------------------------

template <typename AnimationType>
using KeyedAnimationMap =
    std::map<ProgressIndicatorAnimationRegistry::AnimationKey,
             std::unique_ptr<AnimationType>>;

template <typename CallbackListType>
using KeyedAnimationChangedCallbackListMap =
    std::map<ProgressIndicatorAnimationRegistry::AnimationKey,
             CallbackListType>;

// Helpers ---------------------------------------------------------------------

// Notifies any animation changed callbacks registered for the specified `key`
// that the associated animation has changed.
template <typename AnimationType, typename CallbackListType>
void NotifyAnimationChangedForKey(
    KeyedAnimationMap<AnimationType>* animations_by_key,
    KeyedAnimationChangedCallbackListMap<CallbackListType>*
        animation_changed_callback_lists_by_key,
    ProgressIndicatorAnimationRegistry::AnimationKey key) {
  auto callback_lists_it = animation_changed_callback_lists_by_key->find(key);
  if (callback_lists_it == animation_changed_callback_lists_by_key->end())
    return;
  auto animations_it = animations_by_key->find(key);
  callback_lists_it->second.Notify(animations_it != animations_by_key->end()
                                       ? animations_it->second.get()
                                       : nullptr);
}

// Implements:
// * `AddProgressIconAnimationChangedCallbackForKey()`
// * `AddProgressRingAnimationChangedCallbackForKey()`
template <typename CallbackListType>
base::CallbackListSubscription AddAnimationChangedCallbackForKey(
    KeyedAnimationChangedCallbackListMap<CallbackListType>*
        animation_changed_callback_lists_by_key,
    ProgressIndicatorAnimationRegistry::AnimationKey key,
    typename CallbackListType::CallbackType callback) {
  auto it = animation_changed_callback_lists_by_key->find(key);

  // If this is the first time that an animation changed callback is being
  // registered for the specified `key`, set a callback to destroy the created
  // callback list when it becomes empty.
  if (it == animation_changed_callback_lists_by_key->end()) {
    it = animation_changed_callback_lists_by_key
             ->emplace(std::piecewise_construct, std::forward_as_tuple(key),
                       std::forward_as_tuple())
             .first;
    it->second.set_removal_callback(base::BindRepeating(
        [](KeyedAnimationChangedCallbackListMap<CallbackListType>*
               animation_changed_callback_lists_by_key,
           ProgressIndicatorAnimationRegistry::AnimationKey key) {
          auto it = animation_changed_callback_lists_by_key->find(key);
          if (it != animation_changed_callback_lists_by_key->end() &&
              it->second.empty()) {
            animation_changed_callback_lists_by_key->erase(it);
          }
        },
        // `base::Unretained()` is safe because this object owns the callback.
        base::Unretained(animation_changed_callback_lists_by_key), key));
  }

  return it->second.Add(std::move(callback));
}

// Implements:
// * `GetProgressIconAnimationForKey()`
// * `GetProgressRingAnimationForKey()`
template <typename AnimationType>
AnimationType* GetAnimationForKey(
    KeyedAnimationMap<AnimationType>* animations_by_key,
    ProgressIndicatorAnimationRegistry::AnimationKey key) {
  auto it = animations_by_key->find(key);
  return it != animations_by_key->end() ? it->second.get() : nullptr;
}

// Implements:
// * `SetProgressIconAnimationForKey()`
// * `SetProgressRingAnimationForKey()`
template <typename AnimationType, typename CallbackListType>
AnimationType* SetAnimationForKey(
    KeyedAnimationMap<AnimationType>* animations_by_key,
    KeyedAnimationChangedCallbackListMap<CallbackListType>*
        animation_changed_callback_lists_by_key,
    ProgressIndicatorAnimationRegistry::AnimationKey key,
    std::unique_ptr<AnimationType> animation) {
  AnimationType* animation_ptr = animation.get();
  if (animation) {
    (*animations_by_key)[key] = std::move(animation);
    NotifyAnimationChangedForKey(animations_by_key,
                                 animation_changed_callback_lists_by_key, key);
  } else {
    auto it = animations_by_key->find(key);
    if (it != animations_by_key->end()) {
      animations_by_key->erase(it);
      NotifyAnimationChangedForKey(
          animations_by_key, animation_changed_callback_lists_by_key, key);
    }
  }
  return animation_ptr;
}

}  // namespace

// ProgressIndicatorAnimationRegistry ------------------------------------------

ProgressIndicatorAnimationRegistry::ProgressIndicatorAnimationRegistry() =
    default;

ProgressIndicatorAnimationRegistry::~ProgressIndicatorAnimationRegistry() =
    default;

// static
ProgressIndicatorAnimationRegistry::AnimationKey
ProgressIndicatorAnimationRegistry::AsAnimationKey(const void* ptr) {
  return reinterpret_cast<intptr_t>(ptr);
}

base::CallbackListSubscription ProgressIndicatorAnimationRegistry::
    AddProgressIconAnimationChangedCallbackForKey(
        AnimationKey key,
        ProgressIconAnimationChangedCallbackList::CallbackType callback) {
  return AddAnimationChangedCallbackForKey(
      &icon_animation_changed_callback_lists_by_key_, key, std::move(callback));
}

base::CallbackListSubscription ProgressIndicatorAnimationRegistry::
    AddProgressRingAnimationChangedCallbackForKey(
        AnimationKey key,
        ProgressRingAnimationChangedCallbackList::CallbackType callback) {
  return AddAnimationChangedCallbackForKey(
      &ring_animation_changed_callback_lists_by_key_, key, std::move(callback));
}

ProgressIconAnimation*
ProgressIndicatorAnimationRegistry::GetProgressIconAnimationForKey(
    AnimationKey key) {
  return GetAnimationForKey(&icon_animations_by_key_, key);
}

ProgressRingAnimation*
ProgressIndicatorAnimationRegistry::GetProgressRingAnimationForKey(
    AnimationKey key) {
  return GetAnimationForKey(&ring_animations_by_key_, key);
}

ProgressIconAnimation*
ProgressIndicatorAnimationRegistry::SetProgressIconAnimationForKey(
    AnimationKey key,
    std::unique_ptr<ProgressIconAnimation> animation) {
  return SetAnimationForKey(&icon_animations_by_key_,
                            &icon_animation_changed_callback_lists_by_key_, key,
                            std::move(animation));
}

ProgressRingAnimation*
ProgressIndicatorAnimationRegistry::SetProgressRingAnimationForKey(
    AnimationKey key,
    std::unique_ptr<ProgressRingAnimation> animation) {
  return SetAnimationForKey(&ring_animations_by_key_,
                            &ring_animation_changed_callback_lists_by_key_, key,
                            std::move(animation));
}

void ProgressIndicatorAnimationRegistry::EraseAllAnimations() {
  EraseAllAnimationsForKeyIf([](AnimationKey key) { return true; });
}

void ProgressIndicatorAnimationRegistry::EraseAllAnimationsForKey(
    AnimationKey key) {
  SetProgressIconAnimationForKey(key, nullptr);
  SetProgressRingAnimationForKey(key, nullptr);
}

void ProgressIndicatorAnimationRegistry::EraseAllAnimationsForKeyIf(
    base::FunctionRef<bool(AnimationKey key)> predicate) {
  std::set<AnimationKey> keys_to_erase;
  for (const auto& [key, _] : icon_animations_by_key_) {
    if (predicate(key)) {
      keys_to_erase.insert(key);
    }
  }
  for (const auto& [key, _] : ring_animations_by_key_) {
    if (predicate(key)) {
      keys_to_erase.insert(key);
    }
  }
  for (AnimationKey key : keys_to_erase) {
    EraseAllAnimationsForKey(key);
  }
}

}  // namespace ash