Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1332)

Side by Side Diff: ash/popup_message.cc

Issue 15329005: Adding new general bubble error message consisting of icon, message and caption. Using this message… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "ash/popup_message.h"
6
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/controls/image_view.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/widget/widget.h"
16
17 namespace ash {
18 namespace {
19 const int kMessageTopBottomMargin = 10;
20 const int kMessageLeftRightMargin = 10;
21 const int kMessageAppearanceDelay = 200; // msec
22 const int kMessageMinHeight = 29 - 2 * kMessageTopBottomMargin;
23 const SkColor kMessageTextColor = SkColorSetRGB(0x22, 0x22, 0x22);
24
25 // The maximum width of the Message bubble. Borrowed the value from
26 // ash/Message/Message_controller.cc
27 const int kMessageMaxWidth = 250;
28
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;
34
35 // The number of pixels between the icon and the text.
36 const int kHorizontalPopupPaddingBetweenItems = 10;
37
38 // The number of pixels between the text items.
39 const int kVerticalPopupPaddingBetweenItems = 10;
40 } // namespace
41
42 // The implementation of Message of the launcher.
43 class PopupMessage::MessageBubble : public views::BubbleDelegateView {
44 public:
45 MessageBubble(const base::string16& caption,
46 const base::string16& message,
47 IconType message_type,
48 views::View* anchor,
49 views::BubbleBorder::Arrow arrow_orientation,
50 const gfx::Size& size_override,
51 int arrow_offset);
52
53 void Close();
54
55 private:
56 // views::View overrides:
57 virtual gfx::Size GetPreferredSize() OVERRIDE;
58
59 // Each component (width/height) can force a size override for that component
60 // if not 0.
61 gfx::Size size_override_;
62
63 DISALLOW_COPY_AND_ASSIGN(MessageBubble);
64 };
65
66 PopupMessage::MessageBubble::MessageBubble(
67 const base::string16& caption,
68 const base::string16& message,
69 IconType message_type,
70 views::View* anchor,
71 views::BubbleBorder::Arrow arrow,
72 const gfx::Size& size_override,
73 int arrow_offset)
74 : views::BubbleDelegateView(anchor, arrow),
75 size_override_(size_override) {
76 gfx::Insets insets = gfx::Insets(kArrowOffsetTopBottom,
77 kArrowOffsetLeftRight,
78 kArrowOffsetTopBottom,
79 kArrowOffsetLeftRight);
80 // An anchor can have an asymmetrical border for spacing reasons. Adjust the
81 // anchor location for this.
82 if (anchor->border())
83 insets += anchor->border()->GetInsets();
84
85 set_anchor_view_insets(insets);
86 set_close_on_esc(false);
87 set_close_on_deactivate(false);
88 set_use_focusless(true);
89 set_accept_events(false);
90 set_margins(gfx::Insets(kMessageTopBottomMargin, kMessageLeftRightMargin,
91 kMessageTopBottomMargin, kMessageLeftRightMargin));
92 if (arrow_offset)
93 SetArrowOffset(arrow_offset);
94 set_shadow(views::BubbleBorder::SMALL_SHADOW);
95
96 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
97 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
98 kHorizontalPopupPaddingBetweenItems));
99
100 // Add the icon to the first column (if there is one).
101 if (message_type != ICON_NONE) {
102 views::ImageView* icon = new views::ImageView();
103 icon->SetImage(
104 bundle.GetImageNamed(IDR_AURA_WARNING_ICON).ToImageSkia());
105 icon->SetVerticalAlignment(views::ImageView::LEADING);
106 AddChildView(icon);
107 }
108
109 // Create a container for the text items and use it as second column.
110 views::View* details = new views::View();
111 AddChildView(details);
112 details->SetLayoutManager(new views::BoxLayout(
113 views::BoxLayout::kVertical, 0, 0, kVerticalPopupPaddingBetweenItems));
114
115 // The caption label.
116 if (!caption.empty()) {
117 views::Label* caption_label = new views::Label(caption);
118 caption_label->SetMultiLine(true);
119 caption_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
120 caption_label->SetFont(bundle.GetFont(ui::ResourceBundle::BoldFont));
121 caption_label->SetEnabledColor(kMessageTextColor);
122 details->AddChildView(caption_label);
123 }
124
125 // The message label.
126 if (!message.empty()) {
127 views::Label* message_label = new views::Label(message);
128 message_label->SetMultiLine(true);
129 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
130 message_label->SetEnabledColor(kMessageTextColor);
131 details->AddChildView(message_label);
132 }
133 views::BubbleDelegateView::CreateBubble(this);
134 }
135
136 void PopupMessage::MessageBubble::Close() {
137 if (GetWidget())
138 GetWidget()->Close();
139 }
140
141 gfx::Size PopupMessage::MessageBubble::GetPreferredSize() {
142 gfx::Size pref_size = views::BubbleDelegateView::GetPreferredSize();
143 // Override the size with either the provided size or adjust it to not
144 // violate our minimum / maximum sizes.
145 if (size_override_.height())
146 pref_size.set_height(size_override_.height());
147 else if (pref_size.height() < kMessageMinHeight)
148 pref_size.set_height(kMessageMinHeight);
149
150 if (size_override_.width())
151 pref_size.set_width(size_override_.width());
152 else if (pref_size.width() > kMessageMaxWidth)
153 pref_size.set_width(kMessageMaxWidth);
154
155 return pref_size;
156 }
157
158 PopupMessage::PopupMessage(const base::string16& caption,
159 const base::string16& message,
160 IconType message_type,
161 views::View* anchor,
162 views::BubbleBorder::Arrow arrow,
163 const gfx::Size& size_override,
164 int arrow_offset)
165 : view_(NULL) {
166 view_ = new MessageBubble(
167 caption, message, message_type, anchor, arrow, size_override,
168 arrow_offset);
169 widget_ = view_->GetWidget();
170
171 gfx::NativeView native_view = widget_->GetNativeView();
172 views::corewm::SetWindowVisibilityAnimationType(
173 native_view, views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
174 views::corewm::SetWindowVisibilityAnimationTransition(
175 native_view, views::corewm::ANIMATE_HIDE);
176 view_->GetWidget()->Show();
177 }
178
179 PopupMessage::~PopupMessage() {
180 CancelHidingAnimation();
181 Close();
182 }
183
184 void PopupMessage::Close() {
185 if (view_) {
186 view_->Close();
187 view_ = NULL;
188 widget_ = NULL;
189 }
190 }
191
192 void PopupMessage::CancelHidingAnimation() {
193 if (!widget_ || !widget_->GetNativeView())
194 return;
195
196 gfx::NativeView native_view = widget_->GetNativeView();
197 views::corewm::SetWindowVisibilityAnimationTransition(
198 native_view, views::corewm::ANIMATE_NONE);
199 }
200
201 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698