File: rich_hover_button.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 (322 lines) | stat: -rw-r--r-- 11,545 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/controls/rich_hover_button.h"

#include <memory>
#include <string_view>
#include <utility>

#include "base/strings/strcat.h"
#include "chrome/browser/ui/views/accessibility/non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/layer.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/view_class_properties.h"

namespace {

std::unique_ptr<views::ImageView> CreateIconView(ui::ImageModel icon) {
  auto view = std::make_unique<NonAccessibleImageView>();
  view->SetImage(std::move(icon));
  // Make sure hovering over the icon also hovers the `RichHoverButton`.
  view->SetCanProcessEventsWithinSubtree(false);
  // Don't cover |icon| when the ink drops are being painted.
  view->SetPaintToLayer();
  view->layer()->SetFillsBoundsOpaquely(false);
  return view;
}

// TODO(crbug.com/355018927): Remove this when we implement in views::Label.
class SubtitleLabelWrapper : public views::View {
  METADATA_HEADER(SubtitleLabelWrapper, views::View)
 public:
  explicit SubtitleLabelWrapper(std::unique_ptr<views::View> title) {
    SetUseDefaultFillLayout(true);
    title_ = AddChildView(std::move(title));
  }

 private:
  // View:
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override {
    gfx::Size preferred_size = title_->GetPreferredSize(available_size);
    if (!available_size.width().is_bounded()) {
      preferred_size.set_width(title_->GetMinimumSize().width());
    }
    return preferred_size;
  }

  raw_ptr<views::View> title_ = nullptr;
};

BEGIN_METADATA(SubtitleLabelWrapper)
END_METADATA

}  // namespace

RichHoverButton::RichHoverButton() {
  image_container_view()->SetProperty(views::kViewIgnoredByLayoutKey, true);
  label()->SetHandlesTooltips(false);
  label()->SetProperty(views::kViewIgnoredByLayoutKey, true);
  ink_drop_container()->SetProperty(views::kViewIgnoredByLayoutKey, true);

  start_ = children().size();

  views::Builder<RichHoverButton>(this)
      .SetBorder(
          views::CreateEmptyBorder(ChromeLayoutProvider::Get()->GetInsetsMetric(
              ChromeInsetsMetric::INSETS_PAGE_INFO_HOVER_BUTTON)))
      // Main icon placeholder.
      .AddChild(views::Builder<views::View>())
      // Title.
      .AddChild(views::Builder<views::Label>()
                    .CopyAddressTo(&title_)
                    .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
                    .SetTextStyle(views::style::STYLE_BODY_3_MEDIUM)
                    .SetHorizontalAlignment(gfx::ALIGN_LEFT)
                    .SetCanProcessEventsWithinSubtree(false))
      // Action icon placeholder.
      .AddChild(views::Builder<views::View>())
      .BuildChildren();

  custom_view_row_start_ = children().size();

  RecreateLayout();
}

RichHoverButton::RichHoverButton(views::Button::PressedCallback callback,
                                 ui::ImageModel icon,
                                 std::u16string_view title_text,
                                 std::u16string_view subtitle_text,
                                 ui::ImageModel action_icon,
                                 ui::ImageModel state_icon)
    : RichHoverButton() {
  SetCallback(std::move(callback));
  SetIcon(std::move(icon));
  SetTitleText(title_text);
  SetStateIcon(std::move(state_icon));
  SetActionIcon(std::move(action_icon));
  SetSubtitleText(subtitle_text);
}

RichHoverButton::~RichHoverButton() = default;

ui::ImageModel RichHoverButton::GetIcon() const {
  return icon_ ? icon_->GetImageModel() : ui::ImageModel();
}

void RichHoverButton::SetIcon(ui::ImageModel icon) {
  SetIconMember(icon_, start_, std::move(icon), true);
}

std::u16string_view RichHoverButton::GetTitleText() const {
  return title_->GetText();
}

