File: continue_section_view.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (484 lines) | stat: -rw-r--r-- 17,159 bytes parent folder | download
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// 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/app_list/views/continue_section_view.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/app_list_model_provider.h"
#include "ash/app_list/app_list_util.h"
#include "ash/app_list/app_list_view_delegate.h"
#include "ash/app_list/model/search/search_model.h"
#include "ash/app_list/views/app_list_nudge_controller.h"
#include "ash/app_list/views/app_list_toast_view.h"
#include "ash/app_list/views/app_list_view_util.h"
#include "ash/app_list/views/continue_task_view.h"
#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/wm/desks/templates/saved_desk_controller.h"
#include "base/check.h"
#include "base/strings/string_util.h"
#include "extensions/common/constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace {
// Whether the files section has been shown.
bool g_continue_section_files_shown = false;

// Suggested tasks layout constants.
constexpr size_t kMinFilesForContinueSectionClamshellMode = 3;
constexpr size_t kMinFilesForContinueSectionTabletMode = 2;

// Privacy toast icon size.
constexpr size_t kPrivacyIconSizeClamshell = 60;
constexpr size_t kPrivacyIconSizeTablet = 48;

// Privacy toast interior margin
constexpr auto kPrivacyToastInteriorMarginClamshell =
    gfx::Insets::TLBR(12, 12, 12, 16);

// Delay before marking the privacy notice as swhon.
const base::TimeDelta kPrivacyNoticeShownDelay = base::Seconds(6);

// Animation constants.
constexpr base::TimeDelta kDismissToastAnimationDuration =
    base::Milliseconds(200);
constexpr base::TimeDelta kShowSuggestionsAnimationDuration =
    base::Milliseconds(300);

// The vertical inside padding between this view and its parent. Taken from
// AppListBubbleAppsPage.
constexpr int kVerticalPaddingFromParent = 16;

void CleanupLayer(views::View* view) {
  view->DestroyLayer();
}

}  // namespace

ContinueSectionView::ContinueSectionView(AppListViewDelegate* view_delegate,
                                         int columns,
                                         bool tablet_mode)
    : view_delegate_(view_delegate), tablet_mode_(tablet_mode) {
  DCHECK(view_delegate_);

  AppListModelProvider::Get()->AddObserver(this);

  if (Shell::HasInstance())
    Shell::Get()->app_list_controller()->AddObserver(this);

  SetLayoutManager(std::make_unique<views::FlexLayout>())
      ->SetMainAxisAlignment(views::LayoutAlignment::kCenter)
      .SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
      .SetOrientation(views::LayoutOrientation::kVertical)
      .SetDefault(views::kFlexBehaviorKey,
                  views::FlexSpecification(
                      views::MinimumFlexSizeRule::kScaleToMinimumSnapToZero,
                      views::MaximumFlexSizeRule::kUnbounded));

  suggestions_container_ =
      AddChildView(std::make_unique<ContinueTaskContainerView>(
          view_delegate, columns,
          base::BindRepeating(
              &ContinueSectionView::OnSearchResultContainerResultsChanged,
              base::Unretained(this)),
          tablet_mode));
  suggestions_container_->SetVisible(false);
}

ContinueSectionView::~ContinueSectionView() {
  AppListModelProvider::Get()->RemoveObserver(this);
  if (Shell::HasInstance() && Shell::Get()->app_list_controller())
    Shell::Get()->app_list_controller()->RemoveObserver(this);
}

size_t ContinueSectionView::GetTasksSuggestionsCount() const {
  return suggestions_container_->num_results();
}

void ContinueSectionView::DisableFocusForShowingActiveFolder(bool disabled) {
  if (privacy_toast_)
    privacy_toast_->toast_button()->SetEnabled(!disabled);

  suggestions_container_->DisableFocusForShowingActiveFolder(disabled);

  // Prevent items from being accessed by ChromeVox.
  SetViewIgnoredForAccessibility(this, disabled);
}

ContinueTaskView* ContinueSectionView::GetTaskViewAtForTesting(
    size_t index) const {
  DCHECK_GT(GetTasksSuggestionsCount(), index);
  return static_cast<ContinueTaskView*>(
      suggestions_container_->children()[index]);
}

// static
bool ContinueSectionView::EnableContinueSectionFileRemovalMetrics() {
  return g_continue_section_files_shown;
}

