downloads_section.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/system/holding_space/downloads_section.h"
  5. #include "ash/bubble/bubble_utils.h"
  6. #include "ash/bubble/simple_grid_layout.h"
  7. #include "ash/public/cpp/holding_space/holding_space_client.h"
  8. #include "ash/public/cpp/holding_space/holding_space_constants.h"
  9. #include "ash/public/cpp/holding_space/holding_space_controller.h"
  10. #include "ash/public/cpp/holding_space/holding_space_metrics.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 "ash/system/holding_space/holding_space_item_chip_view.h"
  15. #include "base/bind.h"
  16. #include "base/callback_helpers.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. #include "ui/color/color_id.h"
  19. #include "ui/compositor/layer.h"
  20. #include "ui/gfx/paint_vector_icon.h"
  21. #include "ui/views/accessibility/view_accessibility.h"
  22. #include "ui/views/controls/button/button.h"
  23. #include "ui/views/controls/focus_ring.h"
  24. #include "ui/views/controls/highlight_path_generator.h"
  25. #include "ui/views/controls/image_view.h"
  26. #include "ui/views/controls/label.h"
  27. #include "ui/views/layout/box_layout.h"
  28. namespace ash {
  29. namespace {
  30. // CallbackPathGenerator -------------------------------------------------------
  31. class CallbackPathGenerator : public views::HighlightPathGenerator {
  32. public:
  33. using Callback = base::RepeatingCallback<gfx::RRectF()>;
  34. explicit CallbackPathGenerator(Callback callback) : callback_(callback) {}
  35. CallbackPathGenerator(const CallbackPathGenerator&) = delete;
  36. CallbackPathGenerator& operator=(const CallbackPathGenerator&) = delete;
  37. ~CallbackPathGenerator() override = default;
  38. private:
  39. // views::HighlightPathGenerator:
  40. absl::optional<gfx::RRectF> GetRoundRect(const gfx::RectF& rect) override {
  41. return callback_.Run();
  42. }
  43. Callback callback_;
  44. };
  45. // Header ----------------------------------------------------------------------
  46. class Header : public views::Button {
  47. public:
  48. Header() {
  49. SetAccessibleName(
  50. l10n_util::GetStringUTF16(IDS_ASH_HOLDING_SPACE_DOWNLOADS_TITLE));
  51. SetCallback(
  52. base::BindRepeating(&Header::OnPressed, base::Unretained(this)));
  53. SetID(kHoldingSpaceDownloadsSectionHeaderId);
  54. auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
  55. views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
  56. kHoldingSpaceDownloadsHeaderSpacing));
  57. // Label.
  58. auto* label = AddChildView(bubble_utils::CreateLabel(
  59. bubble_utils::LabelStyle::kHeader,
  60. l10n_util::GetStringUTF16(IDS_ASH_HOLDING_SPACE_DOWNLOADS_TITLE)));
  61. label->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  62. layout->SetFlexForView(label, 1);
  63. // Chevron.
  64. chevron_ = AddChildView(std::make_unique<views::ImageView>());
  65. chevron_->SetFlipCanvasOnPaintForRTLUI(true);
  66. // Focus ring.
  67. // Though the entirety of the header is focusable and behaves as a single
  68. // button, the focus ring is drawn as a circle around just the `chevron_`.
  69. views::FocusRing::Get(this)->SetPathGenerator(
  70. std::make_unique<CallbackPathGenerator>(base::BindRepeating(
  71. [](const views::View* chevron) {
  72. const float radius = chevron->width() / 2.f;
  73. gfx::RRectF path(gfx::RectF(chevron->bounds()), radius);
  74. if (base::i18n::IsRTL()) {
  75. // Manually adjust for flipped canvas in RTL.
  76. path.Offset(-chevron->parent()->width(), 0.f);
  77. path.Scale(-1.f, 1.f);
  78. }
  79. return path;
  80. },
  81. base::Unretained(chevron_))));
  82. views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
  83. }
  84. private:
  85. // views::Button:
  86. void OnThemeChanged() override {
  87. views::Button::OnThemeChanged();
  88. AshColorProvider* const ash_color_provider = AshColorProvider::Get();
  89. // Chevron.
  90. chevron_->SetImage(gfx::CreateVectorIcon(
  91. kChevronRightIcon, kHoldingSpaceDownloadsChevronIconSize,
  92. ash_color_provider->GetContentLayerColor(
  93. AshColorProvider::ContentLayerType::kIconColorPrimary)));
  94. }
  95. void OnPressed() {
  96. holding_space_metrics::RecordDownloadsAction(
  97. holding_space_metrics::DownloadsAction::kClick);
  98. HoldingSpaceController::Get()->client()->OpenDownloads(base::DoNothing());
  99. }
  100. // Owned by view hierarchy.
  101. views::ImageView* chevron_ = nullptr;
  102. };
  103. } // namespace
  104. // DownloadsSection ------------------------------------------------------------
  105. DownloadsSection::DownloadsSection(HoldingSpaceViewDelegate* delegate)
  106. : HoldingSpaceItemViewsSection(
  107. delegate,
  108. /*supported_types=*/
  109. {HoldingSpaceItem::Type::kArcDownload,
  110. HoldingSpaceItem::Type::kDiagnosticsLog,
  111. HoldingSpaceItem::Type::kDownload,
  112. HoldingSpaceItem::Type::kLacrosDownload,
  113. HoldingSpaceItem::Type::kNearbyShare,
  114. HoldingSpaceItem::Type::kPrintedPdf, HoldingSpaceItem::Type::kScan,
  115. HoldingSpaceItem::Type::kPhoneHubCameraRoll},
  116. /*max_count=*/kMaxDownloads) {}
  117. DownloadsSection::~DownloadsSection() = default;
  118. const char* DownloadsSection::GetClassName() const {
  119. return "DownloadsSection";
  120. }
  121. std::unique_ptr<views::View> DownloadsSection::CreateHeader() {
  122. auto header = std::make_unique<Header>();
  123. header->SetPaintToLayer();
  124. header->layer()->SetFillsBoundsOpaquely(false);
  125. return header;
  126. }
  127. std::unique_ptr<views::View> DownloadsSection::CreateContainer() {
  128. auto container = std::make_unique<views::View>();
  129. container->SetLayoutManager(std::make_unique<SimpleGridLayout>(
  130. kHoldingSpaceChipCountPerRow,
  131. /*column_spacing=*/kHoldingSpaceSectionContainerChildSpacing,
  132. /*row_spacing=*/kHoldingSpaceSectionContainerChildSpacing));
  133. return container;
  134. }
  135. std::unique_ptr<HoldingSpaceItemView> DownloadsSection::CreateView(
  136. const HoldingSpaceItem* item) {
  137. return std::make_unique<HoldingSpaceItemChipView>(delegate(), item);
  138. }
  139. } // namespace ash