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_view.h" | 5 #include "ui/message_center/message_view.h" |
6 | 6 |
7 #include "grit/ash_resources.h" | 7 #include "grit/ui_resources.h" |
8 #include "grit/ash_strings.h" | 8 #include "grit/ui_strings.h" |
9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
10 #include "ui/base/models/simple_menu_model.h" | 10 #include "ui/base/models/simple_menu_model.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 #include "ui/compositor/scoped_layer_animation_settings.h" | 12 #include "ui/compositor/scoped_layer_animation_settings.h" |
13 #include "ui/views/controls/button/image_button.h" | 13 #include "ui/views/controls/button/image_button.h" |
14 #include "ui/views/controls/image_view.h" | 14 #include "ui/views/controls/image_view.h" |
15 #include "ui/views/controls/scroll_view.h" | |
16 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
17 #include "ui/views/controls/menu/menu_model_adapter.h" | 16 #include "ui/views/controls/menu/menu_model_adapter.h" |
18 #include "ui/views/controls/menu/menu_runner.h" | 17 #include "ui/views/controls/menu/menu_runner.h" |
| 18 #include "ui/views/controls/scroll_view.h" |
19 #include "ui/views/layout/grid_layout.h" | 19 #include "ui/views/layout/grid_layout.h" |
20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 const int kPaddingHorizontal = 18; | 23 const int kPaddingHorizontal = 18; |
24 const int kPaddingBetweenItems = 10; | 24 const int kPaddingBetweenItems = 10; |
25 } | 25 } |
26 | 26 |
27 namespace message_center { | 27 namespace message_center { |
28 | 28 |
29 const SkColor kNotificationColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); | 29 const SkColor kNotificationColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); |
30 const SkColor kNotificationReadColor = SkColorSetRGB(0xfa, 0xfa, 0xfa); | 30 const SkColor kNotificationReadColor = SkColorSetRGB(0xfa, 0xfa, 0xfa); |
31 | 31 |
32 // Menu constants | 32 // Menu constants |
33 const int kTogglePermissionCommand = 0; | 33 const int kTogglePermissionCommand = 0; |
34 const int kToggleExtensionCommand = 1; | 34 const int kToggleExtensionCommand = 1; |
35 const int kShowSettingsCommand = 2; | 35 const int kShowSettingsCommand = 2; |
36 | 36 |
37 // A dropdown menu for notifications. | 37 // A dropdown menu for notifications. |
38 class WebNotificationMenuModel : public ui::SimpleMenuModel, | 38 class WebNotificationMenuModel : public ui::SimpleMenuModel, |
39 public ui::SimpleMenuModel::Delegate { | 39 public ui::SimpleMenuModel::Delegate { |
40 public: | 40 public: |
41 WebNotificationMenuModel(WebNotificationList::Delegate* list_delegate, | 41 WebNotificationMenuModel(NotificationList::Delegate* list_delegate, |
42 const WebNotification& notification) | 42 const NotificationList::Notification& notification) |
43 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 43 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
44 list_delegate_(list_delegate), | 44 list_delegate_(list_delegate), |
45 notification_(notification) { | 45 notification_(notification) { |
46 // Add 'disable notifications' menu item. | 46 // Add 'disable notifications' menu item. |
47 if (!notification.extension_id.empty()) { | 47 if (!notification.extension_id.empty()) { |
48 AddItem(kToggleExtensionCommand, | 48 AddItem(kToggleExtensionCommand, |
49 GetLabelForCommandId(kToggleExtensionCommand)); | 49 GetLabelForCommandId(kToggleExtensionCommand)); |
50 } else if (!notification.display_source.empty()) { | 50 } else if (!notification.display_source.empty()) { |
51 AddItem(kTogglePermissionCommand, | 51 AddItem(kTogglePermissionCommand, |
52 GetLabelForCommandId(kTogglePermissionCommand)); | 52 GetLabelForCommandId(kTogglePermissionCommand)); |
53 } | 53 } |
54 // Add settings menu item. | 54 // Add settings menu item. |
55 if (!notification.display_source.empty()) { | 55 if (!notification.display_source.empty()) { |
56 AddItem(kShowSettingsCommand, | 56 AddItem(kShowSettingsCommand, |
57 GetLabelForCommandId(kShowSettingsCommand)); | 57 GetLabelForCommandId(kShowSettingsCommand)); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 virtual ~WebNotificationMenuModel() { | 61 virtual ~WebNotificationMenuModel() { |
62 } | 62 } |
63 | 63 |
64 // Overridden from ui::SimpleMenuModel: | 64 // Overridden from ui::SimpleMenuModel: |
65 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE { | 65 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE { |
66 switch (command_id) { | 66 switch (command_id) { |
67 case kToggleExtensionCommand: | 67 case kToggleExtensionCommand: |
68 return l10n_util::GetStringUTF16( | 68 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_EXTENSIONS_DISABLE); |
69 IDS_ASH_WEB_NOTFICATION_TRAY_EXTENSIONS_DISABLE); | |
70 case kTogglePermissionCommand: | 69 case kTogglePermissionCommand: |
71 return l10n_util::GetStringFUTF16( | 70 return l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_SITE_DISABLE, |
72 IDS_ASH_WEB_NOTFICATION_TRAY_SITE_DISABLE, | 71 notification_.display_source); |
73 notification_.display_source); | |
74 case kShowSettingsCommand: | 72 case kShowSettingsCommand: |
75 return l10n_util::GetStringUTF16( | 73 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS); |
76 IDS_ASH_WEB_NOTFICATION_TRAY_SETTINGS); | |
77 default: | 74 default: |
78 NOTREACHED(); | 75 NOTREACHED(); |
79 } | 76 } |
80 return string16(); | 77 return string16(); |
81 } | 78 } |
82 | 79 |
83 // Overridden from ui::SimpleMenuModel::Delegate: | 80 // Overridden from ui::SimpleMenuModel::Delegate: |
84 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { | 81 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE { |
85 return false; | 82 return false; |
86 } | 83 } |
(...skipping 18 matching lines...) Expand all Loading... |
105 break; | 102 break; |
106 case kShowSettingsCommand: | 103 case kShowSettingsCommand: |
107 list_delegate_->ShowNotificationSettings(notification_.id); | 104 list_delegate_->ShowNotificationSettings(notification_.id); |
108 break; | 105 break; |
109 default: | 106 default: |
110 NOTREACHED(); | 107 NOTREACHED(); |
111 } | 108 } |
112 } | 109 } |
113 | 110 |
114 private: | 111 private: |
115 WebNotificationList::Delegate* list_delegate_; | 112 NotificationList::Delegate* list_delegate_; |
116 WebNotification notification_; | 113 NotificationList::Notification notification_; |
117 | 114 |
118 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel); | 115 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel); |
119 }; | 116 }; |
120 | 117 |
121 WebNotificationView::WebNotificationView( | 118 MessageView::MessageView( |
122 WebNotificationList::Delegate* list_delegate, | 119 NotificationList::Delegate* list_delegate, |
123 const WebNotification& notification, | 120 const NotificationList::Notification& notification, |
124 int scroll_bar_width) | 121 int scroll_bar_width) |
125 : list_delegate_(list_delegate), | 122 : list_delegate_(list_delegate), |
126 notification_(notification), | 123 notification_(notification), |
127 icon_(NULL), | 124 icon_(NULL), |
128 close_button_(NULL), | 125 close_button_(NULL), |
129 scroller_(NULL), | 126 scroller_(NULL), |
130 gesture_scroll_amount_(0.f) { | 127 gesture_scroll_amount_(0.f) { |
131 SkColor bg_color = notification.is_read ? | 128 SkColor bg_color = notification.is_read ? |
132 kNotificationReadColor : kNotificationColor; | 129 kNotificationReadColor : kNotificationColor; |
133 set_background(views::Background::CreateSolidBackground(bg_color)); | 130 set_background(views::Background::CreateSolidBackground(bg_color)); |
134 SetPaintToLayer(true); | 131 SetPaintToLayer(true); |
135 SetFillsBoundsOpaquely(false); | 132 SetFillsBoundsOpaquely(false); |
136 | 133 |
137 icon_ = new views::ImageView; | 134 icon_ = new views::ImageView; |
138 icon_->SetImageSize( | 135 icon_->SetImageSize( |
139 gfx::Size(kWebNotificationIconSize, kWebNotificationIconSize)); | 136 gfx::Size(kWebNotificationIconSize, kWebNotificationIconSize)); |
140 icon_->SetImage(notification.image); | 137 icon_->SetImage(notification.image); |
141 | 138 |
142 views::Label* title = new views::Label(notification.title); | 139 views::Label* title = new views::Label(notification.title); |
143 title->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 140 title->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
144 title->SetFont(title->font().DeriveFont(0, gfx::Font::BOLD)); | 141 title->SetFont(title->font().DeriveFont(0, gfx::Font::BOLD)); |
145 views::Label* message = new views::Label(notification.message); | 142 views::Label* message = new views::Label(notification.message); |
146 message->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 143 message->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
147 message->SetMultiLine(true); | 144 message->SetMultiLine(true); |
148 | 145 |
149 close_button_ = new views::ImageButton(this); | 146 close_button_ = new views::ImageButton(this); |
150 close_button_->SetImage( | 147 close_button_->SetImage( |
151 views::CustomButton::BS_NORMAL, | 148 views::CustomButton::BS_NORMAL, |
152 ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 149 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(IDR_MESSAGE_CLOSE)); |
153 IDR_AURA_UBER_TRAY_NOTIFY_CLOSE)); | |
154 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | 150 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, |
155 views::ImageButton::ALIGN_MIDDLE); | 151 views::ImageButton::ALIGN_MIDDLE); |
156 | 152 |
157 views::GridLayout* layout = new views::GridLayout(this); | 153 views::GridLayout* layout = new views::GridLayout(this); |
158 SetLayoutManager(layout); | 154 SetLayoutManager(layout); |
159 | 155 |
160 views::ColumnSet* columns = layout->AddColumnSet(0); | 156 views::ColumnSet* columns = layout->AddColumnSet(0); |
161 | 157 |
162 const int padding_width = kPaddingHorizontal / 2; | 158 const int padding_width = kPaddingHorizontal / 2; |
163 columns->AddPaddingColumn(0, padding_width); | 159 columns->AddPaddingColumn(0, padding_width); |
(...skipping 30 matching lines...) Expand all Loading... |
194 layout->AddView(icon_, 1, 2); | 190 layout->AddView(icon_, 1, 2); |
195 layout->AddView(title, 1, 1); | 191 layout->AddView(title, 1, 1); |
196 layout->AddView(close_button_, 1, 1); | 192 layout->AddView(close_button_, 1, 1); |
197 | 193 |
198 layout->StartRow(0, 0); | 194 layout->StartRow(0, 0); |
199 layout->SkipColumns(2); | 195 layout->SkipColumns(2); |
200 layout->AddView(message, 1, 1); | 196 layout->AddView(message, 1, 1); |
201 layout->AddPaddingRow(0, kPaddingBetweenItems); | 197 layout->AddPaddingRow(0, kPaddingBetweenItems); |
202 } | 198 } |
203 | 199 |
204 WebNotificationView::~WebNotificationView() { | 200 MessageView::~MessageView() { |
205 } | 201 } |
206 | 202 |
207 bool WebNotificationView::OnMousePressed(const ui::MouseEvent& event) { | 203 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { |
208 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) { | 204 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) { |
209 ShowMenu(event.location()); | 205 ShowMenu(event.location()); |
210 return true; | 206 return true; |
211 } | 207 } |
212 list_delegate_->OnNotificationClicked(notification_.id); | 208 list_delegate_->OnNotificationClicked(notification_.id); |
213 return true; | 209 return true; |
214 } | 210 } |
215 | 211 |
216 ui::EventResult WebNotificationView::OnGestureEvent( | 212 ui::EventResult MessageView::OnGestureEvent( |
217 const ui::GestureEvent& event) { | 213 const ui::GestureEvent& event) { |
218 if (event.type() == ui::ET_GESTURE_TAP) { | 214 if (event.type() == ui::ET_GESTURE_TAP) { |
219 list_delegate_->OnNotificationClicked(notification_.id); | 215 list_delegate_->OnNotificationClicked(notification_.id); |
220 return ui::ER_CONSUMED; | 216 return ui::ER_CONSUMED; |
221 } | 217 } |
222 | 218 |
223 if (event.type() == ui::ET_GESTURE_LONG_PRESS) { | 219 if (event.type() == ui::ET_GESTURE_LONG_PRESS) { |
224 ShowMenu(event.location()); | 220 ShowMenu(event.location()); |
225 return ui::ER_CONSUMED; | 221 return ui::ER_CONSUMED; |
226 } | 222 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | 257 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); |
262 else | 258 else |
263 RestoreVisualState(); | 259 RestoreVisualState(); |
264 } | 260 } |
265 | 261 |
266 if (scroller_) | 262 if (scroller_) |
267 scroller_->OnGestureEvent(event); | 263 scroller_->OnGestureEvent(event); |
268 return ui::ER_CONSUMED; | 264 return ui::ER_CONSUMED; |
269 } | 265 } |
270 | 266 |
271 void WebNotificationView::ButtonPressed(views::Button* sender, | 267 void MessageView::ButtonPressed(views::Button* sender, |
272 const ui::Event& event) { | 268 const ui::Event& event) { |
273 if (sender == close_button_) | 269 if (sender == close_button_) |
274 list_delegate_->SendRemoveNotification(notification_.id); | 270 list_delegate_->SendRemoveNotification(notification_.id); |
275 } | 271 } |
276 | 272 |
277 void WebNotificationView::OnImplicitAnimationsCompleted() { | 273 void MessageView::OnImplicitAnimationsCompleted() { |
278 list_delegate_->SendRemoveNotification(notification_.id); | 274 list_delegate_->SendRemoveNotification(notification_.id); |
279 } | 275 } |
280 | 276 |
281 void WebNotificationView::ShowMenu(gfx::Point screen_location) { | 277 void MessageView::ShowMenu(gfx::Point screen_location) { |
282 WebNotificationMenuModel menu_model(list_delegate_, notification_); | 278 WebNotificationMenuModel menu_model(list_delegate_, notification_); |
283 if (menu_model.GetItemCount() == 0) | 279 if (menu_model.GetItemCount() == 0) |
284 return; | 280 return; |
285 | 281 |
286 views::MenuModelAdapter menu_model_adapter(&menu_model); | 282 views::MenuModelAdapter menu_model_adapter(&menu_model); |
287 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); | 283 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); |
288 | 284 |
289 views::View::ConvertPointToScreen(this, &screen_location); | 285 views::View::ConvertPointToScreen(this, &screen_location); |
290 ignore_result(menu_runner.RunMenuAt( | 286 ignore_result(menu_runner.RunMenuAt( |
291 GetWidget()->GetTopLevelWidget(), | 287 GetWidget()->GetTopLevelWidget(), |
292 NULL, | 288 NULL, |
293 gfx::Rect(screen_location, gfx::Size()), | 289 gfx::Rect(screen_location, gfx::Size()), |
294 views::MenuItemView::TOPRIGHT, | 290 views::MenuItemView::TOPRIGHT, |
295 views::MenuRunner::HAS_MNEMONICS)); | 291 views::MenuRunner::HAS_MNEMONICS)); |
296 } | 292 } |
297 | 293 |
298 void WebNotificationView::RestoreVisualState() { | 294 void MessageView::RestoreVisualState() { |
299 // Restore the layer state. | 295 // Restore the layer state. |
300 const int kSwipeRestoreDurationMS = 150; | 296 const int kSwipeRestoreDurationMS = 150; |
301 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 297 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
302 settings.SetTransitionDuration( | 298 settings.SetTransitionDuration( |
303 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 299 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
304 layer()->SetTransform(gfx::Transform()); | 300 layer()->SetTransform(gfx::Transform()); |
305 layer()->SetOpacity(1.f); | 301 layer()->SetOpacity(1.f); |
306 } | 302 } |
307 | 303 |
308 void WebNotificationView::SlideOutAndClose(SlideDirection direction) { | 304 void MessageView::SlideOutAndClose(SlideDirection direction) { |
309 const int kSwipeOutTotalDurationMS = 150; | 305 const int kSwipeOutTotalDurationMS = 150; |
310 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); | 306 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); |
311 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 307 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
312 settings.SetTransitionDuration( | 308 settings.SetTransitionDuration( |
313 base::TimeDelta::FromMilliseconds(swipe_out_duration)); | 309 base::TimeDelta::FromMilliseconds(swipe_out_duration)); |
314 settings.AddObserver(this); | 310 settings.AddObserver(this); |
315 | 311 |
316 gfx::Transform transform; | 312 gfx::Transform transform; |
317 transform.SetTranslateX(direction == SLIDE_LEFT ? -width() : width()); | 313 transform.SetTranslateX(direction == SLIDE_LEFT ? -width() : width()); |
318 layer()->SetTransform(transform); | 314 layer()->SetTransform(transform); |
319 layer()->SetOpacity(0.f); | 315 layer()->SetOpacity(0.f); |
320 } | 316 } |
321 | 317 |
322 } // namespace message_center | 318 } // namespace message_center |
OLD | NEW |