// static
void ContinueSectionView::
    ResetContinueSectionFileRemovalMetricEnabledForTest() {
  g_continue_section_files_shown = false;
}

void ContinueSectionView::UpdateSuggestionTasks() {
  suggestions_container_->SetResults(
      AppListModelProvider::Get()->search_model()->results());
}

void ContinueSectionView::OnSearchResultContainerResultsChanged() {
  MaybeCreatePrivacyNotice();
  MaybeAnimateOutPrivacyNotice();
}

bool ContinueSectionView::HasMinimumFilesToShow() const {
  return suggestions_container_->num_file_results() >=
         (tablet_mode_ ? kMinFilesForContinueSectionTabletMode
                       : kMinFilesForContinueSectionClamshellMode);
}

bool ContinueSectionView::HasDesksAdminTemplates() const {
  return suggestions_container_->num_desks_admin_template_results() > 0;
}

bool ContinueSectionView::ShouldShowPrivacyNotice() const {
  if (!nudge_controller_)
    return false;

  // Don't show the privacy notice if the reorder nudge is showing.
  if (nudge_controller_->current_nudge() ==
      AppListNudgeController::NudgeType::kReorderNudge) {
    return false;
  }

  return (HasDesksAdminTemplates() || HasMinimumFilesToShow()) &&
         !(nudge_controller_->IsPrivacyNoticeAccepted() ||
           nudge_controller_->WasPrivacyNoticeShown());
}

bool ContinueSectionView::ShouldShowFilesSection() const {
  return (HasDesksAdminTemplates() || HasMinimumFilesToShow()) &&
         (nudge_controller_->IsPrivacyNoticeAccepted() ||
          nudge_controller_->WasPrivacyNoticeShown()) &&
         !privacy_toast_;
}

void ContinueSectionView::SetShownInBackground(bool shown_in_background) {
  // If the privacy notice becomes inactive when it is shown in background, stop
  // the privacy notice shown timer to restart the count on the next show.
  if (shown_in_background && privacy_notice_shown_timer_.IsRunning()) {
    privacy_notice_shown_timer_.AbandonAndStop();
    return;
  }

  // If the privacy notice becomes active again, restart the
  // `privacy_notice_shown_timer_`.
  if (!shown_in_background && !nudge_controller_->WasPrivacyNoticeShown() &&
      privacy_toast_) {
    privacy_notice_shown_timer_.Start(
        FROM_HERE, kPrivacyNoticeShownDelay,
        base::BindOnce(&ContinueSectionView::OnPrivacyNoticeShowTimerDone,
                       base::Unretained(this)));
  }
}

void ContinueSectionView::SetNudgeController(
    AppListNudgeController* nudge_controller) {
  nudge_controller_ = nudge_controller;
}

void ContinueSectionView::OnPrivacyToastAcknowledged() {
  if (nudge_controller_) {
    nudge_controller_->SetPrivacyNoticeAcceptedPref(true);
    nudge_controller_->SetPrivacyNoticeShown(false);
  }
  AnimateDismissToast(
      base::BindRepeating(&ContinueSectionView::AnimateShowContinueSection,
                          weak_ptr_factory_.GetWeakPtr()));
}

void ContinueSectionView::AnimateDismissToast(base::RepeatingClosure callback) {
  // Prevents setting up new animation if the toast is already hiding.
  // https://crbug.com/1326237.
  DCHECK(privacy_toast_);
  if (privacy_toast_->layer() &&
      privacy_toast_->layer()->GetTargetOpacity() == 0.f) {
    return;
  }

  PrepareForLayerAnimation(privacy_toast_);

  views::AnimationBuilder animation_builder;
  animation_builder.OnEnded(callback);
  animation_builder.OnAborted(callback);

  animation_builder
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetOpacity(privacy_toast_, 0, gfx::Tween::LINEAR)
      .SetDuration(kDismissToastAnimationDuration);
}

void ContinueSectionView::AnimateSlideLauncherContent(int vertical_offset) {
  RemovePrivacyNotice();
  // The sibling views from this should slide down the total of the height
  // difference to make room for the continue section suggestions and title.
  for (views::View* view : parent()->children()) {
    if (view == this)
      continue;
    const bool create_layer = PrepareForLayerAnimation(view);
    auto cleanup = create_layer ? base::BindRepeating(&CleanupLayer, view)
                                : base::DoNothing();
    StartSlideInAnimation(view, vertical_offset, base::Milliseconds(300),
                          gfx::Tween::ACCEL_40_DECEL_100_3, cleanup);
  }
}

