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" |
Joseph Park | f1147f74 | 2024-10-18 17:39:17 | [diff] [blame] | 13 | #include "ui/menus/simple_menu_model.h" |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 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 | |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame] | 22 | ToastSpecification::Builder::Builder(const gfx::VectorIcon& icon) |
| 23 | : toast_specification_( |
| 24 | std::make_unique<ToastSpecification>(base::PassKey<Builder>(), |
| 25 | icon)) {} |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 26 | ToastSpecification::Builder::~Builder() { |
| 27 | // Verify that ToastSpecification::Builder::Build() has been called |
| 28 | // so the toast specification is completely built. |
| 29 | CHECK(!toast_specification_); |
| 30 | } |
| 31 | |
| 32 | ToastSpecification::Builder& ToastSpecification::Builder::AddCloseButton() { |
| 33 | toast_specification_->AddCloseButton(); |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | ToastSpecification::Builder& ToastSpecification::Builder::AddActionButton( |
| 38 | int action_button_string_id, |
| 39 | base::RepeatingClosure closure) { |
| 40 | toast_specification_->AddActionButton(action_button_string_id, |
| 41 | std::move(closure)); |
| 42 | return *this; |
| 43 | } |
| 44 | |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 45 | ToastSpecification::Builder& ToastSpecification::Builder::AddMenu() { |
| 46 | toast_specification_->AddMenu(); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 47 | return *this; |
| 48 | } |
| 49 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 50 | ToastSpecification::Builder& ToastSpecification::Builder::AddGlobalScoped() { |
| 51 | toast_specification_->AddGlobalScope(); |
| 52 | return *this; |
| 53 | } |
| 54 | |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 55 | ToastSpecification::Builder& |
| 56 | ToastSpecification::Builder::SetToastAsActionable() { |
| 57 | toast_specification_->SetToastAsActionable(); |
dljames | 402dc5e | 2025-05-07 23:17:09 | [diff] [blame] | 58 | return *this; |
| 59 | } |
| 60 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 61 | std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() { |
| 62 | ValidateSpecification(); |
| 63 | return std::move(toast_specification_); |
| 64 | } |
| 65 | |
| 66 | void ToastSpecification::Builder::ValidateSpecification() { |
| 67 | // Toasts with an action button must have a close button and not a menu. |
| 68 | if (toast_specification_->action_button_string_id().has_value()) { |
| 69 | CHECK(toast_specification_->has_close_button()); |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 70 | CHECK(!toast_specification_->has_menu()); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 71 | } |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 72 | |
| 73 | // Toasts with a menu can't have a close button. If this behavior is needed, |
| 74 | // discuss with UX how to design this in a way that supports both. |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 75 | if (toast_specification_->has_menu()) { |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 76 | CHECK(!toast_specification_->has_close_button()); |
| 77 | } |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 78 | |
| 79 | // Toasts can be manually set as actionable, or have close / menu buttons. Not |
| 80 | // both. |
| 81 | if (toast_specification_->has_actionable_override()) { |
| 82 | CHECK(!toast_specification_->has_close_button() && |
| 83 | !toast_specification_->has_menu()) |
| 84 | << "Avoid use of SetToastAsActionable() on an already actionable toast"; |
| 85 | } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | ToastSpecification::ToastSpecification( |
| 89 | base::PassKey<ToastSpecification::Builder>, |
| 90 | const gfx::VectorIcon& icon, |
| 91 | int string_id) |
| 92 | : icon_(icon), body_string_id_(string_id) {} |
| 93 | |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame] | 94 | ToastSpecification::ToastSpecification( |
| 95 | base::PassKey<ToastSpecification::Builder>, |
| 96 | const gfx::VectorIcon& icon) |
| 97 | : icon_(icon) {} |
| 98 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 99 | ToastSpecification::~ToastSpecification() = default; |
| 100 | |
| 101 | void ToastSpecification::AddCloseButton() { |
| 102 | has_close_button_ = true; |
| 103 | } |
| 104 | |
| 105 | void ToastSpecification::AddActionButton(int string_id, |
| 106 | base::RepeatingClosure closure) { |
| 107 | CHECK(!closure.is_null()); |
| 108 | action_button_string_id_ = string_id; |
| 109 | action_button_closure_ = std::move(closure); |
| 110 | } |
| 111 | |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 112 | void ToastSpecification::AddMenu() { |
| 113 | has_menu_ = true; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 114 | } |
| 115 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 116 | void ToastSpecification::AddGlobalScope() { |
| 117 | is_global_scope_ = true; |
| 118 | } |
dljames | 402dc5e | 2025-05-07 23:17:09 | [diff] [blame] | 119 | |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 120 | void ToastSpecification::SetToastAsActionable() { |
| 121 | actionable_toast_override_ = true; |
dljames | 402dc5e | 2025-05-07 23:17:09 | [diff] [blame] | 122 | } |