blob: c52a2096b85dc0c988dff6402ecb4e88ca735b6a [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"
15#include "ui/base/models/simple_menu_model.h"
16#include "ui/gfx/vector_icon_types.h"
17
18// ToastSpecification details what the toast should contain when shown.
19class 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 Luongf444e6d2024-09-16 22:08:1343 // 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
stluongb287cbf32024-08-16 20:10:5649 // Toast should only dismiss when explicitly instructed to by feature.
50 // There can only be one persistent toast shown at a time.
51 Builder& AddPersistance();
52
53 std::unique_ptr<ToastSpecification> Build();
54
55 private:
56 void ValidateSpecification();
57
58 std::unique_ptr<ToastSpecification> toast_specification_;
59 };
60
61 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
62 const gfx::VectorIcon& icon,
63 int string_id);
64 ~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 }
75 ui::SimpleMenuModel* menu_model() const { return menu_model_.get(); }
Steven Luongf444e6d2024-09-16 22:08:1376 bool is_global_scope() const { return is_global_scope_; }
stluongb287cbf32024-08-16 20:10:5677 bool is_persistent_toast() const { return is_persistent_toast_; }
78
79 void AddCloseButton();
80 void AddActionButton(int string_id, base::RepeatingClosure closure);
81 void AddMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model);
Steven Luongf444e6d2024-09-16 22:08:1382 void AddGlobalScope();
stluongb287cbf32024-08-16 20:10:5683 void AddPersistance();
84
85 private:
86 const base::raw_ref<const gfx::VectorIcon> icon_;
87 int body_string_id_;
88 bool has_close_button_ = false;
89 std::optional<int> action_button_string_id_;
90 base::RepeatingClosure action_button_closure_;
91 std::unique_ptr<ui::SimpleMenuModel> menu_model_;
Steven Luongf444e6d2024-09-16 22:08:1392 bool is_global_scope_ = false;
stluongb287cbf32024-08-16 20:10:5693 bool is_persistent_toast_ = false;
94};
95
stluong8fc2b9f2024-08-23 23:01:1396#endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_