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