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 "ash/system/web_notification/web_notification_tray.h" | 5 #include "ash/system/web_notification/web_notification_tray.h" |
6 | 6 |
7 #include "ash/root_window_controller.h" | 7 #include "ash/root_window_controller.h" |
8 #include "ash/shelf/shelf_layout_manager.h" | 8 #include "ash/shelf/shelf_layout_manager.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
11 #include "ash/system/status_area_widget.h" | 11 #include "ash/system/status_area_widget.h" |
12 #include "ash/system/tray/tray_background_view.h" | 12 #include "ash/system/tray/tray_background_view.h" |
13 #include "ash/system/tray/tray_bubble_wrapper.h" | 13 #include "ash/system/tray/tray_bubble_wrapper.h" |
14 #include "ash/system/tray/tray_constants.h" | 14 #include "ash/system/tray/tray_constants.h" |
| 15 #include "ash/system/tray/tray_views.h" |
| 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/utf_string_conversions.h" |
15 #include "grit/ash_resources.h" | 18 #include "grit/ash_resources.h" |
16 #include "grit/ui_strings.h" | 19 #include "grit/ui_strings.h" |
17 #include "ui/aura/root_window.h" | 20 #include "ui/aura/root_window.h" |
18 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
19 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
20 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/screen.h" | 24 #include "ui/gfx/screen.h" |
22 #include "ui/message_center/message_center_tray_delegate.h" | 25 #include "ui/message_center/message_center_tray_delegate.h" |
23 #include "ui/message_center/message_center_util.h" | 26 #include "ui/message_center/message_center_util.h" |
24 #include "ui/message_center/views/message_bubble_base.h" | 27 #include "ui/message_center/views/message_bubble_base.h" |
25 #include "ui/message_center/views/message_center_bubble.h" | 28 #include "ui/message_center/views/message_center_bubble.h" |
26 #include "ui/message_center/views/message_popup_bubble.h" | 29 #include "ui/message_center/views/message_popup_bubble.h" |
27 #include "ui/message_center/views/message_popup_collection.h" | 30 #include "ui/message_center/views/message_popup_collection.h" |
28 #include "ui/views/bubble/tray_bubble_view.h" | 31 #include "ui/views/bubble/tray_bubble_view.h" |
29 #include "ui/views/controls/button/image_button.h" | 32 #include "ui/views/controls/button/image_button.h" |
| 33 #include "ui/views/controls/label.h" |
30 #include "ui/views/controls/menu/menu_runner.h" | 34 #include "ui/views/controls/menu/menu_runner.h" |
31 | 35 |
32 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
33 | 37 |
34 namespace message_center { | 38 namespace message_center { |
35 | 39 |
36 MessageCenterTrayDelegate* CreateMessageCenterTray() { | 40 MessageCenterTrayDelegate* CreateMessageCenterTray() { |
37 // On Windows+Ash the Tray will not be hosted in ash::Shell. | 41 // On Windows+Ash the Tray will not be hosted in ash::Shell. |
38 NOTREACHED(); | 42 NOTREACHED(); |
39 return NULL; | 43 return NULL; |
40 } | 44 } |
41 | 45 |
42 } // namespace message_center | 46 } // namespace message_center |
43 | 47 |
44 #endif // defined(OS_CHROMEOS) | 48 #endif // defined(OS_CHROMEOS) |
45 | 49 |
46 namespace ash { | 50 namespace ash { |
| 51 namespace internal { |
| 52 namespace { |
47 | 53 |
48 namespace internal { | 54 // The text cannot be placed in the middle of the button vertically. This |
| 55 // constant is used to modify the vertical position of the text. |
| 56 const int kUnreadLabelBottomOffset = 6; |
| 57 |
| 58 } |
49 | 59 |
50 // Class to initialize and manage the WebNotificationBubble and | 60 // Class to initialize and manage the WebNotificationBubble and |
51 // TrayBubbleWrapper instances for a bubble. | 61 // TrayBubbleWrapper instances for a bubble. |
52 | 62 |
53 class WebNotificationBubbleWrapper { | 63 class WebNotificationBubbleWrapper { |
54 public: | 64 public: |
55 // Takes ownership of |bubble| and creates |bubble_wrapper_|. | 65 // Takes ownership of |bubble| and creates |bubble_wrapper_|. |
56 WebNotificationBubbleWrapper(WebNotificationTray* tray, | 66 WebNotificationBubbleWrapper(WebNotificationTray* tray, |
57 message_center::MessageBubbleBase* bubble) { | 67 message_center::MessageBubbleBase* bubble) { |
58 bubble_.reset(bubble); | 68 bubble_.reset(bubble); |
(...skipping 14 matching lines...) Expand all Loading... |
73 } | 83 } |
74 | 84 |
75 message_center::MessageBubbleBase* bubble() const { return bubble_.get(); } | 85 message_center::MessageBubbleBase* bubble() const { return bubble_.get(); } |
76 | 86 |
77 // Convenience accessors. | 87 // Convenience accessors. |
78 views::TrayBubbleView* bubble_view() const { return bubble_->bubble_view(); } | 88 views::TrayBubbleView* bubble_view() const { return bubble_->bubble_view(); } |
79 | 89 |
80 private: | 90 private: |
81 scoped_ptr<message_center::MessageBubbleBase> bubble_; | 91 scoped_ptr<message_center::MessageBubbleBase> bubble_; |
82 scoped_ptr<internal::TrayBubbleWrapper> bubble_wrapper_; | 92 scoped_ptr<internal::TrayBubbleWrapper> bubble_wrapper_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(WebNotificationBubbleWrapper); |
| 95 }; |
| 96 |
| 97 class WebNotificationButton : public views::ImageButton { |
| 98 public: |
| 99 WebNotificationButton(views::ButtonListener* listener) |
| 100 : views::ImageButton(listener), |
| 101 unread_label_(NULL) { |
| 102 } |
| 103 |
| 104 void SetUnreadCount(int unread_count) { |
| 105 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 106 if (unread_count == 0) { |
| 107 SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed( |
| 108 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_NORMAL)); |
| 109 SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed( |
| 110 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_HOVER)); |
| 111 SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed( |
| 112 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_PRESSED)); |
| 113 |
| 114 if (unread_label_) { |
| 115 delete unread_label_; |
| 116 unread_label_ = NULL; |
| 117 } |
| 118 } else { |
| 119 SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed( |
| 120 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_NORMAL)); |
| 121 SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed( |
| 122 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_HOVER)); |
| 123 SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed( |
| 124 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_PRESSED)); |
| 125 |
| 126 string16 text((unread_count > 9) ? |
| 127 UTF8ToUTF16("9+") : base::IntToString16(unread_count)); |
| 128 if (unread_label_) { |
| 129 unread_label_->SetText(text); |
| 130 } else { |
| 131 unread_label_ = new views::Label(text); |
| 132 SetupLabelForTray(unread_label_); |
| 133 AddChildView(unread_label_); |
| 134 } |
| 135 } |
| 136 InvalidateLayout(); |
| 137 SchedulePaint(); |
| 138 } |
| 139 |
| 140 protected: |
| 141 // Overridden from views::ImageButton: |
| 142 virtual void Layout() OVERRIDE { |
| 143 views::ImageButton::Layout(); |
| 144 if (unread_label_) { |
| 145 gfx::Rect parent_bounds(bounds()); |
| 146 parent_bounds.set_height( |
| 147 parent_bounds.height() - kUnreadLabelBottomOffset); |
| 148 unread_label_->SetBoundsRect(parent_bounds); |
| 149 } |
| 150 } |
| 151 |
| 152 private: |
| 153 views::Label* unread_label_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(WebNotificationButton); |
83 }; | 156 }; |
84 | 157 |
85 } // namespace internal | 158 } // namespace internal |
86 | 159 |
87 WebNotificationTray::WebNotificationTray( | 160 WebNotificationTray::WebNotificationTray( |
88 internal::StatusAreaWidget* status_area_widget) | 161 internal::StatusAreaWidget* status_area_widget) |
89 : TrayBackgroundView(status_area_widget), | 162 : TrayBackgroundView(status_area_widget), |
90 button_(NULL), | 163 button_(NULL), |
91 show_message_center_on_unlock_(false) { | 164 show_message_center_on_unlock_(false) { |
92 button_ = new views::ImageButton(this); | 165 button_ = new internal::WebNotificationButton(this); |
93 button_->set_triggerable_event_flags( | 166 button_->set_triggerable_event_flags( |
94 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); | 167 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); |
95 tray_container()->AddChildView(button_); | 168 tray_container()->AddChildView(button_); |
96 SetVisible(false); | 169 SetVisible(false); |
97 message_center_tray_.reset(new message_center::MessageCenterTray( | 170 message_center_tray_.reset(new message_center::MessageCenterTray( |
98 this, | 171 this, |
99 message_center::MessageCenter::Get())); | 172 message_center::MessageCenter::Get())); |
100 OnMessageCenterTrayChanged(); | 173 OnMessageCenterTrayChanged(); |
101 } | 174 } |
102 | 175 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 HideBubbleWithView(bubble_view); | 443 HideBubbleWithView(bubble_view); |
371 } | 444 } |
372 | 445 |
373 void WebNotificationTray::ButtonPressed(views::Button* sender, | 446 void WebNotificationTray::ButtonPressed(views::Button* sender, |
374 const ui::Event& event) { | 447 const ui::Event& event) { |
375 DCHECK_EQ(button_, sender); | 448 DCHECK_EQ(button_, sender); |
376 PerformAction(event); | 449 PerformAction(event); |
377 } | 450 } |
378 | 451 |
379 void WebNotificationTray::OnMessageCenterTrayChanged() { | 452 void WebNotificationTray::OnMessageCenterTrayChanged() { |
380 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
381 message_center::MessageCenter* message_center = | 453 message_center::MessageCenter* message_center = |
382 message_center_tray_->message_center(); | 454 message_center_tray_->message_center(); |
383 if (message_center->UnreadNotificationCount() > 0) { | 455 button_->SetUnreadCount(message_center->UnreadNotificationCount()); |
384 button_->SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed( | |
385 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_NORMAL)); | |
386 button_->SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed( | |
387 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_HOVER)); | |
388 button_->SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed( | |
389 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_PRESSED)); | |
390 } else { | |
391 button_->SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed( | |
392 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_NORMAL)); | |
393 button_->SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed( | |
394 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_HOVER)); | |
395 button_->SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed( | |
396 IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_PRESSED)); | |
397 } | |
398 if (IsMessageCenterBubbleVisible()) | 456 if (IsMessageCenterBubbleVisible()) |
399 button_->SetState(views::CustomButton::STATE_PRESSED); | 457 button_->SetState(views::CustomButton::STATE_PRESSED); |
400 else | 458 else |
401 button_->SetState(views::CustomButton::STATE_NORMAL); | 459 button_->SetState(views::CustomButton::STATE_NORMAL); |
402 // Change the visibility of the buttons here when rich notifications are not | 460 // Change the visibility of the buttons here when rich notifications are not |
403 // enabled. If rich notifications are enabled, the visibility is changed at | 461 // enabled. If rich notifications are enabled, the visibility is changed at |
404 // UpdateAfterLoginStatusChange() since the visibility won't depend on the | 462 // UpdateAfterLoginStatusChange() since the visibility won't depend on the |
405 // number of notifications. | 463 // number of notifications. |
406 if (!message_center::IsRichNotificationEnabled()) { | 464 if (!message_center::IsRichNotificationEnabled()) { |
407 bool is_visible = | 465 bool is_visible = |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 | 500 |
443 message_center::MessagePopupBubble* | 501 message_center::MessagePopupBubble* |
444 WebNotificationTray::GetPopupBubbleForTest() { | 502 WebNotificationTray::GetPopupBubbleForTest() { |
445 if (!popup_bubble()) | 503 if (!popup_bubble()) |
446 return NULL; | 504 return NULL; |
447 return static_cast<message_center::MessagePopupBubble*>( | 505 return static_cast<message_center::MessagePopupBubble*>( |
448 popup_bubble()->bubble()); | 506 popup_bubble()->bubble()); |
449 } | 507 } |
450 | 508 |
451 } // namespace ash | 509 } // namespace ash |
OLD | NEW |