Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 54 #include "ui/base/win/shell.h" | 54 #include "ui/base/win/shell.h" |
| 55 #endif | 55 #endif |
| 56 | 56 |
| 57 namespace { | 57 namespace { |
| 58 | 58 |
| 59 // Dimensions. | 59 // Dimensions. |
| 60 const int kProgressBarBottomPadding = 0; | 60 const int kProgressBarBottomPadding = 0; |
| 61 | 61 |
| 62 const int kCloseIconTopPadding = 5; | |
| 63 const int kCloseIconRightPadding = 5; | |
| 64 | |
| 65 // static | 62 // static |
| 66 std::unique_ptr<views::Border> MakeEmptyBorder(int top, | 63 std::unique_ptr<views::Border> MakeEmptyBorder(int top, |
| 67 int left, | 64 int left, |
| 68 int bottom, | 65 int bottom, |
| 69 int right) { | 66 int right) { |
| 70 return views::Border::CreateEmptyBorder(top, left, bottom, right); | 67 return views::Border::CreateEmptyBorder(top, left, bottom, right); |
| 71 } | 68 } |
| 72 | 69 |
| 73 // static | 70 // static |
| 74 std::unique_ptr<views::Border> MakeTextBorder(int padding, | 71 std::unique_ptr<views::Border> MakeTextBorder(int padding, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 // targeting. Using the center point of |rect| preserves this function's | 197 // targeting. Using the center point of |rect| preserves this function's |
| 201 // expected behavior for the time being. | 198 // expected behavior for the time being. |
| 202 gfx::Point point = rect.CenterPoint(); | 199 gfx::Point point = rect.CenterPoint(); |
| 203 | 200 |
| 204 // Want to return this for underlying views, otherwise GetCursor is not | 201 // Want to return this for underlying views, otherwise GetCursor is not |
| 205 // called. But buttons are exceptions, they'll have their own event handlings. | 202 // called. But buttons are exceptions, they'll have their own event handlings. |
| 206 std::vector<views::View*> buttons(action_buttons_.begin(), | 203 std::vector<views::View*> buttons(action_buttons_.begin(), |
| 207 action_buttons_.end()); | 204 action_buttons_.end()); |
| 208 if (settings_button_view_) | 205 if (settings_button_view_) |
| 209 buttons.push_back(settings_button_view_); | 206 buttons.push_back(settings_button_view_); |
| 210 if (close_button_) | 207 if (close_button()) |
| 211 buttons.push_back(close_button_.get()); | 208 buttons.push_back(close_button()); |
| 212 | 209 |
| 213 for (size_t i = 0; i < buttons.size(); ++i) { | 210 for (size_t i = 0; i < buttons.size(); ++i) { |
| 214 gfx::Point point_in_child = point; | 211 gfx::Point point_in_child = point; |
| 215 ConvertPointToTarget(this, buttons[i], &point_in_child); | 212 ConvertPointToTarget(this, buttons[i], &point_in_child); |
| 216 if (buttons[i]->HitTestPoint(point_in_child)) | 213 if (buttons[i]->HitTestPoint(point_in_child)) |
| 217 return buttons[i]->GetEventHandlerForPoint(point_in_child); | 214 return buttons[i]->GetEventHandlerForPoint(point_in_child); |
| 218 } | 215 } |
| 219 | 216 |
| 220 return root; | 217 return root; |
| 221 } | 218 } |
| 222 | 219 |
| 223 void NotificationView::CreateOrUpdateViews(const Notification& notification) { | 220 void NotificationView::CreateOrUpdateViews(const Notification& notification) { |
| 224 CreateOrUpdateCloseButtonView(notification); | |
| 225 CreateOrUpdateTitleView(notification); | 221 CreateOrUpdateTitleView(notification); |
| 226 CreateOrUpdateMessageView(notification); | 222 CreateOrUpdateMessageView(notification); |
| 227 CreateOrUpdateProgressBarView(notification); | 223 CreateOrUpdateProgressBarView(notification); |
| 228 CreateOrUpdateListItemViews(notification); | 224 CreateOrUpdateListItemViews(notification); |
| 229 CreateOrUpdateIconView(notification); | 225 CreateOrUpdateIconView(notification); |
| 230 CreateOrUpdateImageView(notification); | 226 CreateOrUpdateImageView(notification); |
| 231 CreateOrUpdateContextMessageView(notification); | 227 CreateOrUpdateContextMessageView(notification); |
| 232 CreateOrUpdateSettingsButtonView(notification); | 228 CreateOrUpdateSettingsButtonView(notification); |
| 233 CreateOrUpdateActionButtonViews(notification); | 229 CreateOrUpdateActionButtonViews(notification); |
| 230 CreateOrUpdateCloseButtonView(notification); | |
|
yoshiki
2016/05/16 03:11:46
When CreateOrUpdateViews() is called from Notifica
xiyuan
2016/05/16 16:11:55
Done.
| |
| 234 } | 231 } |
| 235 | 232 |
| 236 void NotificationView::SetAccessibleName(const Notification& notification) { | 233 void NotificationView::SetAccessibleName(const Notification& notification) { |
| 237 std::vector<base::string16> accessible_lines; | 234 std::vector<base::string16> accessible_lines; |
| 238 accessible_lines.push_back(notification.title()); | 235 accessible_lines.push_back(notification.title()); |
| 239 accessible_lines.push_back(notification.message()); | 236 accessible_lines.push_back(notification.message()); |
| 240 accessible_lines.push_back(notification.context_message()); | 237 accessible_lines.push_back(notification.context_message()); |
| 241 std::vector<NotificationItem> items = notification.items(); | 238 std::vector<NotificationItem> items = notification.items(); |
| 242 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { | 239 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { |
| 243 accessible_lines.push_back(items[i].title + base::ASCIIToUTF16(" ") + | 240 accessible_lines.push_back(items[i].title + base::ASCIIToUTF16(" ") + |
| 244 items[i].message); | 241 items[i].message); |
| 245 } | 242 } |
| 246 set_accessible_name( | 243 set_accessible_name( |
| 247 base::JoinString(accessible_lines, base::ASCIIToUTF16("\n"))); | 244 base::JoinString(accessible_lines, base::ASCIIToUTF16("\n"))); |
| 248 } | 245 } |
| 249 | 246 |
| 250 NotificationView::NotificationView(MessageCenterController* controller, | 247 NotificationView::NotificationView(MessageCenterController* controller, |
| 251 const Notification& notification) | 248 const Notification& notification) |
| 252 : MessageView(controller, | 249 : MessageView(controller, notification), |
| 253 notification.id(), | 250 clickable_(notification.clickable()) { |
| 254 notification.notifier_id(), | |
| 255 notification.small_image().AsImageSkia(), | |
| 256 notification.display_source()), | |
| 257 clickable_(notification.clickable()), | |
| 258 top_view_(NULL), | |
| 259 title_view_(NULL), | |
| 260 message_view_(NULL), | |
| 261 context_message_view_(NULL), | |
| 262 settings_button_view_(NULL), | |
| 263 icon_view_(NULL), | |
| 264 bottom_view_(NULL), | |
| 265 image_container_(NULL), | |
| 266 image_view_(NULL), | |
| 267 progress_bar_view_(NULL) { | |
| 268 // Create the top_view_, which collects into a vertical box all content | 251 // Create the top_view_, which collects into a vertical box all content |
| 269 // at the top of the notification (to the right of the icon) except for the | 252 // at the top of the notification (to the right of the icon) except for the |
| 270 // close button. | 253 // close button. |
| 271 top_view_ = new views::View(); | 254 top_view_ = new views::View(); |
| 272 top_view_->SetLayoutManager( | 255 top_view_->SetLayoutManager( |
| 273 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | 256 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| 274 top_view_->SetBorder( | 257 top_view_->SetBorder( |
| 275 MakeEmptyBorder(kTextTopPadding - 8, 0, kTextBottomPadding - 5, 0)); | 258 MakeEmptyBorder(kTextTopPadding - 8, 0, kTextBottomPadding - 5, 0)); |
| 276 AddChildView(top_view_); | 259 AddChildView(top_view_); |
| 277 // Create the bottom_view_, which collects into a vertical box all content | 260 // Create the bottom_view_, which collects into a vertical box all content |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 title_lines = | 336 title_lines = |
| 354 title_view_->GetLinesForWidthAndLimit(width(), kMaxTitleLines); | 337 title_view_->GetLinesForWidthAndLimit(width(), kMaxTitleLines); |
| 355 } | 338 } |
| 356 if (message_view_) | 339 if (message_view_) |
| 357 message_view_->SetLineLimit(GetMessageLineLimit(title_lines, width())); | 340 message_view_->SetLineLimit(GetMessageLineLimit(title_lines, width())); |
| 358 | 341 |
| 359 // Top views. | 342 // Top views. |
| 360 int top_height = top_view_->GetHeightForWidth(content_width); | 343 int top_height = top_view_->GetHeightForWidth(content_width); |
| 361 top_view_->SetBounds(insets.left(), insets.top(), content_width, top_height); | 344 top_view_->SetBounds(insets.left(), insets.top(), content_width, top_height); |
| 362 | 345 |
| 363 // Close button. | |
| 364 if (close_button_) { | |
| 365 gfx::Rect content_bounds = GetContentsBounds(); | |
| 366 gfx::Size close_size(close_button_->GetPreferredSize()); | |
| 367 gfx::Rect close_rect(content_bounds.right() - close_size.width(), | |
| 368 content_bounds.y(), close_size.width(), | |
| 369 close_size.height()); | |
| 370 close_button_->SetBoundsRect(close_rect); | |
| 371 } | |
| 372 | |
| 373 // Icon. | 346 // Icon. |
| 374 icon_view_->SetBounds(insets.left(), insets.top(), kNotificationIconSize, | 347 icon_view_->SetBounds(insets.left(), insets.top(), kNotificationIconSize, |
| 375 kNotificationIconSize); | 348 kNotificationIconSize); |
| 376 | 349 |
| 377 // Settings & Bottom views. | 350 // Settings & Bottom views. |
| 378 int bottom_y = insets.top() + std::max(top_height, kNotificationIconSize); | 351 int bottom_y = insets.top() + std::max(top_height, kNotificationIconSize); |
| 379 int bottom_height = bottom_view_->GetHeightForWidth(content_width); | 352 int bottom_height = bottom_view_->GetHeightForWidth(content_width); |
| 380 | 353 |
| 381 if (settings_button_view_) { | 354 if (settings_button_view_) { |
| 382 gfx::Size settings_size(settings_button_view_->GetPreferredSize()); | 355 gfx::Size settings_size(settings_button_view_->GetPreferredSize()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 } | 404 } |
| 432 | 405 |
| 433 // See if the button pressed was an action button. | 406 // See if the button pressed was an action button. |
| 434 for (size_t i = 0; i < action_buttons_.size(); ++i) { | 407 for (size_t i = 0; i < action_buttons_.size(); ++i) { |
| 435 if (sender == action_buttons_[i]) { | 408 if (sender == action_buttons_[i]) { |
| 436 controller()->ClickOnNotificationButton(id, i); | 409 controller()->ClickOnNotificationButton(id, i); |
| 437 return; | 410 return; |
| 438 } | 411 } |
| 439 } | 412 } |
| 440 | 413 |
| 441 if (close_button_ && sender == close_button_.get()) { | |
| 442 controller()->RemoveNotification(notification_id(), true); // By user. | |
| 443 } | |
| 444 | |
| 445 // Let the superclass handle everything else. | 414 // Let the superclass handle everything else. |
| 446 // Warning: This may cause the NotificationView itself to be deleted, | 415 // Warning: This may cause the NotificationView itself to be deleted, |
| 447 // so don't do anything afterwards. | 416 // so don't do anything afterwards. |
| 448 MessageView::ButtonPressed(sender, event); | 417 MessageView::ButtonPressed(sender, event); |
| 449 } | 418 } |
| 450 | 419 |
| 451 void NotificationView::CreateOrUpdateTitleView( | 420 void NotificationView::CreateOrUpdateTitleView( |
| 452 const Notification& notification) { | 421 const Notification& notification) { |
| 453 if (notification.title().empty()) { | 422 if (notification.title().empty()) { |
| 454 if (title_view_) { | 423 if (title_view_) { |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 if (new_buttons) { | 716 if (new_buttons) { |
| 748 Layout(); | 717 Layout(); |
| 749 views::Widget* widget = GetWidget(); | 718 views::Widget* widget = GetWidget(); |
| 750 if (widget != NULL) { | 719 if (widget != NULL) { |
| 751 widget->SetSize(widget->GetContentsView()->GetPreferredSize()); | 720 widget->SetSize(widget->GetContentsView()->GetPreferredSize()); |
| 752 GetWidget()->SynthesizeMouseMoveEvent(); | 721 GetWidget()->SynthesizeMouseMoveEvent(); |
| 753 } | 722 } |
| 754 } | 723 } |
| 755 } | 724 } |
| 756 | 725 |
| 757 void NotificationView::CreateOrUpdateCloseButtonView( | |
| 758 const Notification& notification) { | |
| 759 set_slide_out_enabled(!notification.pinned()); | |
| 760 | |
| 761 if (!notification.pinned() && !close_button_) { | |
| 762 PaddedButton* close = new PaddedButton(this); | |
| 763 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding); | |
| 764 close->SetNormalImage(IDR_NOTIFICATION_CLOSE); | |
| 765 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER); | |
| 766 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED); | |
| 767 close->set_animate_on_state_change(false); | |
| 768 close->SetAccessibleName(l10n_util::GetStringUTF16( | |
| 769 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME)); | |
| 770 // The close button should be added to view hierarchy by the derived class. | |
| 771 // This ensures that it is on top of other views. | |
| 772 close->set_owned_by_client(); | |
| 773 close_button_.reset(close); | |
| 774 AddChildView(close_button_.get()); | |
| 775 } else if (notification.pinned() && close_button_) { | |
| 776 close_button_.reset(); | |
| 777 } | |
| 778 } | |
| 779 | |
| 780 int NotificationView::GetMessageLineLimit(int title_lines, int width) const { | 726 int NotificationView::GetMessageLineLimit(int title_lines, int width) const { |
| 781 // Image notifications require that the image must be kept flush against | 727 // Image notifications require that the image must be kept flush against |
| 782 // their icons, but we can allow more text if no image. | 728 // their icons, but we can allow more text if no image. |
| 783 int effective_title_lines = std::max(0, title_lines - 1); | 729 int effective_title_lines = std::max(0, title_lines - 1); |
| 784 int line_reduction_from_title = (image_view_ ? 1 : 2) * effective_title_lines; | 730 int line_reduction_from_title = (image_view_ ? 1 : 2) * effective_title_lines; |
| 785 if (!image_view_) { | 731 if (!image_view_) { |
| 786 // Title lines are counted as twice as big as message lines for the purpose | 732 // Title lines are counted as twice as big as message lines for the purpose |
| 787 // of this calculation. | 733 // of this calculation. |
| 788 // The effect from the title reduction here should be: | 734 // The effect from the title reduction here should be: |
| 789 // * 0 title lines: 5 max lines message. | 735 // * 0 title lines: 5 max lines message. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 811 std::max(0, message_line_limit - line_reduction_from_title); | 757 std::max(0, message_line_limit - line_reduction_from_title); |
| 812 | 758 |
| 813 return message_line_limit; | 759 return message_line_limit; |
| 814 } | 760 } |
| 815 | 761 |
| 816 int NotificationView::GetMessageHeight(int width, int limit) const { | 762 int NotificationView::GetMessageHeight(int width, int limit) const { |
| 817 return message_view_ ? | 763 return message_view_ ? |
| 818 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; | 764 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; |
| 819 } | 765 } |
| 820 | 766 |
| 821 bool NotificationView::IsCloseButtonFocused() { | |
| 822 if (!close_button_) | |
| 823 return false; | |
| 824 | |
| 825 views::FocusManager* focus_manager = GetFocusManager(); | |
| 826 return focus_manager && | |
| 827 focus_manager->GetFocusedView() == close_button_.get(); | |
| 828 } | |
| 829 | |
| 830 void NotificationView::RequestFocusOnCloseButton() { | |
| 831 if (close_button_) | |
| 832 close_button_->RequestFocus(); | |
| 833 } | |
| 834 | |
| 835 bool NotificationView::IsPinned() { | |
| 836 return !close_button_; | |
| 837 } | |
| 838 | |
| 839 } // namespace message_center | 767 } // namespace message_center |
| OLD | NEW |