ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / ui / echo_dialog_view.cc
blob7c3cb9b1db933ca340c8bebf9d14f782bfae96ed
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/ui/echo_dialog_view.h"
7 #include "chrome/browser/chromeos/ui/echo_dialog_listener.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/gfx/font.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/styled_label.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_client_view.h"
16 namespace {
18 const int kDialogLabelTopInset = 20;
19 const int kDialogLabelLeftInset = 20;
20 const int kDialogLabelBottomInset = 20;
21 const int kDialogLabelRightInset = 100;
23 const int kDialogLabelPreferredWidth =
24 350 + kDialogLabelLeftInset + kDialogLabelRightInset;
26 } // namespace
28 namespace chromeos {
30 EchoDialogView::EchoDialogView(EchoDialogListener* listener)
31 : label_(NULL),
32 listener_(listener),
33 ok_button_label_id_(0),
34 cancel_button_label_id_(0) {
37 EchoDialogView::~EchoDialogView() {}
39 void EchoDialogView::InitForEnabledEcho(const base::string16& service_name,
40 const base::string16& origin) {
41 ok_button_label_id_ = IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON;
42 cancel_button_label_id_ = IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON;
44 base::string16 link =
45 l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
47 std::vector<size_t> offsets;
48 base::string16 text = l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT,
49 service_name,
50 link,
51 &offsets);
53 label_ = new views::StyledLabel(text, this);
55 views::StyledLabel::RangeStyleInfo service_name_style;
56 service_name_style.font_style = gfx::Font::UNDERLINE;
57 service_name_style.tooltip = origin;
58 label_->AddStyleRange(
59 gfx::Range(offsets[0], offsets[0] + service_name.length()),
60 service_name_style);
62 views::StyledLabel::RangeStyleInfo link_style =
63 views::StyledLabel::RangeStyleInfo::CreateForLink();
64 link_style.font_style = gfx::Font::NORMAL;
65 label_->AddStyleRange(gfx::Range(offsets[1], offsets[1] + link.length()),
66 link_style);
68 SetLabelBorderAndBounds();
70 AddChildView(label_);
73 void EchoDialogView::InitForDisabledEcho() {
74 ok_button_label_id_ = 0;
75 cancel_button_label_id_ = IDS_ECHO_CONSENT_DISMISS_BUTTON;
77 base::string16 link =
78 l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
80 size_t offset;
81 base::string16 text = l10n_util::GetStringFUTF16(
82 IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset);
84 label_ = new views::StyledLabel(text, this);
86 views::StyledLabel::RangeStyleInfo link_style =
87 views::StyledLabel::RangeStyleInfo::CreateForLink();
88 link_style.font_style = gfx::Font::NORMAL;
89 label_->AddStyleRange(gfx::Range(offset, offset + link.length()), link_style);
91 SetLabelBorderAndBounds();
93 AddChildView(label_);
96 void EchoDialogView::Show(gfx::NativeWindow parent) {
97 DCHECK(cancel_button_label_id_);
99 views::DialogDelegate::CreateDialogWidget(this, parent, parent);
100 GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
101 GetWidget()->Show();
104 int EchoDialogView::GetDefaultDialogButton() const {
105 return ui::DIALOG_BUTTON_NONE;
108 int EchoDialogView::GetDialogButtons() const {
109 int buttons = ui::DIALOG_BUTTON_NONE;
110 if (ok_button_label_id_)
111 buttons |= ui::DIALOG_BUTTON_OK;
112 if (cancel_button_label_id_)
113 buttons |= ui::DIALOG_BUTTON_CANCEL;
114 return buttons;
117 bool EchoDialogView::Accept() {
118 if (listener_) {
119 listener_->OnAccept();
120 listener_ = NULL;
122 return true;
125 bool EchoDialogView::Cancel() {
126 if (listener_) {
127 listener_->OnCancel();
128 listener_ = NULL;
130 return true;
133 base::string16 EchoDialogView::GetDialogButtonLabel(
134 ui::DialogButton button) const {
135 if (button == ui::DIALOG_BUTTON_OK && ok_button_label_id_)
136 return l10n_util::GetStringUTF16(ok_button_label_id_);
137 if (button == ui::DIALOG_BUTTON_CANCEL && cancel_button_label_id_)
138 return l10n_util::GetStringUTF16(cancel_button_label_id_);
139 return base::string16();
142 ui::ModalType EchoDialogView::GetModalType() const {
143 return ui::MODAL_TYPE_WINDOW;
146 bool EchoDialogView::ShouldShowWindowTitle() const {
147 return false;
150 bool EchoDialogView::ShouldShowWindowIcon() const {
151 return false;
154 void EchoDialogView::StyledLabelLinkClicked(const gfx::Range& range,
155 int event_flags) {
156 if (!listener_)
157 return;
158 listener_->OnMoreInfoLinkClicked();
161 gfx::Size EchoDialogView::GetPreferredSize() const {
162 gfx::Size size =
163 gfx::Size(kDialogLabelPreferredWidth,
164 label_->GetHeightForWidth(kDialogLabelPreferredWidth));
165 gfx::Insets insets = GetInsets();
166 size.Enlarge(insets.width(), insets.height());
167 return size;
170 void EchoDialogView::SetLabelBorderAndBounds() {
171 label_->SetBorder(views::Border::CreateEmptyBorder(kDialogLabelTopInset,
172 kDialogLabelLeftInset,
173 kDialogLabelBottomInset,
174 kDialogLabelRightInset));
176 label_->SetBounds(label_->x(),
177 label_->y(),
178 kDialogLabelPreferredWidth,
179 label_->GetHeightForWidth(kDialogLabelPreferredWidth));
182 } // namespace chromeos