blob: 0a9b86adea5b5c0233749b5f073ee396bd0012dc [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);
23 Builder(const Builder& other) = delete;
24 Builder& operator=(const Builder&) = delete;
25
26 ~Builder();
27
28 // Adds a "X" close button to the toast. However, if the toast have a menu,
29 // the close button will be a rounded dismiss button instead.
30 Builder& AddCloseButton();
31
32 // Adds a rounded action button toast with the `action_button_string_id`
33 // for the label. All toasts with an action button must also have a close
34 // button.
35 Builder& AddActionButton(int action_button_string_id,
36 base::RepeatingClosure closure);
37
38 // Adds a three dot menu to the toast. Toasts with an action button are not
39 // allowed to have menu because they must have an "X" close button instead.
Jan Keitel29b357c2024-11-07 17:30:5540 // If the specification includes a menu, the `ToastParams` that are used
41 // to show the Toast must have a non-null `menu_model` member.
42 Builder& AddMenu();
stluongb287cbf32024-08-16 20:10:5643
Steven Luongf444e6d2024-09-16 22:08:1344 // Toasts by default are scoped to the active tab when they are triggered.
45 // Globally scoped toasts will not immediately dismiss when the user
46 // switches tabs or navigates to another site and will rely on timing out to
47 // dismiss.
48 Builder& AddGlobalScoped();
49
stluongb287cbf32024-08-16 20:10:5650 std::unique_ptr<ToastSpecification> Build();
51
52 private:
53 void ValidateSpecification();
54
55 std::unique_ptr<ToastSpecification> toast_specification_;
56 };
57
58 ToastSpecification(base::PassKey<ToastSpecification::Builder>,
59 const gfx::VectorIcon& icon,
60 int string_id);
61 ~ToastSpecification();
62
63 int body_string_id() const { return body_string_id_; }
64 const gfx::VectorIcon& icon() const { return *icon_; }
65 bool has_close_button() const { return has_close_button_; }
66 std::optional<int> action_button_string_id() const {
67 return action_button_string_id_;
68 }
Charles Mengab13b8f62024-09-04 20:11:1869 base::RepeatingClosure action_button_callback() const {
stluongb287cbf32024-08-16 20:10:5670 return action_button_closure_;
71 }
Jan Keitel29b357c2024-11-07 17:30:5572 bool has_menu() const { return has_menu_; }
Steven Luongf444e6d2024-09-16 22:08:1373 bool is_global_scope() const { return is_global_scope_; }
stluongb287cbf32024-08-16 20:10:5674
75 void AddCloseButton();
76 void AddActionButton(int string_id, base::RepeatingClosure closure);
Jan Keitel29b357c2024-11-07 17:30:5577 void AddMenu();
Steven Luongf444e6d2024-09-16 22:08:1378 void AddGlobalScope();
stluongb287cbf32024-08-16 20:10:5679
80 private:
81 const base::raw_ref<const gfx::VectorIcon> icon_;
82 int body_string_id_;
83 bool has_close_button_ = false;
Jan Keitel29b357c2024-11-07 17:30:5584 bool has_menu_ = false;
stluongb287cbf32024-08-16 20:10:5685 std::optional<int> action_button_string_id_;
86 base::RepeatingClosure action_button_closure_;
Steven Luongf444e6d2024-09-16 22:08:1387 bool is_global_scope_ = false;
stluongb287cbf32024-08-16 20:10:5688};
89
stluong8fc2b9f2024-08-23 23:01:1390#endif // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_