| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/sync/one_click_signin_bubble_view.h" | 5 #include "chrome/browser/ui/views/sync/one_click_signin_bubble_view.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/google/google_util.h" | 10 #include "chrome/browser/google/google_util.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 13 #include "grit/chromium_strings.h" | 14 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "grit/theme_resources.h" |
| 17 #include "grit/ui_resources.h" |
| 15 #include "ui/base/keycodes/keyboard_codes.h" | 18 #include "ui/base/keycodes/keyboard_codes.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/views/controls/button/image_button.h" |
| 18 #include "ui/views/controls/button/text_button.h" | 22 #include "ui/views/controls/button/text_button.h" |
| 23 #include "ui/views/controls/image_view.h" |
| 19 #include "ui/views/controls/label.h" | 24 #include "ui/views/controls/label.h" |
| 20 #include "ui/views/controls/link.h" | 25 #include "ui/views/controls/link.h" |
| 21 #include "ui/views/layout/grid_layout.h" | 26 #include "ui/views/layout/grid_layout.h" |
| 22 #include "ui/views/layout/layout_constants.h" | 27 #include "ui/views/layout/layout_constants.h" |
| 23 #include "ui/views/widget/widget.h" | 28 #include "ui/views/widget/widget.h" |
| 24 | 29 |
| 25 // Minimum width for the mutli-line label. | 30 // Minimum width for the mutli-line label. |
| 31 const int kMinimumDialogLabelWidth = 400; |
| 26 const int kMinimumLabelWidth = 240; | 32 const int kMinimumLabelWidth = 240; |
| 33 const int kDialogMargin = 16; |
| 34 |
| 35 namespace { |
| 36 |
| 37 // The column set constants that can be used in the InitContent() function |
| 38 // to layout views. |
| 39 enum OneClickSigninBubbleColumnTypes { |
| 40 COLUMN_SET_FILL_ALIGN, |
| 41 COLUMN_SET_CONTROLS, |
| 42 COLUMN_SET_TITLE_BAR |
| 43 }; |
| 44 |
| 45 class OneClickSigninDialogView : public OneClickSigninBubbleView { |
| 46 public: |
| 47 OneClickSigninDialogView( |
| 48 content::WebContents* web_content, |
| 49 views::View* anchor_view, |
| 50 const BrowserWindow::StartSyncCallback& start_sync_callback); |
| 51 |
| 52 private: |
| 53 // Overridden from views::WidgetDelegate: |
| 54 virtual ui::ModalType GetModalType() const OVERRIDE; |
| 55 |
| 56 // Overridden from OneClickSigninBubbleView: |
| 57 virtual void InitContent(views::GridLayout* layout) OVERRIDE; |
| 58 virtual void GetButtons(views::TextButton** ok_button, |
| 59 views::TextButton** undo_button) OVERRIDE; |
| 60 virtual views::Link* GetAdvancedLink() OVERRIDE; |
| 61 |
| 62 // Overridden from views::LinkListener: |
| 63 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; |
| 64 |
| 65 content::WebContents* web_content_; |
| 66 views::Link* learn_more_link_; |
| 67 views::ImageButton* close_button_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(OneClickSigninDialogView); |
| 70 }; |
| 71 |
| 72 OneClickSigninDialogView::OneClickSigninDialogView( |
| 73 content::WebContents* web_content, |
| 74 views::View* anchor_view, |
| 75 const BrowserWindow::StartSyncCallback& start_sync_callback) |
| 76 : OneClickSigninBubbleView(anchor_view, start_sync_callback), |
| 77 web_content_(web_content), |
| 78 learn_more_link_(NULL), |
| 79 close_button_(NULL) { |
| 80 set_arrow_location(views::BubbleBorder::NONE); |
| 81 set_anchor_insets(gfx::Insets(0, 0, anchor_view->height() / 2, 0)); |
| 82 set_close_on_deactivate(false); |
| 83 set_margins(gfx::Insets(kDialogMargin, kDialogMargin, kDialogMargin, |
| 84 kDialogMargin)); |
| 85 } |
| 86 |
| 87 ui::ModalType OneClickSigninDialogView::GetModalType() const { |
| 88 return ui::MODAL_TYPE_CHILD; |
| 89 } |
| 90 |
| 91 void OneClickSigninDialogView::InitContent(views::GridLayout* layout) { |
| 92 // Column set for title bar. |
| 93 views::ColumnSet* cs = layout->AddColumnSet(COLUMN_SET_TITLE_BAR); |
| 94 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 95 views::GridLayout::USE_PREF, 0, 0); |
| 96 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); |
| 97 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, |
| 98 views::GridLayout::USE_PREF, 0, 0); |
| 99 |
| 100 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 101 |
| 102 { |
| 103 layout->StartRow(0, COLUMN_SET_TITLE_BAR); |
| 104 |
| 105 views::Label* label = new views::Label( |
| 106 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_DIALOG_TITLE)); |
| 107 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 108 label->SetFont(label->font().DeriveFont(3, gfx::Font::BOLD)); |
| 109 layout->AddView(label); |
| 110 |
| 111 close_button_ = new views::ImageButton(this); |
| 112 close_button_->SetImage(views::ImageButton::STATE_NORMAL, |
| 113 rb.GetImageNamed(IDR_CLOSE_BAR).ToImageSkia()); |
| 114 close_button_->SetImage(views::ImageButton::STATE_HOVERED, |
| 115 rb.GetImageNamed(IDR_CLOSE_BAR_H).ToImageSkia()); |
| 116 close_button_->SetImage(views::ImageButton::STATE_PRESSED, |
| 117 rb.GetImageNamed(IDR_CLOSE_BAR_P).ToImageSkia()); |
| 118 layout->AddView(close_button_); |
| 119 } |
| 120 |
| 121 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 122 |
| 123 { |
| 124 layout->StartRow(0, COLUMN_SET_FILL_ALIGN); |
| 125 |
| 126 views::Label* label = new views::Label( |
| 127 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_DIALOG_MESSAGE)); |
| 128 label->SetMultiLine(true); |
| 129 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 130 label->SizeToFit(kMinimumDialogLabelWidth); |
| 131 layout->AddView(label); |
| 132 |
| 133 layout->StartRow(0, COLUMN_SET_FILL_ALIGN); |
| 134 |
| 135 learn_more_link_ = new views::Link( |
| 136 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); |
| 137 learn_more_link_->set_listener(this); |
| 138 learn_more_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 139 layout->AddView(learn_more_link_, 1, 1, views::GridLayout::TRAILING, |
| 140 views::GridLayout::CENTER); |
| 141 } |
| 142 |
| 143 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); |
| 144 } |
| 145 |
| 146 void OneClickSigninDialogView::GetButtons(views::TextButton** ok_button, |
| 147 views::TextButton** undo_button) { |
| 148 *ok_button = new views::NativeTextButton(this); |
| 149 *undo_button = new views::NativeTextButton(this); |
| 150 |
| 151 // The default size of the buttons is too large. To allow them to be smaller |
| 152 // ignore the minimum default size. Furthermore, to make sure they are the |
| 153 // same size, SetText() is called with both strings on both buttons. |
| 154 (*ok_button)->set_ignore_minimum_size(true); |
| 155 (*undo_button)->set_ignore_minimum_size(true); |
| 156 string16 ok_label = |
| 157 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_DIALOG_OK_BUTTON); |
| 158 string16 undo_label = |
| 159 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_DIALOG_UNDO_BUTTON); |
| 160 (*ok_button)->SetText(undo_label); |
| 161 (*ok_button)->SetText(ok_label); |
| 162 (*undo_button)->SetText(ok_label); |
| 163 (*undo_button)->SetText(undo_label); |
| 164 } |
| 165 |
| 166 views::Link* OneClickSigninDialogView::GetAdvancedLink() { |
| 167 views::Link* advanced_link= new views::Link( |
| 168 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_DIALOG_ADVANCED)); |
| 169 advanced_link->set_listener(this); |
| 170 advanced_link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 171 return advanced_link; |
| 172 } |
| 173 |
| 174 void OneClickSigninDialogView::LinkClicked(views::Link* source, |
| 175 int event_flags) { |
| 176 if (source == learn_more_link_) { |
| 177 content::OpenURLParams params( |
| 178 GURL(chrome::kChromeSyncLearnMoreURL), content::Referrer(), |
| 179 NEW_WINDOW, content::PAGE_TRANSITION_LINK, false); |
| 180 web_content_->OpenURL(params); |
| 181 return; |
| 182 } |
| 183 |
| 184 OneClickSigninBubbleView::LinkClicked(source, event_flags); |
| 185 } |
| 186 |
| 187 } // namespace |
| 27 | 188 |
| 28 // OneClickSigninBubbleView ---------------------------------------------------- | 189 // OneClickSigninBubbleView ---------------------------------------------------- |
| 29 | 190 |
| 30 // static | 191 // static |
| 31 OneClickSigninBubbleView* OneClickSigninBubbleView::bubble_view_ = NULL; | 192 OneClickSigninBubbleView* OneClickSigninBubbleView::bubble_view_ = NULL; |
| 32 | 193 |
| 33 // static | 194 // static |
| 34 void OneClickSigninBubbleView::ShowBubble( | 195 void OneClickSigninBubbleView::ShowBubble( |
| 35 views::View* anchor_view, | 196 BrowserWindow::OneClickSigninBubbleType type, |
| 197 ToolbarView* toolbar_view, |
| 36 const BrowserWindow::StartSyncCallback& start_sync) { | 198 const BrowserWindow::StartSyncCallback& start_sync) { |
| 37 if (IsShowing()) | 199 if (IsShowing()) |
| 38 return; | 200 return; |
| 39 | 201 |
| 40 bubble_view_ = | 202 bubble_view_ = type == BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE ? |
| 41 new OneClickSigninBubbleView(anchor_view, start_sync); | 203 new OneClickSigninBubbleView(toolbar_view->app_menu(), start_sync) : |
| 204 new OneClickSigninDialogView(toolbar_view->GetWebContents(), |
| 205 toolbar_view->location_bar(), start_sync); |
| 206 |
| 42 views::BubbleDelegateView::CreateBubble(bubble_view_); | 207 views::BubbleDelegateView::CreateBubble(bubble_view_); |
| 43 bubble_view_->Show(); | 208 bubble_view_->Show(); |
| 44 } | 209 } |
| 45 | 210 |
| 46 // static | 211 // static |
| 47 bool OneClickSigninBubbleView::IsShowing() { | 212 bool OneClickSigninBubbleView::IsShowing() { |
| 48 return bubble_view_ != NULL; | 213 return bubble_view_ != NULL; |
| 49 } | 214 } |
| 50 | 215 |
| 51 // static | 216 // static |
| (...skipping 21 matching lines...) Expand all Loading... |
| 73 views::BubbleDelegateView::AnimationEnded(animation); | 238 views::BubbleDelegateView::AnimationEnded(animation); |
| 74 if (message_loop_for_testing_) | 239 if (message_loop_for_testing_) |
| 75 message_loop_for_testing_->Quit(); | 240 message_loop_for_testing_->Quit(); |
| 76 } | 241 } |
| 77 | 242 |
| 78 void OneClickSigninBubbleView::Init() { | 243 void OneClickSigninBubbleView::Init() { |
| 79 views::GridLayout* layout = new views::GridLayout(this); | 244 views::GridLayout* layout = new views::GridLayout(this); |
| 80 SetLayoutManager(layout); | 245 SetLayoutManager(layout); |
| 81 set_border(views::Border::CreateEmptyBorder(8, 8, 8, 8)); | 246 set_border(views::Border::CreateEmptyBorder(8, 8, 8, 8)); |
| 82 | 247 |
| 83 enum { | 248 // Column set for descriptive text and link. |
| 84 kColumnSetFillAlign, | 249 views::ColumnSet* cs = layout->AddColumnSet(COLUMN_SET_FILL_ALIGN); |
| 85 kColumnSetControls | 250 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 1, |
| 86 }; | 251 views::GridLayout::USE_PREF, 0, 0); |
| 87 | 252 |
| 88 // Column set for descriptive text and link. | 253 // Column set for buttons at bottom of bubble. |
| 89 views::ColumnSet* cs = layout->AddColumnSet(kColumnSetFillAlign); | 254 cs = layout->AddColumnSet(COLUMN_SET_CONTROLS); |
| 90 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0, | |
| 91 views::GridLayout::USE_PREF, 0, kMinimumLabelWidth); | |
| 92 | |
| 93 cs = layout->AddColumnSet(kColumnSetControls); | |
| 94 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 255 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 95 views::GridLayout::USE_PREF, 0, 0); | 256 views::GridLayout::USE_PREF, 0, 0); |
| 96 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); | 257 cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing); |
| 97 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, | 258 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, |
| 98 views::GridLayout::USE_PREF, 0, 0); | 259 views::GridLayout::USE_PREF, 0, 0); |
| 99 cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing); | 260 cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing); |
| 100 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, | 261 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0, |
| 101 views::GridLayout::USE_PREF, 0, 0); | 262 views::GridLayout::USE_PREF, 0, 0); |
| 102 | 263 |
| 264 InitContent(layout); |
| 265 |
| 266 // Add controls at the bottom. |
| 267 advanced_link_= GetAdvancedLink(); |
| 268 GetButtons(&ok_button_, &undo_button_); |
| 269 ok_button_->SetIsDefault(true); |
| 270 |
| 271 layout->StartRow(0, COLUMN_SET_CONTROLS); |
| 272 layout->AddView(advanced_link_); |
| 273 layout->AddView(ok_button_); |
| 274 layout->AddView(undo_button_); |
| 275 |
| 276 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, 0)); |
| 277 } |
| 278 |
| 279 void OneClickSigninBubbleView::InitContent(views::GridLayout* layout) { |
| 103 // Add main text description. | 280 // Add main text description. |
| 104 views::Label* label = new views::Label( | 281 views::Label* label = new views::Label( |
| 105 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_BUBBLE_MESSAGE)); | 282 l10n_util::GetStringUTF16(IDS_ONE_CLICK_SIGNIN_BUBBLE_MESSAGE)); |
| 106 label->SetMultiLine(true); | 283 label->SetMultiLine(true); |
| 107 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 284 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 108 label->SizeToFit(kMinimumLabelWidth); | 285 label->SizeToFit(kMinimumLabelWidth); |
| 109 | 286 |
| 110 layout->StartRow(0, kColumnSetFillAlign); | 287 layout->StartRow(0, COLUMN_SET_FILL_ALIGN); |
| 111 layout->AddView(label); | 288 layout->AddView(label); |
| 112 | 289 |
| 113 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); | 290 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing); |
| 291 } |
| 114 | 292 |
| 115 // Add link for user to do advanced config of sync. | 293 void OneClickSigninBubbleView::GetButtons(views::TextButton** ok_button, |
| 116 advanced_link_= new views::Link( | 294 views::TextButton** undo_button) { |
| 117 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); | 295 *ok_button = new views::NativeTextButton(this); |
| 118 advanced_link_->set_listener(this); | 296 *undo_button = new views::NativeTextButton(this); |
| 119 advanced_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 120 | |
| 121 // Add controls at the bottom. | |
| 122 ok_button_ = new views::NativeTextButton(this); | |
| 123 ok_button_->SetIsDefault(true); | |
| 124 | |
| 125 undo_button_ = new views::NativeTextButton(this); | |
| 126 | 297 |
| 127 // The default size of the buttons is too large. To allow them to be smaller | 298 // The default size of the buttons is too large. To allow them to be smaller |
| 128 // ignore the minimum default size. Furthermore, to make sure they are the | 299 // ignore the minimum default size. Furthermore, to make sure they are the |
| 129 // same size, SetText() is called with both strings on both buttons. | 300 // same size, SetText() is called with both strings on both buttons. |
| 130 ok_button_->set_ignore_minimum_size(true); | 301 (*ok_button)->set_ignore_minimum_size(true); |
| 131 undo_button_->set_ignore_minimum_size(true); | 302 (*undo_button)->set_ignore_minimum_size(true); |
| 132 string16 ok_label = l10n_util::GetStringUTF16(IDS_OK); | 303 string16 ok_label = l10n_util::GetStringUTF16(IDS_OK); |
| 133 string16 undo_label = l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO); | 304 string16 undo_label = l10n_util::GetStringUTF16(IDS_ONE_CLICK_BUBBLE_UNDO); |
| 134 ok_button_->SetText(undo_label); | 305 (*ok_button)->SetText(undo_label); |
| 135 ok_button_->SetText(ok_label); | 306 (*ok_button)->SetText(ok_label); |
| 136 undo_button_->SetText(ok_label); | 307 (*undo_button)->SetText(ok_label); |
| 137 undo_button_->SetText(undo_label); | 308 (*undo_button)->SetText(undo_label); |
| 309 } |
| 138 | 310 |
| 139 layout->StartRow(0, kColumnSetControls); | 311 views::Link* OneClickSigninBubbleView::GetAdvancedLink() { |
| 140 layout->AddView(advanced_link_); | 312 views::Link* advanced_link= new views::Link( |
| 141 layout->AddView(ok_button_); | 313 l10n_util::GetStringUTF16(IDS_SYNC_PROMO_NTP_BUBBLE_ADVANCED)); |
| 142 layout->AddView(undo_button_); | 314 advanced_link->set_listener(this); |
| 143 | 315 advanced_link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 144 AddAccelerator(ui::Accelerator(ui::VKEY_RETURN, 0)); | 316 return advanced_link; |
| 145 } | 317 } |
| 146 | 318 |
| 147 void OneClickSigninBubbleView::WindowClosing() { | 319 void OneClickSigninBubbleView::WindowClosing() { |
| 148 // We have to reset |bubble_view_| here, not in our destructor, because | 320 // We have to reset |bubble_view_| here, not in our destructor, because |
| 149 // we'll be destroyed asynchronously and the shown state will be checked | 321 // we'll be destroyed asynchronously and the shown state will be checked |
| 150 // before then. | 322 // before then. |
| 151 DCHECK_EQ(bubble_view_, this); | 323 DCHECK_EQ(bubble_view_, this); |
| 152 bubble_view_ = NULL; | 324 bubble_view_ = NULL; |
| 153 | 325 |
| 154 if (!start_sync_callback_.is_null()) { | 326 if (!start_sync_callback_.is_null()) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 185 void OneClickSigninBubbleView::ButtonPressed(views::Button* sender, | 357 void OneClickSigninBubbleView::ButtonPressed(views::Button* sender, |
| 186 const ui::Event& event) { | 358 const ui::Event& event) { |
| 187 StartFade(false); | 359 StartFade(false); |
| 188 if (ok_button_ == sender) { | 360 if (ok_button_ == sender) { |
| 189 base::ResetAndReturn(&start_sync_callback_).Run( | 361 base::ResetAndReturn(&start_sync_callback_).Run( |
| 190 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); | 362 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); |
| 191 } else { | 363 } else { |
| 192 start_sync_callback_.Reset(); | 364 start_sync_callback_.Reset(); |
| 193 } | 365 } |
| 194 } | 366 } |
| OLD | NEW |