blob: ee68856046a509778dffa6efecf3933d0e8c16ea [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"
13#include "ui/base/models/simple_menu_model.h"
14
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
41ToastSpecification::Builder& ToastSpecification::Builder::AddMenu(
42 std::unique_ptr<ui::SimpleMenuModel> menu_model) {
43 toast_specification_->AddMenu(std::move(menu_model));
44 return *this;
45}
46
Steven Luongf444e6d2024-09-16 22:08:1347ToastSpecification::Builder& ToastSpecification::Builder::AddGlobalScoped() {
48 toast_specification_->AddGlobalScope();
49 return *this;
50}
51
stluongb287cbf32024-08-16 20:10:5652ToastSpecification::Builder& ToastSpecification::Builder::AddPersistance() {
53 toast_specification_->AddPersistance();
54 return *this;
55}
56
57std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() {
Steven Luongf444e6d2024-09-16 22:08:1358 // Persistent toast is global scoped by default since it should only be
59 // dismissed when explicitly told to do so.
60 if (toast_specification_->is_persistent_toast()) {
61 AddGlobalScoped();
62 }
63
stluongb287cbf32024-08-16 20:10:5664 ValidateSpecification();
65 return std::move(toast_specification_);
66}
67
68void ToastSpecification::Builder::ValidateSpecification() {
69 // Toasts with an action button must have a close button and not a menu.
70 if (toast_specification_->action_button_string_id().has_value()) {
71 CHECK(toast_specification_->has_close_button());
72 CHECK(!toast_specification_->menu_model());
73 }
Alison Gale6a846e82024-10-01 20:06:1574
75 // Toasts with a menu can't have a close button. If this behavior is needed,
76 // discuss with UX how to design this in a way that supports both.
77 if (toast_specification_->menu_model()) {
78 CHECK(!toast_specification_->has_close_button());
79 }
stluongb287cbf32024-08-16 20:10:5680}
81
82ToastSpecification::ToastSpecification(
83 base::PassKey<ToastSpecification::Builder>,
84 const gfx::VectorIcon& icon,
85 int string_id)
86 : icon_(icon), body_string_id_(string_id) {}
87
88ToastSpecification::~ToastSpecification() = default;
89
90void ToastSpecification::AddCloseButton() {
91 has_close_button_ = true;
92}
93
94void ToastSpecification::AddActionButton(int string_id,
95 base::RepeatingClosure closure) {
96 CHECK(!closure.is_null());
97 action_button_string_id_ = string_id;
98 action_button_closure_ = std::move(closure);
99}
100
101void ToastSpecification::AddMenu(
102 std::unique_ptr<ui::SimpleMenuModel> menu_model) {
103 menu_model_ = std::move(menu_model);
104}
105
Steven Luongf444e6d2024-09-16 22:08:13106void ToastSpecification::AddGlobalScope() {
107 is_global_scope_ = true;
108}
109
stluongb287cbf32024-08-16 20:10:56110void ToastSpecification::AddPersistance() {
111 is_persistent_toast_ = true;
112}