search_result_inline_icon_view.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2022 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/app_list/views/search_result_inline_icon_view.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include "ash/app_list/app_list_util.h"
  8. #include "ash/public/cpp/ash_typography.h"
  9. #include "ash/public/cpp/style/color_provider.h"
  10. #include "ash/style/ash_color_provider.h"
  11. #include "ui/gfx/canvas.h"
  12. #include "ui/gfx/paint_vector_icon.h"
  13. #include "ui/views/accessibility/view_accessibility.h"
  14. #include "ui/views/border.h"
  15. #include "ui/views/controls/image_view.h"
  16. #include "ui/views/controls/label.h"
  17. #include "ui/views/layout/fill_layout.h"
  18. #include "ui/views/layout/flex_layout.h"
  19. #include "ui/views/layout/flex_layout_view.h"
  20. #include "ui/views/style/typography.h"
  21. namespace ash {
  22. namespace {
  23. constexpr int kBorderThickness = 1;
  24. constexpr float kButtonCornerRadius = 6.0f;
  25. constexpr int kLeftRightMargin = 6;
  26. constexpr int kIconSize = 14;
  27. constexpr int kLabelMinEdgeLength = 20;
  28. } // namespace
  29. SearchResultInlineIconView::SearchResultInlineIconView() {
  30. SetLayoutManager(std::make_unique<views::FillLayout>());
  31. }
  32. SearchResultInlineIconView::~SearchResultInlineIconView() = default;
  33. void SearchResultInlineIconView::SetIcon(const gfx::VectorIcon& icon) {
  34. DCHECK(!label_);
  35. if (!icon_image_) {
  36. icon_image_ = AddChildView(std::make_unique<views::ImageView>());
  37. icon_image_->SetCanProcessEventsWithinSubtree(false);
  38. icon_image_->SetVisible(true);
  39. }
  40. icon_ = &icon;
  41. icon_image_->SetImage(gfx::CreateVectorIcon(
  42. *icon_, AshColorProvider::Get()->GetContentLayerColor(
  43. AshColorProvider::ContentLayerType::kTextColorURL)));
  44. icon_image_->SetImageSize(gfx::Size(kIconSize, kIconSize));
  45. icon_image_->SetVisible(true);
  46. int icon_top_bottom_margin = (kLabelMinEdgeLength - kIconSize) / 2;
  47. icon_image_->SetBorder(views::CreateEmptyBorder(
  48. gfx::Insets::TLBR(icon_top_bottom_margin, kLeftRightMargin,
  49. icon_top_bottom_margin, kLeftRightMargin)));
  50. SetVisible(true);
  51. }
  52. void SearchResultInlineIconView::SetText(const std::u16string& text) {
  53. DCHECK(!icon_image_);
  54. if (!label_) {
  55. label_ = AddChildView(std::make_unique<views::Label>());
  56. label_->SetBackgroundColor(SK_ColorTRANSPARENT);
  57. label_->SetVisible(true);
  58. label_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
  59. label_->SetTextContext(CONTEXT_SEARCH_RESULT_VIEW_INLINE_ANSWER_DETAILS);
  60. label_->SetTextStyle(views::style::STYLE_EMPHASIZED);
  61. }
  62. label_->SetText(text);
  63. label_->SetVisible(true);
  64. int label_left_right_margin =
  65. std::max(kLeftRightMargin, (kLabelMinEdgeLength - label_->width()) / 2);
  66. label_->SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(
  67. 0, label_left_right_margin, 0, label_left_right_margin)));
  68. label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  69. AshColorProvider::ContentLayerType::kTextColorURL));
  70. SetVisible(true);
  71. }
  72. void SearchResultInlineIconView::OnPaint(gfx::Canvas* canvas) {
  73. cc::PaintFlags paint_flags;
  74. paint_flags.setAntiAlias(true);
  75. paint_flags.setColor(AshColorProvider::Get()->GetContentLayerColor(
  76. AshColorProvider::ContentLayerType::kTextColorURL));
  77. paint_flags.setStyle(cc::PaintFlags::kStroke_Style);
  78. paint_flags.setStrokeWidth(kBorderThickness);
  79. gfx::Rect bounds = GetContentsBounds();
  80. bounds.Inset(gfx::Insets(kBorderThickness));
  81. canvas->DrawRoundRect(bounds, kButtonCornerRadius, paint_flags);
  82. }
  83. void SearchResultInlineIconView::OnThemeChanged() {
  84. views::View::OnThemeChanged();
  85. if (icon_image_) {
  86. DCHECK(icon_);
  87. icon_image_->SetImage(gfx::CreateVectorIcon(
  88. *icon_, AshColorProvider::Get()->GetContentLayerColor(
  89. AshColorProvider::ContentLayerType::kTextColorURL)));
  90. }
  91. if (label_) {
  92. label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  93. AshColorProvider::ContentLayerType::kTextColorURL));
  94. }
  95. }
  96. } // namespace ash