blob: 813f30b37cf73c825eab96c784384eb43839e990 [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"
stluongb287cbf32024-08-16 20:10:5615#include "ui/gfx/vector_icon_types.h"
16
17// ToastSpecification details what the toast should contain when shown.
18class ToastSpecification {
19 public:
20 class Builder final {
21 public:
22 Builder(const gfx::VectorIcon& icon, int body_string_id);
Shakti Sahu226b102b2025-03-13 23:28:5523 explicit Builder(const gfx::VectorIcon& icon);
stluongb287cbf32024-08-16 20:10:5624 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 Keitel29b357c2024-11-07 17:30:5541 // 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();
stluongb287cbf32024-08-16 20:10:5644
Steven Luongf444e6d2024-09-16 22:08:1345 // 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
stluongb287cbf32024-08-16 20:10:5651 std::unique_ptr<ToastSpecification> Build();
52
53 private:
54 void ValidateSpecification();
55
56 std::unique_ptr<ToastSpecification> toast_specification_;
57 };
58
59 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
60 const gfx::VectorIcon& icon,
61 int string_id);
Shakti Sahu226b102b2025-03-13 23:28:5562 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
63 const gfx::VectorIcon& icon);
stluongb287cbf32024-08-16 20:10:5664 ~ToastSpecification();
65
66 int body_string_id() const { return body_string_id_; }
67 const gfx::VectorIcon& icon() const { return *icon_; }
68 bool has_close_button() const { return has_close_button_; }
69 std::optional<int> action_button_string_id() const {
70 return action_button_string_id_;
71 }
Charles Mengab13b8f62024-09-04 20:11:1872 base::RepeatingClosure action_button_callback() const {
stluongb287cbf32024-08-16 20:10:5673 return action_button_closure_;
74 }
Jan Keitel29b357c2024-11-07 17:30:5575 bool has_menu() const { return has_menu_; }
Steven Luongf444e6d2024-09-16 22:08:1376 bool is_global_scope() const { return is_global_scope_; }
stluongb287cbf32024-08-16 20:10:5677
78 void AddCloseButton();
79 void AddActionButton(int string_id, base::RepeatingClosure closure);
Jan Keitel29b357c2024-11-07 17:30:5580 void AddMenu();
Steven Luongf444e6d2024-09-16 22:08:1381 void AddGlobalScope();
stluongb287cbf32024-08-16 20:10:5682
83 private:
84 const base::raw_ref<const gfx::VectorIcon> icon_;
Shakti Sahu226b102b2025-03-13 23:28:5585 int body_string_id_ = 0;
stluongb287cbf32024-08-16 20:10:5686 bool has_close_button_ = false;
Jan Keitel29b357c2024-11-07 17:30:5587 bool has_menu_ = false;
stluongb287cbf32024-08-16 20:10:5688 std::optional<int> action_button_string_id_;
89 base::RepeatingClosure action_button_closure_;
Steven Luongf444e6d2024-09-16 22:08:1390 bool is_global_scope_ = false;
stluongb287cbf32024-08-16 20:10:5691};
92
stluong8fc2b9f2024-08-23 23:01:1393#endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_