File: toast_specification.h

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 (107 lines) | stat: -rw-r--r-- 3,985 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_
#define CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_

#include <memory>
#include <optional>

#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ref.h"
#include "base/types/pass_key.h"
#include "ui/gfx/vector_icon_types.h"

// ToastSpecification details what the toast should contain when shown.
class ToastSpecification {
 public:
  class Builder final {
   public:
    Builder(const gfx::VectorIcon& icon, int body_string_id);
    explicit Builder(const gfx::VectorIcon& icon);
    Builder(const Builder& other) = delete;
    Builder& operator=(const Builder&) = delete;

    ~Builder();

    // Adds a "X" close button to the toast. However, if the toast have a menu,
    // the close button will be a rounded dismiss button instead.
    Builder& AddCloseButton();

    // Adds a rounded action button toast with the `action_button_string_id`
    // for the label. All toasts with an action button must also have a close
    // button.
    Builder& AddActionButton(int action_button_string_id,
                             base::RepeatingClosure closure);

    // Adds a three dot menu to the toast. Toasts with an action button are not
    // allowed to have menu because they must have an "X" close button instead.
    // If the specification includes a menu, the `ToastParams` that are used
    // to show the Toast must have a non-null `menu_model` member.
    Builder& AddMenu();

    // Toasts by default are scoped to the active tab when they are triggered.
    // Globally scoped toasts will not immediately dismiss when the user
    // switches tabs or navigates to another site and will rely on timing out to
    // dismiss.
    Builder& AddGlobalScoped();

    // Explicitly marks this toast as an actionable toast (allows the user to
    // interact with it in some way).
    // NOTE: Only use this for toasts that do not have actionable buttons /
    // menus but have interactions tied to the toast that are handled outside
    // the toast framework such as keyboard shortcuts.
    Builder& SetToastAsActionable();

    std::unique_ptr<ToastSpecification> Build();

   private:
    void ValidateSpecification();

    std::unique_ptr<ToastSpecification> toast_specification_;
  };

  ToastSpecification(base::PassKey<ToastSpecification::Builder>,
                     const gfx::VectorIcon& icon,
                     int string_id);
  ToastSpecification(base::PassKey<ToastSpecification::Builder>,
                     const gfx::VectorIcon& icon);
  ~ToastSpecification();

  int body_string_id() const { return body_string_id_; }
  const gfx::VectorIcon& icon() const { return *icon_; }
  bool has_close_button() const { return has_close_button_; }
  std::optional<int> action_button_string_id() const {
    return action_button_string_id_;
  }
  base::RepeatingClosure action_button_callback() const {
    return action_button_closure_;
  }

  bool has_menu() const { return has_menu_; }
  bool is_global_scope() const { return is_global_scope_; }
  bool is_actionable() const {
    return has_close_button() || has_menu() || has_actionable_override();
  }
  bool has_actionable_override() const { return actionable_toast_override_; }

  void AddCloseButton();
  void AddActionButton(int string_id, base::RepeatingClosure closure);
  void AddMenu();
  void AddGlobalScope();
  void SetToastAsActionable();

 private:
  const base::raw_ref<const gfx::VectorIcon> icon_;
  int body_string_id_ = 0;
  bool has_close_button_ = false;
  bool has_menu_ = false;
  std::optional<int> action_button_string_id_;
  base::RepeatingClosure action_button_closure_;
  bool is_global_scope_ = false;
  bool actionable_toast_override_ = false;
};

#endif  // CHROME_BROWSER_UI_TOASTS_API_TOAST_SPECIFICATION_H_