File: README.md

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Toast UI
The toast system allows features to surface a message and an optional action
for users to take. These are short notifications that serve as confirmation of a
successful user action or provide contextual information about a Chrome
initiated action. Each toast must have an icon and a body text; feature teams
can optionally add an action or close button to toasts. Feature teams that want
to introduce a toast will need to introduce a new value to the toast id enum and
create a toast specification that will be registered with the toast registry.

## Getting Started
### 1. Add a new entry to the [ToastId enum](api/toast_id.h)
Each toast should have a unique `ToastId` which is used to identify the
toast and retrieve its corresponding specification.

### 2. Register your ToastSpecification in [ToastService::RegisterToasts()](toast_service.cc)
The toast specification allows features to declare what components they want
the toast to have. All toasts must have an icon and body text when shown, but
have the option to add other components such as an action button, close button,
three dot menu, or set the toast to be persistent.

Note: When registering your ToastSpecification, you can use strings with a
placeholder. However, you must remember to pass in any values you want to use
to replace the placeholders with in `ToastParams` when you trigger your toast.

#### Registering a Default Toast
```
void ToastService::RegisterToast(BrowserWindowInterface* interface) {
  ...
  toast_registry_->RegisterToast(
    ToastId,
    ToastSpecification::Builder(vector_icon, string_id)
        .Build());
}
```

#### Registering a Toast with a Close Button
```
void ToastService::RegisterToast(BrowserWindowInterface* interface) {
  ...
  toast_registry_->RegisterToast(
    ToastId,
    ToastSpecification::Builder(vector_icon, string_id)
        .AddCloseButton()
        .Build());
}
```

#### Registering a Toast with an Action Button
```
void ToastService::RegisterToast(BrowserWindowInterface* interface) {
  ...
  toast_registry_->RegisterToast(
    ToastId,
    ToastSpecification::Builder(vector_icon, string_id)
        .AddActionButton(button_string_id, button_closure)
        .AddCloseButton()
        .Build());
}
```

Note: there are a couple restrictions when declaring a toast specification with
an action button:
1. A toast with an action button must also have a "X" close button.
2. A toast with an action button cannot have a three dot menu.

#### Registering a Toast with a Menu
```
void ToastService::RegisterToast(BrowserWindowInterface* interface) {
  ...
  toast_registry_->RegisterToast(
    ToastId,
    ToastSpecification::Builder(vector_icon, string_id)
        .AddMenu()
        .Build());
}
```
Even though you have registered the toast with the ToastService, when you
trigger the toast, you must remember to pass in a non-null menu model to
populate the menu in `ToastParams` when you trigger your toast.

Also note that toasts with a menu cannot have a "X" close button. If this
behavior is needed, please consult with UX on how to design this in a way
that supports both.

### 3. Trigger your Toast
When you want to trigger your toast to show, you will need to retrieve the
[ToastController](toast_controller.h) through
[BrowserWindowFeatures](../browser_window/public/browser_window_features.h) and
call `ToastController::MaybeShowToast(ToastParams params)`.

Note: the ToastController can be null for certain browser types so you must check
if the controller exists before attempting to trigger the toast.

#### Triggering a Toast Without Needing to Replace Placeholder Strings
```
ToastController* const toast_controller = browser_window_features->toast_controller();
if (toast_controller) {
  toast_controller->MaybeShowToast(ToastParams(ToastId));
}
```

#### Triggering a Toast and Replacing Placeholder Strings
```
ToastController* const toast_controller = browser_window_features->toast_controller();
if (toast_controller) {
  ToastParams params = ToastParams(ToastId);
  params.body_string_replacement_params_ = {string_1, string_2};
  params.action_button_string_replacement_params_ = {string_3};
  toast_controller->MaybeShowToast(ToastParams(std::move(params)));
}
```

#### Triggering a Toast with a Menu
```
ToastController* const toast_controller = browser_window_features->toast_controller();
if (toast_controller) {
  std::unique_ptr<MyCustomMenuModel> menu_model = ...
  ToastParams params = ToastParams(ToastId);
  params.menu_model = std::move(menu_model);
  toast_controller->MaybeShowToast(ToastParams(std::move(params)));
}
```

Note: even though you have triggered your toast, there is a chance that users
might not see the toast because another toast immediately triggered after your
toast, thus preempting your toast from being seen by users.