1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/global_error_bubble_view.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/global_error/global_error.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/browser/ui/views/elevation_icon_setter.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
namespace {
enum {
TAG_ACCEPT_BUTTON = 1,
TAG_CANCEL_BUTTON,
};
const int kMaxBubbleViewWidth = 262;
// The horizontal padding between the title and the icon.
const int kTitleHorizontalPadding = 5;
// The vertical inset of the wrench bubble anchor from the wrench menu button.
const int kAnchorVerticalInset = 5;
const int kBubblePadding = 6;
} // namespace
// GlobalErrorBubbleViewBase ---------------------------------------------------
// static
GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView(
Browser* browser,
const base::WeakPtr<GlobalErrorWithStandardBubble>& error) {
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
views::View* wrench_button = browser_view->toolbar()->app_menu();
GlobalErrorBubbleView* bubble_view =
new GlobalErrorBubbleView(wrench_button,
views::BubbleBorder::TOP_RIGHT,
browser,
error);
views::BubbleDelegateView::CreateBubble(bubble_view);
bubble_view->GetWidget()->Show();
return bubble_view;
}
// GlobalErrorBubbleView -------------------------------------------------------
GlobalErrorBubbleView::GlobalErrorBubbleView(
views::View* anchor_view,
views::BubbleBorder::Arrow arrow,
Browser* browser,
const base::WeakPtr<GlobalErrorWithStandardBubble>& error)
: BubbleDelegateView(anchor_view, arrow),
browser_(browser),
error_(error) {
// Compensate for built-in vertical padding in the anchor view's image.
set_anchor_view_insets(
gfx::Insets(kAnchorVerticalInset, 0, kAnchorVerticalInset, 0));
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
gfx::Image image = error_->GetBubbleViewIcon();
CHECK(!image.IsEmpty());
scoped_ptr<views::ImageView> image_view(new views::ImageView());
image_view->SetImage(image.ToImageSkia());
base::string16 title_string(error_->GetBubbleViewTitle());
scoped_ptr<views::Label> title_label(new views::Label(title_string));
title_label->SetMultiLine(true);
title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title_label->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
std::vector<base::string16> message_strings(error_->GetBubbleViewMessages());
std::vector<views::Label*> message_labels;
for (size_t i = 0; i < message_strings.size(); ++i) {
views::Label* message_label = new views::Label(message_strings[i]);
message_label->SetMultiLine(true);
message_label->SizeToFit(kMaxBubbleViewWidth);
message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message_labels.push_back(message_label);
}
base::string16 accept_string(error_->GetBubbleViewAcceptButtonLabel());
scoped_ptr<views::LabelButton> accept_button(
new views::LabelButton(this, accept_string));
accept_button->SetStyle(views::Button::STYLE_BUTTON);
accept_button->SetIsDefault(true);
accept_button->set_tag(TAG_ACCEPT_BUTTON);
if (error_->ShouldAddElevationIconToAcceptButton())
elevation_icon_setter_.reset(new ElevationIconSetter(accept_button.get()));
base::string16 cancel_string(error_->GetBubbleViewCancelButtonLabel());
scoped_ptr<views::LabelButton> cancel_button;
if (!cancel_string.empty()) {
cancel_button.reset(new views::LabelButton(this, cancel_string));
cancel_button->SetStyle(views::Button::STYLE_BUTTON);
cancel_button->set_tag(TAG_CANCEL_BUTTON);
}
views::GridLayout* layout = new views::GridLayout(this);
SetLayoutManager(layout);
layout->SetInsets(kBubblePadding, kBubblePadding,
kBubblePadding, kBubblePadding);
// Top row, icon and title.
views::ColumnSet* cs = layout->AddColumnSet(0);
cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::LEADING,
0, views::GridLayout::USE_PREF, 0, 0);
cs->AddPaddingColumn(0, kTitleHorizontalPadding);
cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
1, views::GridLayout::USE_PREF, 0, 0);
// Middle rows, message labels.
cs = layout->AddColumnSet(1);
cs->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
1, views::GridLayout::USE_PREF, 0, 0);
// Bottom row, accept and cancel button.
cs = layout->AddColumnSet(2);
cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing);
cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING,
0, views::GridLayout::USE_PREF, 0, 0);
if (cancel_button.get()) {
cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing);
cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING,
0, views::GridLayout::USE_PREF, 0, 0);
}
layout->StartRow(1, 0);
layout->AddView(image_view.release());
layout->AddView(title_label.release());
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
for (size_t i = 0; i < message_labels.size(); ++i) {
layout->StartRow(1, 1);
layout->AddView(message_labels[i]);
if (i < message_labels.size() - 1)
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
}
layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing);
layout->StartRow(0, 2);
layout->AddView(accept_button.release());
if (cancel_button.get())
layout->AddView(cancel_button.release());
// Adjust the message label size in case buttons are too long.
for (size_t i = 0; i < message_labels.size(); ++i)
message_labels[i]->SizeToFit(layout->GetPreferredSize(this).width());
// These bubbles show at times where activation is sporadic (like at startup,
// or a new window opening). Make sure the bubble doesn't disappear before the
// user sees it, if the bubble needs to be acknowledged.
set_close_on_deactivate(error_->ShouldCloseOnDeactivate());
}
GlobalErrorBubbleView::~GlobalErrorBubbleView() {
// |elevation_icon_setter_| references |accept_button_|, so make sure it is
// destroyed before |accept_button_|.
elevation_icon_setter_.reset();
}
void GlobalErrorBubbleView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
if (error_) {
if (sender->tag() == TAG_ACCEPT_BUTTON)
error_->BubbleViewAcceptButtonPressed(browser_);
else if (sender->tag() == TAG_CANCEL_BUTTON)
error_->BubbleViewCancelButtonPressed(browser_);
else
NOTREACHED();
}
GetWidget()->Close();
}
void GlobalErrorBubbleView::WindowClosing() {
if (error_)
error_->BubbleViewDidClose(browser_);
}
void GlobalErrorBubbleView::CloseBubbleView() {
GetWidget()->Close();
}
|