File: capture_mode_util.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 (632 lines) | stat: -rw-r--r-- 24,393 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Copyright 2020 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/capture_mode_util.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/capture_mode/capture_mode_camera_controller.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/stop_recording_button_tray.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/clipboard_history_controller.h"
#include "ash/public/cpp/window_finder.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/typography.h"
#include "ash/system/privacy/privacy_indicators_controller.h"
#include "base/check.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "chromeos/ui/frame/frame_header.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_provider_manager.h"
#include "ui/compositor/layer.h"
#include "ui/display/screen.h"
#include "ui/events/ash/keyboard_capability.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash::capture_mode_util {

namespace {

constexpr float kBannerViewTopRadius = 0.0f;
constexpr float kBannerViewBottomRadius = 8.0f;
constexpr float kScaleUpFactor = 0.8f;

// The app ID used for the capture mode privacy indicators.
constexpr char kCaptureModePrivacyIndicatorsId[] = "system-capture-mode";

// Returns the target visibility of the camera preview, given the
// `confine_bounds_short_side_length`. The out parameter
// `out_is_surface_too_small` will be set to true if the preview should be
// hidden due to the surface within which it's confined is too small. Otherwise,
// it's unchanged.
bool CalculateCameraPreviewTargetVisibility(
    int confine_bounds_short_side_length,
    bool* out_is_surface_too_small) {
  DCHECK(out_is_surface_too_small);

  // If the short side of the bounds within which the camera preview should be
  // confined is too small, the camera should be hidden.
  if (confine_bounds_short_side_length <
      capture_mode::kMinCaptureSurfaceShortSideLengthForVisibleCamera) {
    *out_is_surface_too_small = true;
    return false;
  }

  // Now that we determined that its size doesn't affect its visibility, we need
  // to check if we're in a capture mode session that is in a state that affects
  // the camera preview's visibility.
  auto* controller = CaptureModeController::Get();
  return !controller->IsActive() ||
         controller->capture_mode_session()
             ->CalculateCameraPreviewTargetVisibility();
}

void FadeInWidget(views::Widget* widget,
                  const AnimationParams& animation_params) {
  DCHECK(widget);
  auto* layer = widget->GetLayer();
  DCHECK(!widget->GetNativeWindow()->TargetVisibility() ||
         layer->GetTargetOpacity() < 1.f);

  // Please notice the order matters here. When the opacity is set to 0.f, if
  // there's any on-going fade out animation, the `OnEnded` in `FadeOutWidget`
  // will be triggered, which will hide the widget and set its opacity to 1.f.
  // So `Show` should be triggered after setting the opacity to 0 to undo the
  // work done by the FadeOutWidget's OnEnded .
  if (layer->opacity() == 1.f)
    layer->SetOpacity(0.f);
  if (!widget->GetNativeWindow()->TargetVisibility())
    widget->Show();

  if (animation_params.apply_scale_up_animation) {
    layer->SetTransform(
        capture_mode_util::GetScaleTransformAboutCenter(layer, kScaleUpFactor));
  }

  views::AnimationBuilder builder;
  auto& animation_sequence_block =
      builder
          .SetPreemptionStrategy(
              ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
          .Once()
          .SetDuration(animation_params.animation_duration)
          .SetOpacity(layer, 1.f, animation_params.tween_type);

  // We should only set transform here if `apply_scale_up_animation` is true,
  // otherwise, it may mess up with the snap animation in
  // `SetCameraPreviewBounds`.
  if (animation_params.apply_scale_up_animation) {
    animation_sequence_block.SetTransform(layer, gfx::Transform(),
                                          gfx::Tween::ACCEL_20_DECEL_100);
  }
}

void FadeOutWidget(views::Widget* widget,
                   const AnimationParams& animation_params) {
  DCHECK(widget);
  DCHECK(widget->GetNativeWindow()->TargetVisibility());

  auto* layer = widget->GetLayer();
  DCHECK_EQ(layer->GetTargetOpacity(), 1.f);

  views::AnimationBuilder()
      .OnEnded(base::BindOnce(
          [](base::WeakPtr<views::Widget> the_widget) {
            if (!the_widget)
              return;

            // Please notice, the order matters here. If we set the layer's
            // opacity back to 1.f before calling `Hide`, flickering can be
            // seen.
            the_widget->Hide();
            the_widget->GetLayer()->SetOpacity(1.f);
          },
          widget->GetWeakPtr()))
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(animation_params.animation_duration)
      .SetOpacity(layer, 0.f, animation_params.tween_type);
}

}  // namespace

PrefService* GetActiveUserPrefService() {
  DCHECK(Shell::Get()->session_controller()->IsActiveUserSessionStarted());

  auto* pref_service =
      Shell::Get()->session_controller()->GetActivePrefService();
  DCHECK(pref_service);
  return pref_service;
}

bool IsCaptureModeActive() {
  return CaptureModeController::Get()->IsActive();
}

gfx::PointF GetEventScreenLocation(const ui::LocatedEvent& event) {
  return event.target()->GetScreenLocationF(event);
}

gfx::Point GetLocationForFineTunePosition(const gfx::Rect& rect,
                                          FineTunePosition position) {
  switch (position) {
    case FineTunePosition::kTopLeftVertex:
      return rect.origin();
    case FineTunePosition::kTopEdge:
      return rect.top_center();
    case FineTunePosition::kTopRightVertex:
      return rect.top_right();
    case FineTunePosition::kRightEdge:
      return rect.right_center();
    case FineTunePosition::kBottomRightVertex:
      return rect.bottom_right();
    case FineTunePosition::kBottomEdge:
      return rect.bottom_center();
    case FineTunePosition::kBottomLeftVertex:
      return rect.bottom_left();
    case FineTunePosition::kLeftEdge:
      return rect.left_center();
    default:
      break;
  }

  NOTREACHED();
}

bool IsCornerFineTunePosition(FineTunePosition position) {
  switch (position) {
    case FineTunePosition::kTopLeftVertex:
    case FineTunePosition::kTopRightVertex:
    case FineTunePosition::kBottomRightVertex:
    case FineTunePosition::kBottomLeftVertex:
      return true;
    default:
      break;
  }
  return false;
}

StopRecordingButtonTray* GetStopRecordingButtonForRoot(aura::Window* root) {
  DCHECK(root);
  DCHECK(root->IsRootWindow());

  // Recording can end when a display being fullscreen-captured gets removed, in
  // this case, we don't need to hide the button.
  if (root->is_destroying())
    return nullptr;

  // Can be null while shutting down.
  auto* root_window_controller = RootWindowController::ForWindow(root);
  if (!root_window_controller)
    return nullptr;

  auto* stop_recording_button = root_window_controller->GetStatusAreaWidget()
                                    ->stop_recording_button_tray();
  DCHECK(stop_recording_button);
  return stop_recording_button;
}

void SetStopRecordingButtonVisibility(aura::Window* root, bool visible) {
  if (auto* stop_recording_button = GetStopRecordingButtonForRoot(root))
    stop_recording_button->SetVisiblePreferred(visible);
}

void TriggerAccessibilityAlert(const std::string& message) {
  Shell::Get()
      ->accessibility_controller()
      ->TriggerAccessibilityAlertWithMessage(message);
}

void TriggerAccessibilityAlert(int message_id) {
  TriggerAccessibilityAlert(l10n_util::GetStringUTF8(message_id));
}

void TriggerAccessibilityAlertSoon(const std::string& message) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(
          &AccessibilityController::TriggerAccessibilityAlertWithMessage,
          Shell::Get()->accessibility_controller()->GetWeakPtr(), message));
}

