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 | #ifndef CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_ |
| 6 | #define CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_ |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <optional> |
| 10 | |
| 11 | #include "base/functional/callback.h" |
| 12 | #include "base/functional/callback_forward.h" |
| 13 | #include "base/memory/raw_ref.h" |
| 14 | #include "base/types/pass_key.h" |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 15 | #include "ui/gfx/vector_icon_types.h" |
Joseph Park | f1147f74 | 2024-10-18 17:39:17 | [diff] [blame] | 16 | #include "ui/menus/simple_menu_model.h" |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 17 | |
| 18 | // ToastSpecification details what the toast should contain when shown. |
| 19 | class ToastSpecification { |
| 20 | public: |
| 21 | class Builder final { |
| 22 | public: |
| 23 | Builder(const gfx::VectorIcon& icon, int body_string_id); |
| 24 | Builder(const Builder& other) = delete; |
| 25 | Builder& operator=(const Builder&) = delete; |
| 26 | |
| 27 | ~Builder(); |
| 28 | |
| 29 | // Adds a "X" close button to the toast. However, if the toast have a menu, |
| 30 | // the close button will be a rounded dismiss button instead. |
| 31 | Builder& AddCloseButton(); |
| 32 | |
| 33 | // Adds a rounded action button toast with the `action_button_string_id` |
| 34 | // for the label. All toasts with an action button must also have a close |
| 35 | // button. |
| 36 | Builder& AddActionButton(int action_button_string_id, |
| 37 | base::RepeatingClosure closure); |
| 38 | |
| 39 | // Adds a three dot menu to the toast. Toasts with an action button are not |
| 40 | // allowed to have menu because they must have an "X" close button instead. |
| 41 | Builder& AddMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model); |
| 42 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 43 | // Toasts by default are scoped to the active tab when they are triggered. |
| 44 | // Globally scoped toasts will not immediately dismiss when the user |
| 45 | // switches tabs or navigates to another site and will rely on timing out to |
| 46 | // dismiss. |
| 47 | Builder& AddGlobalScoped(); |
| 48 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 49 | std::unique_ptr<ToastSpecification> Build(); |
| 50 | |
| 51 | private: |
| 52 | void ValidateSpecification(); |
| 53 | |
| 54 | std::unique_ptr<ToastSpecification> toast_specification_; |
| 55 | }; |
| 56 | |
| 57 | ToastSpecification(base::PassKey<ToastSpecification::Builder>, |
| 58 | const gfx::VectorIcon& icon, |
| 59 | int string_id); |
| 60 | ~ToastSpecification(); |
| 61 | |
| 62 | int body_string_id() const { return body_string_id_; } |
| 63 | const gfx::VectorIcon& icon() const { return *icon_; } |
| 64 | bool has_close_button() const { return has_close_button_; } |
| 65 | std::optional<int> action_button_string_id() const { |
| 66 | return action_button_string_id_; |
| 67 | } |
Charles Meng | ab13b8f6 | 2024-09-04 20:11:18 | [diff] [blame] | 68 | base::RepeatingClosure action_button_callback() const { |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 69 | return action_button_closure_; |
| 70 | } |
| 71 | ui::SimpleMenuModel* menu_model() const { return menu_model_.get(); } |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 72 | bool is_global_scope() const { return is_global_scope_; } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 73 | |
| 74 | void AddCloseButton(); |
| 75 | void AddActionButton(int string_id, base::RepeatingClosure closure); |
| 76 | void AddMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model); |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 77 | void AddGlobalScope(); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | const base::raw_ref<const gfx::VectorIcon> icon_; |
| 81 | int body_string_id_; |
| 82 | bool has_close_button_ = false; |
| 83 | std::optional<int> action_button_string_id_; |
| 84 | base::RepeatingClosure action_button_closure_; |
| 85 | std::unique_ptr<ui::SimpleMenuModel> menu_model_; |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 86 | bool is_global_scope_ = false; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 87 | }; |
| 88 | |
stluong | 8fc2b9f | 2024-08-23 23:01:13 | [diff] [blame] | 89 | #endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_ |