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 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 55 | std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() { |
| 56 | ValidateSpecification(); |
| 57 | return std::move(toast_specification_); |
| 58 | } |
| 59 | |
| 60 | void ToastSpecification::Builder::ValidateSpecification() { |
| 61 | // Toasts with an action button must have a close button and not a menu. |
| 62 | if (toast_specification_->action_button_string_id().has_value()) { |
| 63 | CHECK(toast_specification_->has_close_button()); |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 64 | CHECK(!toast_specification_->has_menu()); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 65 | } |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 66 | |
| 67 | // Toasts with a menu can't have a close button. If this behavior is needed, |
| 68 | // discuss with UX how to design this in a way that supports both. |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 69 | if (toast_specification_->has_menu()) { |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 70 | CHECK(!toast_specification_->has_close_button()); |
| 71 | } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | ToastSpecification::ToastSpecification( |
| 75 | base::PassKey<ToastSpecification::Builder>, |
| 76 | const gfx::VectorIcon& icon, |
| 77 | int string_id) |
| 78 | : icon_(icon), body_string_id_(string_id) {} |
| 79 | |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame^] | 80 | ToastSpecification::ToastSpecification( |
| 81 | base::PassKey<ToastSpecification::Builder>, |
| 82 | const gfx::VectorIcon& icon) |
| 83 | : icon_(icon) {} |
| 84 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 85 | ToastSpecification::~ToastSpecification() = default; |
| 86 | |
| 87 | void ToastSpecification::AddCloseButton() { |
| 88 | has_close_button_ = true; |
| 89 | } |
| 90 | |
| 91 | void ToastSpecification::AddActionButton(int string_id, |
| 92 | base::RepeatingClosure closure) { |
| 93 | CHECK(!closure.is_null()); |
| 94 | action_button_string_id_ = string_id; |
| 95 | action_button_closure_ = std::move(closure); |
| 96 | } |
| 97 | |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 98 | void ToastSpecification::AddMenu() { |
| 99 | has_menu_ = true; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 100 | } |
| 101 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 102 | void ToastSpecification::AddGlobalScope() { |
| 103 | is_global_scope_ = true; |
| 104 | } |