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
5 #include "ash/capture_mode/capture_mode_type_view.h"
6
7 #include <memory>
8
9 #include "ash/capture_mode/capture_mode_controller.h"
10 #include "ash/capture_mode/capture_mode_metrics.h"
11 #include "ash/capture_mode/capture_mode_toggle_button.h"
12 #include "ash/resources/vector_icons/vector_icons.h"
13 #include "ash/strings/grit/ash_strings.h"
14 #include "ash/style/ash_color_provider.h"
15 #include "base/bind.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/views/background.h"
18 #include "ui/views/layout/box_layout.h"
19 #include "ui/views/metadata/metadata_impl_macros.h"
20
21 namespace ash {
22
23 namespace {
24
25 constexpr int kBackgroundCornerRadius = 18;
26
27 constexpr gfx::Insets kViewInsets{2};
28
29 constexpr int kButtonSpacing = 2;
30
31 } // namespace
32
CaptureModeTypeView()33 CaptureModeTypeView::CaptureModeTypeView()
34 : image_toggle_button_(
35 AddChildView(std::make_unique<CaptureModeToggleButton>(
36 base::BindRepeating(&CaptureModeTypeView::OnImageToggle,
37 base::Unretained(this)),
38 kCaptureModeImageIcon))),
39 video_toggle_button_(
40 AddChildView(std::make_unique<CaptureModeToggleButton>(
41 base::BindRepeating(&CaptureModeTypeView::OnVideoToggle,
42 base::Unretained(this)),
43 kCaptureModeVideoIcon))) {
44 auto* color_provider = AshColorProvider::Get();
45 const SkColor bg_color = color_provider->GetControlsLayerColor(
46 AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive);
47 SetBackground(
48 views::CreateRoundedRectBackground(bg_color, kBackgroundCornerRadius));
49 SetBorder(views::CreateEmptyBorder(kViewInsets));
50 auto* box_layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
51 views::BoxLayout::Orientation::kHorizontal, gfx::Insets(0),
52 kButtonSpacing));
53 box_layout->set_cross_axis_alignment(
54 views::BoxLayout::CrossAxisAlignment::kCenter);
55 auto* controller = CaptureModeController::Get();
56 // We can't have more than one recording at the same time.
57 video_toggle_button_->SetEnabled(!controller->is_recording_in_progress());
58 OnCaptureTypeChanged(controller->type());
59
60 image_toggle_button_->SetTooltipText(
61 l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_TOOLTIP_SCREENSHOT));
62 video_toggle_button_->SetTooltipText(
63 l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_TOOLTIP_SCREENRECORD));
64 }
65
66 CaptureModeTypeView::~CaptureModeTypeView() = default;
67
OnCaptureTypeChanged(CaptureModeType new_type)68 void CaptureModeTypeView::OnCaptureTypeChanged(CaptureModeType new_type) {
69 DCHECK(!CaptureModeController::Get()->is_recording_in_progress() ||
70 new_type == CaptureModeType::kImage);
71 image_toggle_button_->SetToggled(new_type == CaptureModeType::kImage);
72 video_toggle_button_->SetToggled(new_type == CaptureModeType::kVideo);
73 DCHECK_NE(image_toggle_button_->GetToggled(),
74 video_toggle_button_->GetToggled());
75 }
76
OnImageToggle()77 void CaptureModeTypeView::OnImageToggle() {
78 RecordCaptureModeBarButtonType(CaptureModeBarButtonType::kScreenCapture);
79 CaptureModeController::Get()->SetType(CaptureModeType::kImage);
80 }
81
OnVideoToggle()82 void CaptureModeTypeView::OnVideoToggle() {
83 auto* controller = CaptureModeController::Get();
84 DCHECK(!controller->is_recording_in_progress());
85 RecordCaptureModeBarButtonType(CaptureModeBarButtonType::kScreenRecord);
86 controller->SetType(CaptureModeType::kVideo);
87 }
88
89 BEGIN_METADATA(CaptureModeTypeView, views::View)
90 END_METADATA
91
92 } // namespace ash
93