capture_mode_type_view.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/capture_mode/capture_mode_type_view.h"
  5. #include <memory>
  6. #include "ash/capture_mode/capture_mode_camera_controller.h"
  7. #include "ash/capture_mode/capture_mode_constants.h"
  8. #include "ash/capture_mode/capture_mode_controller.h"
  9. #include "ash/capture_mode/capture_mode_metrics.h"
  10. #include "ash/capture_mode/capture_mode_toggle_button.h"
  11. #include "ash/resources/vector_icons/vector_icons.h"
  12. #include "ash/strings/grit/ash_strings.h"
  13. #include "ash/style/ash_color_provider.h"
  14. #include "base/bind.h"
  15. #include "ui/base/l10n/l10n_util.h"
  16. #include "ui/base/metadata/metadata_impl_macros.h"
  17. #include "ui/views/background.h"
  18. #include "ui/views/border.h"
  19. #include "ui/views/layout/box_layout.h"
  20. namespace ash {
  21. namespace {
  22. constexpr int kBackgroundCornerRadius = 18;
  23. constexpr gfx::Insets kViewInsets{2};
  24. // If `projector_mode` is false, Creates and initializes the toggle button that
  25. // is used for switching to image capture as a child of the given
  26. // `capture_mode_type_view` and returns a pointer to it.
  27. // `on_image_toggle_method` is a pointer to a member function inside
  28. // `CaptureModeTypeView` which will be triggered when the image toggle button is
  29. // pressed. Returns nullptr if `projector_mode` is true.
  30. CaptureModeToggleButton* MaybeCreateImageToggleButton(
  31. CaptureModeTypeView* capture_mode_type_view,
  32. void (CaptureModeTypeView::*on_image_toggle_method)(),
  33. bool projector_mode) {
  34. if (projector_mode)
  35. return nullptr;
  36. CaptureModeToggleButton* image_toggle_button =
  37. capture_mode_type_view->AddChildView(
  38. std::make_unique<CaptureModeToggleButton>(
  39. base::BindRepeating(on_image_toggle_method,
  40. base::Unretained(capture_mode_type_view)),
  41. kCaptureModeImageIcon));
  42. image_toggle_button->SetTooltipText(
  43. l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_TOOLTIP_SCREENSHOT));
  44. return image_toggle_button;
  45. }
  46. } // namespace
  47. CaptureModeTypeView::CaptureModeTypeView(bool projector_mode)
  48. : image_toggle_button_(
  49. MaybeCreateImageToggleButton(this,
  50. &CaptureModeTypeView::OnImageToggle,
  51. projector_mode)),
  52. video_toggle_button_(
  53. AddChildView(std::make_unique<CaptureModeToggleButton>(
  54. base::BindRepeating(&CaptureModeTypeView::OnVideoToggle,
  55. base::Unretained(this)),
  56. kCaptureModeVideoIcon))) {
  57. auto* color_provider = AshColorProvider::Get();
  58. const SkColor bg_color = color_provider->GetControlsLayerColor(
  59. AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive);
  60. SetBackground(
  61. views::CreateRoundedRectBackground(bg_color, kBackgroundCornerRadius));
  62. SetBorder(views::CreateEmptyBorder(kViewInsets));
  63. auto* box_layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
  64. views::BoxLayout::Orientation::kHorizontal, gfx::Insets(0),
  65. capture_mode::kSpaceBetweenCaptureModeTypeButtons));
  66. box_layout->set_cross_axis_alignment(
  67. views::BoxLayout::CrossAxisAlignment::kCenter);
  68. auto* controller = CaptureModeController::Get();
  69. if (controller->is_recording_in_progress()) {
  70. // We can't have more than one recording at the same time.
  71. video_toggle_button_->SetEnabled(false);
  72. }
  73. video_toggle_button_->SetTooltipText(
  74. l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_TOOLTIP_SCREENRECORD));
  75. }
  76. CaptureModeTypeView::~CaptureModeTypeView() = default;
  77. void CaptureModeTypeView::OnCaptureTypeChanged(CaptureModeType new_type) {
  78. auto* controller = CaptureModeController::Get();
  79. const bool is_video = new_type == CaptureModeType::kVideo;
  80. DCHECK(!controller->is_recording_in_progress() || !is_video);
  81. video_toggle_button_->SetToggled(is_video);
  82. if (image_toggle_button_) {
  83. image_toggle_button_->SetToggled(!is_video);
  84. DCHECK_NE(image_toggle_button_->GetToggled(),
  85. video_toggle_button_->GetToggled());
  86. }
  87. auto* camera_controller = controller->camera_controller();
  88. DCHECK(camera_controller);
  89. // Set the value to true for `SetShouldShowPreview` when the capture mode
  90. // session is started and switched to a video recording mode before recording
  91. // starts. False when it is switched to image capture mode.
  92. // Don't trigger `SetShouldShowPreview` if there's a video recording in
  93. // progress, since the capture type is restricted to `kImage` at this use case
  94. // and we don't want to affect the camera preview for the in_progress video
  95. // recording.
  96. if (!controller->is_recording_in_progress())
  97. camera_controller->SetShouldShowPreview(is_video);
  98. }
  99. void CaptureModeTypeView::OnImageToggle() {
  100. RecordCaptureModeBarButtonType(CaptureModeBarButtonType::kScreenCapture);
  101. CaptureModeController::Get()->SetType(CaptureModeType::kImage);
  102. }
  103. void CaptureModeTypeView::OnVideoToggle() {
  104. auto* controller = CaptureModeController::Get();
  105. DCHECK(!controller->is_recording_in_progress());
  106. RecordCaptureModeBarButtonType(CaptureModeBarButtonType::kScreenRecord);
  107. controller->SetType(CaptureModeType::kVideo);
  108. }
  109. BEGIN_METADATA(CaptureModeTypeView, views::View)
  110. END_METADATA
  111. } // namespace ash