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
|
// Copyright 2012 The Chromium Authors
// 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 <stddef.h>
#include <vector>
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/bubble_anchor_util.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/chrome_layout_provider.h"
#include "chrome/browser/ui/views/elevation_icon_setter.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "ui/base/buildflags.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
// GlobalErrorBubbleViewBase ---------------------------------------------------
// static
GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView(
Browser* browser,
const base::WeakPtr<GlobalErrorWithStandardBubble>& error) {
views::View* anchor_view = BrowserView::GetBrowserViewForBrowser(browser)
->toolbar_button_provider()
->GetAppMenuButton();
GlobalErrorBubbleView* bubble_view = new GlobalErrorBubbleView(
anchor_view, views::BubbleBorder::TOP_RIGHT, browser, error);
views::BubbleDialogDelegateView::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)
: BubbleDialogDelegateView(anchor_view,
arrow,
views::BubbleBorder::DIALOG_SHADOW,
/*autosize=*/true),
error_(error) {
// error_ is a WeakPtr, but it's always non-null during construction.
DCHECK(error_);
WidgetDelegate::SetTitle(error_->GetBubbleViewTitle());
WidgetDelegate::SetShowCloseButton(error_->ShouldShowCloseButton());
WidgetDelegate::RegisterWindowClosingCallback(base::BindOnce(
&GlobalErrorWithStandardBubble::BubbleViewDidClose, error_, browser));
SetDefaultButton(static_cast<int>(ui::mojom::DialogButton::kOk));
SetButtons(!error_->GetBubbleViewCancelButtonLabel().empty()
? static_cast<int>(ui::mojom::DialogButton::kCancel) |
static_cast<int>(ui::mojom::DialogButton::kOk)
: static_cast<int>(ui::mojom::DialogButton::kOk));
SetButtonLabel(ui::mojom::DialogButton::kOk,
error_->GetBubbleViewAcceptButtonLabel());
SetButtonLabel(ui::mojom::DialogButton::kCancel,
error_->GetBubbleViewCancelButtonLabel());
// Note that error is already a WeakPtr, so these callbacks will simply do
// nothing if they are invoked after its destruction.
SetAcceptCallback(base::BindOnce(
&GlobalErrorWithStandardBubble::BubbleViewAcceptButtonPressed, error,
base::Unretained(browser)));
SetCancelCallback(base::BindOnce(
&GlobalErrorWithStandardBubble::BubbleViewCancelButtonPressed, error,
base::Unretained(browser)));
if (!error_->GetBubbleViewDetailsButtonLabel().empty()) {
SetExtraView(std::make_unique<views::MdTextButton>(
base::BindRepeating(
&GlobalErrorWithStandardBubble::BubbleViewDetailsButtonPressed,
error_, browser),
error_->GetBubbleViewDetailsButtonLabel()));
}
}
GlobalErrorBubbleView::~GlobalErrorBubbleView() = default;
void GlobalErrorBubbleView::Init() {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical, gfx::Insets(),
ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL)));
for (const auto& message_string : error_->GetBubbleViewMessages()) {
auto* message_label =
AddChildView(std::make_unique<views::Label>(message_string));
message_label->SetMultiLine(true);
message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message_label->SetMaximumWidth(362);
}
// 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.
// |error_| is assumed to be valid, and stay valid, at least until Init()
// returns.
set_close_on_deactivate(error_->ShouldCloseOnDeactivate());
}
void GlobalErrorBubbleView::OnWidgetInitialized() {
views::LabelButton* ok_button = GetOkButton();
if (ok_button && error_ && error_->ShouldAddElevationIconToAcceptButton()) {
elevation_icon_setter_ = std::make_unique<ElevationIconSetter>(ok_button);
}
}
void GlobalErrorBubbleView::CloseBubbleView() {
GetWidget()->Close();
}
BEGIN_METADATA(GlobalErrorBubbleView)
END_METADATA
|