| OLD | NEW |
| 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" | |
| 22 #include "ui/message_center/views/message_simple_view.h" | 21 #include "ui/message_center/views/message_simple_view.h" |
| 23 #include "ui/native_theme/native_theme.h" | 22 #include "ui/native_theme/native_theme.h" |
| 24 #include "ui/views/controls/button/image_button.h" | 23 #include "ui/views/controls/button/image_button.h" |
| 25 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/label.h" | 25 #include "ui/views/controls/label.h" |
| 27 #include "ui/views/layout/box_layout.h" | 26 #include "ui/views/layout/box_layout.h" |
| 28 #include "ui/views/layout/fill_layout.h" | 27 #include "ui/views/layout/fill_layout.h" |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 333 } |
| 335 | 334 |
| 336 // Currently all roads lead to the generic NotificationView. | 335 // Currently all roads lead to the generic NotificationView. |
| 337 return new NotificationView(notification, observer, expanded); | 336 return new NotificationView(notification, observer, expanded); |
| 338 } | 337 } |
| 339 | 338 |
| 340 NotificationView::NotificationView(const Notification& notification, | 339 NotificationView::NotificationView(const Notification& notification, |
| 341 NotificationChangeObserver* observer, | 340 NotificationChangeObserver* observer, |
| 342 bool expanded) | 341 bool expanded) |
| 343 : MessageView(notification, observer, expanded) { | 342 : 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 | |
| 347 // Create the opaque background that's above the view's shadow. | 343 // Create the opaque background that's above the view's shadow. |
| 348 background_view_ = new views::View(); | 344 background_view_ = new views::View(); |
| 349 background_view_->set_background( | 345 background_view_->set_background( |
| 350 views::Background::CreateSolidBackground(kBackgroundColor)); | 346 views::Background::CreateSolidBackground(kBackgroundColor)); |
| 351 | 347 |
| 352 // Create the top_view_, which collects into a vertical box all content | 348 // Create the top_view_, which collects into a vertical box all content |
| 353 // at the top of the notification (to the right of the icon) except for the | 349 // at the top of the notification (to the right of the icon) except for the |
| 354 // close button. | 350 // close button. |
| 355 top_view_ = new ContainerView(); | 351 top_view_ = new ContainerView(); |
| 356 | 352 |
| 357 // Create the title view if appropriate. | 353 // Create the title view if appropriate. |
| 358 title_view_ = NULL; | 354 title_view_ = NULL; |
| 359 if (!notification.title().empty()) { | 355 if (!notification.title().empty()) { |
| 360 gfx::Font font = views::Label().font().DeriveFont(4); | 356 title_view_ = new views::Label(notification.title()); |
| 361 title_view_ = new BoundedLabel(notification.title(), font, 1); | |
| 362 title_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 357 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 size_t lines = (is_expanded() && notification.image().IsEmpty()) ? 7 : 2; | 372 message_view_ = new views::Label(notification.message()); |
| 373 message_view_ = new BoundedLabel(notification.message(), lines); | |
| 374 message_view_->SetVisible(!is_expanded() || !notification.items().size()); | 373 message_view_->SetVisible(!is_expanded() || !notification.items().size()); |
| 375 message_view_->set_collapse_when_hidden(true); | 374 message_view_->set_collapse_when_hidden(true); |
| 376 message_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 375 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); |
| 377 message_view_->SetEnabledColor(kMessageColor); | 380 message_view_->SetEnabledColor(kMessageColor); |
| 378 message_view_->SetBackgroundColor(kMessageBackgroundColor); | 381 message_view_->SetBackgroundColor(kMessageBackgroundColor); |
| 379 message_view_->set_border(MakeBorder(0, 3)); | 382 message_view_->set_border(MakeBorder(0, 3)); |
| 380 top_view_->AddChildView(message_view_); | 383 top_view_->AddChildView(message_view_); |
| 381 expandable = (message_view_->GetPreferredLines() > lines); | |
| 382 } | 384 } |
| 383 | 385 |
| 384 // Create the list item views (up to a maximum). | 386 // Create the list item views (up to a maximum). |
| 385 std::vector<NotificationItem> items = notification.items(); | 387 std::vector<NotificationItem> items = notification.items(); |
| 386 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { | 388 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { |
| 387 ItemView* item_view = new ItemView(items[i]); | 389 ItemView* item_view = new ItemView(items[i]); |
| 388 item_view->SetVisible(is_expanded()); | 390 item_view->SetVisible(is_expanded()); |
| 389 item_view->set_border(MakeBorder(0, 4)); | 391 item_view->set_border(MakeBorder(0, 4)); |
| 390 item_views_.push_back(item_view); | 392 item_views_.push_back(item_view); |
| 391 top_view_->AddChildView(item_view); | 393 top_view_->AddChildView(item_view); |
| 392 expandable = true; | |
| 393 } | 394 } |
| 394 | 395 |
| 395 // Create the notification icon view. | 396 // Create the notification icon view. |
| 396 icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia()); | 397 icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia()); |
| 397 | 398 |
| 398 // Create the bottom_view_, which collects into a vertical box all content | 399 // Create the bottom_view_, which collects into a vertical box all content |
| 399 // below the notification icon except for the expand button. | 400 // below the notification icon except for the expand button. |
| 400 bottom_view_ = new ContainerView(); | 401 bottom_view_ = new ContainerView(); |
| 401 bottom_view_->set_background( | 402 bottom_view_->set_background( |
| 402 views::Background::CreateSolidBackground(kBackgroundColor)); | 403 views::Background::CreateSolidBackground(kBackgroundColor)); |
| 403 | 404 |
| 404 // Create the image view if appropriate. | 405 // Create the image view if appropriate. |
| 405 image_view_ = NULL; | 406 image_view_ = NULL; |
| 406 if (!notification.image().IsEmpty()) { | 407 if (!notification.image().IsEmpty()) { |
| 407 image_view_ = new ProportionalImageView(notification.image().AsImageSkia()); | 408 image_view_ = new ProportionalImageView(notification.image().AsImageSkia()); |
| 408 image_view_->SetVisible(is_expanded()); | 409 image_view_->SetVisible(is_expanded()); |
| 409 bottom_view_->AddChildView(image_view_); | 410 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_; |
| 428 expand_button()->SetVisible(expandable && !is_expanded()); | 429 expand_button()->SetVisible(expandable && !is_expanded()); |
| 429 | 430 |
| 430 // Put together the different content and control views. Layering those allows | 431 // Put together the different content and control views. Layering those allows |
| 431 // for proper layout logic and it also allows the close and expand buttons to | 432 // for proper layout logic and it also allows the close and expand buttons to |
| 432 // overlap the content as needed to provide large enough click and touch areas | 433 // overlap the content as needed to provide large enough click and touch areas |
| 433 // (<http://crbug.com/168822> and <http://crbug.com/168856>). | 434 // (<http://crbug.com/168822> and <http://crbug.com/168856>). |
| 434 AddChildView(background_view_); | 435 AddChildView(background_view_); |
| 435 AddChildView(top_view_); | 436 AddChildView(top_view_); |
| 436 AddChildView(icon_view_); | 437 AddChildView(icon_view_); |
| 437 AddChildView(bottom_view_); | 438 AddChildView(bottom_view_); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 observer()->OnButtonClicked(notification_id(), i); | 495 observer()->OnButtonClicked(notification_id(), i); |
| 495 return; | 496 return; |
| 496 } | 497 } |
| 497 } | 498 } |
| 498 | 499 |
| 499 // Let the superclass handled anything other than action buttons. | 500 // Let the superclass handled anything other than action buttons. |
| 500 MessageView::ButtonPressed(sender, event); | 501 MessageView::ButtonPressed(sender, event); |
| 501 | 502 |
| 502 // Show and hide subviews appropriately on expansion. | 503 // Show and hide subviews appropriately on expansion. |
| 503 if (sender == expand_button()) { | 504 if (sender == expand_button()) { |
| 504 if (message_view_ && !item_views_.size() && !image_view_) | 505 if (message_view_) |
| 505 message_view_->SetMaxLines(7); | 506 message_view_->SetVisible(!item_views_.size()); |
| 506 else if (message_view_ && item_views_.size()) | |
| 507 message_view_->SetVisible(false); | |
| 508 for (size_t i = 0; i < item_views_.size(); ++i) | 507 for (size_t i = 0; i < item_views_.size(); ++i) |
| 509 item_views_[i]->SetVisible(true); | 508 item_views_[i]->SetVisible(true); |
| 510 if (image_view_) | 509 if (image_view_) |
| 511 image_view_->SetVisible(true); | 510 image_view_->SetVisible(true); |
| 512 expand_button()->SetVisible(false); | 511 expand_button()->SetVisible(false); |
| 513 PreferredSizeChanged(); | 512 PreferredSizeChanged(); |
| 514 SchedulePaint(); | 513 SchedulePaint(); |
| 515 } | 514 } |
| 516 } | 515 } |
| 517 | 516 |
| 518 } // namespace message_center | 517 } // namespace message_center |
| OLD | NEW |