phone_hub_interstitial_view.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/phonehub/phone_hub_interstitial_view.h"
  5. #include <memory>
  6. #include <string>
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/style/ash_color_provider.h"
  12. #include "ash/system/phonehub/ui_constants.h"
  13. #include "ash/system/tray/tray_constants.h"
  14. #include "ash/system/tray/tray_popup_utils.h"
  15. #include "skia/ext/image_operations.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/base/metadata/metadata_impl_macros.h"
  18. #include "ui/base/models/image_model.h"
  19. #include "ui/base/resource/resource_bundle.h"
  20. #include "ui/compositor/layer.h"
  21. #include "ui/gfx/geometry/insets.h"
  22. #include "ui/gfx/geometry/size.h"
  23. #include "ui/gfx/image/image_skia_operations.h"
  24. #include "ui/views/border.h"
  25. #include "ui/views/controls/button/button.h"
  26. #include "ui/views/controls/button/label_button.h"
  27. #include "ui/views/controls/label.h"
  28. #include "ui/views/controls/progress_bar.h"
  29. #include "ui/views/layout/box_layout_view.h"
  30. #include "ui/views/layout/flex_layout_view.h"
  31. #include "ui/views/view_class_properties.h"
  32. namespace ash {
  33. namespace {
  34. constexpr auto kLabelInsets = gfx::Insets::VH(0, 4);
  35. }
  36. PhoneHubInterstitialView::PhoneHubInterstitialView(bool show_progress,
  37. bool show_image) {
  38. // In dark light mode, we switch TrayBubbleView to use a textured layer
  39. // instead of solid color layer, so no need to create an extra layer here.
  40. if (!features::IsDarkLightModeEnabled()) {
  41. SetPaintToLayer();
  42. layer()->SetFillsBoundsOpaquely(false);
  43. }
  44. auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>());
  45. layout->SetOrientation(views::BoxLayout::Orientation::kVertical);
  46. auto* color_provider = AshColorProvider::Get();
  47. if (show_progress) {
  48. auto* progress_bar_container =
  49. AddChildView(std::make_unique<views::BoxLayoutView>());
  50. progress_bar_container->SetOrientation(
  51. views::BoxLayout::Orientation::kVertical);
  52. progress_bar_container->SetMainAxisAlignment(
  53. views::BoxLayout::MainAxisAlignment::kCenter);
  54. progress_bar_ = progress_bar_container->AddChildView(
  55. std::make_unique<views::ProgressBar>(2));
  56. progress_bar_->SetForegroundColor(color_provider->GetContentLayerColor(
  57. AshColorProvider::ContentLayerType::kIconColorProminent));
  58. progress_bar_->SetValue(-1.0);
  59. }
  60. auto* content_container =
  61. AddChildView(std::make_unique<views::FlexLayoutView>());
  62. content_container->SetOrientation(views::LayoutOrientation::kVertical);
  63. content_container->SetMainAxisAlignment(views::LayoutAlignment::kCenter);
  64. content_container->SetInteriorMargin(
  65. gfx::Insets::VH(0, kBubbleHorizontalSidePaddingDip) +
  66. (features::IsDarkLightModeEnabled() ? gfx::Insets::TLBR(0, 0, 16, 0)
  67. : gfx::Insets()));
  68. // Set up image if any.
  69. if (show_image) {
  70. image_ =
  71. content_container->AddChildView(std::make_unique<views::ImageView>());
  72. image_->SetProperty(views::kMarginsKey, gfx::Insets::VH(20, 0));
  73. image_->SetProperty(views::kCrossAxisAlignmentKey,
  74. views::LayoutAlignment::kCenter);
  75. image_->SetImageSize(gfx::Size(216, 216));
  76. }
  77. // Set up title view, which should be left-aligned.
  78. title_ = content_container->AddChildView(std::make_unique<views::Label>());
  79. title_->SetProperty(views::kCrossAxisAlignmentKey,
  80. views::LayoutAlignment::kStart);
  81. title_->SetProperty(views::kMarginsKey, kLabelInsets);
  82. title_->SetLineHeight(48);
  83. auto label_color = color_provider->GetContentLayerColor(
  84. AshColorProvider::ContentLayerType::kTextColorPrimary);
  85. title_->SetEnabledColor(label_color);
  86. TrayPopupUtils::SetLabelFontList(title_,
  87. TrayPopupUtils::FontStyle::kSubHeader);
  88. // Set up multi-line description view.
  89. description_ =
  90. content_container->AddChildView(std::make_unique<views::Label>());
  91. description_->SetProperty(views::kMarginsKey,
  92. kLabelInsets + gfx::Insets::TLBR(0, 0, 12, 0));
  93. description_->SetProperty(
  94. views::kFlexBehaviorKey,
  95. views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
  96. views::MaximumFlexSizeRule::kUnbounded, true));
  97. description_->SetEnabledColor(label_color);
  98. TrayPopupUtils::SetLabelFontList(
  99. description_, TrayPopupUtils::FontStyle::kDetailedViewLabel);
  100. description_->SetMultiLine(true);
  101. description_->SetLineHeight(20);
  102. description_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  103. // Set up button container view, which should be right-aligned.
  104. button_container_ =
  105. content_container->AddChildView(std::make_unique<views::BoxLayoutView>());
  106. button_container_->SetProperty(views::kCrossAxisAlignmentKey,
  107. views::LayoutAlignment::kEnd);
  108. button_container_->SetProperty(views::kMarginsKey,
  109. gfx::Insets::TLBR(16, 0, 0, 0));
  110. button_container_->SetBetweenChildSpacing(8);
  111. }
  112. PhoneHubInterstitialView::~PhoneHubInterstitialView() = default;
  113. void PhoneHubInterstitialView::SetImage(const ui::ImageModel& image_model) {
  114. // Expect a non-null |image_| view and a nonempty |image_model|.
  115. DCHECK(image_);
  116. DCHECK(!image_model.IsEmpty());
  117. image_->SetImage(image_model);
  118. }
  119. void PhoneHubInterstitialView::SetTitle(const std::u16string& title) {
  120. // Expect a non-empty string for the title.
  121. DCHECK(!title.empty());
  122. title_->SetText(title);
  123. }
  124. void PhoneHubInterstitialView::SetDescription(const std::u16string& desc) {
  125. // Expect a non-empty string for the description.
  126. DCHECK(!desc.empty());
  127. description_->SetText(desc);
  128. }
  129. void PhoneHubInterstitialView::AddButton(
  130. std::unique_ptr<views::Button> button) {
  131. description_->SetProperty(views::kMarginsKey, kLabelInsets);
  132. button_container_->AddChildView(std::move(button));
  133. }
  134. BEGIN_METADATA(PhoneHubInterstitialView, views::View)
  135. END_METADATA
  136. } // namespace ash