stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 1 | // 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 | |
stluong | 8fc2b9f | 2024-08-23 23:01:13 | [diff] [blame] | 5 | #include "chrome/browser/ui/toasts/api/toast_specification.h" |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 6 | |
| 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 | |
| 15 | ToastSpecification::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 | |
| 22 | ToastSpecification::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 | |
| 28 | ToastSpecification::Builder& ToastSpecification::Builder::AddCloseButton() { |
| 29 | toast_specification_->AddCloseButton(); |
| 30 | return *this; |
| 31 | } |
| 32 | |
| 33 | ToastSpecification::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 | |
| 41 | ToastSpecification::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 Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 47 | ToastSpecification::Builder& ToastSpecification::Builder::AddGlobalScoped() { |
| 48 | toast_specification_->AddGlobalScope(); |
| 49 | return *this; |
| 50 | } |
| 51 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 52 | ToastSpecification::Builder& ToastSpecification::Builder::AddPersistance() { |
| 53 | toast_specification_->AddPersistance(); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() { |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 58 | // 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 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 64 | ValidateSpecification(); |
| 65 | return std::move(toast_specification_); |
| 66 | } |
| 67 | |
| 68 | void 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 Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame^] | 74 | |
| 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 | } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ToastSpecification::ToastSpecification( |
| 83 | base::PassKey<ToastSpecification::Builder>, |
| 84 | const gfx::VectorIcon& icon, |
| 85 | int string_id) |
| 86 | : icon_(icon), body_string_id_(string_id) {} |
| 87 | |
| 88 | ToastSpecification::~ToastSpecification() = default; |
| 89 | |
| 90 | void ToastSpecification::AddCloseButton() { |
| 91 | has_close_button_ = true; |
| 92 | } |
| 93 | |
| 94 | void 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 | |
| 101 | void ToastSpecification::AddMenu( |
| 102 | std::unique_ptr<ui::SimpleMenuModel> menu_model) { |
| 103 | menu_model_ = std::move(menu_model); |
| 104 | } |
| 105 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 106 | void ToastSpecification::AddGlobalScope() { |
| 107 | is_global_scope_ = true; |
| 108 | } |
| 109 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 110 | void ToastSpecification::AddPersistance() { |
| 111 | is_persistent_toast_ = true; |
| 112 | } |