blob: 3e2a34e6ac431fd953ee96e964208d0199b8d10f [file] [log] [blame]
stluongb287cbf32024-08-16 20:10:561// 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
stluong8fc2b9f2024-08-23 23:01:135#ifndef CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_
6#define CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_
stluongb287cbf32024-08-16 20:10:567
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"
dljames402dc5e2025-05-07 23:17:0915#include "ui/base/accelerators/accelerator.h"
stluongb287cbf32024-08-16 20:10:5616#include "ui/gfx/vector_icon_types.h"
17
dljames402dc5e2025-05-07 23:17:0918// ToastSpecification details the supported actions and elements it contains.
stluongb287cbf32024-08-16 20:10:5619class ToastSpecification {
20 public:
21 class Builder final {
22 public:
23 Builder(const gfx::VectorIcon& icon, int body_string_id);
Shakti Sahu226b102b2025-03-13 23:28:5524 explicit Builder(const gfx::VectorIcon& icon);
stluongb287cbf32024-08-16 20:10:5625 Builder(const Builder& other) = delete;
26 Builder& operator=(const Builder&) = delete;
27
28 ~Builder();
29
30 // Adds a "X" close button to the toast. However, if the toast have a menu,
31 // the close button will be a rounded dismiss button instead.
32 Builder& AddCloseButton();
33
34 // Adds a rounded action button toast with the `action_button_string_id`
35 // for the label. All toasts with an action button must also have a close
36 // button.
37 Builder& AddActionButton(int action_button_string_id,
38 base::RepeatingClosure closure);
39
40 // Adds a three dot menu to the toast. Toasts with an action button are not
41 // allowed to have menu because they must have an "X" close button instead.
Jan Keitel29b357c2024-11-07 17:30:5542 // If the specification includes a menu, the `ToastParams` that are used
43 // to show the Toast must have a non-null `menu_model` member.
44 Builder& AddMenu();
stluongb287cbf32024-08-16 20:10:5645
Steven Luongf444e6d2024-09-16 22:08:1346 // Toasts by default are scoped to the active tab when they are triggered.
47 // Globally scoped toasts will not immediately dismiss when the user
48 // switches tabs or navigates to another site and will rely on timing out to
49 // dismiss.
50 Builder& AddGlobalScoped();
51
dljames402dc5e2025-05-07 23:17:0952 // Adds the accelerator that is used to trigger an action when this toast is
53 // displayed. The accelerator is handled by the view.
54 Builder& AddAccelerator(ui::Accelerator accelerator,
55 base::RepeatingClosure callback);
56
stluongb287cbf32024-08-16 20:10:5657 std::unique_ptr<ToastSpecification> Build();
58
59 private:
60 void ValidateSpecification();
61
62 std::unique_ptr<ToastSpecification> toast_specification_;
63 };
64
65 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
66 const gfx::VectorIcon& icon,
67 int string_id);
Shakti Sahu226b102b2025-03-13 23:28:5568 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
69 const gfx::VectorIcon& icon);
stluongb287cbf32024-08-16 20:10:5670 ~ToastSpecification();
71
72 int body_string_id() const { return body_string_id_; }
73 const gfx::VectorIcon& icon() const { return *icon_; }
74 bool has_close_button() const { return has_close_button_; }
75 std::optional<int> action_button_string_id() const {
76 return action_button_string_id_;
77 }
Charles Mengab13b8f62024-09-04 20:11:1878 base::RepeatingClosure action_button_callback() const {
stluongb287cbf32024-08-16 20:10:5679 return action_button_closure_;
80 }
dljames402dc5e2025-05-07 23:17:0981 const ui::Accelerator& accelerator() const { return accelerator_; }
82 base::RepeatingClosure accelerator_callback() const {
83 return accelerator_callback_;
84 }
85
Jan Keitel29b357c2024-11-07 17:30:5586 bool has_menu() const { return has_menu_; }
Steven Luongf444e6d2024-09-16 22:08:1387 bool is_global_scope() const { return is_global_scope_; }
dljames402dc5e2025-05-07 23:17:0988 bool has_accelerator() const { return !accelerator_.IsEmpty(); }
stluongb287cbf32024-08-16 20:10:5689
90 void AddCloseButton();
91 void AddActionButton(int string_id, base::RepeatingClosure closure);
Jan Keitel29b357c2024-11-07 17:30:5592 void AddMenu();
Steven Luongf444e6d2024-09-16 22:08:1393 void AddGlobalScope();
dljames402dc5e2025-05-07 23:17:0994 void AddAccelerator(ui::Accelerator accelerator,
95 base::RepeatingClosure callback);
stluongb287cbf32024-08-16 20:10:5696
97 private:
98 const base::raw_ref<const gfx::VectorIcon> icon_;
Shakti Sahu226b102b2025-03-13 23:28:5599 int body_string_id_ = 0;
stluongb287cbf32024-08-16 20:10:56100 bool has_close_button_ = false;
Jan Keitel29b357c2024-11-07 17:30:55101 bool has_menu_ = false;
stluongb287cbf32024-08-16 20:10:56102 std::optional<int> action_button_string_id_;
103 base::RepeatingClosure action_button_closure_;
Steven Luongf444e6d2024-09-16 22:08:13104 bool is_global_scope_ = false;
dljames402dc5e2025-05-07 23:17:09105 ui::Accelerator accelerator_;
106 base::RepeatingClosure accelerator_callback_;
stluongb287cbf32024-08-16 20:10:56107};
108
stluong8fc2b9f2024-08-23 23:01:13109#endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_