Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / sync / profile_signin_confirmation_dialog_views.cc
blobeda8fa5fdb861faeef3b1a2ad136392eafa73af7
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/ui/views/sync/profile_signin_confirmation_dialog_views.h"
7 #include <algorithm>
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_navigator.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/host_desktop.h"
14 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/constrained_window/constrained_window_views.h"
18 #include "components/signin/core/common/profile_management_switches.h"
19 #include "content/public/browser/web_contents.h"
20 #include "google_apis/gaia/gaia_auth_util.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/font.h"
24 #include "ui/gfx/native_widget_types.h"
25 #include "ui/gfx/range/range.h"
26 #include "ui/views/background.h"
27 #include "ui/views/border.h"
28 #include "ui/views/controls/label.h"
29 #include "ui/views/controls/styled_label.h"
30 #include "ui/views/layout/box_layout.h"
31 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/layout/layout_constants.h"
33 #include "ui/views/widget/widget.h"
34 #include "ui/views/window/dialog_client_view.h"
36 ProfileSigninConfirmationDialogViews::ProfileSigninConfirmationDialogViews(
37 Browser* browser,
38 const std::string& username,
39 ui::ProfileSigninConfirmationDelegate* delegate)
40 : browser_(browser),
41 username_(username),
42 delegate_(delegate),
43 prompt_for_new_profile_(true),
44 continue_signin_button_(NULL) {
47 ProfileSigninConfirmationDialogViews::~ProfileSigninConfirmationDialogViews() {}
49 // static
50 void ProfileSigninConfirmationDialogViews::ShowDialog(
51 Browser* browser,
52 Profile* profile,
53 const std::string& username,
54 ui::ProfileSigninConfirmationDelegate* delegate) {
55 // Hides the new avatar bubble if it is currently shown. The new avatar bubble
56 // should be automatically closed when it loses focus. However on windows the
57 // profile signin confirmation dialog is not modal yet thus it does not take
58 // away focus, thus as a temporary workaround we need to manually close the
59 // bubble.
60 // TODO(guohui): removes the workaround once the profile confirmation dialog
61 // is fixed.
62 if (switches::IsNewAvatarMenu() && ProfileChooserView::IsShowing())
63 ProfileChooserView::Hide();
65 ProfileSigninConfirmationDialogViews* dialog =
66 new ProfileSigninConfirmationDialogViews(
67 browser, username, delegate);
68 ui::CheckShouldPromptForNewProfile(
69 profile,
70 // This callback is guaranteed to be invoked, and once it is, the dialog
71 // owns itself.
72 base::Bind(&ProfileSigninConfirmationDialogViews::Show,
73 base::Unretained(dialog)));
76 void ProfileSigninConfirmationDialogViews::Show(bool prompt_for_new_profile) {
77 prompt_for_new_profile_ = prompt_for_new_profile;
78 constrained_window::CreateBrowserModalDialogViews(
79 this, browser_->window()->GetNativeWindow())->Show();
82 base::string16 ProfileSigninConfirmationDialogViews::GetWindowTitle() const {
83 return l10n_util::GetStringUTF16(
84 IDS_ENTERPRISE_SIGNIN_TITLE_NEW_STYLE);
87 base::string16 ProfileSigninConfirmationDialogViews::GetDialogButtonLabel(
88 ui::DialogButton button) const {
89 if (button == ui::DIALOG_BUTTON_OK) {
90 // If we're giving the option to create a new profile, then OK is
91 // "Create new profile". Otherwise it is "Continue signin".
92 return l10n_util::GetStringUTF16(
93 prompt_for_new_profile_ ?
94 IDS_ENTERPRISE_SIGNIN_CREATE_NEW_PROFILE_NEW_STYLE :
95 IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
97 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CANCEL);
100 int ProfileSigninConfirmationDialogViews::GetDefaultDialogButton() const {
101 return ui::DIALOG_BUTTON_NONE;
104 views::View* ProfileSigninConfirmationDialogViews::CreateExtraView() {
105 if (prompt_for_new_profile_) {
106 const base::string16 continue_signin_text =
107 l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_CONTINUE_NEW_STYLE);
108 continue_signin_button_ =
109 new views::LabelButton(this, continue_signin_text);
110 continue_signin_button_->SetStyle(views::Button::STYLE_BUTTON);
111 continue_signin_button_->SetFocusable(true);
113 return continue_signin_button_;
116 bool ProfileSigninConfirmationDialogViews::Accept() {
117 if (delegate_) {
118 if (prompt_for_new_profile_)
119 delegate_->OnSigninWithNewProfile();
120 else
121 delegate_->OnContinueSignin();
122 delegate_ = NULL;
124 return true;
127 bool ProfileSigninConfirmationDialogViews::Cancel() {
128 if (delegate_) {
129 delegate_->OnCancelSignin();
130 delegate_ = NULL;
132 return true;
135 void ProfileSigninConfirmationDialogViews::OnClosed() {
136 Cancel();
139 ui::ModalType ProfileSigninConfirmationDialogViews::GetModalType() const {
140 return ui::MODAL_TYPE_WINDOW;
143 void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged(
144 const ViewHierarchyChangedDetails& details) {
145 views::DialogDelegateView::ViewHierarchyChanged(details);
146 if (!details.is_add || details.child != this)
147 return;
149 const SkColor kPromptBarBackgroundColor =
150 ui::GetSigninConfirmationPromptBarColor(
151 ui::kSigninConfirmationPromptBarBackgroundAlpha);
153 // Create the prompt label.
154 size_t offset;
155 const base::string16 domain =
156 base::ASCIIToUTF16(gaia::ExtractDomainName(username_));
157 const base::string16 username = base::ASCIIToUTF16(username_);
158 const base::string16 prompt_text =
159 l10n_util::GetStringFUTF16(
160 IDS_ENTERPRISE_SIGNIN_ALERT_NEW_STYLE,
161 domain, &offset);
162 views::StyledLabel* prompt_label = new views::StyledLabel(prompt_text, this);
163 prompt_label->SetDisplayedOnBackgroundColor(kPromptBarBackgroundColor);
165 views::StyledLabel::RangeStyleInfo bold_style;
166 bold_style.font_style = gfx::Font::BOLD;
167 prompt_label->AddStyleRange(
168 gfx::Range(offset, offset + domain.size()), bold_style);
170 // Create the prompt bar.
171 views::View* prompt_bar = new views::View;
172 prompt_bar->SetBorder(views::Border::CreateSolidSidedBorder(
177 ui::GetSigninConfirmationPromptBarColor(
178 ui::kSigninConfirmationPromptBarBorderAlpha)));
179 prompt_bar->set_background(views::Background::CreateSolidBackground(
180 kPromptBarBackgroundColor));
182 // Create the explanation label.
183 std::vector<size_t> offsets;
184 const base::string16 learn_more_text =
185 l10n_util::GetStringUTF16(
186 IDS_ENTERPRISE_SIGNIN_PROFILE_LINK_LEARN_MORE);
187 const base::string16 signin_explanation_text =
188 l10n_util::GetStringFUTF16(prompt_for_new_profile_ ?
189 IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITH_PROFILE_CREATION_NEW_STYLE :
190 IDS_ENTERPRISE_SIGNIN_EXPLANATION_WITHOUT_PROFILE_CREATION_NEW_STYLE,
191 username, learn_more_text, &offsets);
192 explanation_label_ = new views::StyledLabel(signin_explanation_text, this);
193 explanation_label_->AddStyleRange(
194 gfx::Range(offsets[1], offsets[1] + learn_more_text.size()),
195 views::StyledLabel::RangeStyleInfo::CreateForLink());
197 // Layout the components.
198 views::GridLayout* dialog_layout = new views::GridLayout(this);
199 SetLayoutManager(dialog_layout);
201 // Use GridLayout inside the prompt bar because StyledLabel requires it.
202 views::GridLayout* prompt_layout = views::GridLayout::CreatePanel(prompt_bar);
203 prompt_bar->SetLayoutManager(prompt_layout);
204 prompt_layout->AddColumnSet(0)->AddColumn(
205 views::GridLayout::FILL, views::GridLayout::CENTER, 100,
206 views::GridLayout::USE_PREF, 0, 0);
207 prompt_layout->StartRow(0, 0);
208 prompt_layout->AddView(prompt_label);
209 // Use a column set with no padding.
210 dialog_layout->AddColumnSet(0)->AddColumn(
211 views::GridLayout::FILL, views::GridLayout::FILL, 100,
212 views::GridLayout::USE_PREF, 0, 0);
213 dialog_layout->StartRow(0, 0);
214 dialog_layout->AddView(
215 prompt_bar, 1, 1,
216 views::GridLayout::FILL, views::GridLayout::FILL, 0, 0);
218 // Use a new column set for the explanation label so we can add padding.
219 dialog_layout->AddPaddingRow(0.0, views::kPanelVertMargin);
220 views::ColumnSet* explanation_columns = dialog_layout->AddColumnSet(1);
221 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
222 explanation_columns->AddColumn(
223 views::GridLayout::FILL, views::GridLayout::FILL, 100,
224 views::GridLayout::USE_PREF, 0, 0);
225 explanation_columns->AddPaddingColumn(0.0, views::kButtonHEdgeMarginNew);
226 dialog_layout->StartRow(0, 1);
227 const int kPreferredWidth = 440;
228 dialog_layout->AddView(
229 explanation_label_, 1, 1,
230 views::GridLayout::FILL, views::GridLayout::FILL,
231 kPreferredWidth, explanation_label_->GetHeightForWidth(kPreferredWidth));
234 void ProfileSigninConfirmationDialogViews::StyledLabelLinkClicked(
235 const gfx::Range& range,
236 int event_flags) {
237 chrome::NavigateParams params(
238 browser_,
239 GURL("https://support.google.com/chromebook/answer/1331549"),
240 ui::PAGE_TRANSITION_LINK);
241 params.disposition = NEW_POPUP;
242 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
243 chrome::Navigate(&params);
246 void ProfileSigninConfirmationDialogViews::ButtonPressed(
247 views::Button* sender,
248 const ui::Event& event) {
249 DCHECK(prompt_for_new_profile_);
250 DCHECK_EQ(continue_signin_button_, sender);
251 if (delegate_) {
252 delegate_->OnContinueSignin();
253 delegate_ = NULL;
255 GetWidget()->Close();