void RichHoverButton::SetTitleText(std::u16string_view title_text) {
  title_->SetText(std::u16string(title_text));
  UpdateAccessibleName();
}

ui::ImageModel RichHoverButton::GetStateIcon() const {
  return state_icon_ ? state_icon_->GetImageModel() : ui::ImageModel();
}

void RichHoverButton::SetStateIcon(ui::ImageModel state_icon) {
  SetIconMember(state_icon_, start_ + 2, std::move(state_icon), false);
}

ui::ImageModel RichHoverButton::GetActionIcon() const {
  return action_icon_ ? action_icon_->GetImageModel() : ui::ImageModel();
}

void RichHoverButton::SetActionIcon(ui::ImageModel action_icon) {
  SetIconMember(action_icon_, start_ + (state_icon_ ? 3 : 2),
                std::move(action_icon), true);
}

std::u16string_view RichHoverButton::GetSubtitleText() const {
  return subtitle_ ? subtitle_->GetText() : std::u16string_view();
}

void RichHoverButton::SetSubtitleText(std::u16string_view subtitle_text) {
  if (subtitle_text.empty()) {
    subtitle_ = nullptr;
    for (const auto& v : subtitle_row_views_) {
      RemoveChildViewT(v);
    }
    subtitle_row_views_.clear();
  } else {
    if (subtitle_row_views_.empty()) {
      subtitle_row_views_.push_back(AddChildView(
          std::make_unique<views::View>()));  // Skip main icon column.
      auto subtitle = std::make_unique<views::Label>();
      subtitle_ = subtitle.get();
      subtitle_row_views_.push_back(AddChildView(
          std::make_unique<SubtitleLabelWrapper>(std::move(subtitle))));
      subtitle_->SetTextStyle(views::style::STYLE_BODY_5);
      subtitle_->SetEnabledColor(ui::kColorLabelForegroundSecondary);
      subtitle_->SetMultiLine(subtitle_multiline_);
      subtitle_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
      subtitle_->SetAutoColorReadabilityEnabled(false);
      base::Extend(subtitle_row_views_, AddFillerViews(children().size()));
    }
    subtitle_->SetText(std::u16string(subtitle_text));
  }
  RecreateLayout();
  UpdateAccessibleName();
}

bool RichHoverButton::GetSubtitleMultiline() const {
  return subtitle_multiline_;
}

void RichHoverButton::SetSubtitleMultiline(bool subtitle_multiline) {
  subtitle_multiline_ = subtitle_multiline;
  if (subtitle_) {
    subtitle_->SetMultiLine(subtitle_multiline);
  }
}

void RichHoverButton::SetTitleTextStyleAndColor(int style,
                                                ui::ColorId color_id) {
  title_->SetTextStyle(style);
  title_->SetEnabledColor(color_id);
}

void RichHoverButton::SetSubtitleTextStyleAndColor(int style,
                                                   ui::ColorId color_id) {
  if (subtitle_) {
    subtitle_->SetTextStyle(style);
    subtitle_->SetEnabledColor(color_id);
  }
}

void RichHoverButton::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  return Button::OnBoundsChanged(previous_bounds);
}

views::View* RichHoverButton::GetTooltipHandlerForPoint(
    const gfx::Point& point) {
  return Button::GetTooltipHandlerForPoint(point);
}

gfx::Size RichHoverButton::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  return Button::CalculatePreferredSize(available_size);
}

void RichHoverButton::SetIconMember(raw_ptr<views::ImageView>& icon_member,
                                    size_t child_index,
                                    ui::ImageModel icon,
                                    bool use_placeholder) {
  if (icon.IsEmpty()) {
    if (icon_member) {
      icon_member = nullptr;
      RemoveChildViewT(children()[child_index]);
      if (use_placeholder) {
        AddChildViewAt(std::make_unique<views::View>(), child_index);
      } else {
        RecreateLayout();
      }
    }
  } else if (!icon_member) {
    if (use_placeholder) {
      RemoveChildViewT(children()[child_index]);
    }
    icon_member = AddChildViewAt(CreateIconView(std::move(icon)), child_index);
    if (!use_placeholder) {
      RecreateLayout();
    }
  } else {
    icon_member->SetImage(std::move(icon));
  }
}

