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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/views/notification_view.h"
#include "base/command_line.h"
#include "base/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/text/text_elider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/size.h"
#include "ui/message_center/message_center_constants.h"
#include "ui/message_center/message_center_switches.h"
#include "ui/message_center/message_center_util.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_change_observer.h"
#include "ui/message_center/notification_types.h"
#include "ui/message_center/views/message_simple_view.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
namespace {
// Dimensions.
const int kIconColumnWidth = message_center::kNotificationIconSize;
const int kIconToTextPadding = 16;
const int kTextTopPadding = 6;
const int kTextLeftPadding = kIconColumnWidth + kIconToTextPadding;
const int kTextBottomPadding = 6;
const int kTextRightPadding = 23;
const int kItemTitleToMessagePadding = 3;
const int kButtonHeight = 38;
const int kButtonHorizontalPadding = 16;
const int kButtonVecticalPadding = 0;
const int kButtonIconTopPadding = 11;
const int kButtonIconToTitlePadding = 16;
const int kButtonTitleTopPadding = 0;
// Notification colors. The text background colors below are used only to keep
// view::Label from modifying the text color and will not actually be drawn.
// See view::Label's SetEnabledColor() and SetBackgroundColor() for details.
const SkColor kBackgroundColor = SkColorSetRGB(255, 255, 255);
const SkColor kTitleColor = SkColorSetRGB(68, 68, 68);
const SkColor kTitleBackgroundColor = SK_ColorWHITE;
const SkColor kMessageColor = SkColorSetRGB(136, 136, 136);
const SkColor kMessageBackgroundColor = SK_ColorBLACK;
const SkColor kButtonSeparatorColor = SkColorSetRGB(234, 234, 234);
// Static.
views::Border* MakeBorder(int top,
int bottom,
int left = kTextLeftPadding,
int right = kTextRightPadding,
SkColor color = 0x00000000) {
return (color == 0x00000000) ?
views::Border::CreateEmptyBorder(top, left, bottom, right) :
views::Border::CreateSolidSidedBorder(top, left, bottom, right, color);
}
// ContainerView ///////////////////////////////////////////////////////////////
// ContainerViews are vertical BoxLayout views that propagates their childrens'
// ChildPreferredSizeChanged() and ChildVisibilityChanged() calls.
class ContainerView : public views::View {
public:
ContainerView();
virtual ~ContainerView();
protected:
virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
virtual void ChildVisibilityChanged(View* child) OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(ContainerView);
};
ContainerView::ContainerView() {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
}
ContainerView::~ContainerView() {
}
void ContainerView::ChildPreferredSizeChanged(View* child) {
PreferredSizeChanged();
}
void ContainerView::ChildVisibilityChanged(View* child) {
PreferredSizeChanged();
}
// ItemView ////////////////////////////////////////////////////////////////////
// ItemViews are responsible for drawing each list notification item's title and
// message next to each other within a single column.
class ItemView : public views::View {
public:
ItemView(const message_center::NotificationItem& item);
virtual ~ItemView();
virtual void SetVisible(bool visible) OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(ItemView);
};
ItemView::ItemView(const message_center::NotificationItem& item) {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
0, 0, kItemTitleToMessagePadding));
views::Label* title = new views::Label(item.title);
title->set_collapse_when_hidden(true);
title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title->SetElideBehavior(views::Label::ELIDE_AT_END);
title->SetEnabledColor(kTitleColor);
title->SetBackgroundColor(kTitleBackgroundColor);
AddChildView(title);
views::Label* message = new views::Label(item.message);
message->set_collapse_when_hidden(true);
message->SetHorizontalAlignment(gfx::ALIGN_LEFT);
message->SetElideBehavior(views::Label::ELIDE_AT_END);
message->SetEnabledColor(kMessageColor);
message->SetBackgroundColor(kMessageBackgroundColor);
AddChildView(message);
PreferredSizeChanged();
SchedulePaint();
}
ItemView::~ItemView() {
}
void ItemView::SetVisible(bool visible) {
views::View::SetVisible(visible);
for (int i = 0; i < child_count(); ++i)
child_at(i)->SetVisible(visible);
}
// ProportionalImageView ///////////////////////////////////////////////////////
// ProportionalImageViews center their images to preserve their proportion.
class ProportionalImageView : public views::View {
public:
ProportionalImageView(const gfx::ImageSkia& image);
virtual ~ProportionalImageView();
// Overridden from views::View:
virtual gfx::Size GetPreferredSize() OVERRIDE;
virtual int GetHeightForWidth(int width) OVERRIDE;
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
private:
gfx::Size GetImageSizeForWidth(int width);
gfx::ImageSkia image_;
DISALLOW_COPY_AND_ASSIGN(ProportionalImageView);
};
ProportionalImageView::ProportionalImageView(const gfx::ImageSkia& image)
: image_(image) {
}
ProportionalImageView::~ProportionalImageView() {
}
gfx::Size ProportionalImageView::GetPreferredSize() {
gfx::Size size = GetImageSizeForWidth(image_.width());
return gfx::Size(size.width() + GetInsets().width(),
size.height() + GetInsets().height());
}
int ProportionalImageView::GetHeightForWidth(int width) {
return GetImageSizeForWidth(width).height();
}
void ProportionalImageView::OnPaint(gfx::Canvas* canvas) {
views::View::OnPaint(canvas);
gfx::Size draw_size(GetImageSizeForWidth(width()));
if (!draw_size.IsEmpty()) {
gfx::Rect draw_bounds = GetLocalBounds();
draw_bounds.Inset(GetInsets());
draw_bounds.ClampToCenteredSize(draw_size);
gfx::Size image_size(image_.size());
if (image_size == draw_size) {
canvas->DrawImageInt(image_, draw_bounds.x(), draw_bounds.y());
} else {
// Resize case
SkPaint paint;
paint.setFilterBitmap(true);
canvas->DrawImageInt(image_, 0, 0,
image_size.width(), image_size.height(),
draw_bounds.x(), draw_bounds.y(),
draw_size.width(), draw_size.height(),
true, paint);
}
}
}
gfx::Size ProportionalImageView::GetImageSizeForWidth(int width) {
gfx::Size size = visible() ? image_.size() : gfx::Size();
if (width > 0 && !size.IsEmpty()) {
double proportion = size.height() / (double) size.width();
size.SetSize(width, std::max(0.5 + width * proportion, 1.0));
if (size.height() > message_center::kNotificationMaximumImageHeight) {
int height = message_center::kNotificationMaximumImageHeight;
size.SetSize(std::max(0.5 + height / proportion, 1.0), height);
}
}
return size;
}
// NotificationButton //////////////////////////////////////////////////////////
// NotificationButtons render the action buttons of notifications.
class NotificationButton : public views::CustomButton {
public:
NotificationButton(views::ButtonListener* listener);
virtual ~NotificationButton();
void SetIcon(const gfx::ImageSkia& icon);
void SetTitle(const string16& title);
// Overridden from views::View:
virtual gfx::Size GetPreferredSize() OVERRIDE;
virtual int GetHeightForWidth(int width) OVERRIDE;
private:
views::ImageView* icon_;
views::Label* title_;
};
NotificationButton::NotificationButton(views::ButtonListener* listener)
: views::CustomButton(listener),
icon_(NULL),
title_(NULL) {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
kButtonHorizontalPadding,
kButtonVecticalPadding,
kButtonIconToTitlePadding));
}
NotificationButton::~NotificationButton() {
}
void NotificationButton::SetIcon(const gfx::ImageSkia& image) {
if (icon_ != NULL)
delete icon_; // This removes the icon from this view's children.
if (image.isNull()) {
icon_ = NULL;
} else {
icon_ = new views::ImageView();
icon_->SetImageSize(gfx::Size(message_center::kNotificationButtonIconSize,
message_center::kNotificationButtonIconSize));
icon_->SetImage(image);
icon_->SetHorizontalAlignment(views::ImageView::LEADING);
icon_->SetVerticalAlignment(views::ImageView::LEADING);
icon_->set_border(MakeBorder(kButtonIconTopPadding, 0, 0, 0));
AddChildViewAt(icon_, 0);
}
}
void NotificationButton::SetTitle(const string16& title) {
if (title_ != NULL)
delete title_; // This removes the title from this view's children.
if (title.empty()) {
title_ = NULL;
} else {
title_ = new views::Label(title);
title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title_->SetElideBehavior(views::Label::ELIDE_AT_END);
title_->SetEnabledColor(kTitleColor);
title_->SetBackgroundColor(kTitleBackgroundColor);
title_->set_border(MakeBorder(kButtonTitleTopPadding, 0, 0, 0));
AddChildView(title_);
}
}
gfx::Size NotificationButton::GetPreferredSize() {
return gfx::Size(std::numeric_limits<int>::max(), kButtonHeight);
}
int NotificationButton::GetHeightForWidth(int width) {
return kButtonHeight;
}
} // namespace
namespace message_center {
// NotificationView ////////////////////////////////////////////////////////////
// static
MessageView* NotificationView::Create(const Notification& notification,
NotificationChangeObserver* observer,
bool expanded) {
// For the time being, use MessageSimpleView for simple notifications unless
// one of the use-the-new-style flags are set. This preserves the appearance
// of notifications created by existing code that uses webkitNotifications.
if (notification.type() == NOTIFICATION_TYPE_SIMPLE &&
!IsRichNotificationEnabled() &&
!CommandLine::ForCurrentProcess()->HasSwitch(
message_center::switches::kEnableNewSimpleNotifications)) {
return new MessageSimpleView(notification, observer);
}
switch (notification.type()) {
case NOTIFICATION_TYPE_BASE_FORMAT:
case NOTIFICATION_TYPE_IMAGE:
case NOTIFICATION_TYPE_MULTIPLE:
case NOTIFICATION_TYPE_SIMPLE:
break;
default:
// If the caller asks for an unrecognized kind of view (entirely possible
// if an application is running on an older version of this code that
// doesn't have the requested kind of notification template), we'll fall
// back to a notification instance that will provide at least basic
// functionality.
LOG(WARNING) << "Unable to fulfill request for unrecognized "
<< "notification type " << notification.type() << ". "
<< "Falling back to simple notification type.";
}
// Currently all roads lead to the generic NotificationView.
return new NotificationView(notification, observer, expanded);
}
NotificationView::NotificationView(const Notification& notification,
NotificationChangeObserver* observer,
bool expanded)
: MessageView(notification, observer, expanded) {
// Create the opaque background that's above the view's shadow.
background_view_ = new views::View();
background_view_->set_background(
views::Background::CreateSolidBackground(kBackgroundColor));
// Create the top_view_, which collects into a vertical box all content
// at the top of the notification (to the right of the icon) except for the
// close button.
top_view_ = new ContainerView();
// Create the title view if appropriate.
title_view_ = NULL;
if (!notification.title().empty()) {
title_view_ = new views::Label(notification.title());
title_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
if (is_expanded())
title_view_->SetMultiLine(true);
else
title_view_->SetElideBehavior(views::Label::ELIDE_AT_END);
title_view_->SetFont(title_view_->font().DeriveFont(4));
title_view_->SetEnabledColor(kTitleColor);
title_view_->SetBackgroundColor(kTitleBackgroundColor);
title_view_->set_border(MakeBorder(kTextTopPadding, 3));
top_view_->AddChildView(title_view_);
}
// Create the message view if appropriate.
message_view_ = NULL;
if (!notification.message().empty()) {
message_view_ = new views::Label(notification.message());
message_view_->SetVisible(!is_expanded() || !notification.items().size());
message_view_->set_collapse_when_hidden(true);
message_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
if (is_expanded())
message_view_->SetMultiLine(true);
else
message_view_->SetElideBehavior(views::Label::ELIDE_AT_END);
message_view_->SetEnabledColor(kMessageColor);
message_view_->SetBackgroundColor(kMessageBackgroundColor);
message_view_->set_border(MakeBorder(0, 3));
top_view_->AddChildView(message_view_);
}
// Create the list item views (up to a maximum).
std::vector<NotificationItem> items = notification.items();
for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) {
ItemView* item_view = new ItemView(items[i]);
item_view->SetVisible(is_expanded());
item_view->set_border(MakeBorder(0, 4));
item_views_.push_back(item_view);
top_view_->AddChildView(item_view);
}
// Create the notification icon view.
icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia());
// Create the bottom_view_, which collects into a vertical box all content
// below the notification icon except for the expand button.
bottom_view_ = new ContainerView();
bottom_view_->set_background(
views::Background::CreateSolidBackground(kBackgroundColor));
// Create the image view if appropriate.
image_view_ = NULL;
if (!notification.image().IsEmpty()) {
image_view_ = new ProportionalImageView(notification.image().AsImageSkia());
image_view_->SetVisible(is_expanded());
bottom_view_->AddChildView(image_view_);
}
// Create action buttons if appropriate.
std::vector<ButtonInfo> buttons = notification.buttons();
for (size_t i = 0; i < buttons.size(); ++i) {
views::View* separator = new views::ImageView();
separator->set_border(MakeBorder(1, 0, 0, 0, kButtonSeparatorColor));
bottom_view_->AddChildView(separator);
NotificationButton* button = new NotificationButton(this);
ButtonInfo button_info = buttons[i];
button->SetTitle(button_info.title);
button->SetIcon(button_info.icon.AsImageSkia());
action_buttons_.push_back(button);
bottom_view_->AddChildView(button);
}
// Hide the expand button if appropriate.
bool expandable = item_views_.size() || image_view_;
expand_button()->SetVisible(expandable && !is_expanded());
// Put together the different content and control views. Layering those allows
// for proper layout logic and it also allows the close and expand buttons to
// overlap the content as needed to provide large enough click and touch areas
// (<http://crbug.com/168822> and <http://crbug.com/168856>).
AddChildView(background_view_);
AddChildView(top_view_);
AddChildView(icon_view_);
AddChildView(bottom_view_);
AddChildView(close_button());
AddChildView(expand_button());
}
NotificationView::~NotificationView() {
}
gfx::Size NotificationView::GetPreferredSize() {
int top_width = top_view_->GetPreferredSize().width();
int bottom_width = bottom_view_->GetPreferredSize().width();
int preferred_width = std::max(top_width, bottom_width) + GetInsets().width();
return gfx::Size(preferred_width, GetHeightForWidth(preferred_width));
}
int NotificationView::GetHeightForWidth(int width) {
gfx::Insets insets = GetInsets();
int top_height = top_view_->GetHeightForWidth(width - insets.width());
int bottom_height = bottom_view_->GetHeightForWidth(width - insets.width());
int icon_size = message_center::kNotificationIconSize;
return std::max(top_height, icon_size) + bottom_height + insets.height();
}
void NotificationView::Layout() {
gfx::Insets insets = GetInsets();
int content_width = width() - insets.width();
int content_right = width() - insets.right();
background_view_->SetBounds(insets.left(), insets.top(),
content_width, height() - insets.height());
int top_height = top_view_->GetHeightForWidth(content_width);
top_view_->SetBounds(insets.left(), insets.top(), content_width, top_height);
int icon_size = message_center::kNotificationIconSize;
icon_view_->SetBounds(insets.left(), insets.top(), icon_size, icon_size);
int bottom_y = insets.top() + std::max(top_height, icon_size);
int bottom_height = bottom_view_->GetHeightForWidth(content_width);
bottom_view_->SetBounds(insets.left(), bottom_y,
content_width, bottom_height);
gfx::Size close_size(close_button()->GetPreferredSize());
close_button()->SetBounds(content_right - close_size.width(), insets.top(),
close_size.width(), close_size.height());
gfx::Size expand_size(expand_button()->GetPreferredSize());
int expand_y = bottom_y - expand_size.height();
expand_button()->SetBounds(content_right - expand_size.width(), expand_y,
expand_size.width(), expand_size.height());
}
void NotificationView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
// See if the button pressed was an action button.
for (size_t i = 0; i < action_buttons_.size(); ++i) {
if (sender == action_buttons_[i]) {
observer()->OnButtonClicked(notification_id(), i);
return;
}
}
// Let the superclass handled anything other than action buttons.
MessageView::ButtonPressed(sender, event);
// Show and hide subviews appropriately on expansion.
if (sender == expand_button()) {
if (message_view_)
message_view_->SetVisible(!item_views_.size());
for (size_t i = 0; i < item_views_.size(); ++i)
item_views_[i]->SetVisible(true);
if (image_view_)
image_view_->SetVisible(true);
expand_button()->SetVisible(false);
PreferredSizeChanged();
SchedulePaint();
}
}
} // namespace message_center
|