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 | |
| 5 | #include "chrome/browser/ui/toasts/toast_specification.h" |
| 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 | |
| 47 | ToastSpecification::Builder& ToastSpecification::Builder::AddPersistance() { |
| 48 | toast_specification_->AddPersistance(); |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | std::unique_ptr<ToastSpecification> ToastSpecification::Builder::Build() { |
| 53 | ValidateSpecification(); |
| 54 | return std::move(toast_specification_); |
| 55 | } |
| 56 | |
| 57 | void ToastSpecification::Builder::ValidateSpecification() { |
| 58 | // Toasts with an action button must have a close button and not a menu. |
| 59 | if (toast_specification_->action_button_string_id().has_value()) { |
| 60 | CHECK(toast_specification_->has_close_button()); |
| 61 | CHECK(!toast_specification_->menu_model()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ToastSpecification::ToastSpecification( |
| 66 | base::PassKey<ToastSpecification::Builder>, |
| 67 | const gfx::VectorIcon& icon, |
| 68 | int string_id) |
| 69 | : icon_(icon), body_string_id_(string_id) {} |
| 70 | |
| 71 | ToastSpecification::~ToastSpecification() = default; |
| 72 | |
| 73 | void ToastSpecification::AddCloseButton() { |
| 74 | has_close_button_ = true; |
| 75 | } |
| 76 | |
| 77 | void ToastSpecification::AddActionButton(int string_id, |
| 78 | base::RepeatingClosure closure) { |
| 79 | CHECK(!closure.is_null()); |
| 80 | action_button_string_id_ = string_id; |
| 81 | action_button_closure_ = std::move(closure); |
| 82 | } |
| 83 | |
| 84 | void ToastSpecification::AddMenu( |
| 85 | std::unique_ptr<ui::SimpleMenuModel> menu_model) { |
| 86 | menu_model_ = std::move(menu_model); |
| 87 | } |
| 88 | |
| 89 | void ToastSpecification::AddPersistance() { |
| 90 | is_persistent_toast_ = true; |
| 91 | } |