void ContinueSectionView::AnimateShowContinueSection() {
  int height_difference = privacy_toast_->GetPreferredSize().height() -
                          suggestions_container_->GetPreferredSize().height();

  const gfx::Tween::Type animation_tween = gfx::Tween::ACCEL_40_DECEL_100_3;

  // The initial position for the launcher continue section should be right
  // below the search divider.
  gfx::Transform initial_transform;
  initial_transform.Translate(0, -kVerticalPaddingFromParent);

  PrepareForLayerAnimation(suggestions_container_);
  suggestions_container_->layer()->SetTransform(initial_transform);

  privacy_toast_->DestroyLayer();
  privacy_toast_->SetVisible(false);

  suggestions_container_->AnimateSlideInSuggestions(
      height_difference + kVerticalPaddingFromParent,
      kShowSuggestionsAnimationDuration, animation_tween);

  AnimateSlideLauncherContent(height_difference);

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetTransform(suggestions_container_, gfx::Transform(), animation_tween)
      .SetDuration(kShowSuggestionsAnimationDuration);
}

void ContinueSectionView::RemovePrivacyNotice() {
  if (privacy_toast_) {
    RemoveChildViewT(privacy_toast_.get());
    privacy_toast_ = nullptr;
  }
  UpdateElementsVisibility();
  PreferredSizeChanged();
}

void ContinueSectionView::OnPrivacyNoticeShowTimerDone() {
  if (!nudge_controller_)
    return;

  nudge_controller_->SetPrivacyNoticeShownPref(true);
}

void ContinueSectionView::MaybeCreatePrivacyNotice() {
  DCHECK(nudge_controller_);

  if (!ShouldShowPrivacyNotice()) {
    // Reset the nudge controller state if privacy notice shouldn't be showing.
    if (nudge_controller_->current_nudge() ==
        AppListNudgeController::NudgeType::kPrivacyNotice) {
      nudge_controller_->SetPrivacyNoticeShown(false);
    }

    return;
  }

  nudge_controller_->SetPrivacyNoticeShown(true);

  if (privacy_toast_)
    return;

  // When creating the privacy notice and after a delay, mark the nudge as
  // shown, which will keep the user from seeing the nudge again.
  privacy_notice_shown_timer_.Start(
      FROM_HERE, kPrivacyNoticeShownDelay,
      base::BindOnce(&ContinueSectionView::OnPrivacyNoticeShowTimerDone,
                     base::Unretained(this)));

  const int privacy_toast_text_id =
      tablet_mode_ ? IDS_ASH_LAUNCHER_CONTINUE_SECTION_PRIVACY_TEXT_TABLET
                   : IDS_ASH_LAUNCHER_CONTINUE_SECTION_PRIVACY_TEXT;

  privacy_toast_ = AddChildView(
      AppListToastView::Builder(
          l10n_util::GetStringUTF16(privacy_toast_text_id))
          .SetButton(l10n_util::GetStringUTF16(
                         IDS_ASH_LAUNCHER_CONTINUE_SECTION_PRIVACY_BUTTON),
                     base::BindRepeating(
                         &ContinueSectionView::OnPrivacyToastAcknowledged,
                         base::Unretained(this)))
          .SetStyleForTabletMode(tablet_mode_)
          .SetIcon(
              ui::ResourceBundle::GetSharedInstance().GetThemedLottieImageNamed(
                  IDR_APP_LIST_CONTINUE_SECTION_NOTICE_IMAGE))
          .SetIconSize(tablet_mode_ ? kPrivacyIconSizeTablet
                                    : kPrivacyIconSizeClamshell)
          .SetIconBackground(true)
          .Build());
  privacy_toast_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
                               views::MaximumFlexSizeRule::kScaleToMaximum));
  if (!tablet_mode_)
    privacy_toast_->UpdateInteriorMargins(kPrivacyToastInteriorMarginClamshell);
}

bool ContinueSectionView::FirePrivacyNoticeShownTimerForTest() {
  if (!privacy_notice_shown_timer_.IsRunning())
    return false;

  privacy_notice_shown_timer_.FireNow();
  return true;
}

