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
3 // found in the LICENSE file.
5 #include "ui/message_center/message_view.h"
7 #include "grit/ui_resources.h"
8 #include "grit/ui_strings.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/base/models/simple_menu_model.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/compositor/scoped_layer_animation_settings.h"
13 #include "ui/views/controls/button/image_button.h"
14 #include "ui/views/controls/menu/menu_model_adapter.h"
15 #include "ui/views/controls/menu/menu_runner.h"
16 #include "ui/views/controls/scroll_view.h"
17 #include "ui/views/widget/widget.h"
19 namespace message_center
{
22 const int kTogglePermissionCommand
= 0;
23 const int kToggleExtensionCommand
= 1;
24 const int kShowSettingsCommand
= 2;
26 // A dropdown menu for notifications.
27 class WebNotificationMenuModel
: public ui::SimpleMenuModel
,
28 public ui::SimpleMenuModel::Delegate
{
30 WebNotificationMenuModel(NotificationList::Delegate
* list_delegate
,
31 const NotificationList::Notification
& notification
)
32 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
33 list_delegate_(list_delegate
),
34 notification_(notification
) {
35 // Add 'disable notifications' menu item.
36 if (!notification
.extension_id
.empty()) {
37 AddItem(kToggleExtensionCommand
,
38 GetLabelForCommandId(kToggleExtensionCommand
));
39 } else if (!notification
.display_source
.empty()) {
40 AddItem(kTogglePermissionCommand
,
41 GetLabelForCommandId(kTogglePermissionCommand
));
43 // Add settings menu item.
44 if (!notification
.display_source
.empty()) {
45 AddItem(kShowSettingsCommand
,
46 GetLabelForCommandId(kShowSettingsCommand
));
50 virtual ~WebNotificationMenuModel() {
53 // Overridden from ui::SimpleMenuModel:
54 virtual string16
GetLabelForCommandId(int command_id
) const OVERRIDE
{
56 case kToggleExtensionCommand
:
57 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_EXTENSIONS_DISABLE
);
58 case kTogglePermissionCommand
:
59 return l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_SITE_DISABLE
,
60 notification_
.display_source
);
61 case kShowSettingsCommand
:
62 return l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS
);
69 // Overridden from ui::SimpleMenuModel::Delegate:
70 virtual bool IsCommandIdChecked(int command_id
) const OVERRIDE
{
74 virtual bool IsCommandIdEnabled(int command_id
) const OVERRIDE
{
78 virtual bool GetAcceleratorForCommandId(
80 ui::Accelerator
* accelerator
) OVERRIDE
{
84 virtual void ExecuteCommand(int command_id
) OVERRIDE
{
86 case kToggleExtensionCommand
:
87 list_delegate_
->DisableNotificationByExtension(notification_
.id
);
89 case kTogglePermissionCommand
:
90 list_delegate_
->DisableNotificationByUrl(notification_
.id
);
92 case kShowSettingsCommand
:
93 list_delegate_
->ShowNotificationSettings(notification_
.id
);
101 NotificationList::Delegate
* list_delegate_
;
102 NotificationList::Notification notification_
;
104 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel
);
107 MessageView::MessageView(
108 NotificationList::Delegate
* list_delegate
,
109 const NotificationList::Notification
& notification
)
110 : list_delegate_(list_delegate
),
111 notification_(notification
),
114 close_button_
= new views::ImageButton(this);
115 close_button_
->SetImage(
116 views::CustomButton::STATE_NORMAL
,
117 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(IDR_MESSAGE_CLOSE
));
118 close_button_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
119 views::ImageButton::ALIGN_MIDDLE
);
122 MessageView::MessageView() {
125 MessageView::~MessageView() {
128 bool MessageView::OnMousePressed(const ui::MouseEvent
& event
) {
129 if (event
.flags() & ui::EF_RIGHT_MOUSE_BUTTON
) {
130 ShowMenu(event
.location());
133 list_delegate_
->OnNotificationClicked(notification_
.id
);
137 void MessageView::OnGestureEvent(ui::GestureEvent
* event
) {
138 if (event
->type() == ui::ET_GESTURE_TAP
) {
139 list_delegate_
->OnNotificationClicked(notification_
.id
);
144 if (event
->type() == ui::ET_GESTURE_LONG_PRESS
) {
145 ShowMenu(event
->location());
150 SlideOutView::OnGestureEvent(event
);
151 if (event
->handled())
154 if (!event
->IsScrollGestureEvent())
158 scroller_
->OnGestureEvent(event
);
162 void MessageView::ButtonPressed(views::Button
* sender
,
163 const ui::Event
& event
) {
164 if (sender
== close_button_
)
165 list_delegate_
->SendRemoveNotification(notification_
.id
);
168 void MessageView::ShowMenu(gfx::Point screen_location
) {
169 WebNotificationMenuModel
menu_model(list_delegate_
, notification_
);
170 if (menu_model
.GetItemCount() == 0)
173 views::MenuModelAdapter
menu_model_adapter(&menu_model
);
174 views::MenuRunner
menu_runner(menu_model_adapter
.CreateMenu());
176 views::View::ConvertPointToScreen(this, &screen_location
);
177 ignore_result(menu_runner
.RunMenuAt(
178 GetWidget()->GetTopLevelWidget(),
180 gfx::Rect(screen_location
, gfx::Size()),
181 views::MenuItemView::TOPRIGHT
,
182 views::MenuRunner::HAS_MNEMONICS
));
185 void MessageView::OnSlideOut() {
186 list_delegate_
->SendRemoveNotification(notification_
.id
);
189 } // namespace message_center