camera_roll_thumbnail.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2021 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/phonehub/camera_roll_thumbnail.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. #include "ash/components/phonehub/camera_roll_manager.h"
  7. #include "ash/components/phonehub/user_action_recorder.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/style/ash_color_provider.h"
  10. #include "base/bind.h"
  11. #include "ui/base/metadata/metadata_impl_macros.h"
  12. #include "ui/color/color_id.h"
  13. #include "ui/gfx/paint_vector_icon.h"
  14. #include "ui/views/controls/highlight_path_generator.h"
  15. namespace ash {
  16. namespace {
  17. // Appearance in dip.
  18. constexpr int kCameraRollThumbnailBorderRadius = 12;
  19. constexpr gfx::Size kCameraRollThumbnailBorderSize(74, 74);
  20. constexpr gfx::Point kCameraRollThumbnailVideoCircleOrigin(37, 37);
  21. constexpr int kCameraRollThumbnailVideoCircleRadius = 16;
  22. constexpr gfx::Point kCameraRollThumbnailVideoIconOrigin(27, 27);
  23. constexpr int kCameraRollThumbnailVideoIconSize = 20;
  24. } // namespace
  25. CameraRollThumbnail::CameraRollThumbnail(
  26. const int index,
  27. const phonehub::CameraRollItem& item,
  28. phonehub::CameraRollManager* camera_roll_manager,
  29. phonehub::UserActionRecorder* user_action_recorder)
  30. : views::MenuButton(base::BindRepeating(&CameraRollThumbnail::ButtonPressed,
  31. base::Unretained(this))),
  32. index_(index),
  33. metadata_(item.metadata()),
  34. video_type_(metadata_.mime_type().find("video/") == 0),
  35. image_(item.thumbnail().AsImageSkia()),
  36. camera_roll_manager_(camera_roll_manager),
  37. user_action_recorder_(user_action_recorder) {
  38. SetFocusBehavior(FocusBehavior::ALWAYS);
  39. views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
  40. views::InstallRoundRectHighlightPathGenerator(
  41. this, gfx::Insets(), kCameraRollThumbnailBorderRadius);
  42. SetClipPath(SkPath::RRect(SkRRect::MakeRectXY(
  43. SkRect::Make(SkIRect::MakeWH(kCameraRollThumbnailBorderSize.width(),
  44. kCameraRollThumbnailBorderSize.height())),
  45. SkIntToScalar(kCameraRollThumbnailBorderRadius),
  46. SkIntToScalar(kCameraRollThumbnailBorderRadius))));
  47. set_context_menu_controller(this);
  48. phone_hub_metrics::LogCameraRollContentShown(index_, GetMediaType());
  49. }
  50. CameraRollThumbnail::~CameraRollThumbnail() = default;
  51. void CameraRollThumbnail::PaintButtonContents(gfx::Canvas* canvas) {
  52. views::MenuButton::PaintButtonContents(canvas);
  53. auto* color_provider = AshColorProvider::Get();
  54. canvas->DrawColor(color_provider->GetControlsLayerColor(
  55. AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
  56. canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(), 0, 0,
  57. kCameraRollThumbnailBorderSize.width(),
  58. kCameraRollThumbnailBorderSize.height(), false);
  59. if (video_type_) {
  60. cc::PaintFlags flags;
  61. flags.setAntiAlias(true);
  62. flags.setColor(color_provider->GetBaseLayerColor(
  63. AshColorProvider::BaseLayerType::kTransparent80));
  64. flags.setStyle(cc::PaintFlags::kFill_Style);
  65. canvas->DrawCircle(kCameraRollThumbnailVideoCircleOrigin,
  66. kCameraRollThumbnailVideoCircleRadius, flags);
  67. canvas->DrawImageInt(
  68. CreateVectorIcon(
  69. kPhoneHubCameraRollItemVideoIcon, kCameraRollThumbnailVideoIconSize,
  70. color_provider->GetContentLayerColor(
  71. AshColorProvider::ContentLayerType::kIconColorPrimary)),
  72. kCameraRollThumbnailVideoIconOrigin.x(),
  73. kCameraRollThumbnailVideoIconOrigin.y());
  74. }
  75. }
  76. void CameraRollThumbnail::ShowContextMenuForViewImpl(
  77. views::View* source,
  78. const gfx::Point& point,
  79. ui::MenuSourceType source_type) {
  80. phone_hub_metrics::LogCameraRollContentClicked(index_, GetMediaType());
  81. menu_runner_ = std::make_unique<views::MenuRunner>(
  82. GetMenuModel(), views::MenuRunner::CONTEXT_MENU |
  83. views::MenuRunner::FIXED_ANCHOR |
  84. views::MenuRunner::USE_ASH_SYS_UI_LAYOUT);
  85. menu_runner_->RunMenuAt(GetWidget(), button_controller(), GetBoundsInScreen(),
  86. views::MenuAnchorPosition::kBubbleTopRight,
  87. ui::MENU_SOURCE_NONE);
  88. }
  89. void CameraRollThumbnail::ButtonPressed() {
  90. phone_hub_metrics::LogCameraRollContentClicked(index_, GetMediaType());
  91. DownloadRequested();
  92. }
  93. ui::SimpleMenuModel* CameraRollThumbnail::GetMenuModel() {
  94. if (!menu_model_)
  95. menu_model_ = std::make_unique<CameraRollMenuModel>(base::BindRepeating(
  96. &CameraRollThumbnail::DownloadRequested, base::Unretained(this)));
  97. return menu_model_.get();
  98. }
  99. void CameraRollThumbnail::DownloadRequested() {
  100. PA_LOG(INFO) << "Downloading Camera Roll Item: index=" << index_;
  101. camera_roll_manager_->DownloadItem(metadata_);
  102. user_action_recorder_->RecordCameraRollDownloadAttempt();
  103. phone_hub_metrics::LogCameraRollContextMenuDownload(index_, GetMediaType());
  104. }
  105. phone_hub_metrics::CameraRollMediaType CameraRollThumbnail::GetMediaType() {
  106. return video_type_ ? phone_hub_metrics::CameraRollMediaType::kVideo
  107. : phone_hub_metrics::CameraRollMediaType::kPhoto;
  108. }
  109. BEGIN_METADATA(CameraRollThumbnail, views::MenuButton)
  110. END_METADATA
  111. } // namespace ash