File: app_list_toast_view.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (415 lines) | stat: -rw-r--r-- 13,375 bytes parent folder | download
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/app_list/views/app_list_toast_view.h"

#include <memory>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/app_list_view_delegate.h"
#include "ash/bubble/bubble_utils.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/icon_button.h"
#include "ash/style/pill_button.h"
#include "ash/style/typography.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/vector_icon_utils.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace ash {
namespace {

constexpr int kCornerRadius = 16;
constexpr auto kInteriorMargin = gfx::Insets::TLBR(8, 8, 8, 16);
constexpr auto kTitleContainerMargin = gfx::Insets::TLBR(0, 16, 0, 24);
constexpr auto kCloseButtonMargin = gfx::Insets::TLBR(0, 8, 0, 0);

constexpr int kToastHeight = 32;
constexpr int kToastMaximumWidth = 640;
constexpr int kToastMinimumWidth = 288;

constexpr int kIconCornerRadius = 8;

class IconImageWithBackground : public views::ImageView {
 public:
  IconImageWithBackground() = default;
  IconImageWithBackground(const IconImageWithBackground&) = delete;
  IconImageWithBackground& operator=(const IconImageWithBackground&) = delete;
  ~IconImageWithBackground() override = default;

 private:
  // views::ImageView:
  void OnPaint(gfx::Canvas* canvas) override {
    if (GetImage().isNull())
      return;

    cc::PaintFlags flags;
    flags.setStyle(cc::PaintFlags::kFill_Style);
    flags.setColor(AshColorProvider::Get()->GetControlsLayerColor(
        AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
    canvas->DrawRoundRect(GetContentsBounds(), kIconCornerRadius, flags);
    SkPath mask;
    mask.addRoundRect(gfx::RectToSkRect(GetContentsBounds()), kIconCornerRadius,
                      kIconCornerRadius);
    canvas->ClipPath(mask, true);
    views::ImageView::OnPaint(canvas);
  }
};

}  // namespace

AppListToastView::Builder::Builder(const std::u16string title)
    : title_(title) {}

AppListToastView::Builder::~Builder() = default;

std::unique_ptr<AppListToastView> AppListToastView::Builder::Build() {
  std::unique_ptr<AppListToastView> toast =
      std::make_unique<AppListToastView>(title_, style_for_tablet_mode_);

  if (view_delegate_)
    toast->SetViewDelegate(view_delegate_);

  if (icon_) {
    toast->SetIcon(*icon_);
  }

  if (icon_size_)
    toast->SetIconSize(*icon_size_);

  if (has_icon_background_)
    toast->AddIconBackground();

  if (button_callback_)
    toast->SetButton(*button_text_, button_callback_);

  if (close_button_callback_)
    toast->SetCloseButton(close_button_callback_);

  if (subtitle_)
    toast->SetSubtitle(*subtitle_);

  if (subtitle_ && is_subtitle_multiline_) {
    toast->SetSubtitleMultiline(is_subtitle_multiline_);
  }

  return toast;
}

AppListToastView::Builder& AppListToastView::Builder::SetIcon(
    const ui::ImageModel& icon) {
  icon_ = icon;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetSubtitle(
    const std::u16string subtitle) {
  subtitle_ = subtitle;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetSubtitleMultiline(
    bool multiline) {
  is_subtitle_multiline_ = multiline;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetIconSize(
    int icon_size) {
  icon_size_ = icon_size;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetButton(
    const std::u16string button_text,
    views::Button::PressedCallback button_callback) {
  DCHECK(button_callback);

  button_text_ = button_text;
  button_callback_ = button_callback;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetCloseButton(
    views::Button::PressedCallback close_button_callback) {
  DCHECK(close_button_callback);

  close_button_callback_ = close_button_callback;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetStyleForTabletMode(
    bool style_for_tablet_mode) {
  style_for_tablet_mode_ = style_for_tablet_mode;
  return *this;
}

AppListToastView::Builder& AppListToastView::Builder::SetViewDelegate(
    AppListViewDelegate* delegate) {
  view_delegate_ = delegate;
  return *this;
}

void AppListToastView::SetViewDelegate(AppListViewDelegate* delegate) {
  view_delegate_ = delegate;
}

AppListToastView::Builder& AppListToastView::Builder::SetIconBackground(
    bool has_icon_background) {
  has_icon_background_ = has_icon_background;
  return *this;
}

bool AppListToastView::IsToastButton(views::View* view) {
  return views::IsViewClass<ToastPillButton>(view);
}

AppListToastView::AppListToastView(const std::u16string title,
                                   bool style_for_tablet_mode) {
  layout_manager_ = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal, kInteriorMargin));
  layout_manager_->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kCenter);

  label_container_ = AddChildView(std::make_unique<views::View>());
  label_container_->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));
  label_container_->SetProperty(views::kMarginsKey, kTitleContainerMargin);

  title_label_ =
      label_container_->AddChildView(std::make_unique<views::Label>(title));

  const bool is_jelly_enabled = chromeos::features::IsJellyEnabled();
  const ui::ColorId title_color_id =
      is_jelly_enabled
          ? static_cast<ui::ColorId>(cros_tokens::kCrosSysOnSurface)
          : kColorAshTextColorPrimary;
  bubble_utils::ApplyStyle(title_label_,
                           style_for_tablet_mode ? TypographyToken::kCrosBody1
                                                 : TypographyToken::kCrosBody2,
                           title_color_id);

  title_label_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  title_label_->SetMultiLine(true);
  SetTitleLabelMaximumWidth();

  layout_manager_->SetFlexForView(label_container_, 1);

  if (style_for_tablet_mode) {
    SetPaintToLayer();
    layer()->SetFillsBoundsOpaquely(false);
    layer()->SetBackgroundBlur(ColorProvider::kBackgroundBlurSigma);
    layer()->SetBackdropFilterQuality(ColorProvider::kBackgroundBlurQuality);
    layer()->SetRoundedCornerRadius(gfx::RoundedCornersF(kCornerRadius));

    const ui::ColorId background_color_id =
        is_jelly_enabled
            ? static_cast<ui::ColorId>(cros_tokens::kCrosSysSystemBaseElevated)
            : kColorAshShieldAndBase80;
    SetBackground(views::CreateThemedRoundedRectBackground(background_color_id,
                                                           kCornerRadius));
    SetBorder(std::make_unique<views::HighlightBorder>(
        kCornerRadius,
        is_jelly_enabled
            ? views::HighlightBorder::Type::kHighlightBorderNoShadow
            : views::HighlightBorder::Type::kHighlightBorder1));
  } else {
    const ui::ColorId background_color_id =
        is_jelly_enabled
            ? static_cast<ui::ColorId>(cros_tokens::kCrosSysSystemOnBase)
            : kColorAshControlBackgroundColorInactive;
    SetBackground(views::CreateThemedRoundedRectBackground(background_color_id,
                                                           kCornerRadius));
  }
}

AppListToastView::~AppListToastView() = default;

void AppListToastView::SetButton(
    std::u16string button_text,
    views::Button::PressedCallback button_callback) {
  DCHECK(button_callback);

  toast_button_ =
      AddChildView(std::make_unique<AppListToastView::ToastPillButton>(
          view_delegate_, button_callback, button_text,
          PillButton::Type::kDefaultWithoutIcon,
          /*icon=*/nullptr));
  toast_button_->SetBorder(views::NullBorder());
}

void AppListToastView::SetCloseButton(
    views::Button::PressedCallback close_button_callback) {
  DCHECK(close_button_callback);

  close_button_ = AddChildView(std::make_unique<IconButton>(
      close_button_callback, IconButton::Type::kMediumFloating,
      &vector_icons::kCloseIcon,
      IDS_ASH_LAUNCHER_CLOSE_SORT_TOAST_BUTTON_SPOKEN_TEXT));
  close_button_->SetProperty(views::kMarginsKey, kCloseButtonMargin);
}

void AppListToastView::SetTitle(const std::u16string title) {
  title_label_->SetText(title);
}

void AppListToastView::SetSubtitle(const std::u16string subtitle) {
  if (subtitle_label_) {
    subtitle_label_->SetText(subtitle);
    return;
  }

  subtitle_label_ =
      label_container_->AddChildView(std::make_unique<views::Label>(subtitle));
  const ui::ColorId label_color_id =
      chromeos::features::IsJellyEnabled()
          ? static_cast<ui::ColorId>(cros_tokens::kCrosSysOnSurfaceVariant)
          : kColorAshTextColorSecondary;
  bubble_utils::ApplyStyle(subtitle_label_, TypographyToken::kCrosAnnotation1,
                           label_color_id);
  subtitle_label_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
}

void AppListToastView::SetSubtitleMultiline(bool multiline) {
  if (!subtitle_label_) {
    return;
  }

  subtitle_label_->SetMultiLine(multiline);
}

void AppListToastView::SetIcon(const ui::ImageModel& icon) {
  CreateIconView();

  default_icon_ = icon;
  UpdateIconImage();
}

void AppListToastView::SetIconSize(int icon_size) {
  icon_size_ = icon_size;
  if (icon_)
    UpdateIconImage();
}

void AppListToastView::AddIconBackground() {
  if (icon_) {
    RemoveChildViewT(icon_.get());
    icon_ = nullptr;
  }

  has_icon_background_ = true;
  CreateIconView();
  UpdateIconImage();
}

gfx::Size AppListToastView::GetMaximumSize() const {
  return gfx::Size(kToastMaximumWidth,
                   GetLayoutManager()->GetPreferredSize(this).height());
}

gfx::Size AppListToastView::GetMinimumSize() const {
  return gfx::Size(kToastMinimumWidth, kToastHeight);
}

gfx::Size AppListToastView::CalculatePreferredSize() const {
  gfx::Size preferred_size = GetLayoutManager()->GetPreferredSize(this);
  preferred_size.SetToMax(GetMinimumSize());
  preferred_size.SetToMin(GetMaximumSize());
  return preferred_size;
}

void AppListToastView::UpdateInteriorMargins(const gfx::Insets& margin) {
  layout_manager_->set_inside_border_insets(margin);
}

AppListToastView::ToastPillButton::ToastPillButton(
    AppListViewDelegate* view_delegate,
    PressedCallback callback,
    const std::u16string& text,
    Type type,
    const gfx::VectorIcon* icon)
    : PillButton(callback, text, type, icon), view_delegate_(view_delegate) {
  views::FocusRing::Get(this)->SetHasFocusPredicate(
      base::BindRepeating([](const View* view) {
        const auto* v = views::AsViewClass<ToastPillButton>(view);
        CHECK(v);
        // With a `view_delegate_` present, focus ring should only show when
        // button is focused and keyboard traversal is engaged.
        return (!v->view_delegate_ ||
                v->view_delegate_->KeyboardTraversalEngaged()) &&
               v->HasFocus();
      }));
}

void AppListToastView::ToastPillButton::OnFocus() {
  PillButton::OnFocus();
  views::FocusRing::Get(this)->SchedulePaint();
}

void AppListToastView::ToastPillButton::OnBlur() {
  PillButton::OnBlur();
  views::FocusRing::Get(this)->SchedulePaint();
}

BEGIN_METADATA(AppListToastView, ToastPillButton, PillButton)
END_METADATA

void AppListToastView::UpdateIconImage() {
  if (!icon_)
    return;

  if (!default_icon_) {
    return;
  }

  icon_->SetImage(*default_icon_);
}

void AppListToastView::CreateIconView() {
  if (icon_)
    return;

  icon_ = AddChildViewAt(has_icon_background_
                             ? std::make_unique<IconImageWithBackground>()
                             : std::make_unique<views::ImageView>(),
                         0);
  icon_->SetVerticalAlignment(views::ImageView::Alignment::kCenter);
  icon_->SetHorizontalAlignment(views::ImageView::Alignment::kCenter);
}

int AppListToastView::GetExpandedTitleLabelWidth() {
  // TODO(b/274260097): Investigate to use size() or GetPreferredSize().
  const int icon_width = icon_ ? icon_->size().width() : 0;
  const int button_width =
      toast_button_ ? toast_button_->GetPreferredSize().width() : 0;
  return GetPreferredSize().width() - kInteriorMargin.width() - icon_width -
         button_width - kTitleContainerMargin.width();
}

void AppListToastView::SetTitleLabelMaximumWidth() {
  // TODO(crbug/682266): This is a temporary fix for the issue where the multi
  // line label appears cut-off.
  title_label_->SetMaximumWidth(GetExpandedTitleLabelWidth());
}

}  // namespace ash