void TriggerAccessibilityAlertSoon(int message_id) {
  TriggerAccessibilityAlertSoon(l10n_util::GetStringUTF8(message_id));
}

void AdjustBoundsWithinConfinedBounds(const gfx::Rect& confined_bounds,
                                      gfx::Rect& out_bounds) {
  if (int confined_x = confined_bounds.x(); confined_x > out_bounds.x()) {
    out_bounds.set_x(confined_x);
  } else if (int confined_right = confined_bounds.right();
             confined_right < out_bounds.right()) {
    out_bounds.set_x(confined_right - out_bounds.width());
  }

  if (int confined_y = confined_bounds.y(); confined_y > out_bounds.y()) {
    out_bounds.set_y(confined_y);
  } else if (int confined_bottom = confined_bounds.bottom();
             confined_bottom < out_bounds.bottom()) {
    out_bounds.set_y(confined_bottom - out_bounds.height());
  }
}

CameraPreviewSnapPosition GetCameraNextHorizontalSnapPosition(
    CameraPreviewSnapPosition current,
    bool going_left) {
  switch (current) {
    case CameraPreviewSnapPosition::kTopLeft:
      return going_left ? current : CameraPreviewSnapPosition::kTopRight;
    case CameraPreviewSnapPosition::kTopRight:
      return going_left ? CameraPreviewSnapPosition::kTopLeft : current;
    case CameraPreviewSnapPosition::kBottomLeft:
      return going_left ? current : CameraPreviewSnapPosition::kBottomRight;
    case CameraPreviewSnapPosition::kBottomRight:
      return going_left ? CameraPreviewSnapPosition::kBottomLeft : current;
  }
}

