1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/popup_message.h"
7 #include "ash/wm/window_animations.h"
8 #include "grit/ash_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/insets.h"
11 #include "ui/views/bubble/bubble_delegate.h"
12 #include "ui/views/bubble/bubble_frame_view.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/box_layout.h"
16 #include "ui/views/widget/widget.h"
20 const int kMessageTopBottomMargin
= 10;
21 const int kMessageLeftRightMargin
= 10;
22 const int kMessageMinHeight
= 29 - 2 * kMessageTopBottomMargin
;
23 const SkColor kMessageTextColor
= SkColorSetRGB(0x22, 0x22, 0x22);
25 // The maximum width of the Message bubble. Borrowed the value from
26 // ash/message/message_controller.cc
27 const int kMessageMaxWidth
= 250;
29 // The offset for the Message bubble - making sure that the bubble is flush
30 // with the shelf. The offset includes the arrow size in pixels as well as
31 // the activation bar and other spacing elements.
32 const int kArrowOffsetLeftRight
= 11;
33 const int kArrowOffsetTopBottom
= 7;
35 // The number of pixels between the icon and the text.
36 const int kHorizontalPopupPaddingBetweenItems
= 10;
38 // The number of pixels between the text items.
39 const int kVerticalPopupPaddingBetweenItems
= 10;
42 // The implementation of Message of the launcher.
43 class PopupMessage::MessageBubble
: public views::BubbleDelegateView
{
45 MessageBubble(const base::string16
& caption
,
46 const base::string16
& message
,
47 IconType message_type
,
49 views::BubbleBorder::Arrow arrow_orientation
,
50 const gfx::Size
& size_override
,
56 // views::View overrides:
57 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
59 // Each component (width/height) can force a size override for that component
61 gfx::Size size_override_
;
63 DISALLOW_COPY_AND_ASSIGN(MessageBubble
);
66 PopupMessage::MessageBubble::MessageBubble(const base::string16
& caption
,
67 const base::string16
& message
,
68 IconType message_type
,
70 views::BubbleBorder::Arrow arrow
,
71 const gfx::Size
& size_override
,
73 : views::BubbleDelegateView(anchor
, arrow
),
74 size_override_(size_override
) {
75 gfx::Insets insets
= gfx::Insets(kArrowOffsetTopBottom
,
76 kArrowOffsetLeftRight
,
77 kArrowOffsetTopBottom
,
78 kArrowOffsetLeftRight
);
79 // An anchor can have an asymmetrical border for spacing reasons. Adjust the
80 // anchor location for this.
82 insets
+= anchor
->border()->GetInsets();
84 set_anchor_view_insets(insets
);
85 set_close_on_esc(false);
86 set_close_on_deactivate(false);
87 set_use_focusless(true);
88 set_accept_events(false);
90 set_margins(gfx::Insets(kMessageTopBottomMargin
, kMessageLeftRightMargin
,
91 kMessageTopBottomMargin
, kMessageLeftRightMargin
));
92 set_shadow(views::BubbleBorder::SMALL_SHADOW
);
94 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
95 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0,
96 kHorizontalPopupPaddingBetweenItems
));
98 // Here is the layout:
99 // arrow_offset (if not 0)
102 // +-------------------------------------------------+
104 // icon | [!] Caption in bold which can be multi line | caption_label
106 // | Message text which can be multi line | message_label
109 // +-------------------------------------------------+
110 // |------------details container--------------|
111 // Note that the icon, caption and massage are optional.
113 // Add the icon to the first column (if there is one).
114 if (message_type
!= ICON_NONE
) {
115 views::ImageView
* icon
= new views::ImageView();
117 bundle
.GetImageNamed(IDR_AURA_WARNING_ICON
).ToImageSkia());
118 icon
->SetVerticalAlignment(views::ImageView::LEADING
);
122 // Create a container for the text items and use it as second column.
123 views::View
* details
= new views::View();
124 AddChildView(details
);
125 details
->SetLayoutManager(new views::BoxLayout(
126 views::BoxLayout::kVertical
, 0, 0, kVerticalPopupPaddingBetweenItems
));
128 // The caption label.
129 if (!caption
.empty()) {
130 views::Label
* caption_label
= new views::Label(caption
);
131 caption_label
->SetMultiLine(true);
132 caption_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
133 caption_label
->SetFontList(
134 bundle
.GetFontList(ui::ResourceBundle::BoldFont
));
135 caption_label
->SetEnabledColor(kMessageTextColor
);
136 details
->AddChildView(caption_label
);
139 // The message label.
140 if (!message
.empty()) {
141 views::Label
* message_label
= new views::Label(message
);
142 message_label
->SetMultiLine(true);
143 message_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
144 message_label
->SetEnabledColor(kMessageTextColor
);
145 details
->AddChildView(message_label
);
147 views::BubbleDelegateView::CreateBubble(this);
149 // Change the arrow offset if needed.
151 // With the creation of the bubble, the bubble got already placed (and
152 // possibly re-oriented to fit on the screen). Since it is not possible to
153 // set the arrow offset before the creation, we need to set the offset,
154 // and the orientation variables again and force a re-placement.
155 GetBubbleFrameView()->bubble_border()->set_arrow_offset(arrow_offset
);
156 GetBubbleFrameView()->bubble_border()->set_arrow(arrow
);
157 SetAlignment(views::BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR
);
161 void PopupMessage::MessageBubble::Close() {
163 GetWidget()->Close();
166 gfx::Size
PopupMessage::MessageBubble::GetPreferredSize() {
167 gfx::Size pref_size
= views::BubbleDelegateView::GetPreferredSize();
168 // Override the size with either the provided size or adjust it to not
169 // violate our minimum / maximum sizes.
170 if (size_override_
.height())
171 pref_size
.set_height(size_override_
.height());
172 else if (pref_size
.height() < kMessageMinHeight
)
173 pref_size
.set_height(kMessageMinHeight
);
175 if (size_override_
.width())
176 pref_size
.set_width(size_override_
.width());
177 else if (pref_size
.width() > kMessageMaxWidth
)
178 pref_size
.set_width(kMessageMaxWidth
);
183 PopupMessage::PopupMessage(const base::string16
& caption
,
184 const base::string16
& message
,
185 IconType message_type
,
187 views::BubbleBorder::Arrow arrow
,
188 const gfx::Size
& size_override
,
191 view_
= new MessageBubble(
192 caption
, message
, message_type
, anchor
, arrow
, size_override
,
194 widget_
= view_
->GetWidget();
196 gfx::NativeView native_view
= widget_
->GetNativeView();
197 wm::SetWindowVisibilityAnimationType(
198 native_view
, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL
);
199 wm::SetWindowVisibilityAnimationTransition(
200 native_view
, wm::ANIMATE_HIDE
);
201 view_
->GetWidget()->Show();
204 PopupMessage::~PopupMessage() {
205 CancelHidingAnimation();
209 void PopupMessage::Close() {
217 void PopupMessage::CancelHidingAnimation() {
218 if (!widget_
|| !widget_
->GetNativeView())
221 gfx::NativeView native_view
= widget_
->GetNativeView();
222 wm::SetWindowVisibilityAnimationTransition(
223 native_view
, wm::ANIMATE_NONE
);