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
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379

ash / assistant / ui / main_stage / animated_container_view.cc [blame]

// Copyright 2019 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/assistant/ui/main_stage/animated_container_view.h"

#include <utility>

#include "ash/assistant/model/assistant_interaction_model.h"
#include "ash/assistant/model/assistant_response.h"
#include "ash/assistant/ui/assistant_view_delegate.h"
#include "ash/assistant/ui/main_stage/element_animator.h"
#include "ash/public/cpp/assistant/controller/assistant_interaction_controller.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/callback_layer_animation_observer.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"

namespace ash {

// AnimatedContainerView::ScopedDisablePreferredSizeChanged --------------------

class AnimatedContainerView::ScopedDisablePreferredSizeChanged {
 public:
  explicit ScopedDisablePreferredSizeChanged(AnimatedContainerView* view)
      : view_(view), original_value_(view_->propagate_preferred_size_changed_) {
    view_->SetPropagatePreferredSizeChanged(false);
  }

  ~ScopedDisablePreferredSizeChanged() {
    view_->SetPropagatePreferredSizeChanged(original_value_);
  }

 private:
  const raw_ptr<AnimatedContainerView> view_;
  const bool original_value_;
};

// AnimatedContainerView -------------------------------------------------------

AnimatedContainerView::AnimatedContainerView(AssistantViewDelegate* delegate)
    : delegate_(delegate) {
  assistant_controller_observation_.Observe(AssistantController::Get());
  AssistantInteractionController::Get()->GetModel()->AddObserver(this);

  AddScrollViewObserver(this);
}

AnimatedContainerView::~AnimatedContainerView() {
  if (response_)
    response_.get()->RemoveObserver(this);

  if (AssistantInteractionController::Get())
    AssistantInteractionController::Get()->GetModel()->RemoveObserver(this);

  RemoveScrollViewObserver(this);
}

void AnimatedContainerView::PreferredSizeChanged() {
  // Because views are added/removed in batches, we attempt to prevent
  // over-propagation of the PreferredSizeChanged event during batched view
  // hierarchy add/remove operations. This helps to reduce layout passes.
  if (propagate_preferred_size_changed_)
    AssistantScrollView::PreferredSizeChanged();
}

void AnimatedContainerView::OnChildViewRemoved(View* observed_view,
                                               View* child) {
  for (auto it = animators_.begin(); it != animators_.end(); ++it) {
    if (it->get()->view() == child) {
      animators_.erase(it);
      return;
    }
  }
}

void AnimatedContainerView::OnAssistantControllerDestroying() {
  AssistantInteractionController::Get()->GetModel()->RemoveObserver(this);
  DCHECK(assistant_controller_observation_.IsObservingSource(
      AssistantController::Get()));
  assistant_controller_observation_.Reset();
}

void AnimatedContainerView::OnCommittedQueryChanged(
    const AssistantQuery& query) {
  FadeOutViews();
}

void AnimatedContainerView::OnResponseChanged(
    const scoped_refptr<AssistantResponse>& response) {
  ChangeResponse(response);
}

void AnimatedContainerView::OnResponseCleared() {
  RemoveAllViews();
  queued_response_ = nullptr;
}

void AnimatedContainerView::OnUiElementAdded(
    const AssistantUiElement* ui_element) {
  std::unique_ptr<ElementAnimator> animator = HandleUiElement(ui_element);
  if (animator)
    AddElementAnimatorAndAnimateInView(std::move(animator));
}

void AnimatedContainerView::OnSuggestionsAdded(
    const std::vector<AssistantSuggestion>& suggestions) {
  // We can prevent over-propagation of the PreferredSizeChanged event by
  // stopping propagation during batched view hierarchy add/remove operations.
  ScopedDisablePreferredSizeChanged disable_preferred_size_changed(this);
  for (const auto& suggestion : suggestions) {
    auto animator = HandleSuggestion(suggestion);
    if (animator)
      AddElementAnimatorAndAnimateInView(std::move(animator));
  }
}

void AnimatedContainerView::RemoveAllViews() {
  if (response_)
    response_.get()->RemoveObserver(this);

  // We explicitly abort all in progress animations here because we will remove
  // their views immediately and we want to ensure that any animation observers
  // will be notified of an abort, not an animation completion.  Otherwise there
  // is potential to enter into a bad state (see crbug/952996).
  for (const auto& animator : animators_) {
    animator->AbortAnimation();
    // TODO(b/237704325): Fix ChromeVox focusing on removed chip views
    animator->view()->SetVisible(false);
  }

  animators_.clear();

  // We can prevent over-propagation of the PreferredSizeChanged event by
  // stopping propagation during batched view hierarchy add/remove operations.
  ScopedDisablePreferredSizeChanged disable_preferred_size_changed(this);
  content_view()->RemoveAllChildViews();

  // We inform our derived class all views have been removed.
  OnAllViewsRemoved();

  // Once the response has been cleared from the stage, we are free to release
  // our shared pointer. This allows resources associated with the underlying
  // views to be freed, provided there are no other usages.
  response_.reset();
}

void AnimatedContainerView::SetPropagatePreferredSizeChanged(bool propagate) {
  if (propagate == propagate_preferred_size_changed_)
    return;

  propagate_preferred_size_changed_ = propagate;

  // When we are no longer stopping propagation of PreferredSizeChanged events,
  // we fire an event off to ensure the view hierarchy is properly laid out.
  if (propagate_preferred_size_changed_)
    PreferredSizeChanged();
}

std::unique_ptr<ElementAnimator> AnimatedContainerView::HandleUiElement(
    const AssistantUiElement* ui_element) {
  return nullptr;
}

std::unique_ptr<ElementAnimator> AnimatedContainerView::HandleSuggestion(
    const AssistantSuggestion& suggestion) {
  return nullptr;
}

void AnimatedContainerView::ChangeResponse(
    const scoped_refptr<const AssistantResponse>& response) {
  if (response_)
    response_.get()->RemoveObserver(this);

  // We may have to postpone the response while we animate the previous response
  // off stage. We use a shared pointer to ensure that any views we add to the
  // view hierarchy can be removed before the underlying views are destroyed.
  queued_response_ = response;

  // If we are currently animating-/fading-out the old content, don't interrupt
  // it. When the animating-/fading-out is completed, it will detect we've got a
  // queued response and animate it in.
  if (animate_out_in_progress_ || fade_out_in_progress_)
    return;

  // If we don't have any pre-existing content, there is nothing to animate off
  // stage so we can proceed to add the new response.
  if (content_view()->children().empty()) {
    AddResponse(std::move(queued_response_));
    return;
  }

  animate_out_in_progress_ = true;

  // There is a previous response on stage, so we'll animate it off before
  // adding the new response. The new response will be added upon invocation
  // of the exit animation ended callback.
  auto* animation_observer = new ui::CallbackLayerAnimationObserver(
      /*animation_ended_callback=*/base::BindRepeating(
          AnimatedContainerView::AnimateOutObserverCallback,
          weak_factory_.GetWeakPtr()));

  for (const auto& animator : animators_)
    animator->AnimateOut(animation_observer);

  // Set the observer to active so that we receive callback events.
  animation_observer->SetActive();
}

void AnimatedContainerView::AddResponse(
    scoped_refptr<const AssistantResponse> response) {
  // All children should be animated out and removed before the new response is
  // added.
  DCHECK(content_view()->children().empty());

  // We cache a reference to the |response| to ensure that the instance is not
  // destroyed before we have removed associated views from the view hierarchy.
  response_ = std::move(response);

  // In response processing v2, we observe the |response_| so that we handle
  // new suggestions and UI elements that continue to stream in.
  response_.get()->AddObserver(this);

  // We can prevent over-propagation of the PreferredSizeChanged event by
  // stopping propagation during batched view hierarchy add/remove operations.
  ScopedDisablePreferredSizeChanged disable_preferred_size_changed(this);

  // Create views/animators for the suggestions and UI elements belonging to the
  // |response_|. Note that this will also cause them to begin animating in.
  OnSuggestionsAdded(response_->GetSuggestions());
  for (const auto& ui_element : response_->GetUiElements())
    OnUiElementAdded(ui_element.get());
}

bool AnimatedContainerView::IsAnimatingViews() const {
  return base::ranges::any_of(
      animators_, [](const std::unique_ptr<ElementAnimator>& animator) {
        return animator->layer()->GetAnimator()->is_animating();
      });
}

void AnimatedContainerView::AddElementAnimatorAndAnimateInView(
    std::unique_ptr<ElementAnimator> animator) {
  DCHECK_EQ(animator->view()->parent(), content_view());
  animators_.push_back(std::move(animator));

  // We don't allow interactions while animating.
  DisableInteractions();

  auto* animation_observer = new ui::CallbackLayerAnimationObserver(
      /*animation_ended_callback=*/base::BindRepeating(
          AnimatedContainerView::AnimateInObserverCallback,
          weak_factory_.GetWeakPtr()));

  // Start animating in the view.
  animators_.back()->AnimateIn(animation_observer);

  // Set the observer to active so that we receive callback events.
  animation_observer->SetActive();
}

void AnimatedContainerView::FadeOutViews() {
  // If there's already an animation in progress, there's nothing for us to do.
  if (fade_out_in_progress_)
    return;

  fade_out_in_progress_ = true;

  // We don't allow interactions while waiting for the next query response. The
  // contents will be faded out, so it should not be interactive.
  DisableInteractions();

  auto* animation_observer = new ui::CallbackLayerAnimationObserver(
      /*animation_ended_callback=*/base::BindRepeating(
          AnimatedContainerView::FadeOutObserverCallback,
          weak_factory_.GetWeakPtr()));

  for (const auto& animator : animators_)
    animator->FadeOut(animation_observer);

  // Set the observer to active so that we receive callback events.
  animation_observer->SetActive();
}

void AnimatedContainerView::SetInteractionsEnabled(bool enabled) {
  SetCanProcessEventsWithinSubtree(enabled);
  // We also need to enable/disable the individual views, to enable/disable
  // processing of key events.
  for (const auto& animator : animators_)
    animator->view()->SetEnabled(enabled);
}

bool AnimatedContainerView::AnimateInObserverCallback(
    const base::WeakPtr<AnimatedContainerView>& weak_ptr,
    const ui::CallbackLayerAnimationObserver& observer) {
  // If the AnimatedContainerView is destroyed we just return true to delete our
  // observer. No further action is needed.
  if (!weak_ptr)
    return true;

  // If the animation was aborted, we just return true to delete our observer.
  // No further action is needed.
  if (observer.aborted_count())
    return true;

  // If there are no further animations in progress, we can make our view
  // interactive again and notify derived classes that all views have animated
  // in. Note that in response processing v2, another animation may have kicked
  // off prior to this animation finishing. Once all animations have completed
  // interactivity will be restored and derivate classes notified.
  if (!weak_ptr->IsAnimatingViews()) {
    weak_ptr->EnableInteractions();
    weak_ptr->OnAllViewsAnimatedIn();
  }

  // We return true to delete our observer.
  return true;
}

bool AnimatedContainerView::AnimateOutObserverCallback(
    const base::WeakPtr<AnimatedContainerView>& weak_ptr,
    const ui::CallbackLayerAnimationObserver& observer) {
  // If the AnimatedContainerView is destroyed we just return true to delete our
  // observer. No further action is needed.
  if (!weak_ptr)
    return true;

  weak_ptr->animate_out_in_progress_ = false;

  // If the exit animation was aborted, we just return true to delete our
  // observer. No further action is needed.
  if (observer.aborted_count())
    return true;

  // All views have finished their exit animations so it's safe to perform
  // clearing of their views and managed resources.
  weak_ptr->RemoveAllViews();

  // It is safe to add our queued response, if one exists, to the view
  // hierarchy now that we've cleared the previous response from the stage.
  if (weak_ptr->queued_response_)
    weak_ptr->AddResponse(std::move(weak_ptr->queued_response_));

  // We return true to delete our observer.
  return true;
}

bool AnimatedContainerView::FadeOutObserverCallback(
    const base::WeakPtr<AnimatedContainerView>& weak_ptr,
    const ui::CallbackLayerAnimationObserver& observer) {
  // If the AnimatedContainerView is destroyed we just return true to delete our
  // observer. No further action is needed.
  if (!weak_ptr)
    return true;

  weak_ptr->fade_out_in_progress_ = false;

  // If the exit animation was aborted, we just return true to delete our
  // observer. No further action is needed.
  if (observer.aborted_count())
    return true;

  // If the new response arrived while the fade-out was in progress, we will
  // start handling it now.
  if (weak_ptr->queued_response_)
    weak_ptr->ChangeResponse(std::move(weak_ptr->queued_response_));

  // We return true to delete our observer.
  return true;
}

BEGIN_METADATA(AnimatedContainerView)
END_METADATA

}  // namespace ash