CameraPreviewSnapPosition GetCameraNextVerticalSnapPosition(
    CameraPreviewSnapPosition current,
    bool going_up) {
  switch (current) {
    case CameraPreviewSnapPosition::kTopLeft:
      return going_up ? current : CameraPreviewSnapPosition::kBottomLeft;
    case CameraPreviewSnapPosition::kTopRight:
      return going_up ? current : CameraPreviewSnapPosition::kBottomRight;
    case CameraPreviewSnapPosition::kBottomLeft:
      return going_up ? CameraPreviewSnapPosition::kTopLeft : current;
    case CameraPreviewSnapPosition::kBottomRight:
      return going_up ? CameraPreviewSnapPosition::kTopRight : current;
  }
}

std::unique_ptr<views::View> CreateClipboardShortcutView() {
  std::unique_ptr<views::View> clipboard_shortcut_view =
      std::make_unique<views::View>();

  clipboard_shortcut_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal));

  const std::u16string shortcut_key = l10n_util::GetStringUTF16(
      Shell::Get()->keyboard_capability()->HasLauncherButtonOnAnyKeyboard()
          ? IDS_ASH_SHORTCUT_MODIFIER_LAUNCHER
          : IDS_ASH_SHORTCUT_MODIFIER_SEARCH);

  const std::u16string label_text = l10n_util::GetStringFUTF16(
      IDS_ASH_MULTIPASTE_SCREENSHOT_NOTIFICATION_NUDGE, shortcut_key);

  views::Label* shortcut_label =
      clipboard_shortcut_view->AddChildView(std::make_unique<views::Label>());
  shortcut_label->SetText(label_text);
  shortcut_label->SetBackgroundColor(cros_tokens::kCrosSysPrimary);
  shortcut_label->SetEnabledColor(cros_tokens::kCrosSysOnPrimary);
  ash::TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosBody2,
                                             *shortcut_label);
  return clipboard_shortcut_view;
}

