summaryrefslogtreecommitdiff
path: root/chromium/ui/views/controls/message_box_view.cc
blob: 1c699affa71e3debcfc515a637b36ec6203622d7 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/message_box_view.h"

#include <stddef.h>

#include <memory>
#include <numeric>
#include <utility>

#include "base/i18n/rtl.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/metadata/metadata_impl_macros.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/client_view.h"
#include "ui/views/window/dialog_delegate.h"

namespace {

constexpr int kDefaultMessageWidth = 400;

// Paragraph separators are defined in
// http://www.unicode.org/Public/6.0.0/ucd/extracted/DerivedBidiClass.txt
//
// # Bidi_Class=Paragraph_Separator
//
// 000A          ; B # Cc       <control-000A>
// 000D          ; B # Cc       <control-000D>
// 001C..001E    ; B # Cc   [3] <control-001C>..<control-001E>
// 0085          ; B # Cc       <control-0085>
// 2029          ; B # Zp       PARAGRAPH SEPARATOR
bool IsParagraphSeparator(base::char16 c) {
  return (c == 0x000A || c == 0x000D || c == 0x001C || c == 0x001D ||
          c == 0x001E || c == 0x0085 || c == 0x2029);
}

// Splits |text| into a vector of paragraphs.
// Given an example "\nabc\ndef\n\n\nhij\n", the split results should be:
// "", "abc", "def", "", "", "hij", and "".
void SplitStringIntoParagraphs(const base::string16& text,
                               std::vector<base::string16>* paragraphs) {
  paragraphs->clear();

  size_t start = 0;
  for (size_t i = 0; i < text.length(); ++i) {
    if (IsParagraphSeparator(text[i])) {
      paragraphs->push_back(text.substr(start, i - start));
      start = i + 1;
    }
  }
  paragraphs->push_back(text.substr(start, text.length() - start));
}

}  // namespace

namespace views {

///////////////////////////////////////////////////////////////////////////////
// MessageBoxView, public:

MessageBoxView::MessageBoxView(const base::string16& message,
                               bool detect_directionality)
    : inter_row_vertical_spacing_(LayoutProvider::Get()->GetDistanceMetric(
          DISTANCE_RELATED_CONTROL_VERTICAL)),
      message_width_(kDefaultMessageWidth) {
  const LayoutProvider* provider = LayoutProvider::Get();

  auto message_contents = std::make_unique<View>();
  // We explicitly set insets on the message contents instead of the scroll view
  // so that the scroll view borders are not capped by dialog insets.
  message_contents->SetBorder(CreateEmptyBorder(GetHorizontalInsets(provider)));
  message_contents->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));
  auto add_label = [&message_contents, this](
                       const base::string16& text, bool multi_line,
                       gfx::HorizontalAlignment alignment) {
    auto message_label =
        std::make_unique<Label>(text, style::CONTEXT_DIALOG_BODY_TEXT);
    message_label->SetMultiLine(!text.empty());
    message_label->SetAllowCharacterBreak(true);
    message_label->SetHorizontalAlignment(alignment);
    message_labels_.push_back(
        message_contents->AddChildView(std::move(message_label)));
  };
  if (detect_directionality) {
    std::vector<base::string16> texts;
    SplitStringIntoParagraphs(message, &texts);
    for (const auto& text : texts) {
      // Avoid empty multi-line labels, which have a height of 0.
      add_label(text, !text.empty(), gfx::ALIGN_TO_HEAD);
    }
  } else {
    add_label(message, true, gfx::ALIGN_LEFT);
  }
  auto scroll_view = std::make_unique<ScrollView>();
  scroll_view->ClipHeightTo(0, provider->GetDistanceMetric(
                                   DISTANCE_DIALOG_SCROLLABLE_AREA_MAX_HEIGHT));
  scroll_view->SetContents(std::move(message_contents));
  scroll_view_ = AddChildView(std::move(scroll_view));
  // Don't enable text selection if multiple labels are used, since text
  // selection can't span multiple labels.
  if (message_labels_.size() == 1u)
    message_labels_[0]->SetSelectable(true);

  prompt_field_ = AddChildView(std::make_unique<Textfield>());
  prompt_field_->SetAccessibleName(message);
  prompt_field_->SetVisible(false);

  checkbox_ = AddChildView(std::make_unique<Checkbox>());
  checkbox_->SetVisible(false);

  link_ = AddChildView(std::make_unique<Link>());
  link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  link_->SetVisible(false);

  ResetLayoutManager();
}

MessageBoxView::~MessageBoxView() = default;

views::Textfield* MessageBoxView::GetVisiblePromptField() {
  return prompt_field_ && prompt_field_->GetVisible() ? prompt_field_ : nullptr;
}

base::string16 MessageBoxView::GetInputText() {
  return prompt_field_ && prompt_field_->GetVisible() ? prompt_field_->GetText()
                                                      : base::string16();
}

bool MessageBoxView::HasVisibleCheckBox() const {
  return checkbox_ && checkbox_->GetVisible();
}

bool MessageBoxView::IsCheckBoxSelected() {
  return checkbox_ && checkbox_->GetVisible() && checkbox_->GetChecked();
}

void MessageBoxView::SetCheckBoxLabel(const base::string16& label) {
  DCHECK(checkbox_);
  if (checkbox_->GetVisible() && checkbox_->GetText() == label)
    return;

  checkbox_->SetText(label);
  checkbox_->SetVisible(true);
  ResetLayoutManager();
}

