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 | |
| 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 | |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame^] | 41 | ToastSpecification::Builder& ToastSpecification::Builder::AddMenu() { |
| 42 | toast_specification_->AddMenu(); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 43 | return *this; |
| 44 | } |
| 45 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 46 | ToastSpecification::Builder& ToastSpecification::Builder::AddGlobalScoped() { |
| 47 | toast_specification_->AddGlobalScope(); |
| 48 | return *this; |
| 49 | } |
| 50 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 51 | std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() { |
| 52 | ValidateSpecification(); |
| 53 | return std::move(toast_specification_); |
| 54 | } |
| 55 | |
| 56 | void 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 Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame^] | 60 | CHECK(!toast_specification_->has_menu()); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 61 | } |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 62 | |
| 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 Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame^] | 65 | if (toast_specification_->has_menu()) { |
Alison Gale | 6a846e8 | 2024-10-01 20:06:15 | [diff] [blame] | 66 | CHECK(!toast_specification_->has_close_button()); |
| 67 | } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | ToastSpecification::ToastSpecification( |
| 71 | base::PassKey<ToastSpecification::Builder>, |
| 72 | const gfx::VectorIcon& icon, |
| 73 | int string_id) |
| 74 | : icon_(icon), body_string_id_(string_id) {} |
| 75 | |
| 76 | ToastSpecification::~ToastSpecification() = default; |
| 77 | |
| 78 | void ToastSpecification::AddCloseButton() { |
| 79 | has_close_button_ = true; |
| 80 | } |
| 81 | |
| 82 | void 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 Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame^] | 89 | void ToastSpecification::AddMenu() { |
| 90 | has_menu_ = true; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 91 | } |
| 92 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 93 | void ToastSpecification::AddGlobalScope() { |
| 94 | is_global_scope_ = true; |
| 95 | } |