File: infobar_view.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (139 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download | duplicates (5)
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
// Copyright 2012 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_VIEWS_INFOBARS_INFOBAR_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_VIEW_H_

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

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_container.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/controls/menu/menu_types.h"
#include "ui/views/focus/external_focus_tracker.h"
#include "ui/views/view.h"

namespace views {
class ImageButton;
class ImageView;
class Label;
class Link;
class MenuRunner;
}  // namespace views

class InfoBarView : public infobars::InfoBar,
                    public views::View,
                    public views::ExternalFocusTracker {
  METADATA_HEADER(InfoBarView, views::View)

 public:
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kInfoBarElementId);
  DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kDismissButtonElementId);

  explicit InfoBarView(std::unique_ptr<infobars::InfoBarDelegate> delegate);
  InfoBarView(const InfoBarView&) = delete;
  InfoBarView& operator=(const InfoBarView&) = delete;
  ~InfoBarView() override;

  // views::View:
  void Layout(PassKey) override;
  gfx::Size CalculatePreferredSize(
      const views::SizeBounds& available_size) const override;
  void OnThemeChanged() override;

  // views::ExternalFocusTracker:
  void OnWillChangeFocus(View* focused_before, View* focused_now) override;

 protected:
  using Labels = std::vector<views::Label*>;

  // Creates a label with the appropriate font and color for an infobar.
  std::unique_ptr<views::Label> CreateLabel(const std::u16string& text) const;

  // Creates a link with the appropriate font and color for an infobar.
  // By default, `text` will be used as a accessible text if it's not explicitly
  // provided. NOTE: Subclasses must ignore link clicks if we're unowned.
  std::unique_ptr<views::Link> CreateLink(
      const std::u16string& text,
      const std::optional<std::u16string>& accessible_text = std::nullopt);

  // Given |views| and the total |available_width| to display them in, sets
  // each view's size so that the longest view shrinks until it reaches the
  // length of the next-longest view, then both shrink until reaching the
  // length of the next-longest, and so forth.
  static void AssignWidths(Views* views, int available_width);

  // Returns the minimum width the content (that is, everything between the icon
  // and the close button) can be shrunk to.  This is used to prevent the close
  // button from overlapping views that cannot be shrunk any further.
  virtual int GetContentMinimumWidth() const;

  // These return x coordinates delimiting the usable area for subclasses to lay
  // out their controls.
  int GetStartX() const;
  int GetEndX() const;

  // Given a |view|, returns the centered y position, taking into account
  // animation so the control "slides in" (or out) as we animate open and
  // closed.
  int OffsetY(views::View* view) const;

  // infobars::InfoBar:
  void PlatformSpecificShow(bool animate) override;
  void PlatformSpecificHide(bool animate) override;
  void PlatformSpecificOnHeightRecalculated() override;

  // Adds `child` to `content_container_` and returns the raw pointer. All
  // subclasses should call this instead of AddChildView().
  template <typename ViewClass>
  ViewClass* AddContentChildView(std::unique_ptr<ViewClass> child) {
    CHECK(content_container_);
    return content_container_->AddChildView(std::move(child));
  }

 private:
  FRIEND_TEST_ALL_PREFIXES(InfoBarViewTest, GetDrawSeparator);

  // Make all AddChildView* overloads private so downstream subclasses
  // cannot call them directly.
  using View::AddChildView;
  using View::AddChildViewAt;
  using View::AddChildViewRaw;

  // Does the actual work for AssignWidths().  Assumes |views| is sorted by
  // decreasing preferred width.
  static void AssignWidthsSorted(Views* views, int available_width);

  // Sets various attributes on |label| that are common to all child links and
  // labels.
  void SetLabelDetails(views::Label* label) const;

  // Callback used by the link created by CreateLink().
  void LinkClicked(const ui::Event& event);

  void CloseButtonPressed();

  // The optional icon at the left edge of the InfoBar.
  raw_ptr<views::ImageView> icon_ = nullptr;

  // Container that owns all views that should appear between `icon_` and
  // `close_button_`.  Subclasses **must** add their children to this view via
  // the AddContentChildView() helper; doing so guarantees that the close
  // button always stays the last child without any explicit re‑ordering.
  raw_ptr<views::View> content_container_ = nullptr;

  // The close button at the right edge of the InfoBar.
  raw_ptr<views::ImageButton> close_button_ = nullptr;

  // Used to run the menu.
  std::unique_ptr<views::MenuRunner> menu_runner_;
};

#endif  // CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_VIEW_H_