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