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

ash / wm / desks / templates / saved_desk_animations.cc [blame]

// Copyright 2021 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/wm/desks/templates/saved_desk_animations.h"

#include "ui/aura/window_occlusion_tracker.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/tween.h"
#include "ui/views/animation/animation_builder.h"

namespace ash {

namespace {

// The time duration for widgets to fade in.
constexpr int kFadeInDurationMs = 100;

// The time duration for widgets to fade out.
constexpr int kFadeOutDurationMs = 100;

// Fade in animation using AnimationBuilder.
void FadeInLayer(ui::Layer* layer, int duration_in_ms) {
  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(base::Milliseconds(duration_in_ms))
      .SetOpacity(layer, 1.0f, gfx::Tween::LINEAR);
}

// Fade out animation using AnimationBuilder.
void FadeOutLayer(ui::Layer* layer,
                  base::OnceClosure on_animation_ended_callback,
                  int duration_in_ms) {
  // Pause the window occlusion tracker since there may be occlusion observers
  // that wake up and trigger overview mode to exit, which destroys the layers
  // that are involved here. See http://b/273562648 for more info.
  aura::WindowOcclusionTracker::ScopedPause pause_occlusion_tracker;

  std::pair<base::OnceClosure, base::OnceClosure> split =
      base::SplitOnceCallback(std::move(on_animation_ended_callback));

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .OnEnded(std::move(split.first))
      .OnAborted(std::move(split.second))
      .Once()
      .SetDuration(base::TimeDelta())
      .SetOpacity(layer, 1.0f)
      .Then()
      .SetDuration(base::Milliseconds(duration_in_ms))
      .SetOpacity(layer, 0.0f, gfx::Tween::LINEAR);
}

}  // namespace

void PerformFadeInLayer(ui::Layer* layer, bool animate) {
  if (!animate) {
    layer->SetOpacity(1.f);
    return;
  }
  // If the layer is already at, or animating to opaque, then we don't animate.
  if (layer->GetTargetOpacity() == 1.0f) {
    return;
  }

  FadeInLayer(layer, kFadeInDurationMs);
}

void PerformFadeOutLayer(ui::Layer* layer,
                         bool animate,
                         base::OnceClosure on_animation_ended_callback) {
  if (!animate) {
    layer->SetOpacity(0.f);
    return;
  }

  FadeOutLayer(layer, std::move(on_animation_ended_callback),
               kFadeOutDurationMs);
}

}  // namespace ash