void RichHoverButton::RecreateLayout() {
  const int icon_label_spacing = ChromeLayoutProvider::Get()->GetDistanceMetric(
      DISTANCE_RICH_HOVER_BUTTON_ICON_HORIZONTAL);
  views::TableLayout* table_layout =
      SetLayoutManager(std::make_unique<views::TableLayout>());
  table_layout
      // Column for main image.
      ->AddColumn(views::LayoutAlignment::kCenter,
                  views::LayoutAlignment::kCenter,
                  views::TableLayout::kFixedSize,
                  views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
      .AddPaddingColumn(views::TableLayout::kFixedSize, icon_label_spacing)
      // Column for title.
      .AddColumn(views::LayoutAlignment::kStretch,
                 views::LayoutAlignment::kCenter, 1.0f,
                 views::TableLayout::ColumnSize::kUsePreferred, 0, 0);
  if (state_icon_) {
    table_layout
        // Column for state icon.
        ->AddPaddingColumn(views::TableLayout::kFixedSize, icon_label_spacing)
        .AddColumn(views::LayoutAlignment::kCenter,
                   views::LayoutAlignment::kCenter,
                   views::TableLayout::kFixedSize,
                   views::TableLayout::ColumnSize::kFixed, 16, 0);
  }
  table_layout
      // Column for action icon.
      ->AddPaddingColumn(views::TableLayout::kFixedSize, icon_label_spacing)
      .AddColumn(views::LayoutAlignment::kCenter,
                 views::LayoutAlignment::kCenter,
                 views::TableLayout::kFixedSize,
                 views::TableLayout::ColumnSize::kFixed, 16, 0)
      .AddRows(1, views::TableLayout::kFixedSize,
               // Force row to have sufficient height for full line-height of
               // the title.
               views::TypographyProvider::Get().GetLineHeight(
                   views::style::CONTEXT_DIALOG_BODY_TEXT,
                   views::style::STYLE_PRIMARY));
  if (!custom_view_row_views_.empty()) {
    // Row for custom view.
    table_layout->AddRows(1, views::TableLayout::kFixedSize);
  }
  if (!subtitle_row_views_.empty()) {
    // Row for subtitle.
    table_layout->AddRows(1, views::TableLayout::kFixedSize);
  }
}

void RichHoverButton::UpdateAccessibleName() {
  const std::u16string_view title_text = GetTitleText();
  const std::u16string_view subtitle_text = GetSubtitleText();
  HoverButton::GetViewAccessibility().SetName(
      subtitle_text.empty() ? std::u16string(title_text)
                            : base::StrCat({title_text, u"\n", subtitle_text}));
}

std::vector<raw_ptr<views::View>> RichHoverButton::AddFillerViews(
    size_t start) {
  std::vector<raw_ptr<views::View>> vec;
  if (state_icon_) {
    vec.push_back(AddChildViewAt(std::make_unique<views::View>(), start++));
  }
  vec.push_back(AddChildViewAt(std::make_unique<views::View>(), start));
  return vec;
}

BEGIN_METADATA(RichHoverButton)
ADD_PROPERTY_METADATA(ui::ImageModel, Icon)
ADD_PROPERTY_METADATA(std::u16string_view, TitleText)
ADD_PROPERTY_METADATA(ui::ImageModel, StateIcon)
ADD_PROPERTY_METADATA(ui::ImageModel, ActionIcon)
ADD_PROPERTY_METADATA(std::u16string_view, SubtitleText)
ADD_PROPERTY_METADATA(bool, SubtitleMultiline)
END_METADATA