| 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/global_error_bubble_view.h" | 5 #include "chrome/browser/ui/views/global_error_bubble_view.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/ui/global_error/global_error.h" | 12 #include "chrome/browser/ui/global_error/global_error.h" |
| 13 #include "chrome/browser/ui/global_error/global_error_service.h" | 13 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 15 #include "chrome/browser/ui/layout_constants.h" | 15 #include "chrome/browser/ui/layout_constants.h" |
| 16 #include "chrome/browser/ui/views/elevation_icon_setter.h" | 16 #include "chrome/browser/ui/views/elevation_icon_setter.h" |
| 17 #include "chrome/browser/ui/views/frame/browser_view.h" | 17 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 18 #include "chrome/browser/ui/views/toolbar/app_menu_button.h" | 18 #include "chrome/browser/ui/views/toolbar/app_menu_button.h" |
| 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 22 #include "ui/views/bubble/bubble_frame_view.h" | 22 #include "ui/views/bubble/bubble_frame_view.h" |
| 23 #include "ui/views/controls/button/blue_button.h" | 23 #include "ui/views/controls/button/blue_button.h" |
| 24 #include "ui/views/controls/button/label_button.h" | 24 #include "ui/views/controls/button/label_button.h" |
| 25 #include "ui/views/controls/image_view.h" | 25 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/label.h" | 26 #include "ui/views/controls/label.h" |
| 27 #include "ui/views/layout/grid_layout.h" | 27 #include "ui/views/layout/grid_layout.h" |
| 28 #include "ui/views/layout/layout_constants.h" | 28 #include "ui/views/layout/layout_constants.h" |
| 29 #include "ui/views/window/dialog_client_view.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 enum { | 33 const int kMaxBubbleViewWidth = 362; |
| 33 TAG_ACCEPT_BUTTON = 1, | |
| 34 TAG_CANCEL_BUTTON, | |
| 35 }; | |
| 36 | |
| 37 const int kMaxBubbleViewWidth = 262; | |
| 38 | |
| 39 const int kBubblePadding = 19; | |
| 40 | |
| 41 // Spacing between bubble text and buttons. | |
| 42 const int kLabelToButtonVerticalSpacing = 14; | |
| 43 | 34 |
| 44 } // namespace | 35 } // namespace |
| 45 | 36 |
| 46 // GlobalErrorBubbleViewBase --------------------------------------------------- | 37 // GlobalErrorBubbleViewBase --------------------------------------------------- |
| 47 | 38 |
| 48 // static | 39 // static |
| 49 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView( | 40 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView( |
| 50 Browser* browser, | 41 Browser* browser, |
| 51 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) { | 42 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) { |
| 52 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 43 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 53 views::View* app_menu_button = browser_view->toolbar()->app_menu_button(); | 44 views::View* app_menu_button = browser_view->toolbar()->app_menu_button(); |
| 54 GlobalErrorBubbleView* bubble_view = new GlobalErrorBubbleView( | 45 GlobalErrorBubbleView* bubble_view = new GlobalErrorBubbleView( |
| 55 app_menu_button, views::BubbleBorder::TOP_RIGHT, browser, error); | 46 app_menu_button, views::BubbleBorder::TOP_RIGHT, browser, error); |
| 56 views::BubbleDelegateView::CreateBubble(bubble_view); | 47 views::BubbleDialogDelegateView::CreateBubble(bubble_view); |
| 57 bubble_view->GetWidget()->Show(); | 48 bubble_view->GetWidget()->Show(); |
| 58 return bubble_view; | 49 return bubble_view; |
| 59 } | 50 } |
| 60 | 51 |
| 61 // GlobalErrorBubbleView ------------------------------------------------------- | 52 // GlobalErrorBubbleView ------------------------------------------------------- |
| 62 | 53 |
| 63 GlobalErrorBubbleView::GlobalErrorBubbleView( | 54 GlobalErrorBubbleView::GlobalErrorBubbleView( |
| 64 views::View* anchor_view, | 55 views::View* anchor_view, |
| 65 views::BubbleBorder::Arrow arrow, | 56 views::BubbleBorder::Arrow arrow, |
| 66 Browser* browser, | 57 Browser* browser, |
| 67 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) | 58 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) |
| 68 : BubbleDelegateView(anchor_view, arrow), | 59 : BubbleDialogDelegateView(anchor_view, arrow), |
| 69 browser_(browser), | 60 browser_(browser), |
| 70 error_(error) { | 61 error_(error) {} |
| 71 // Set content margins to left-align the bubble text with the title. | |
| 72 // BubbleFrameView adds enough padding below title, no top padding needed. | |
| 73 set_margins(gfx::Insets(0, kBubblePadding, kBubblePadding, kBubblePadding)); | |
| 74 | 62 |
| 63 GlobalErrorBubbleView::~GlobalErrorBubbleView() {} |
| 64 |
| 65 base::string16 GlobalErrorBubbleView::GetWindowTitle() const { |
| 66 return error_->GetBubbleViewTitle(); |
| 67 } |
| 68 |
| 69 gfx::ImageSkia GlobalErrorBubbleView::GetWindowIcon() { |
| 70 gfx::Image image = error_->GetBubbleViewIcon(); |
| 71 DCHECK(!image.IsEmpty()); |
| 72 return *image.ToImageSkia(); |
| 73 } |
| 74 |
| 75 bool GlobalErrorBubbleView::ShouldShowWindowIcon() const { |
| 76 return true; |
| 77 } |
| 78 |
| 79 void GlobalErrorBubbleView::WindowClosing() { |
| 80 if (error_) |
| 81 error_->BubbleViewDidClose(browser_); |
| 82 } |
| 83 |
| 84 void GlobalErrorBubbleView::Init() { |
| 75 // Compensate for built-in vertical padding in the anchor view's image. | 85 // Compensate for built-in vertical padding in the anchor view's image. |
| 76 set_anchor_view_insets(gfx::Insets( | 86 set_anchor_view_insets(gfx::Insets( |
| 77 GetLayoutConstant(LOCATION_BAR_BUBBLE_ANCHOR_VERTICAL_INSET), 0)); | 87 GetLayoutConstant(LOCATION_BAR_BUBBLE_ANCHOR_VERTICAL_INSET), 0)); |
| 78 | 88 |
| 79 std::vector<base::string16> message_strings(error_->GetBubbleViewMessages()); | 89 std::vector<base::string16> message_strings(error_->GetBubbleViewMessages()); |
| 80 std::vector<views::Label*> message_labels; | 90 std::vector<views::Label*> message_labels; |
| 81 for (size_t i = 0; i < message_strings.size(); ++i) { | 91 for (size_t i = 0; i < message_strings.size(); ++i) { |
| 82 views::Label* message_label = new views::Label(message_strings[i]); | 92 views::Label* message_label = new views::Label(message_strings[i]); |
| 83 message_label->SetMultiLine(true); | 93 message_label->SetMultiLine(true); |
| 84 message_label->SizeToFit(kMaxBubbleViewWidth); | |
| 85 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 94 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 86 message_labels.push_back(message_label); | 95 message_labels.push_back(message_label); |
| 87 } | 96 } |
| 88 | 97 |
| 89 base::string16 accept_string(error_->GetBubbleViewAcceptButtonLabel()); | |
| 90 scoped_ptr<views::BlueButton> accept_button( | |
| 91 new views::BlueButton(this, accept_string)); | |
| 92 accept_button->SetIsDefault(true); | |
| 93 accept_button->set_tag(TAG_ACCEPT_BUTTON); | |
| 94 | |
| 95 if (error_->ShouldAddElevationIconToAcceptButton()) | |
| 96 elevation_icon_setter_.reset( | |
| 97 new ElevationIconSetter( | |
| 98 accept_button.get(), | |
| 99 base::Bind(&GlobalErrorBubbleView::SizeToContents, | |
| 100 base::Unretained(this)))); | |
| 101 | |
| 102 base::string16 cancel_string(error_->GetBubbleViewCancelButtonLabel()); | |
| 103 scoped_ptr<views::LabelButton> cancel_button; | |
| 104 if (!cancel_string.empty()) { | |
| 105 cancel_button.reset(new views::LabelButton(this, cancel_string)); | |
| 106 cancel_button->SetStyle(views::Button::STYLE_BUTTON); | |
| 107 cancel_button->set_tag(TAG_CANCEL_BUTTON); | |
| 108 } | |
| 109 | |
| 110 views::GridLayout* layout = new views::GridLayout(this); | 98 views::GridLayout* layout = new views::GridLayout(this); |
| 111 SetLayoutManager(layout); | 99 SetLayoutManager(layout); |
| 112 | 100 |
| 113 // First row, message labels. | 101 // First row, message labels. |
| 114 views::ColumnSet* cs = layout->AddColumnSet(0); | 102 views::ColumnSet* cs = layout->AddColumnSet(0); |
| 115 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | 103 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 116 1, views::GridLayout::USE_PREF, 0, 0); | 104 views::GridLayout::FIXED, kMaxBubbleViewWidth, 0); |
| 117 | |
| 118 // Second row, accept and cancel button. | |
| 119 cs = layout->AddColumnSet(1); | |
| 120 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | |
| 121 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, | |
| 122 0, views::GridLayout::USE_PREF, 0, 0); | |
| 123 if (cancel_button.get()) { | |
| 124 cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing); | |
| 125 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, | |
| 126 0, views::GridLayout::USE_PREF, 0, 0); | |
| 127 } | |
| 128 | 105 |
| 129 for (size_t i = 0; i < message_labels.size(); ++i) { | 106 for (size_t i = 0; i < message_labels.size(); ++i) { |
| 130 layout->StartRow(1, 0); | 107 layout->StartRow(1, 0); |
| 131 layout->AddView(message_labels[i]); | 108 layout->AddView(message_labels[i]); |
| 132 if (i < message_labels.size() - 1) | 109 if (i < message_labels.size() - 1) |
| 133 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 110 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 134 } | 111 } |
| 135 layout->AddPaddingRow(0, kLabelToButtonVerticalSpacing); | |
| 136 | |
| 137 layout->StartRow(0, 1); | |
| 138 layout->AddView(accept_button.release()); | |
| 139 if (cancel_button.get()) | |
| 140 layout->AddView(cancel_button.release()); | |
| 141 | |
| 142 // Adjust the message label size in case buttons are too long. | |
| 143 for (size_t i = 0; i < message_labels.size(); ++i) | |
| 144 message_labels[i]->SizeToFit(layout->GetPreferredSize(this).width()); | |
| 145 | 112 |
| 146 // These bubbles show at times where activation is sporadic (like at startup, | 113 // These bubbles show at times where activation is sporadic (like at startup, |
| 147 // or a new window opening). Make sure the bubble doesn't disappear before the | 114 // or a new window opening). Make sure the bubble doesn't disappear before the |
| 148 // user sees it, if the bubble needs to be acknowledged. | 115 // user sees it, if the bubble needs to be acknowledged. |
| 149 set_close_on_deactivate(error_->ShouldCloseOnDeactivate()); | 116 set_close_on_deactivate(error_->ShouldCloseOnDeactivate()); |
| 150 } | 117 } |
| 151 | 118 |
| 152 GlobalErrorBubbleView::~GlobalErrorBubbleView() { | 119 void GlobalErrorBubbleView::UpdateButton(views::LabelButton* button, |
| 153 // |elevation_icon_setter_| references |accept_button_|, so make sure it is | 120 ui::DialogButton type) { |
| 154 // destroyed before |accept_button_|. | 121 BubbleDialogDelegateView::UpdateButton(button, type); |
| 155 elevation_icon_setter_.reset(); | 122 if (type == ui::DIALOG_BUTTON_OK && |
| 156 } | 123 error_->ShouldAddElevationIconToAcceptButton()) { |
| 157 | 124 elevation_icon_setter_.reset(new ElevationIconSetter( |
| 158 void GlobalErrorBubbleView::ButtonPressed(views::Button* sender, | 125 button, base::Bind(&GlobalErrorBubbleView::SizeToContents, |
| 159 const ui::Event& event) { | 126 base::Unretained(this)))); |
| 160 if (error_) { | |
| 161 if (sender->tag() == TAG_ACCEPT_BUTTON) | |
| 162 error_->BubbleViewAcceptButtonPressed(browser_); | |
| 163 else if (sender->tag() == TAG_CANCEL_BUTTON) | |
| 164 error_->BubbleViewCancelButtonPressed(browser_); | |
| 165 else | |
| 166 NOTREACHED(); | |
| 167 } | 127 } |
| 168 GetWidget()->Close(); | |
| 169 } | |
| 170 | |
| 171 base::string16 GlobalErrorBubbleView::GetWindowTitle() const { | |
| 172 return error_->GetBubbleViewTitle(); | |
| 173 } | |
| 174 | |
| 175 gfx::ImageSkia GlobalErrorBubbleView::GetWindowIcon() { | |
| 176 gfx::Image image = error_->GetBubbleViewIcon(); | |
| 177 DCHECK(!image.IsEmpty()); | |
| 178 return *image.ToImageSkia(); | |
| 179 } | |
| 180 | |
| 181 bool GlobalErrorBubbleView::ShouldShowWindowIcon() const { | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 void GlobalErrorBubbleView::WindowClosing() { | |
| 186 if (error_) | |
| 187 error_->BubbleViewDidClose(browser_); | |
| 188 } | 128 } |
| 189 | 129 |
| 190 bool GlobalErrorBubbleView::ShouldShowCloseButton() const { | 130 bool GlobalErrorBubbleView::ShouldShowCloseButton() const { |
| 191 return error_ && error_->ShouldShowCloseButton(); | 131 return error_ && error_->ShouldShowCloseButton(); |
| 192 } | 132 } |
| 193 | 133 |
| 134 bool GlobalErrorBubbleView::ShouldDefaultButtonBeBlue() const { |
| 135 return true; |
| 136 } |
| 137 |
| 138 base::string16 GlobalErrorBubbleView::GetDialogButtonLabel( |
| 139 ui::DialogButton button) const { |
| 140 return button == ui::DIALOG_BUTTON_OK |
| 141 ? error_->GetBubbleViewAcceptButtonLabel() |
| 142 : error_->GetBubbleViewCancelButtonLabel(); |
| 143 } |
| 144 |
| 145 int GlobalErrorBubbleView::GetDialogButtons() const { |
| 146 return ui::DIALOG_BUTTON_OK | |
| 147 (error_->GetBubbleViewCancelButtonLabel().empty() |
| 148 ? 0 |
| 149 : ui::DIALOG_BUTTON_CANCEL); |
| 150 } |
| 151 |
| 152 bool GlobalErrorBubbleView::Cancel() { |
| 153 error_->BubbleViewCancelButtonPressed(browser_); |
| 154 return true; |
| 155 } |
| 156 |
| 157 bool GlobalErrorBubbleView::Accept() { |
| 158 error_->BubbleViewAcceptButtonPressed(browser_); |
| 159 return true; |
| 160 } |
| 161 |
| 162 bool GlobalErrorBubbleView::Close() { |
| 163 // Don't fall through to either Cancel() or Accept(). |
| 164 return true; |
| 165 } |
| 166 |
| 194 void GlobalErrorBubbleView::CloseBubbleView() { | 167 void GlobalErrorBubbleView::CloseBubbleView() { |
| 195 GetWidget()->Close(); | 168 GetWidget()->Close(); |
| 196 } | 169 } |
| OLD | NEW |