Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / message_center / message_view.cc
blob2fbc8341d26539a1ad36c72547278347f085eb22
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 {
21 // Menu constants
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 {
29 public:
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 {
55 switch (command_id) {
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);
63 default:
64 NOTREACHED();
66 return string16();
69 // Overridden from ui::SimpleMenuModel::Delegate:
70 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
71 return false;
74 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
75 return false;
78 virtual bool GetAcceleratorForCommandId(
79 int command_id,
80 ui::Accelerator* accelerator) OVERRIDE {
81 return false;
84 virtual void ExecuteCommand(int command_id) OVERRIDE {
85 switch (command_id) {
86 case kToggleExtensionCommand:
87 list_delegate_->DisableNotificationByExtension(notification_.id);
88 break;
89 case kTogglePermissionCommand:
90 list_delegate_->DisableNotificationByUrl(notification_.id);
91 break;
92 case kShowSettingsCommand:
93 list_delegate_->ShowNotificationSettings(notification_.id);
94 break;
95 default:
96 NOTREACHED();
100 private:
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),
112 close_button_(NULL),
113 scroller_(NULL) {
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());
131 return true;
133 list_delegate_->OnNotificationClicked(notification_.id);
134 return true;
137 void MessageView::OnGestureEvent(ui::GestureEvent* event) {
138 if (event->type() == ui::ET_GESTURE_TAP) {
139 list_delegate_->OnNotificationClicked(notification_.id);
140 event->SetHandled();
141 return;
144 if (event->type() == ui::ET_GESTURE_LONG_PRESS) {
145 ShowMenu(event->location());
146 event->SetHandled();
147 return;
150 SlideOutView::OnGestureEvent(event);
151 if (event->handled())
152 return;
154 if (!event->IsScrollGestureEvent())
155 return;
157 if (scroller_)
158 scroller_->OnGestureEvent(event);
159 event->SetHandled();
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)
171 return;
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(),
179 NULL,
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