forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_dialog_view.cc
130 lines (107 loc) · 5.03 KB
/
echo_dialog_view.cc
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
// Copyright 2013 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 "chrome/browser/ash/notifications/echo_dialog_view.h"
#include <stddef.h>
#include "chrome/browser/ash/notifications/echo_dialog_listener.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/font.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
namespace ash {
EchoDialogView::EchoDialogView(EchoDialogListener* listener,
const EchoDialogView::Params& params) {
auto* learn_more_button = DialogDelegate::SetExtraView(
views::CreateVectorImageButtonWithNativeTheme(
base::BindRepeating(&EchoDialogListener::OnMoreInfoLinkClicked,
base::Unretained(listener)),
vector_icons::kHelpOutlineIcon));
learn_more_button->SetAccessibleName(
l10n_util::GetStringUTF16(IDS_CHROMEOS_ACC_LEARN_MORE));
chrome::RecordDialogCreation(chrome::DialogIdentifier::ECHO);
if (params.echo_enabled) {
DialogDelegate::SetButtons(ui::DIALOG_BUTTON_OK |
ui::DIALOG_BUTTON_CANCEL);
DialogDelegate::SetButtonLabel(
ui::DIALOG_BUTTON_OK,
l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON));
DialogDelegate::SetButtonLabel(
ui::DIALOG_BUTTON_CANCEL,
l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON));
InitForEnabledEcho(params.service_name, params.origin);
} else {
DialogDelegate::SetButtons(ui::DIALOG_BUTTON_CANCEL);
DialogDelegate::SetButtonLabel(
ui::DIALOG_BUTTON_CANCEL,
l10n_util::GetStringUTF16(IDS_ECHO_CONSENT_DISMISS_BUTTON));
InitForDisabledEcho();
}
DialogDelegate::SetAcceptCallback(base::BindOnce(
&EchoDialogListener::OnAccept, base::Unretained(listener)));
DialogDelegate::SetCancelCallback(base::BindOnce(
&EchoDialogListener::OnCancel, base::Unretained(listener)));
DialogDelegate::SetShowTitle(false);
DialogDelegate::SetShowCloseButton(false);
DialogDelegate::SetModalType(ui::MODAL_TYPE_WINDOW);
DialogDelegate::set_fixed_width(
views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
}
EchoDialogView::~EchoDialogView() = default;
void EchoDialogView::Show(gfx::NativeWindow parent) {
views::DialogDelegate::CreateDialogWidget(this, parent, parent);
GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
GetWidget()->Show();
}
void EchoDialogView::InitForEnabledEcho(const std::u16string& service_name,
const std::u16string& origin) {
size_t offset;
std::u16string text = l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT,
service_name, &offset);
auto label = std::make_unique<views::StyledLabel>();
label->SetText(text);
views::StyledLabel::RangeStyleInfo service_name_style;
gfx::FontList font_list = label->GetFontList();
service_name_style.custom_font =
font_list.DeriveWithStyle(gfx::Font::UNDERLINE);
service_name_style.tooltip = origin;
label->AddStyleRange(gfx::Range(offset, offset + service_name.length()),
service_name_style);
SetBorderAndLabel(std::move(label), font_list);
}
void EchoDialogView::InitForDisabledEcho() {
auto label = std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT));
label->SetMultiLine(true);
label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
// grab the font list before std::move(label) or it'll be nullptr
gfx::FontList font_list = label->font_list();
SetBorderAndLabel(std::move(label), font_list);
}
void EchoDialogView::SetBorderAndLabel(std::unique_ptr<views::View> label,
const gfx::FontList& label_font_list) {
SetLayoutManager(std::make_unique<views::FillLayout>());
// Without a title, top padding isn't correctly calculated. This adds the
// text's internal leading to the top padding. See
// FontList::DeriveWithHeightUpperBound() for font padding details.
int top_inset_padding =
label_font_list.GetBaseline() - label_font_list.GetCapHeight();
gfx::Insets insets =
views::LayoutProvider::Get()->GetDialogInsetsForContentType(
views::DialogContentType::kText, views::DialogContentType::kText);
insets += gfx::Insets(top_inset_padding, 0, 0, 0);
SetBorder(views::CreateEmptyBorder(insets));
AddChildView(std::move(label));
}
BEGIN_METADATA(EchoDialogView, views::DialogDelegateView)
END_METADATA
} // namespace ash