void MessageBoxView::SetCheckBoxSelected(bool selected) {
  // Only update the checkbox's state after the checkbox is shown.
  if (!checkbox_->GetVisible())
    return;
  checkbox_->SetChecked(selected);
}

void MessageBoxView::SetLink(const base::string16& text,
                             Link::ClickedCallback callback) {
  DCHECK(!text.empty());
  DCHECK(!callback.is_null());
  DCHECK(link_);

  link_->SetCallback(std::move(callback));
  if (link_->GetVisible() && link_->GetText() == text)
    return;
  link_->SetText(text);
  link_->SetVisible(true);
  ResetLayoutManager();
}

void MessageBoxView::SetInterRowVerticalSpacing(int spacing) {
  if (inter_row_vertical_spacing_ == spacing)
    return;

  inter_row_vertical_spacing_ = spacing;
  ResetLayoutManager();
}

void MessageBoxView::SetMessageWidth(int width) {
  if (message_width_ == width)
    return;

  message_width_ = width;
  ResetLayoutManager();
}

void MessageBoxView::SetPromptField(const base::string16& default_prompt) {
  DCHECK(prompt_field_);
  if (prompt_field_->GetVisible() && prompt_field_->GetText() == default_prompt)
    return;
  prompt_field_->SetText(default_prompt);
  prompt_field_->SetVisible(true);
  ResetLayoutManager();
}

///////////////////////////////////////////////////////////////////////////////
// MessageBoxView, View overrides:

void MessageBoxView::ViewHierarchyChanged(
    const ViewHierarchyChangedDetails& details) {
  if (details.child == this && details.is_add) {
    if (prompt_field_ && prompt_field_->GetVisible())
      prompt_field_->SelectAll(true);
  }
}

bool MessageBoxView::AcceleratorPressed(const ui::Accelerator& accelerator) {
  // We only accept Ctrl-C.
  DCHECK(accelerator.key_code() == 'C' && accelerator.IsCtrlDown());

  // We must not intercept Ctrl-C when we have a text box and it's focused.
  if (prompt_field_ && prompt_field_->HasFocus())
    return false;

  // Don't intercept Ctrl-C if we only use a single message label supporting
  // text selection.
  if (message_labels_.size() == 1u && message_labels_[0]->GetSelectable())
    return false;

  ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
  scw.WriteText(std::accumulate(message_labels_.cbegin(),
                                message_labels_.cend(), base::string16(),
                                [](base::string16& left, Label* right) {
                                  return left + right->GetText();
                                }));
  return true;
}

///////////////////////////////////////////////////////////////////////////////
// MessageBoxView, private:

void MessageBoxView::ResetLayoutManager() {
  // Initialize the Grid Layout Manager used for this dialog box.
  GridLayout* layout = SetLayoutManager(std::make_unique<views::GridLayout>());

  // Add the column set for the message displayed at the top of the dialog box.
  constexpr int kMessageViewColumnSetId = 0;
  ColumnSet* column_set = layout->AddColumnSet(kMessageViewColumnSetId);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                        GridLayout::ColumnSize::kFixed, message_width_, 0);

  const LayoutProvider* provider = LayoutProvider::Get();

  // Column set for extra elements, if any.
  constexpr int kExtraViewColumnSetId = 1;
  if (prompt_field_->GetVisible() || checkbox_->GetVisible() ||
      link_->GetVisible()) {
    auto horizontal_insets = GetHorizontalInsets(provider);
    column_set = layout->AddColumnSet(kExtraViewColumnSetId);
    column_set->AddPaddingColumn(0, horizontal_insets.left());
    column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                          GridLayout::ColumnSize::kUsePreferred, 0, 0);
    column_set->AddPaddingColumn(0, horizontal_insets.right());
  }

  layout->StartRow(0, kMessageViewColumnSetId);
  layout->AddExistingView(scroll_view_);

  views::DialogContentType trailing_content_type = views::TEXT;
  if (prompt_field_->GetVisible()) {
    layout->AddPaddingRow(0, inter_row_vertical_spacing_);
    layout->StartRow(0, kExtraViewColumnSetId);
    layout->AddExistingView(prompt_field_);
    trailing_content_type = views::CONTROL;
  }

  if (checkbox_->GetVisible()) {
    layout->AddPaddingRow(0, inter_row_vertical_spacing_);
    layout->StartRow(0, kExtraViewColumnSetId);
    layout->AddExistingView(checkbox_);
    trailing_content_type = views::TEXT;
  }

  if (link_->GetVisible()) {
    layout->AddPaddingRow(0, inter_row_vertical_spacing_);
    layout->StartRow(0, kExtraViewColumnSetId);
    layout->AddExistingView(link_);
    trailing_content_type = views::TEXT;
  }

  gfx::Insets border_insets = provider->GetDialogInsetsForContentType(
      views::TEXT, trailing_content_type);
  // Horizontal insets have already been applied to the message contents and
  // controls as padding columns. Only apply the missing vertical insets.
  border_insets.Set(border_insets.top(), 0, border_insets.bottom(), 0);
  SetBorder(CreateEmptyBorder(border_insets));

  InvalidateLayout();
}

gfx::Insets MessageBoxView::GetHorizontalInsets(
    const LayoutProvider* provider) {
  gfx::Insets horizontal_insets =
      provider->GetInsetsMetric(views::INSETS_DIALOG);
  horizontal_insets.Set(0, horizontal_insets.left(), 0,
                        horizontal_insets.right());
  return horizontal_insets;
}

BEGIN_METADATA(MessageBoxView, View)
END_METADATA

}  // namespace views