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

ash / wm / overview / scoped_overview_wallpaper_clipper.cc [blame]

// Copyright 2024 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/overview/scoped_overview_wallpaper_clipper.h"

#include <optional>

#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/wallpaper/views/wallpaper_view.h"
#include "ash/wallpaper/views/wallpaper_widget_controller.h"
#include "ash/wm/overview/overview_constants.h"
#include "ash/wm/overview/overview_grid.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/notreached.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/wm/core/coordinate_conversion.h"

namespace ash {

namespace {

struct AnimationSettings {
  base::TimeDelta duration;
  gfx::Tween::Type tween_type;
};

constexpr AnimationSettings kEnterInformedRestoreAnimationSettings{
    .duration = base::Milliseconds(800),
    .tween_type = gfx::Tween::EASE_IN_OUT_EMPHASIZED};

constexpr AnimationSettings kEnterOverviewAnimationSettings{
    .duration = base::Milliseconds(500),
    .tween_type = gfx::Tween::EASE_IN_OUT_EMPHASIZED};

constexpr AnimationSettings kShowBirchBarInOverviewAnimationSettings{
    .duration = base::Milliseconds(200),
    .tween_type = gfx::Tween::FAST_OUT_LINEAR_IN};

constexpr AnimationSettings kShowBirchBarByUserAnimationSettings{
    .duration = base::Milliseconds(250),
    .tween_type = gfx::Tween::ACCEL_LIN_DECEL_100_3};

constexpr AnimationSettings kHideBirchBarByUserAnimationSettings{
    .duration = base::Milliseconds(100),
    .tween_type = gfx::Tween::LINEAR};

constexpr AnimationSettings kRestoreAnimationSettings{
    .duration = base::Milliseconds(200),
    .tween_type = gfx::Tween::ACCEL_LIN_DECEL_100};

constexpr AnimationSettings kRelayoutBirchBarAnimationSettings{
    .duration = base::Milliseconds(100),
    .tween_type = gfx::Tween::LINEAR};

void RemoveWallpaperClipper(
    WallpaperWidgetController* wallpaper_widget_controller) {
  if (wallpaper_widget_controller->wallpaper_underlay_layer()) {
    wallpaper_widget_controller->wallpaper_underlay_layer()->SetVisible(false);
  }

  if (auto* wallpaper_view_layer =
          wallpaper_widget_controller->wallpaper_view()->layer()) {
    wallpaper_view_layer->SetClipRect(gfx::Rect());
  }
}

}  // namespace

ScopedOverviewWallpaperClipper::ScopedOverviewWallpaperClipper(
    OverviewGrid* overview_grid,
    AnimationType animation_type)
    : overview_grid_(overview_grid) {
  aura::Window* root_window = overview_grid_->root_window();
  WallpaperWidgetController* wallpaper_widget_controller =
      RootWindowController::ForWindow(root_window)
          ->wallpaper_widget_controller();

  // Stop animating the wallpaper view layer. There may be an ongoing clip
  // and/or rounded corner animation from the last overview exit. This ensures
  // the callback completes before we make changes to the wallpaper again.
  auto* wallpaper_view_layer =
      wallpaper_widget_controller->wallpaper_view()->layer();
  wallpaper_view_layer->GetAnimator()->StopAnimating();

  auto* wallpaper_underlay_layer =
      wallpaper_widget_controller->wallpaper_underlay_layer();
  // TODO(http://b/333952534): Remove this check once `wallpaper_underlay_layer`
  // is always created.
  CHECK(wallpaper_underlay_layer);
  wallpaper_underlay_layer->SetVisible(true);

  RefreshWallpaperClipBounds(animation_type, base::DoNothing());
}

ScopedOverviewWallpaperClipper::~ScopedOverviewWallpaperClipper() {
  // Switching to tablet mode will force mirroring displays which would destroy
  // non primary root windows. The wallpaper widget controller would already be
  // destroyed at this point.
  if (auto* wallpaper_widget_controller =
          RootWindowController::ForWindow(overview_grid_->root_window())
              ->wallpaper_widget_controller()) {
    RefreshWallpaperClipBounds(
        AnimationType::kRestore,
        base::BindOnce(&RemoveWallpaperClipper,
                       base::Unretained(wallpaper_widget_controller)));
  }
}

void ScopedOverviewWallpaperClipper::RefreshWallpaperClipBounds(
    AnimationType animation_type,
    base::OnceClosure animation_end_callback) {
  aura::Window* root_window = overview_grid_->root_window();
  WallpaperWidgetController* wallpaper_widget_controller =
      RootWindowController::ForWindow(root_window)
          ->wallpaper_widget_controller();
  auto* wallpaper_view_layer =
      wallpaper_widget_controller->wallpaper_view()->layer();

  gfx::Rect target_clip_rect = animation_type == AnimationType::kRestore
                                   ? display::Screen::GetScreen()
                                         ->GetDisplayNearestWindow(root_window)
                                         .bounds()
                                   : overview_grid_->GetWallpaperClipBounds();
  // Convert the clip bounds to the parent's coordinates, as layer bounds are
  // always relative to their parent.
  wm::ConvertRectFromScreen(root_window, &target_clip_rect);

  // If the animation type is none, directly set the clip rect and rounded
  // corners if the target properties are changed and run the callback.
  if (animation_type == AnimationType::kNone) {
    if (target_clip_rect != wallpaper_view_layer->GetTargetClipRect()) {
      wallpaper_view_layer->SetClipRect(target_clip_rect);
    }

    if (kWallpaperClipRoundedCornerRadii !=
        wallpaper_view_layer->GetTargetRoundedCornerRadius()) {
      wallpaper_view_layer->SetRoundedCornerRadius(
          kWallpaperClipRoundedCornerRadii);
    }

    if (animation_end_callback) {
      std::move(animation_end_callback).Run();
    }
    return;
  }

  // Otherwise, perform animation according to the given type.
  AnimationSettings animation_settings;
  std::optional<gfx::RoundedCornersF> rounded_corners;

  // Set animation settings according to animation type.
  switch (animation_type) {
    case AnimationType::kEnterInformedRestore:
      animation_settings = kEnterInformedRestoreAnimationSettings;
      rounded_corners = kWallpaperClipRoundedCornerRadii;
      break;
    case AnimationType::kEnterOverview:
      animation_settings = kEnterOverviewAnimationSettings;
      rounded_corners = kWallpaperClipRoundedCornerRadii;
      break;
    case AnimationType::kShowBirchBarInOverview:
      animation_settings = kShowBirchBarInOverviewAnimationSettings;
      break;
    case AnimationType::kShowBirchBarByUser:
      animation_settings = kShowBirchBarByUserAnimationSettings;
      break;
    case AnimationType::kHideBirchBarByUser:
      animation_settings = kHideBirchBarByUserAnimationSettings;
      break;
    case AnimationType::kRelayoutBirchBar:
      animation_settings = kRelayoutBirchBarAnimationSettings;
      break;
    case AnimationType::kRestore:
      animation_settings = kRestoreAnimationSettings;
      rounded_corners = gfx::RoundedCornersF();
      break;
    case AnimationType::kNone:
      NOTREACHED();
  }

  views::AnimationBuilder animation_builder;
  if (animation_end_callback) {
    animation_builder.OnEnded(std::move(animation_end_callback));
  }

  views::AnimationSequenceBlock& animation_sequence =
      animation_builder
          .SetPreemptionStrategy(
              ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
          .Once()
          .SetDuration(animation_settings.duration)
          .SetClipRect(wallpaper_view_layer, target_clip_rect,
                       animation_settings.tween_type);

  if (rounded_corners) {
    animation_sequence.SetRoundedCorners(wallpaper_view_layer, *rounded_corners,
                                         animation_settings.tween_type);
  }
}

}  // namespace ash