File: chapter_item_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (153 lines) | stat: -rw-r--r-- 6,028 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/global_media_controls/public/views/chapter_item_view.h"

#include <string>

#include "base/containers/fixed_flat_map.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "components/global_media_controls/media_view_utils.h"
#include "components/global_media_controls/public/format_duration.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/fill_layout.h"

namespace global_media_controls {

namespace {
constexpr auto kItemCornerRadius =
    gfx::RoundedCornersF(kDefaultArtworkCornerRadius);
constexpr gfx::Size kImageSize(64, 40);

// A `HighlightPathGenerator` that uses caller-supplied rounded rect corners.
class RoundedCornerHighlightPathGenerator
    : public views::HighlightPathGenerator {
 public:
  explicit RoundedCornerHighlightPathGenerator(
      const gfx::RoundedCornersF& corners)
      : corners_(corners) {}

  RoundedCornerHighlightPathGenerator(
      const RoundedCornerHighlightPathGenerator&) = delete;
  RoundedCornerHighlightPathGenerator& operator=(
      const RoundedCornerHighlightPathGenerator&) = delete;

  // views::HighlightPathGenerator:
  std::optional<gfx::RRectF> GetRoundRect(const gfx::RectF& rect) override {
    return gfx::RRectF(rect, corners_);
  }

 private:
  // The user-supplied rounded rect corners.
  const gfx::RoundedCornersF corners_;
};
}  // namespace

// TODO(b/327508008): Polish paddings, a11y, color and etc.
ChapterItemView::ChapterItemView(
    const media_session::ChapterInformation& chapter,
    const media_message_center::MediaColorTheme& theme,
    base::RepeatingCallback<void(const base::TimeDelta time)>
        on_chapter_pressed)
    : chapter_(chapter),
      theme_(theme),
      on_chapter_pressed_callback_(std::move(on_chapter_pressed)) {
  SetCallback(base::BindRepeating(&ChapterItemView::PerformAction,
                                  base::Unretained(this)));

  views::Builder<views::View>(this)
      .SetUseDefaultFillLayout(true)
      .SetAccessibleRole(ax::mojom::Role::kButton)
      .SetAccessibleName(u"TBD")
      .SetFocusBehavior(FocusBehavior::ALWAYS)
      .SetPaintToLayer()
      .AddChildren(
          views::Builder<views::BoxLayoutView>()
              .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
              .SetInsideBorderInsets(gfx::Insets(8))
              .SetBetweenChildSpacing(8)
              .AddChildren(
                  views::Builder<views::ImageView>()
                      .CopyAddressTo(&artwork_view_)
                      .SetPreferredSize(kImageSize),
                  views::Builder<views::BoxLayoutView>()
                      .SetOrientation(views::BoxLayout::Orientation::kVertical)
                      .AddChildren(
                          views::Builder<views::Label>()
                              .SetText(chapter.title())
                              .SetID(kChapterItemViewTitleId)
                              .SetFontList(gfx::FontList(
                                  {"Google Sans"}, gfx::Font::NORMAL, 13,
                                  gfx::Font::Weight::NORMAL))
                              .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                              .SetEnabledColor(
                                  theme_.primary_foreground_color_id),
                          views::Builder<views::Label>()
                              .SetText(
                                  GetFormattedDuration(chapter.startTime()))
                              .SetID(kChapterItemViewStartTimeId)
                              .SetFontList(gfx::FontList(
                                  {"Google Sans"}, gfx::Font::NORMAL, 12,
                                  gfx::Font::Weight::NORMAL))
                              .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                              .SetEnabledColor(
                                  theme_.secondary_foreground_color_id))))
      .BuildChildren();

  layer()->SetFillsBoundsOpaquely(false);
  layer()->SetRoundedCornerRadius(kItemCornerRadius);
  SetUpFocusHighlight(kItemCornerRadius);
}

ChapterItemView::~ChapterItemView() = default;

void ChapterItemView::UpdateArtwork(const gfx::ImageSkia& image) {
  if (image.isNull()) {
    // Hide the image so the other contents will adjust to fill the container.
    artwork_view_->SetVisible(false);
  }

  // Rescales the image.
  artwork_view_->SetImageSize(
      ScaleImageSizeToFitView(image.size(), kImageSize));
  artwork_view_->SetImage(ui::ImageModel::FromImageSkia(image));

  // Draws the image with rounded corners.
  auto path = SkPath().addRoundRect(
      RectToSkRect(gfx::Rect(kImageSize.width(), kImageSize.height())),
      kDefaultArtworkCornerRadius, kDefaultArtworkCornerRadius);
  artwork_view_->SetClipPath(path);

  artwork_view_->SetVisible(true);
}

void ChapterItemView::PerformAction(const ui::Event& event) {
  on_chapter_pressed_callback_.Run(chapter_.startTime());
}

void ChapterItemView::SetUpFocusHighlight(
    const gfx::RoundedCornersF& item_corner_radius) {
  views::FocusRing::Get(this)->SetColorId(theme_.focus_ring_color_id);
  views::FocusRing::Get(this)->SetHaloThickness(3.0f);
  views::HighlightPathGenerator::Install(
      this, std::make_unique<RoundedCornerHighlightPathGenerator>(
                item_corner_radius));
}

BEGIN_METADATA(ChapterItemView);
END_METADATA

}  // namespace global_media_controls