// Creates the banner view that will show on top of the notification image.
std::unique_ptr<views::View> CreateBannerView() {
  std::unique_ptr<views::View> banner_view = std::make_unique<views::View>();

  auto* layout =
      banner_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kHorizontal,
          gfx::Insets::VH(kBannerVerticalInsetDip, kBannerHorizontalInsetDip),
          kBannerIconTextSpacingDip));

  const ui::ColorId background_color_id = cros_tokens::kCrosSysPrimary;
  banner_view->SetBackground(views::CreateRoundedRectBackground(
      background_color_id, kBannerViewTopRadius, kBannerViewBottomRadius));

  views::ImageView* icon =
      banner_view->AddChildView(std::make_unique<views::ImageView>());
  icon->SetImage(ui::ImageModel::FromVectorIcon(
      kCaptureModeCopiedToClipboardIcon, cros_tokens::kCrosSysOnPrimary,
      kBannerIconSizeDip));

  views::Label* label = banner_view->AddChildView(
      std::make_unique<views::Label>(l10n_util::GetStringUTF16(
          IDS_ASH_SCREEN_CAPTURE_SCREENSHOT_COPIED_TO_CLIPBOARD)));
  label->SetBackgroundColor(kColorAshControlBackgroundColorActive);
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->SetEnabledColor(cros_tokens::kCrosSysOnPrimary);
  ash::TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosBody2,
                                             *label);

  if (!display::Screen::GetScreen()->InTabletMode()) {
    banner_view->AddChildView(CreateClipboardShortcutView());
    layout->SetFlexForView(label, 1);

    // Notify the clipboard history of the created notification.
    ClipboardHistoryController::Get()->OnScreenshotNotificationCreated();
  }
  return banner_view;
}

// Creates the play icon view which shows on top of the video thumbnail in the
// notification.
std::unique_ptr<views::View> CreatePlayIconView() {
  auto play_view = std::make_unique<views::ImageView>();
  play_view->SetImage(ui::ImageModel::FromVectorIcon(
      kCaptureModePlayIcon, kColorAshIconColorPrimary, kPlayIconSizeDip));
  play_view->SetHorizontalAlignment(views::ImageView::Alignment::kCenter);
  play_view->SetVerticalAlignment(views::ImageView::Alignment::kCenter);
  play_view->SetBackground(views::CreateRoundedRectBackground(
      kColorAshShieldAndBase80, kPlayIconBackgroundCornerRadiusDip));
  return play_view;
}

gfx::Point GetLocalCenterPoint(ui::Layer* layer) {
  return gfx::Rect(layer->GetTargetBounds().size()).CenterPoint();
}

gfx::Transform GetScaleTransformAboutCenter(ui::Layer* layer, float scale) {
  return gfx::GetScaleTransform(GetLocalCenterPoint(layer), scale);
}

CameraPreviewSizeSpecs CalculateCameraPreviewSizeSpecs(
    const gfx::Size& confine_bounds_size,
    bool is_collapsed) {
  // We divide the shorter side of the confine bounds by a divider to calculate
  // the expanded diameter. Note that both expanded and collapsed diameters are
  // clamped at a minimum value of `kMinCameraPreviewDiameter`.
  const int short_side =
      std::min(confine_bounds_size.width(), confine_bounds_size.height());
  const int expanded_diameter =
      std::max(short_side / capture_mode::kCaptureSurfaceShortSideDivider,
               capture_mode::kMinCameraPreviewDiameter);

  // If the expanded diameter is below a certain threshold, we consider it too
  // small to allow it to collapse, and in that case the resize button will be
  // hidden.
  const bool is_collapsible =
      expanded_diameter >= capture_mode::kMinCollapsibleCameraPreviewDiameter;

  // Pick the actual diameter based on whether the preview is currently expanded
  // or collapsed.
  const int diameter =
      !is_collapsed
          ? expanded_diameter
          : std::max(expanded_diameter / capture_mode::kCollapsedPreviewDivider,
                     capture_mode::kMinCameraPreviewDiameter);

  bool is_surface_too_small = false;
  const bool should_be_visible =
      CalculateCameraPreviewTargetVisibility(short_side, &is_surface_too_small);

  // If the surface was determined to be too small, the preview should be
  // hidden.
  DCHECK(!is_surface_too_small || !should_be_visible);

  return CameraPreviewSizeSpecs{gfx::Size(diameter, diameter), is_collapsible,
                                should_be_visible, is_surface_too_small};
}