void ContinueSectionView::MaybeAnimateOutPrivacyNotice() {
  if (Shell::HasInstance() && Shell::Get()->app_list_controller() &&
      Shell::Get()->app_list_controller()->IsVisible() && !tablet_mode_ &&
      privacy_toast_ && !ShouldShowPrivacyNotice()) {
    AnimateDismissToast(
        base::BindRepeating(&ContinueSectionView::AnimateSlideLauncherContent,
                            weak_ptr_factory_.GetWeakPtr(),
                            privacy_toast_->GetPreferredSize().height()));
    return;
  }
  UpdateElementsVisibility();
}

void ContinueSectionView::UpdateElementsVisibility() {
  // If the user chose to hide the section, set visibility false and reset the
  // privacy toast.
  if (view_delegate_->ShouldHideContinueSection()) {
    SetVisible(false);
    if (privacy_toast_) {
      RemoveChildViewT(privacy_toast_.get());
      privacy_toast_ = nullptr;
      nudge_controller_->SetPrivacyNoticeShown(false);
      privacy_notice_shown_timer_.AbandonAndStop();
    }
    return;
  }
  const bool show_files_section = ShouldShowFilesSection();
  const bool show_privacy_notice = ShouldShowPrivacyNotice();

  SetVisible(show_files_section || show_privacy_notice);

  if (show_files_section)
    g_continue_section_files_shown = true;

  const bool suggestions_visibility_changed =
      show_files_section != suggestions_container_->GetVisible();
  suggestions_container_->SetVisible(show_files_section);

  if (suggestions_visibility_changed)
    PreferredSizeChanged();
}

void ContinueSectionView::AddedToWidget() {
  GetFocusManager()->AddFocusChangeListener(this);
}

void ContinueSectionView::RemovedFromWidget() {
  GetFocusManager()->RemoveFocusChangeListener(this);
}

void ContinueSectionView::OnDidChangeFocus(views::View* focused_before,
                                           views::View* focused_now) {
  // Tablet mode does not have a scrollable container or continue label.
  if (tablet_mode_)
    return;
  // Nothing to do if views are losing focus.
  if (!focused_now)
    return;
  // If a child of the privacy toast gained focus (e.g. the OK button) then
  // ensure the whole toast is visible.
  if (privacy_toast_ && privacy_toast_->Contains(focused_now)) {
    // The parent view owns the continue label, which provides more context
    // for the privacy notice. Ensure the label is visible.
    parent()->ScrollViewToVisible();
    return;
  }
  // If a suggested task gained focus then ensure the continue label is visible
  // so the user knows what this section is.
  if (suggestions_container_->Contains(focused_now)) {
    // The parent view owns the continue label, so ensure label visibility.
    parent()->ScrollViewToVisible();
  }
}

void ContinueSectionView::OnActiveAppListModelsChanged(
    AppListModel* model,
    SearchModel* search_model) {
  UpdateSuggestionTasks();
}

void ContinueSectionView::OnAppListVisibilityChanged(bool shown,
                                                     int64_t display_id) {
  if (!shown && privacy_toast_) {
    // Abort any in-progress privacy toast animation. This may delete the toast
    // view. Explicitly aborting the animation avoids a double-delete in
    // RemoveChildViewT() below. https://crbug.com/1357434
    if (privacy_toast_->layer())
      privacy_toast_->layer()->GetAnimator()->AbortAllAnimations();

    if (privacy_toast_) {
      RemoveChildViewT(privacy_toast_.get());
      privacy_toast_ = nullptr;
    }
  }

  // Update the nudge type in nudge controller if the privacy notice is
  // considered shown and will not be shown again.
  if (!shown && nudge_controller_->WasPrivacyNoticeShown() &&
      nudge_controller_->current_nudge() ==
          AppListNudgeController::NudgeType::kPrivacyNotice) {
    nudge_controller_->SetPrivacyNoticeShown(false);
  }

  // When hiding the launcher, stop the privacy notice shown timer to restart
  // the count on the next show.
  if (!shown && privacy_notice_shown_timer_.IsRunning())
    privacy_notice_shown_timer_.AbandonAndStop();

  if (shown)
    MaybeCreatePrivacyNotice();
  UpdateElementsVisibility();

  if (privacy_toast_)
    PreferredSizeChanged();
}

BEGIN_METADATA(ContinueSectionView, views::View)
END_METADATA

}  // namespace ash