File: user_nudge_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 9,406 bytes parent folder | download | duplicates (5)
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
// 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/capture_mode/user_nudge_controller.h"

#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/style/dark_light_mode_controller_impl.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "ui/aura/window.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/animation/animation_sequence_block.h"

namespace ash {

namespace {

constexpr float kBaseRingOpacity = 0.21f;
constexpr float kRippleRingOpacity = 0.5f;

constexpr float kBaseRingScaleUpFactor = 1.1f;
constexpr float kRippleRingScaleUpFactor = 3.0f;
constexpr float kHighlightedViewScaleUpFactor = 1.2f;

constexpr base::TimeDelta kVisibilityChangeDuration = base::Milliseconds(200);
constexpr base::TimeDelta kScaleUpDuration = base::Milliseconds(500);
constexpr base::TimeDelta kScaleDownDelay = base::Milliseconds(650);
constexpr base::TimeDelta kScaleDownOffset = kScaleUpDuration + kScaleDownDelay;
constexpr base::TimeDelta kScaleDownDuration = base::Milliseconds(1350);
constexpr base::TimeDelta kRippleAnimationDuration = base::Milliseconds(2000);

constexpr base::TimeDelta kDelayToShowNudge = base::Milliseconds(1000);
constexpr base::TimeDelta kDelayToRepeatNudge = base::Milliseconds(2500);

// Returns the given `view`'s layer bounds in root coordinates ignoring any
// transforms it or any of its ancestors may have.
gfx::Rect GetViewLayerBoundsInRootNoTransform(views::View* view) {
  auto* layer = view->layer();
  DCHECK(layer);
  gfx::Point origin;
  while (layer) {
    const auto layer_origin = layer->bounds().origin();
    origin.Offset(layer_origin.x(), layer_origin.y());
    layer = layer->parent();
  }
  return gfx::Rect(origin, view->layer()->size());
}

}  // namespace

UserNudgeController::UserNudgeController(CaptureModeSession* session,
                                         views::View* view_to_be_highlighted)
    : capture_session_(session),
      view_to_be_highlighted_(view_to_be_highlighted) {
  widget_observation_.Observe(view_to_be_highlighted->GetWidget());

  view_to_be_highlighted_->SetPaintToLayer();
  view_to_be_highlighted_->layer()->SetFillsBoundsOpaquely(false);

  // Rings are created initially with 0 opacity. Calling SetVisible() will
  // animate them towards their correct state.
  const SkColor ring_color =
      DarkLightModeControllerImpl::Get()->IsDarkModeEnabled() ? SK_ColorWHITE
                                                              : SK_ColorBLACK;
  base_ring_.SetColor(ring_color);
  base_ring_.SetOpacity(0);
  ripple_ring_.SetColor(ring_color);
  ripple_ring_.SetOpacity(0);

  Reposition();
}

UserNudgeController::~UserNudgeController() {
  if (should_dismiss_nudge_forever_)
    CaptureModeController::Get()->DisableSunfishRegionNudgeForever();
  capture_session_->capture_toast_controller()->MaybeDismissCaptureToast(
      CaptureToastType::kUserNudge,
      /*animate=*/false);
}

void UserNudgeController::Reposition() {
  auto* parent_window = GetParentWindow();

  auto* parent_layer = parent_window->layer();
  if (parent_layer != base_ring_.parent()) {
    parent_layer->Add(&base_ring_);
    parent_layer->Add(&ripple_ring_);
  }

  const auto view_bounds_in_root =
      GetViewLayerBoundsInRootNoTransform(view_to_be_highlighted_);
  base_ring_.SetBounds(view_bounds_in_root);
  base_ring_.SetRoundedCornerRadius(
      gfx::RoundedCornersF(view_bounds_in_root.width() / 2.f));
  ripple_ring_.SetBounds(view_bounds_in_root);
  ripple_ring_.SetRoundedCornerRadius(
      gfx::RoundedCornersF(view_bounds_in_root.width() / 2.f));
}

void UserNudgeController::SetVisible(bool visible) {
  if (is_visible_ == visible)
    return;

  is_visible_ = visible;
  auto* capture_toast_controller = capture_session_->capture_toast_controller();

  views::AnimationBuilder builder;
  builder.SetPreemptionStrategy(
      ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);

  if (!is_visible_) {
    // We should no longer repeat the nudge animation.
    timer_.Stop();
    // We should also stop any ongoing animation on the `base_ring_` in
    // particular since we observe this animation ending to schedule a repeat.
    // See OnBaseRingAnimationEnded().
    base_ring_.GetAnimator()->AbortAllAnimations();

    // Animate all animation layers and the toast widget to 0 opacity.
    builder.Once()
        .SetDuration(kVisibilityChangeDuration)
        .SetOpacity(&base_ring_, 0, gfx::Tween::FAST_OUT_SLOW_IN)
        .SetOpacity(&ripple_ring_, 0, gfx::Tween::FAST_OUT_SLOW_IN);
    capture_toast_controller->MaybeDismissCaptureToast(
        CaptureToastType::kUserNudge);
    return;
  }

  // Animate the `base_ring_` and the `toast_widget_` to their default shown
  // opacity. Note that we don't need to show the `ripple_ring_` since it only
  // shows as the nudge animation is being performed.
  // Once those elements reach their default shown opacity, we perform the nudge
  // animation.
  builder
      .OnEnded(base::BindOnce(&UserNudgeController::PerformNudgeAnimations,
                              weak_ptr_factory_.GetWeakPtr()))
      .Once()
      .SetDuration(kDelayToShowNudge)
      .SetOpacity(&base_ring_, kBaseRingOpacity, gfx::Tween::FAST_OUT_SLOW_IN);
  capture_toast_controller->ShowCaptureToast(CaptureToastType::kUserNudge);
}

void UserNudgeController::OnWidgetVisibilityChanged(views::Widget* widget,
                                                    bool visible) {
  // These layers are created as siblings of `view_to_be_highlighted_`'s
  // widget's layer, but they should not be visible when the widget is hidden.
  if (!visible) {
    base_ring_.SetOpacity(0.f);
    ripple_ring_.SetOpacity(0.f);
  }
}

void UserNudgeController::PerformNudgeAnimations() {
  PerformBaseRingAnimation();
  PerformRippleRingAnimation();
  PerformViewScaleAnimation();
}

void UserNudgeController::PerformBaseRingAnimation() {
  // The `base_ring_` should scale up around the center of the
  // `view_to_be_highlighted_` to grab the user's attention, and then scales
  // back down to its original size.
  const gfx::Transform scale_up_transform =
      capture_mode_util::GetScaleTransformAboutCenter(&base_ring_,
                                                      kBaseRingScaleUpFactor);
  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .OnEnded(base::BindOnce(&UserNudgeController::OnBaseRingAnimationEnded,
                              base::Unretained(this)))
      .Once()
      .SetDuration(kScaleUpDuration)
      .SetTransform(&base_ring_, scale_up_transform,
                    gfx::Tween::ACCEL_40_DECEL_20)
      .Offset(kScaleDownOffset)
      .SetDuration(kScaleDownDuration)
      .SetTransform(&base_ring_, gfx::Transform(),
                    gfx::Tween::FAST_OUT_SLOW_IN_3);
}

void UserNudgeController::PerformRippleRingAnimation() {
  // The ripple scales up to 3x the size of the `view_to_be_highlighted_` and
  // around its center while fading out.
  ripple_ring_.SetOpacity(kRippleRingOpacity);
  ripple_ring_.SetTransform(gfx::Transform());
  const gfx::Transform scale_up_transform =
      capture_mode_util::GetScaleTransformAboutCenter(&ripple_ring_,
                                                      kRippleRingScaleUpFactor);
  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(kRippleAnimationDuration)
      .SetOpacity(&ripple_ring_, 0, gfx::Tween::ACCEL_0_80_DECEL_80)
      .SetTransform(&ripple_ring_, scale_up_transform,
                    gfx::Tween::ACCEL_0_40_DECEL_100);
}

void UserNudgeController::PerformViewScaleAnimation() {
  // The `view_to_be_highlighted_` scales up and down around its own center in
  // a similar fashion to that of the `base_ring_`.
  auto* view_layer = view_to_be_highlighted_->layer();
  const gfx::Transform scale_up_transform =
      capture_mode_util::GetScaleTransformAboutCenter(
          view_layer, kHighlightedViewScaleUpFactor);
  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(kScaleUpDuration)
      .SetTransform(view_layer, scale_up_transform,
                    gfx::Tween::ACCEL_40_DECEL_20)
      .Offset(kScaleDownOffset)
      .SetDuration(kScaleDownDuration)
      .SetTransform(view_layer, gfx::Transform(),
                    gfx::Tween::FAST_OUT_SLOW_IN_3);
}

void UserNudgeController::OnBaseRingAnimationEnded() {
  timer_.Start(FROM_HERE, kDelayToRepeatNudge,
               base::BindOnce(&UserNudgeController::PerformNudgeAnimations,
                              weak_ptr_factory_.GetWeakPtr()));
}

aura::Window* UserNudgeController::GetParentWindow() {
  auto* root_window =
      view_to_be_highlighted_->GetWidget()->GetNativeWindow()->GetRootWindow();
  DCHECK(root_window);
  return root_window->GetChildById(kShellWindowId_OverlayContainer);
}

}  // namespace ash