File: message_box_view.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (159 lines) | stat: -rw-r--r-- 5,744 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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 UI_VIEWS_CONTROLS_MESSAGE_BOX_VIEW_H_
#define UI_VIEWS_CONTROLS_MESSAGE_BOX_VIEW_H_

#include <stdint.h>

#include <string>
#include <string_view>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/controls/link.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"

namespace views {

class Checkbox;
class Label;
class LayoutProvider;
class ScrollView;
class Textfield;

// This class displays the contents of a message box. It is intended for use
// within a constrained window, and has options for a message, prompt, OK
// and Cancel buttons.
class VIEWS_EXPORT MessageBoxView : public BoxLayoutView {
  METADATA_HEADER(MessageBoxView, BoxLayoutView)

 public:
  using MessageLabels = std::vector<raw_ptr<views::Label, VectorExperimental>>;

  // |detect_directionality| indicates whether |message|'s directionality is
  // auto-detected.
  // For a message from a web page (not from Chrome's UI), such as script
  // dialog text, each paragraph's directionality is auto-detected using the
  // directionality of the paragraph's first strong character's. Please refer
  // to HTML5 spec for details.
  // http://dev.w3.org/html5/spec/Overview.html#text-rendered-in-native-user-interfaces:
  // The spec does not say anything about alignment. And we choose to
  // align all paragraphs according to the direction of the first paragraph.
  explicit MessageBoxView(const std::u16string& message = std::u16string(),
                          bool detect_directionality = false);

  MessageBoxView(const MessageBoxView&) = delete;
  MessageBoxView& operator=(const MessageBoxView&) = delete;

  ~MessageBoxView() override;

  // Updates the message label shown in the message box.
  void SetMessageLabel(std::u16string_view message);

  // Returns the visible prompt field, returns nullptr otherwise.
  views::Textfield* GetVisiblePromptField();

  // Returns user entered data in the prompt field, returns an empty string if
  // no visible prompt field.
  std::u16string_view GetInputText();

  // Returns true if this message box has a visible checkbox, false otherwise.
  bool HasVisibleCheckBox() const;

  // Returns true if a checkbox is selected, false otherwise. (And false if
  // the message box has no checkbox.)
  bool IsCheckBoxSelected();

  // Shows a checkbox with the specified label to the message box if this is the
  // first call. Otherwise, it changes the label of the current checkbox. To
  // start, the message box has no visible checkbox until this function is
  // called.
  void SetCheckBoxLabel(const std::u16string& label);

  // Sets the state of the check-box if it is visible.
  void SetCheckBoxSelected(bool selected);

  // Sets the text and the callback of the link. |text| must be non-empty.
  void SetLink(const std::u16string& text, Link::ClickedCallback callback);

  void SetInterRowVerticalSpacing(int spacing);
  void SetMessageWidth(int width);

  // Adds a prompt field with |default_prompt| as the displayed text.
  void SetPromptField(const std::u16string& default_prompt);

  const std::u16string& label_text_for_testing() const { return label_text_; }

 protected:
  // View:
  gfx::Size CalculatePreferredSize(
      const SizeBounds& available_size) const override;
  void ViewHierarchyChanged(
      const ViewHierarchyChangedDetails& details) override;
  // Handles Ctrl-C and writes the message in the system clipboard.
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override;

 private:
  FRIEND_TEST_ALL_PREFIXES(MessageBoxViewTest, CheckMessageOnlySize);
  FRIEND_TEST_ALL_PREFIXES(MessageBoxViewTest, CheckWithOptionalViewsSize);
  FRIEND_TEST_ALL_PREFIXES(MessageBoxViewTest, CheckInterRowHeightChange);

  // Sets up the layout manager based on currently initialized views and layout
  // parameters. Should be called when a view is initialized or changed.
  void ResetLayoutManager();

  // Return the proper horizontal insets based on the given layout provider.
  gfx::Insets GetHorizontalInsets(const LayoutProvider* provider);

  // Should auto-detect directionality of message label.
  const bool detect_directionality_;

  // Text set/updated in the message box.
  std::u16string label_text_;

  // View of the message box contents.
  raw_ptr<BoxLayoutView> message_contents_ = nullptr;

  // Message for the message box.
  MessageLabels message_labels_;

  // Scrolling view containing the message labels.
  raw_ptr<ScrollView> scroll_view_ = nullptr;

  // Input text field for the message box.
  raw_ptr<Textfield> prompt_field_ = nullptr;

  // Checkbox for the message box.
  raw_ptr<Checkbox> checkbox_ = nullptr;

  // Link displayed at the bottom of the view.
  raw_ptr<Link> link_ = nullptr;

  // Spacing between rows in the grid layout.
  int inter_row_vertical_spacing_ = 0;

  // Maximum width of the message label.
  int message_width_ = 0;
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, MessageBoxView, BoxLayoutView)
VIEW_BUILDER_PROPERTY(const std::u16string&, CheckBoxLabel)
VIEW_BUILDER_PROPERTY(bool, CheckBoxSelected)
VIEW_BUILDER_METHOD(SetLink, const std::u16string&, Link::ClickedCallback)
VIEW_BUILDER_PROPERTY(int, InterRowVerticalSpacing)
VIEW_BUILDER_PROPERTY(int, MessageWidth)
VIEW_BUILDER_PROPERTY(const std::u16string&, PromptField)
END_VIEW_BUILDER

}  // namespace views

DEFINE_VIEW_BUILDER(VIEWS_EXPORT, views::MessageBoxView)

#endif  // UI_VIEWS_CONTROLS_MESSAGE_BOX_VIEW_H_