This source file includes following definitions.
- size_override_
- Close
- GetPreferredSize
- Close
- CancelHidingAnimation
#include "ash/popup_message.h"
#include "ash/wm/window_animations.h"
#include "grit/ash_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/insets.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
const int kMessageTopBottomMargin = 10;
const int kMessageLeftRightMargin = 10;
const int kMessageMinHeight = 29 - 2 * kMessageTopBottomMargin;
const SkColor kMessageTextColor = SkColorSetRGB(0x22, 0x22, 0x22);
const int kMessageMaxWidth = 250;
const int kArrowOffsetLeftRight = 11;
const int kArrowOffsetTopBottom = 7;
const int kHorizontalPopupPaddingBetweenItems = 10;
const int kVerticalPopupPaddingBetweenItems = 10;
}
class PopupMessage::MessageBubble : public views::BubbleDelegateView {
public:
MessageBubble(const base::string16& caption,
const base::string16& message,
IconType message_type,
views::View* anchor,
views::BubbleBorder::Arrow arrow_orientation,
const gfx::Size& size_override,
int arrow_offset);
void Close();
private:
virtual gfx::Size GetPreferredSize() OVERRIDE;
gfx::Size size_override_;
DISALLOW_COPY_AND_ASSIGN(MessageBubble);
};
PopupMessage::MessageBubble::MessageBubble(const base::string16& caption,
const base::string16& message,
IconType message_type,
views::View* anchor,
views::BubbleBorder::Arrow arrow,
const gfx::Size& size_override,
int arrow_offset)
: views::BubbleDelegateView(anchor, arrow),
size_override_(size_override) {
gfx::Insets insets = gfx::Insets(kArrowOffsetTopBottom,
kArrowOffsetLeftRight,
kArrowOffsetTopBottom,
kArrowOffsetLeftRight);
if (anchor->border())
insets += anchor->border()->GetInsets();
set_anchor_view_insets(insets);
set_close_on_esc(false);
set_close_on_deactivate(false);
set_use_focusless(true);
set_accept_events(false);
set_margins(gfx::Insets(kMessageTopBottomMargin, kMessageLeftRightMargin,
kMessageTopBottomMargin, kMessageLeftRightMargin));
set_shadow(views::BubbleBorder::SMALL_SHADOW);
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
kHorizontalPopupPaddingBetweenItems));
if (message_type != ICON_NONE) {
views::ImageView* icon = new views::ImageView();
icon->SetImage(
bundle.GetImageNamed(IDR_AURA_WARNING_ICON).ToImageSkia());
icon->SetVerticalAlignment(views::ImageView::LEADING);
AddChildView(icon);
}
views::View* details = new views::View();
AddChildView(details);
details->SetLayoutManager(new views::BoxLayout(
views::BoxLayout::kVertical, 0, 0, kVerticalPopupPaddingBetweenItems));
if (!caption.empty()) {
views::Label* caption_label = new views::Label(caption);
caption_label->SetMultiLine(true);
caption_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
caption_label->SetFontList(
bundle.GetFontList(ui::ResourceBundle::BoldFont));
caption_label->SetEnabledColor(kMessageTextColor);
details->AddChildView(caption_label);
}
if (!message.empty()) {
views::Label* message_label = new views::Label(message);
message_label->SetMultiLine(true);
message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message_label->SetEnabledColor(kMessageTextColor);
details->AddChildView(message_label);
}
views::BubbleDelegateView::CreateBubble(this);
if (arrow_offset) {
GetBubbleFrameView()->bubble_border()->set_arrow_offset(arrow_offset);
GetBubbleFrameView()->bubble_border()->set_arrow(arrow);
SetAlignment(views::BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR);
}
}
void PopupMessage::MessageBubble::Close() {
if (GetWidget())
GetWidget()->Close();
}
gfx::Size PopupMessage::MessageBubble::GetPreferredSize() {
gfx::Size pref_size = views::BubbleDelegateView::GetPreferredSize();
if (size_override_.height())
pref_size.set_height(size_override_.height());
else if (pref_size.height() < kMessageMinHeight)
pref_size.set_height(kMessageMinHeight);
if (size_override_.width())
pref_size.set_width(size_override_.width());
else if (pref_size.width() > kMessageMaxWidth)
pref_size.set_width(kMessageMaxWidth);
return pref_size;
}
PopupMessage::PopupMessage(const base::string16& caption,
const base::string16& message,
IconType message_type,
views::View* anchor,
views::BubbleBorder::Arrow arrow,
const gfx::Size& size_override,
int arrow_offset)
: view_(NULL) {
view_ = new MessageBubble(
caption, message, message_type, anchor, arrow, size_override,
arrow_offset);
widget_ = view_->GetWidget();
gfx::NativeView native_view = widget_->GetNativeView();
wm::SetWindowVisibilityAnimationType(
native_view, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
wm::SetWindowVisibilityAnimationTransition(
native_view, wm::ANIMATE_HIDE);
view_->GetWidget()->Show();
}
PopupMessage::~PopupMessage() {
CancelHidingAnimation();
Close();
}
void PopupMessage::Close() {
if (view_) {
view_->Close();
view_ = NULL;
widget_ = NULL;
}
}
void PopupMessage::CancelHidingAnimation() {
if (!widget_ || !widget_->GetNativeView())
return;
gfx::NativeView native_view = widget_->GetNativeView();
wm::SetWindowVisibilityAnimationTransition(
native_view, wm::ANIMATE_NONE);
}
}