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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/bubble/info_bubble.h"
#include <memory>
#include <utility>
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
// The visible width of bubble borders (differs from the actual width) in px.
constexpr int kBubbleBorderVisibleWidth = 1;
} // namespace
class InfoBubbleFrame : public BubbleFrameView {
public:
explicit InfoBubbleFrame(const gfx::Insets& content_margins)
: BubbleFrameView(gfx::Insets(), content_margins) {}
InfoBubbleFrame(const InfoBubbleFrame&) = delete;
InfoBubbleFrame& operator=(const InfoBubbleFrame&) = delete;
~InfoBubbleFrame() override = default;
gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) const override {
return available_bounds_;
}
void set_available_bounds(const gfx::Rect& available_bounds) {
available_bounds_ = available_bounds;
}
private:
// Bounds that this frame should try to keep bubbles within (screen coords).
gfx::Rect available_bounds_;
};
InfoBubble::InfoBubble(View* anchor,
BubbleBorder::Arrow arrow,
const std::u16string& message)
: BubbleDialogDelegateView(anchor,
arrow,
views::BubbleBorder::DIALOG_SHADOW,
true) {
DialogDelegate::SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
set_margins(LayoutProvider::Get()->GetInsetsMetric(
InsetsMetric::INSETS_TOOLTIP_BUBBLE));
SetCanActivate(false);
SetAccessibleWindowRole(ax::mojom::Role::kAlertDialog);
// TODO(pbos): This hacks around a bug where focus order in the parent dialog
// breaks because it tries to focus InfoBubble without anything focusable in
// it. FocusSearch should handle this case and this should be removable.
set_focus_traversable_from_anchor_view(false);
SetLayoutManager(std::make_unique<FillLayout>());
label_ = AddChildView(std::make_unique<Label>(message));
label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label_->SetMultiLine(true);
}
InfoBubble::~InfoBubble() = default;
void InfoBubble::Show() {
BubbleDialogDelegateView::CreateBubble(this);
UpdatePosition();
}
void InfoBubble::Hide() {
Widget* widget = GetWidget();
if (widget && !widget->IsClosed()) {
widget->Close();
}
}
std::unique_ptr<NonClientFrameView> InfoBubble::CreateNonClientFrameView(
Widget* widget) {
DCHECK(!frame_);
auto frame = std::make_unique<InfoBubbleFrame>(margins());
frame->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen());
auto border = std::make_unique<BubbleBorder>(arrow(), GetShadow());
border->SetColor(background_color());
frame->SetBubbleBorder(std::move(border));
frame_ = frame.get();
return frame;
}
gfx::Size InfoBubble::CalculatePreferredSize(
const SizeBounds& available_size) const {
if (preferred_width_ == 0) {
return BubbleDialogDelegateView::CalculatePreferredSize(available_size);
}
int pref_width = preferred_width_;
pref_width -= frame_->GetInsets().width();
pref_width -= 2 * kBubbleBorderVisibleWidth;
return gfx::Size(pref_width, GetLayoutManager()->GetPreferredHeightForWidth(
this, pref_width));
}
void InfoBubble::OnWidgetBoundsChanged(Widget* widget,
const gfx::Rect& new_bounds) {
BubbleDialogDelegateView::OnWidgetBoundsChanged(widget, new_bounds);
if (anchor_widget() == widget) {
frame_->set_available_bounds(widget->GetWindowBoundsInScreen());
}
}
void InfoBubble::UpdatePosition() {
Widget* const widget = GetWidget();
if (!widget) {
return;
}
if (anchor_widget()->IsVisible() &&
!GetAnchorView()->GetVisibleBounds().IsEmpty()) {
widget->SetVisibilityChangedAnimationsEnabled(true);
widget->ShowInactive();
} else {
widget->SetVisibilityChangedAnimationsEnabled(false);
widget->Hide();
}
}
BEGIN_METADATA(InfoBubble)
END_METADATA
} // namespace views
|