aura::Window* GetTopMostCapturableWindowAtPoint(
    const gfx::Point& screen_point) {
  auto* controller = CaptureModeController::Get();
  std::set<aura::Window*> ignore_windows;
  auto* camera_controller = controller->camera_controller();
  if (auto* camera_preview_widget = camera_controller->camera_preview_widget())
    ignore_windows.insert(camera_preview_widget->GetNativeWindow());

  if (controller->IsActive()) {
    std::set<aura::Window*> session_windows =
        controller->capture_mode_session()->GetWindowsToIgnoreFromWidgets();
    ignore_windows.insert(session_windows.begin(), session_windows.end());
  }

  return GetTopmostWindowAtPoint(screen_point, ignore_windows);
}

bool GetWidgetCurrentVisibility(views::Widget* widget) {
  // Note that we use `aura::Window::TargetVisibility()` rather than
  // `views::Widget::IsVisible()` (which in turn uses
  // `aura::Window::IsVisible()`). The reason is because the latter takes into
  // account whether window's layer is drawn or not. We want to calculate the
  // current visibility only based on the actual visibility of the window
  // itself, so that we can correctly compare it against `target_visibility`.
  // Note that the widget may be a child of the unparented container (which is
  // always hidden), yet the native window is shown.
  return widget->GetNativeWindow()->TargetVisibility() &&
         widget->GetLayer()->GetTargetOpacity() > 0.f;
}

bool SetWidgetVisibility(views::Widget* widget,
                         bool target_visibility,
                         std::optional<AnimationParams> animation_params) {
  DCHECK(widget);
  if (target_visibility == GetWidgetCurrentVisibility(widget))
    return false;

  if (animation_params) {
    if (target_visibility)
      FadeInWidget(widget, *animation_params);
    else
      FadeOutWidget(widget, *animation_params);
  } else {
    if (target_visibility)
      widget->Show();
    else
      widget->Hide();
  }
  return true;
}

aura::Window* GetPreferredRootWindow(
    std::optional<gfx::Point> location_in_screen) {
  const int64_t display_id =
      (location_in_screen
           ? display::Screen::GetScreen()->GetDisplayNearestPoint(
                 *location_in_screen)
           : Shell::Get()->cursor_manager()->GetDisplay())
          .id();

  // The Display object returned by `CursorManager::GetDisplay()` may be stale,
  // but will have the correct id.
  DCHECK_NE(display::kInvalidDisplayId, display_id);
  auto* root = Shell::GetRootWindowForDisplayId(display_id);
  return root ? root : Shell::GetPrimaryRootWindow();
}

void ConfigLabelView(views::Label* label_view) {
  label_view->SetEnabledColor(kColorAshTextColorPrimary);
  label_view->SetBackgroundColor(SK_ColorTRANSPARENT);
  label_view->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label_view->SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
}

views::BoxLayout* CreateAndInitBoxLayoutForView(views::View* view) {
  auto* box_layout = view->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
      capture_mode::kBetweenChildSpacing));
  box_layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);
  return box_layout;
}

void MaybeUpdateCaptureModePrivacyIndicators() {
  // Privacy indicator is only enabled when Video Conference is disabled.
  if (features::IsVideoConferenceEnabled()) {
    return;
  }

  auto* controller = CaptureModeController::Get();
  const bool is_camera_used =
      !!controller->camera_controller()->camera_preview_widget();
  const bool is_microphone_used = controller->IsAudioRecordingInProgress();

  PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
      /*app_id=*/kCaptureModePrivacyIndicatorsId,
      /*app_name=*/
      l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_CAPTURE_MODE_BUTTON_LABEL),
      is_camera_used, is_microphone_used, /*delegate=*/
      base::MakeRefCounted<PrivacyIndicatorsNotificationDelegate>(),
      PrivacyIndicatorsSource::kScreenCapture);
}

