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 10 matching lines...) Expand all Loading... | |
| 21 #include "ui/views/bubble/bubble_frame_view.h" | 21 #include "ui/views/bubble/bubble_frame_view.h" |
| 22 #include "ui/views/controls/button/blue_button.h" | 22 #include "ui/views/controls/button/blue_button.h" |
| 23 #include "ui/views/controls/button/label_button.h" | 23 #include "ui/views/controls/button/label_button.h" |
| 24 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
| 25 #include "ui/views/controls/label.h" | 25 #include "ui/views/controls/label.h" |
| 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 const int kMaxBubbleViewWidth = 362; |
| 32 TAG_ACCEPT_BUTTON = 1, | |
| 33 TAG_CANCEL_BUTTON, | |
| 34 }; | |
| 35 | |
| 36 const int kMaxBubbleViewWidth = 262; | |
| 37 | 32 |
| 38 // The vertical inset of the app bubble anchor from the app menu button. | 33 // The vertical inset of the app bubble anchor from the app menu button. |
| 39 const int kAnchorVerticalInset = 5; | 34 const int kAnchorVerticalInset = 5; |
| 40 | 35 |
| 41 const int kBubblePadding = 19; | |
| 42 | |
| 43 // Spacing between bubble text and buttons. | |
| 44 const int kLabelToButtonVerticalSpacing = 14; | |
| 45 | |
| 46 } // namespace | 36 } // namespace |
| 47 | 37 |
| 48 // GlobalErrorBubbleViewBase --------------------------------------------------- | 38 // GlobalErrorBubbleViewBase --------------------------------------------------- |
| 49 | 39 |
| 50 // static | 40 // static |
| 51 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView( | 41 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView( |
| 52 Browser* browser, | 42 Browser* browser, |
| 53 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) { | 43 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) { |
| 54 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 44 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 55 views::View* app_menu_button = browser_view->toolbar()->app_menu_button(); | 45 views::View* app_menu_button = browser_view->toolbar()->app_menu_button(); |
| 56 GlobalErrorBubbleView* bubble_view = new GlobalErrorBubbleView( | 46 GlobalErrorBubbleView* bubble_view = new GlobalErrorBubbleView( |
| 57 app_menu_button, views::BubbleBorder::TOP_RIGHT, browser, error); | 47 app_menu_button, views::BubbleBorder::TOP_RIGHT, browser, error); |
| 58 views::BubbleDelegateView::CreateBubble(bubble_view); | 48 views::BubbleDialogDelegateView::CreateBubble(bubble_view); |
| 59 bubble_view->GetWidget()->Show(); | 49 bubble_view->GetWidget()->Show(); |
| 60 return bubble_view; | 50 return bubble_view; |
| 61 } | 51 } |
| 62 | 52 |
| 63 // GlobalErrorBubbleView ------------------------------------------------------- | 53 // GlobalErrorBubbleView ------------------------------------------------------- |
| 64 | 54 |
| 65 GlobalErrorBubbleView::GlobalErrorBubbleView( | 55 GlobalErrorBubbleView::GlobalErrorBubbleView( |
| 66 views::View* anchor_view, | 56 views::View* anchor_view, |
| 67 views::BubbleBorder::Arrow arrow, | 57 views::BubbleBorder::Arrow arrow, |
| 68 Browser* browser, | 58 Browser* browser, |
| 69 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) | 59 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) |
| 70 : BubbleDelegateView(anchor_view, arrow), | 60 : BubbleDialogDelegateView(anchor_view, arrow), |
| 71 browser_(browser), | 61 browser_(browser), |
| 72 error_(error) { | 62 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. | 63 // Compensate for built-in vertical padding in the anchor view's image. |
| 78 set_anchor_view_insets( | 64 set_anchor_view_insets( |
| 79 gfx::Insets(kAnchorVerticalInset, 0, kAnchorVerticalInset, 0)); | 65 gfx::Insets(kAnchorVerticalInset, 0, kAnchorVerticalInset, 0)); |
| 80 | 66 |
| 81 std::vector<base::string16> message_strings(error_->GetBubbleViewMessages()); | 67 std::vector<base::string16> message_strings(error_->GetBubbleViewMessages()); |
| 82 std::vector<views::Label*> message_labels; | 68 std::vector<views::Label*> message_labels; |
| 83 for (size_t i = 0; i < message_strings.size(); ++i) { | 69 for (size_t i = 0; i < message_strings.size(); ++i) { |
| 84 views::Label* message_label = new views::Label(message_strings[i]); | 70 views::Label* message_label = new views::Label(message_strings[i]); |
| 85 message_label->SetMultiLine(true); | 71 message_label->SetMultiLine(true); |
| 86 message_label->SizeToFit(kMaxBubbleViewWidth); | |
| 87 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 72 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 88 message_labels.push_back(message_label); | 73 message_labels.push_back(message_label); |
| 89 } | 74 } |
| 90 | 75 |
| 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); | 76 views::GridLayout* layout = new views::GridLayout(this); |
| 113 SetLayoutManager(layout); | 77 SetLayoutManager(layout); |
| 114 | 78 |
| 115 // First row, message labels. | 79 // First row, message labels. |
| 116 views::ColumnSet* cs = layout->AddColumnSet(0); | 80 views::ColumnSet* cs = layout->AddColumnSet(0); |
| 117 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | 81 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 118 1, views::GridLayout::USE_PREF, 0, 0); | 82 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 | 83 |
| 131 for (size_t i = 0; i < message_labels.size(); ++i) { | 84 for (size_t i = 0; i < message_labels.size(); ++i) { |
| 132 layout->StartRow(1, 0); | 85 layout->StartRow(1, 0); |
| 133 layout->AddView(message_labels[i]); | 86 layout->AddView(message_labels[i]); |
| 134 if (i < message_labels.size() - 1) | 87 if (i < message_labels.size() - 1) |
| 135 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 88 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 136 } | 89 } |
| 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 | 90 |
| 148 // These bubbles show at times where activation is sporadic (like at startup, | 91 // 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 | 92 // 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. | 93 // user sees it, if the bubble needs to be acknowledged. |
| 151 set_close_on_deactivate(error_->ShouldCloseOnDeactivate()); | 94 set_close_on_deactivate(error_->ShouldCloseOnDeactivate()); |
| 152 } | 95 } |
| 153 | 96 |
| 154 GlobalErrorBubbleView::~GlobalErrorBubbleView() { | 97 GlobalErrorBubbleView::~GlobalErrorBubbleView() { |
| 155 // |elevation_icon_setter_| references |accept_button_|, so make sure it is | 98 // |elevation_icon_setter_| references |accept_button_|, so make sure it is |
| 156 // destroyed before |accept_button_|. | 99 // destroyed before |accept_button_|. |
| 157 elevation_icon_setter_.reset(); | 100 elevation_icon_setter_.reset(); |
| 158 } | 101 } |
| 159 | 102 |
| 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 { | 103 base::string16 GlobalErrorBubbleView::GetWindowTitle() const { |
| 174 return error_->GetBubbleViewTitle(); | 104 return error_->GetBubbleViewTitle(); |
| 175 } | 105 } |
| 176 | 106 |
| 177 gfx::ImageSkia GlobalErrorBubbleView::GetWindowIcon() { | 107 gfx::ImageSkia GlobalErrorBubbleView::GetWindowIcon() { |
| 178 gfx::Image image = error_->GetBubbleViewIcon(); | 108 gfx::Image image = error_->GetBubbleViewIcon(); |
| 179 DCHECK(!image.IsEmpty()); | 109 DCHECK(!image.IsEmpty()); |
| 180 return *image.ToImageSkia(); | 110 return *image.ToImageSkia(); |
| 181 } | 111 } |
| 182 | 112 |
| 183 bool GlobalErrorBubbleView::ShouldShowWindowIcon() const { | 113 bool GlobalErrorBubbleView::ShouldShowWindowIcon() const { |
| 184 return true; | 114 return true; |
| 185 } | 115 } |
| 186 | 116 |
| 187 void GlobalErrorBubbleView::WindowClosing() { | 117 void GlobalErrorBubbleView::WindowClosing() { |
| 188 if (error_) | 118 if (error_) |
| 189 error_->BubbleViewDidClose(browser_); | 119 error_->BubbleViewDidClose(browser_); |
| 190 } | 120 } |
| 191 | 121 |
| 192 bool GlobalErrorBubbleView::ShouldShowCloseButton() const { | 122 bool GlobalErrorBubbleView::ShouldShowCloseButton() const { |
| 193 return error_ && error_->ShouldShowCloseButton(); | 123 return error_ && error_->ShouldShowCloseButton(); |
| 194 } | 124 } |
| 195 | 125 |
| 126 bool GlobalErrorBubbleView::ShouldDefaultButtonBeBlue() const { | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 base::string16 GlobalErrorBubbleView::GetDialogButtonLabel( | |
| 131 ui::DialogButton button) const { | |
| 132 return button == ui::DIALOG_BUTTON_OK | |
| 133 ? error_->GetBubbleViewAcceptButtonLabel() | |
| 134 : error_->GetBubbleViewCancelButtonLabel(); | |
| 135 } | |
| 136 | |
| 137 int GlobalErrorBubbleView::GetDialogButtons() const { | |
| 138 return ui::DIALOG_BUTTON_OK | | |
| 139 (error_->GetBubbleViewCancelButtonLabel().empty() | |
| 140 ? 0 | |
| 141 : ui::DIALOG_BUTTON_CANCEL); | |
| 142 } | |
| 143 | |
| 144 bool GlobalErrorBubbleView::Cancel() { | |
| 145 error_->BubbleViewCancelButtonPressed(browser_); | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 bool GlobalErrorBubbleView::Accept() { | |
| 150 error_->BubbleViewAcceptButtonPressed(browser_); | |
| 151 return true; | |
| 152 } | |
| 153 | |
| 154 bool GlobalErrorBubbleView::Close() { | |
| 155 return true; | |
|
msw
2016/03/31 22:37:18
Why doesn't this call Cancel / Accept, like Dialog
Evan Stade
2016/04/01 20:58:15
hmm, I don't think so. ButtonPressed had a NOTREAC
| |
| 156 } | |
| 157 | |
| 196 void GlobalErrorBubbleView::CloseBubbleView() { | 158 void GlobalErrorBubbleView::CloseBubbleView() { |
| 197 GetWidget()->Close(); | 159 GetWidget()->Close(); |
| 198 } | 160 } |
| OLD | NEW |