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

Side by Side Diff: ui/message_center/views/notification_view.cc

Issue 12742005: Added text line limits to collapsed and expanded notifications. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/message_center/views/notification_view.h" 5 #include "ui/message_center/views/notification_view.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "grit/ui_resources.h" 9 #include "grit/ui_resources.h"
10 #include "ui/base/accessibility/accessible_view_state.h" 10 #include "ui/base/accessibility/accessible_view_state.h"
11 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/base/text/text_elider.h" 12 #include "ui/base/text/text_elider.h"
13 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/size.h" 14 #include "ui/gfx/size.h"
15 #include "ui/message_center/message_center_constants.h" 15 #include "ui/message_center/message_center_constants.h"
16 #include "ui/message_center/message_center_switches.h" 16 #include "ui/message_center/message_center_switches.h"
17 #include "ui/message_center/message_center_util.h" 17 #include "ui/message_center/message_center_util.h"
18 #include "ui/message_center/notification.h" 18 #include "ui/message_center/notification.h"
19 #include "ui/message_center/notification_change_observer.h" 19 #include "ui/message_center/notification_change_observer.h"
20 #include "ui/message_center/notification_types.h" 20 #include "ui/message_center/notification_types.h"
21 #include "ui/message_center/views/bounded_label.h"
21 #include "ui/message_center/views/message_simple_view.h" 22 #include "ui/message_center/views/message_simple_view.h"
22 #include "ui/native_theme/native_theme.h" 23 #include "ui/native_theme/native_theme.h"
23 #include "ui/views/controls/button/image_button.h" 24 #include "ui/views/controls/button/image_button.h"
24 #include "ui/views/controls/image_view.h" 25 #include "ui/views/controls/image_view.h"
25 #include "ui/views/controls/label.h" 26 #include "ui/views/controls/label.h"
26 #include "ui/views/layout/box_layout.h" 27 #include "ui/views/layout/box_layout.h"
27 #include "ui/views/layout/fill_layout.h" 28 #include "ui/views/layout/fill_layout.h"
28 29
29 namespace { 30 namespace {
30 31
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 } 334 }
334 335
335 // Currently all roads lead to the generic NotificationView. 336 // Currently all roads lead to the generic NotificationView.
336 return new NotificationView(notification, observer, expanded); 337 return new NotificationView(notification, observer, expanded);
337 } 338 }
338 339
339 NotificationView::NotificationView(const Notification& notification, 340 NotificationView::NotificationView(const Notification& notification,
340 NotificationChangeObserver* observer, 341 NotificationChangeObserver* observer,
341 bool expanded) 342 bool expanded)
342 : MessageView(notification, observer, expanded) { 343 : MessageView(notification, observer, expanded) {
344 // As we build the view, we'll see if any part of it is expandable.
345 bool expandable = false;
346
343 // Create the opaque background that's above the view's shadow. 347 // Create the opaque background that's above the view's shadow.
344 background_view_ = new views::View(); 348 background_view_ = new views::View();
345 background_view_->set_background( 349 background_view_->set_background(
346 views::Background::CreateSolidBackground(kBackgroundColor)); 350 views::Background::CreateSolidBackground(kBackgroundColor));
347 351
348 // Create the top_view_, which collects into a vertical box all content 352 // Create the top_view_, which collects into a vertical box all content
349 // at the top of the notification (to the right of the icon) except for the 353 // at the top of the notification (to the right of the icon) except for the
350 // close button. 354 // close button.
351 top_view_ = new ContainerView(); 355 top_view_ = new ContainerView();
352 356
353 // Create the title view if appropriate. 357 // Create the title view if appropriate.
354 title_view_ = NULL; 358 title_view_ = NULL;
355 if (!notification.title().empty()) { 359 if (!notification.title().empty()) {
356 title_view_ = new views::Label(notification.title()); 360 gfx::Font font = views::Label().font().DeriveFont(4);
361 title_view_ = new BoundedLabel(notification.title(), font, 1);
357 title_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 362 title_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
358 if (is_expanded())
359 title_view_->SetMultiLine(true);
360 else
361 title_view_->SetElideBehavior(views::Label::ELIDE_AT_END);
362 title_view_->SetFont(title_view_->font().DeriveFont(4));
363 title_view_->SetEnabledColor(kTitleColor); 363 title_view_->SetEnabledColor(kTitleColor);
364 title_view_->SetBackgroundColor(kTitleBackgroundColor); 364 title_view_->SetBackgroundColor(kTitleBackgroundColor);
365 title_view_->set_border(MakeBorder(kTextTopPadding, 3)); 365 title_view_->set_border(MakeBorder(kTextTopPadding, 3));
366 top_view_->AddChildView(title_view_); 366 top_view_->AddChildView(title_view_);
367 } 367 }
368 368
369 // Create the message view if appropriate. 369 // Create the message view if appropriate.
370 message_view_ = NULL; 370 message_view_ = NULL;
371 if (!notification.message().empty()) { 371 if (!notification.message().empty()) {
372 message_view_ = new views::Label(notification.message()); 372 size_t lines = (is_expanded() && notification.image().IsEmpty()) ? 7 : 2;
373 message_view_ = new BoundedLabel(notification.message(), lines);
373 message_view_->SetVisible(!is_expanded() || !notification.items().size()); 374 message_view_->SetVisible(!is_expanded() || !notification.items().size());
374 message_view_->set_collapse_when_hidden(true); 375 message_view_->set_collapse_when_hidden(true);
375 message_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 376 message_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
376 if (is_expanded())
377 message_view_->SetMultiLine(true);
378 else
379 message_view_->SetElideBehavior(views::Label::ELIDE_AT_END);
380 message_view_->SetEnabledColor(kMessageColor); 377 message_view_->SetEnabledColor(kMessageColor);
381 message_view_->SetBackgroundColor(kMessageBackgroundColor); 378 message_view_->SetBackgroundColor(kMessageBackgroundColor);
382 message_view_->set_border(MakeBorder(0, 3)); 379 message_view_->set_border(MakeBorder(0, 3));
383 top_view_->AddChildView(message_view_); 380 top_view_->AddChildView(message_view_);
381 expandable = (message_view_->GetPreferredLines() > lines);
384 } 382 }
385 383
386 // Create the list item views (up to a maximum). 384 // Create the list item views (up to a maximum).
387 std::vector<NotificationItem> items = notification.items(); 385 std::vector<NotificationItem> items = notification.items();
388 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { 386 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) {
389 ItemView* item_view = new ItemView(items[i]); 387 ItemView* item_view = new ItemView(items[i]);
390 item_view->SetVisible(is_expanded()); 388 item_view->SetVisible(is_expanded());
391 item_view->set_border(MakeBorder(0, 4)); 389 item_view->set_border(MakeBorder(0, 4));
392 item_views_.push_back(item_view); 390 item_views_.push_back(item_view);
393 top_view_->AddChildView(item_view); 391 top_view_->AddChildView(item_view);
392 expandable = true;
394 } 393 }
395 394
396 // Create the notification icon view. 395 // Create the notification icon view.
397 icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia()); 396 icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia());
398 397
399 // Create the bottom_view_, which collects into a vertical box all content 398 // Create the bottom_view_, which collects into a vertical box all content
400 // below the notification icon except for the expand button. 399 // below the notification icon except for the expand button.
401 bottom_view_ = new ContainerView(); 400 bottom_view_ = new ContainerView();
402 bottom_view_->set_background( 401 bottom_view_->set_background(
403 views::Background::CreateSolidBackground(kBackgroundColor)); 402 views::Background::CreateSolidBackground(kBackgroundColor));
404 403
405 // Create the image view if appropriate. 404 // Create the image view if appropriate.
406 image_view_ = NULL; 405 image_view_ = NULL;
407 if (!notification.image().IsEmpty()) { 406 if (!notification.image().IsEmpty()) {
408 image_view_ = new ProportionalImageView(notification.image().AsImageSkia()); 407 image_view_ = new ProportionalImageView(notification.image().AsImageSkia());
409 image_view_->SetVisible(is_expanded()); 408 image_view_->SetVisible(is_expanded());
410 bottom_view_->AddChildView(image_view_); 409 bottom_view_->AddChildView(image_view_);
410 expandable = true;
411 } 411 }
412 412
413 // Create action buttons if appropriate. 413 // Create action buttons if appropriate.
414 std::vector<ButtonInfo> buttons = notification.buttons(); 414 std::vector<ButtonInfo> buttons = notification.buttons();
415 for (size_t i = 0; i < buttons.size(); ++i) { 415 for (size_t i = 0; i < buttons.size(); ++i) {
416 views::View* separator = new views::ImageView(); 416 views::View* separator = new views::ImageView();
417 separator->set_border(MakeBorder(1, 0, 0, 0, kButtonSeparatorColor)); 417 separator->set_border(MakeBorder(1, 0, 0, 0, kButtonSeparatorColor));
418 bottom_view_->AddChildView(separator); 418 bottom_view_->AddChildView(separator);
419 NotificationButton* button = new NotificationButton(this); 419 NotificationButton* button = new NotificationButton(this);
420 ButtonInfo button_info = buttons[i]; 420 ButtonInfo button_info = buttons[i];
421 button->SetTitle(button_info.title); 421 button->SetTitle(button_info.title);
422 button->SetIcon(button_info.icon.AsImageSkia()); 422 button->SetIcon(button_info.icon.AsImageSkia());
423 action_buttons_.push_back(button); 423 action_buttons_.push_back(button);
424 bottom_view_->AddChildView(button); 424 bottom_view_->AddChildView(button);
425 } 425 }
426 426
427 // Hide the expand button if appropriate. 427 // Hide the expand button if appropriate.
428 bool expandable = item_views_.size() || image_view_;
429 expand_button()->SetVisible(expandable && !is_expanded()); 428 expand_button()->SetVisible(expandable && !is_expanded());
430 429
431 // Put together the different content and control views. Layering those allows 430 // Put together the different content and control views. Layering those allows
432 // for proper layout logic and it also allows the close and expand buttons to 431 // for proper layout logic and it also allows the close and expand buttons to
433 // overlap the content as needed to provide large enough click and touch areas 432 // overlap the content as needed to provide large enough click and touch areas
434 // (<http://crbug.com/168822> and <http://crbug.com/168856>). 433 // (<http://crbug.com/168822> and <http://crbug.com/168856>).
435 AddChildView(background_view_); 434 AddChildView(background_view_);
436 AddChildView(top_view_); 435 AddChildView(top_view_);
437 AddChildView(icon_view_); 436 AddChildView(icon_view_);
438 AddChildView(bottom_view_); 437 AddChildView(bottom_view_);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 observer()->OnButtonClicked(notification_id(), i); 494 observer()->OnButtonClicked(notification_id(), i);
496 return; 495 return;
497 } 496 }
498 } 497 }
499 498
500 // Let the superclass handled anything other than action buttons. 499 // Let the superclass handled anything other than action buttons.
501 MessageView::ButtonPressed(sender, event); 500 MessageView::ButtonPressed(sender, event);
502 501
503 // Show and hide subviews appropriately on expansion. 502 // Show and hide subviews appropriately on expansion.
504 if (sender == expand_button()) { 503 if (sender == expand_button()) {
505 if (message_view_) 504 if (message_view_ && !item_views_.size() && !image_view_)
506 message_view_->SetVisible(!item_views_.size()); 505 message_view_->SetMaxLines(7);
506 else if (message_view_ && item_views_.size())
507 message_view_->SetVisible(false);
507 for (size_t i = 0; i < item_views_.size(); ++i) 508 for (size_t i = 0; i < item_views_.size(); ++i)
508 item_views_[i]->SetVisible(true); 509 item_views_[i]->SetVisible(true);
509 if (image_view_) 510 if (image_view_)
510 image_view_->SetVisible(true); 511 image_view_->SetVisible(true);
511 expand_button()->SetVisible(false); 512 expand_button()->SetVisible(false);
512 PreferredSizeChanged(); 513 PreferredSizeChanged();
513 SchedulePaint(); 514 SchedulePaint();
514 } 515 }
515 } 516 }
516 517
517 } // namespace message_center 518 } // namespace message_center
OLDNEW
« ui/message_center/run_all_unittests.cc ('K') | « ui/message_center/views/notification_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698