ui::ColorProvider* GetColorProviderForNativeTheme() {
  auto* native_theme = ui::NativeTheme::GetInstanceForNativeUi();
  return ui::ColorProviderManager::Get().GetColorProviderFor(
      native_theme->GetColorProviderKey(nullptr));
}

bool IsEventTargetedOnWidget(const ui::LocatedEvent& event,
                             views::Widget* widget) {
  auto* target = static_cast<aura::Window*>(event.target());
  return widget && widget->GetNativeWindow()->Contains(target);
}

gfx::Rect CalculateHighlightLayerBounds(const gfx::PointF& center_point,
                                        int highlight_layer_radius) {
  return gfx::Rect(center_point.x() - highlight_layer_radius,
                   center_point.y() - highlight_layer_radius,
                   highlight_layer_radius * 2, highlight_layer_radius * 2);
}

void SetHighlightBorder(views::View* view,
                        int corner_radius,
                        views::HighlightBorder::Type type) {
  view->SetBorder(
      std::make_unique<views::HighlightBorder>(corner_radius, type));
}

chromeos::FrameHeader* GetWindowFrameHeader(aura::Window* window) {
  CHECK(window);

  if (auto* widget = views::Widget::GetWidgetForNativeWindow(window)) {
    return chromeos::FrameHeader::Get(widget);
  }

  return nullptr;
}

gfx::Rect GetCaptureWindowConfineBounds(aura::Window* window) {
  CHECK(window);
  CHECK(!window->IsRootWindow());

  // When the surface being captured is a window, on-capture-surface UI
  // elements, such as the selfie camera or the demo tools key combo widget,
  // need to be confined within the *local* bounds of this window, since
  // they are added as direct children of the window so that they can get
  // captured.
  gfx::Rect result(window->bounds().size());

  // Inset from the top by the height of the frame header, in order to avoid for
  // example having the selfie camera intersecting with the caption buttons.
  // TODO(afakhry): This will not work for lacros. Fix this if it becomes a
  // priority.
  if (auto* frame_header = GetWindowFrameHeader(window)) {
    result.Inset(gfx::Insets::TLBR(frame_header->GetHeaderHeight(), 0, 0, 0));
  }

  return result;
}

gfx::Rect GetEffectivePartialRegionBounds(
    const gfx::Rect& partial_region_bounds,
    aura::Window* root_window) {
  CHECK(root_window);

  gfx::Rect result = partial_region_bounds;
  result.AdjustToFit(root_window->bounds());
  return result;
}

void AddActionButton(views::Button::PressedCallback callback,
                     std::u16string text,
                     const gfx::VectorIcon* icon,
                     const ActionButtonRank rank,
                     ActionButtonViewID id) {
  if (auto* controller = CaptureModeController::Get(); controller->IsActive()) {
    controller->capture_mode_session()->AddActionButton(std::move(callback),
                                                        text, icon, rank, id);
  }
}

void AnimateToOpacity(views::Widget* widget,
                      const float opacity,
                      const base::TimeDelta duration) {
  ui::Layer* layer = widget->GetLayer();
  if (layer->GetTargetOpacity() == opacity) {
    return;
  }

  // If the target opacity is 0.f, disable events on the widget.
  const bool visible = opacity != 0.f;
  widget->GetContentsView()->SetCanProcessEventsWithinSubtree(visible);
  widget->GetNativeWindow()->SetEventTargetingPolicy(
      visible ? aura::EventTargetingPolicy::kTargetAndDescendants
              : aura::EventTargetingPolicy::kNone);

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(duration)
      .SetOpacity(layer, opacity, gfx::Tween::FAST_OUT_SLOW_IN);
}

}  // namespace ash::capture_mode_util