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" |
| 16 | |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 17 | // ToastSpecification details what the toast should contain when shown. |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 18 | class ToastSpecification { |
| 19 | public: |
| 20 | class Builder final { |
| 21 | public: |
| 22 | Builder(const gfx::VectorIcon& icon, int body_string_id); |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame] | 23 | explicit Builder(const gfx::VectorIcon& icon); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 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. |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 41 | // If the specification includes a menu, the `ToastParams` that are used |
| 42 | // to show the Toast must have a non-null `menu_model` member. |
| 43 | Builder& AddMenu(); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 44 | |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 45 | // Toasts by default are scoped to the active tab when they are triggered. |
| 46 | // Globally scoped toasts will not immediately dismiss when the user |
| 47 | // switches tabs or navigates to another site and will rely on timing out to |
| 48 | // dismiss. |
| 49 | Builder& AddGlobalScoped(); |
| 50 | |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 51 | // Explicitly marks this toast as an actionable toast (allows the user to |
| 52 | // interact with it in some way). |
| 53 | // NOTE: Only use this for toasts that do not have actionable buttons / |
| 54 | // menus but have interactions tied to the toast that are handled outside |
| 55 | // the toast framework such as keyboard shortcuts. |
| 56 | Builder& SetToastAsActionable(); |
dljames | 402dc5e | 2025-05-07 23:17:09 | [diff] [blame] | 57 | |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 58 | std::unique_ptr<ToastSpecification> Build(); |
| 59 | |
| 60 | private: |
| 61 | void ValidateSpecification(); |
| 62 | |
| 63 | std::unique_ptr<ToastSpecification> toast_specification_; |
| 64 | }; |
| 65 | |
| 66 | ToastSpecification(base::PassKey<ToastSpecification::Builder>, |
| 67 | const gfx::VectorIcon& icon, |
| 68 | int string_id); |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame] | 69 | ToastSpecification(base::PassKey<ToastSpecification::Builder>, |
| 70 | const gfx::VectorIcon& icon); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 71 | ~ToastSpecification(); |
| 72 | |
| 73 | int body_string_id() const { return body_string_id_; } |
| 74 | const gfx::VectorIcon& icon() const { return *icon_; } |
| 75 | bool has_close_button() const { return has_close_button_; } |
| 76 | std::optional<int> action_button_string_id() const { |
| 77 | return action_button_string_id_; |
| 78 | } |
Charles Meng | ab13b8f6 | 2024-09-04 20:11:18 | [diff] [blame] | 79 | base::RepeatingClosure action_button_callback() const { |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 80 | return action_button_closure_; |
| 81 | } |
dljames | 402dc5e | 2025-05-07 23:17:09 | [diff] [blame] | 82 | |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 83 | bool has_menu() const { return has_menu_; } |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 84 | bool is_global_scope() const { return is_global_scope_; } |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 85 | bool is_actionable() const { |
| 86 | return has_close_button() || has_menu() || has_actionable_override(); |
| 87 | } |
| 88 | bool has_actionable_override() const { return actionable_toast_override_; } |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 89 | |
| 90 | void AddCloseButton(); |
| 91 | void AddActionButton(int string_id, base::RepeatingClosure closure); |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 92 | void AddMenu(); |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 93 | void AddGlobalScope(); |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 94 | void SetToastAsActionable(); |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | const base::raw_ref<const gfx::VectorIcon> icon_; |
Shakti Sahu | 226b102b | 2025-03-13 23:28:55 | [diff] [blame] | 98 | int body_string_id_ = 0; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 99 | bool has_close_button_ = false; |
Jan Keitel | 29b357c | 2024-11-07 17:30:55 | [diff] [blame] | 100 | bool has_menu_ = false; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 101 | std::optional<int> action_button_string_id_; |
| 102 | base::RepeatingClosure action_button_closure_; |
Steven Luong | f444e6d | 2024-09-16 22:08:13 | [diff] [blame] | 103 | bool is_global_scope_ = false; |
dljames | 8481729 | 2025-05-21 22:46:18 | [diff] [blame] | 104 | bool actionable_toast_override_ = false; |
stluong | b287cbf3 | 2024-08-16 20:10:56 | [diff] [blame] | 105 | }; |
| 106 | |
stluong | 8fc2b9f | 2024-08-23 23:01:13 | [diff] [blame] | 107 | #endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_ |