This source file includes following definitions.
- GetAvailableScreenBounds
- set_available_bounds
- show_above_anchor_
- Show
- Hide
- UpdatePosition
- CreateNonClientFrameView
- GetPreferredSize
- OnWidgetDestroyed
- OnWidgetBoundsChanged
#include "chrome/browser/ui/views/autofill/info_bubble.h"
#include "base/i18n/rtl.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
namespace autofill {
namespace {
const int kBubbleBorderVisibleWidth = 1;
const int kInfoBubbleHorizontalMargin = 14;
const int kInfoBubbleVerticalMargin = 12;
}
class InfoBubbleFrame : public views::BubbleFrameView {
public:
explicit InfoBubbleFrame(const gfx::Insets& content_margins)
: views::BubbleFrameView(content_margins) {}
virtual ~InfoBubbleFrame() {}
virtual gfx::Rect GetAvailableScreenBounds(const gfx::Rect& rect) OVERRIDE {
return available_bounds_;
}
void set_available_bounds(const gfx::Rect& available_bounds) {
available_bounds_ = available_bounds;
}
private:
gfx::Rect available_bounds_;
DISALLOW_COPY_AND_ASSIGN(InfoBubbleFrame);
};
InfoBubble::InfoBubble(views::View* anchor,
const base::string16& message)
: anchor_(anchor),
frame_(NULL),
align_to_anchor_edge_(false),
preferred_width_(233),
show_above_anchor_(false) {
DCHECK(anchor_);
SetAnchorView(anchor_);
set_margins(gfx::Insets(kInfoBubbleVerticalMargin,
kInfoBubbleHorizontalMargin,
kInfoBubbleVerticalMargin,
kInfoBubbleHorizontalMargin));
set_use_focusless(true);
SetLayoutManager(new views::FillLayout);
views::Label* label = new views::Label(message);
label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label->SetMultiLine(true);
AddChildView(label);
}
InfoBubble::~InfoBubble() {}
void InfoBubble::Show() {
if (show_above_anchor_)
set_arrow(views::BubbleBorder::vertical_mirror(arrow()));
widget_ = views::BubbleDelegateView::CreateBubble(this);
if (align_to_anchor_edge_) {
frame_->bubble_border()->set_arrow(arrow());
SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE);
}
UpdatePosition();
}
void InfoBubble::Hide() {
views::Widget* widget = GetWidget();
if (widget && !widget->IsClosed())
widget->Close();
}
void InfoBubble::UpdatePosition() {
if (!widget_)
return;
if (!anchor_->GetVisibleBounds().IsEmpty()) {
SizeToContents();
widget_->SetVisibilityChangedAnimationsEnabled(true);
widget_->ShowInactive();
} else {
widget_->SetVisibilityChangedAnimationsEnabled(false);
widget_->Hide();
}
}
views::NonClientFrameView* InfoBubble::CreateNonClientFrameView(
views::Widget* widget) {
DCHECK(!frame_);
frame_ = new InfoBubbleFrame(margins());
frame_->set_available_bounds(anchor_widget()->GetWindowBoundsInScreen());
frame_->SetBubbleBorder(scoped_ptr<views::BubbleBorder>(
new views::BubbleBorder(arrow(), shadow(), color())));
return frame_;
}
gfx::Size InfoBubble::GetPreferredSize() {
int pref_width = preferred_width_;
pref_width -= frame_->GetInsets().width();
pref_width -= 2 * kBubbleBorderVisibleWidth;
return gfx::Size(pref_width, GetHeightForWidth(pref_width));
}
void InfoBubble::OnWidgetDestroyed(views::Widget* widget) {
if (widget == widget_)
widget_ = NULL;
}
void InfoBubble::OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) {
views::BubbleDelegateView::OnWidgetBoundsChanged(widget, new_bounds);
if (anchor_widget() == widget)
frame_->set_available_bounds(widget->GetWindowBoundsInScreen());
}
}