blob: 7fba90f6bb3995c0a69a044339949cce77af3b9e [file] [log] [blame]
stluongb287cbf32024-08-16 20:10:561// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
stluong8fc2b9f2024-08-23 23:01:135#include "chrome/browser/ui/toasts/api/toast_specification.h"
stluongb287cbf32024-08-16 20:10:566
7#include <memory>
8
9#include "base/check.h"
10#include "base/functional/callback.h"
11#include "base/functional/callback_forward.h"
12#include "base/types/pass_key.h"
Joseph Parkf1147f742024-10-18 17:39:1713#include "ui/menus/simple_menu_model.h"
stluongb287cbf32024-08-16 20:10:5614
15ToastSpecification::Builder::Builder(const gfx::VectorIcon& icon,
16 int body_string_id)
17 : toast_specification_(
18 std::make_unique<ToastSpecification>(base::PassKey<Builder>(),
19 icon,
20 body_string_id)) {}
21
22ToastSpecification::Builder::~Builder() {
23 // Verify that ToastSpecification::Builder::Build() has been called
24 // so the toast specification is completely built.
25 CHECK(!toast_specification_);
26}
27
28ToastSpecification::Builder& ToastSpecification::Builder::AddCloseButton() {
29 toast_specification_->AddCloseButton();
30 return *this;
31}
32
33ToastSpecification::Builder& ToastSpecification::Builder::AddActionButton(
34 int action_button_string_id,
35 base::RepeatingClosure closure) {
36 toast_specification_->AddActionButton(action_button_string_id,
37 std::move(closure));
38 return *this;
39}
40
Jan Keitel29b357c2024-11-07 17:30:5541ToastSpecification::Builder& ToastSpecification::Builder::AddMenu() {
42 toast_specification_->AddMenu();
stluongb287cbf32024-08-16 20:10:5643 return *this;
44}
45
Steven Luongf444e6d2024-09-16 22:08:1346ToastSpecification::Builder& ToastSpecification::Builder::AddGlobalScoped() {
47 toast_specification_->AddGlobalScope();
48 return *this;
49}
50
stluongb287cbf32024-08-16 20:10:5651std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() {
52 ValidateSpecification();
53 return std::move(toast_specification_);
54}
55
56void ToastSpecification::Builder::ValidateSpecification() {
57 // Toasts with an action button must have a close button and not a menu.
58 if (toast_specification_->action_button_string_id().has_value()) {
59 CHECK(toast_specification_->has_close_button());
Jan Keitel29b357c2024-11-07 17:30:5560 CHECK(!toast_specification_->has_menu());
stluongb287cbf32024-08-16 20:10:5661 }
Alison Gale6a846e82024-10-01 20:06:1562
63 // Toasts with a menu can't have a close button. If this behavior is needed,
64 // discuss with UX how to design this in a way that supports both.
Jan Keitel29b357c2024-11-07 17:30:5565 if (toast_specification_->has_menu()) {
Alison Gale6a846e82024-10-01 20:06:1566 CHECK(!toast_specification_->has_close_button());
67 }
stluongb287cbf32024-08-16 20:10:5668}
69
70ToastSpecification::ToastSpecification(
71 base::PassKey<ToastSpecification::Builder>,
72 const gfx::VectorIcon& icon,
73 int string_id)
74 : icon_(icon), body_string_id_(string_id) {}
75
76ToastSpecification::~ToastSpecification() = default;
77
78void ToastSpecification::AddCloseButton() {
79 has_close_button_ = true;
80}
81
82void ToastSpecification::AddActionButton(int string_id,
83 base::RepeatingClosure closure) {
84 CHECK(!closure.is_null());
85 action_button_string_id_ = string_id;
86 action_button_closure_ = std::move(closure);
87}
88
Jan Keitel29b357c2024-11-07 17:30:5589void ToastSpecification::AddMenu() {
90 has_menu_ = true;
stluongb287cbf32024-08-16 20:10:5691}
92
Steven Luongf444e6d2024-09-16 22:08:1393void ToastSpecification::AddGlobalScope() {
94 is_global_scope_ = true;
95}