File: toast_view.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,268 bytes parent folder | download | duplicates (6)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// 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_TOAST_VIEW_H_
#define CHROME_BROWSER_UI_TOASTS_TOAST_VIEW_H_

#include <memory>
#include <optional>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/widget/widget.h"

namespace ui {
class MenuModel;
}  // namespace ui

namespace views {
class ImageButton;
class ImageView;
class Label;
class MdTextButton;
class MenuModelAdapter;
class MenuRunner;
}  // namespace views

namespace toasts {

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class ToastCloseReason {
  kAutoDismissed = 0,
  kActionButton = 1,
  kCloseButton = 2,
  kPreempted = 3,
  kMenuItemClick = 4,
  kFeatureDismiss = 5,
  kAbort = 6,
  kMaxValue = kAbort
};

// The view for toasts.
class ToastView : public views::BubbleDialogDelegateView,
                  public views::AnimationDelegateViews {
  METADATA_HEADER(ToastView, views::BubbleDialogDelegateView)

 public:
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kToastViewId);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kToastActionButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kToastCloseButton);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kToastMenuButton);
  ToastView(
      views::View* anchor_view,
      const std::u16string& toast_text,
      const gfx::VectorIcon& icon,
      const ui::ImageModel* image_override,
      bool should_hide_ui_for_fullscreen,
      base::RepeatingCallback<void(ToastCloseReason)> on_toast_close_callback);
  ~ToastView() override;

  // Must be called prior to Init (which is called from
  // views::BubbleDialogDelegateView::CreateBubble).
  void AddActionButton(
      const std::u16string& action_button_text,
      base::RepeatingClosure action_button_callback = base::DoNothing());

  // Must be called prior to Init (which is called from
  // views::BubbleDialogDelegateView::CreateBubble).
  void AddCloseButton(
      base::RepeatingClosure close_button_callback = base::DoNothing());

  // Adds a three-dot menu button that, when clicked, runs the menu defined by
  // `model`. Must be called prior to `Init` (which is called from
  // views::BubbleDialogDelegateView::CreateBubble).
  void AddMenu(std::unique_ptr<ui::MenuModel> model);

  // views::BubbleDialogDelegateView:
  void Init() override;

  // AnimationDelegateViews:
  void AnimationProgressed(const gfx::Animation* animation) override;

  void AnimateIn();

  // Animates out the toast, then closes the toast widget.
  void Close(ToastCloseReason close_reason);

  void UpdateRenderToastOverWebContentsAndPaint(
      const bool render_toast_over_web_contents);

  views::Label* label_for_testing() { return label_; }
  views::MdTextButton* action_button_for_testing() { return action_button_; }
  views::ImageButton* close_button_for_testing() { return close_button_; }

  // Gets the icon/image size from the layout provider.
  static int GetIconSize();

 protected:
  // views::BubbleDialogDelegateView:
  gfx::Rect GetBubbleBounds() override;
  void OnThemeChanged() override;

 private:
  void AnimateOut(base::OnceClosure callback, bool show_height_animation);

  // Opens the menu if it is not already open or closes the existing menu
  // otherwise. Assumes that `menu_model_` and `menu_button_` are not null.
  void OnMenuButtonClicked();

  // Resets `menu_runner_` and removes the highlight from the menu button.
  void OnMenuClosed();

  gfx::LinearAnimation height_animation_{this};
  gfx::Rect starting_widget_bounds_;
  gfx::Rect target_widget_bounds_;
  gfx::Tween::Type height_animation_tween_;

  const std::u16string toast_text_;
  const raw_ref<const gfx::VectorIcon> icon_;
  const raw_ptr<const ui::ImageModel> image_override_;
  bool render_toast_over_web_contents_;
  bool has_close_button_ = false;
  bool has_action_button_ = false;
  std::u16string action_button_text_;
  base::RepeatingClosure action_button_callback_;
  base::RepeatingClosure close_button_callback_;
  base::RepeatingCallback<void(ToastCloseReason)> toast_close_callback_;
  std::unique_ptr<ui::MenuModel> menu_model_;
  // Wraps `menu_model_` and triggers closing the toast after executing menu
  // commands.
  std::unique_ptr<views::MenuModelAdapter> menu_model_adapter_;
  std::unique_ptr<views::MenuRunner> menu_runner_;

  // Raw pointers to child views.
  raw_ptr<views::Label> label_ = nullptr;
  raw_ptr<views::ImageView> icon_view_ = nullptr;
  raw_ptr<views::MdTextButton> action_button_ = nullptr;
  raw_ptr<views::ImageButton> close_button_ = nullptr;

  raw_ptr<views::ImageButton> menu_button_ = nullptr;

  // If there was an attempt to close `this` while a menu was showing,
  // `pending_close_reason_` contains the reason for closing the view and it
  // is used to close the view once the menu stops showing.
  std::optional<ToastCloseReason> pending_close_reason_;
};

}  // namespace toasts

#endif  // CHROME_BROWSER_UI_TOASTS_TOAST_VIEW_H_