Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Side by Side Diff: ui/message_center/views/message_view.cc

Issue 75133006: Move knowledge about MessageCenter out of MessageView. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more fix Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/message_center/views/message_view.h ('k') | ui/message_center/views/notification_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/message_view.h" 5 #include "ui/message_center/views/message_view.h"
6 6
7 #include "grit/ui_resources.h" 7 #include "grit/ui_resources.h"
8 #include "grit/ui_strings.h" 8 #include "grit/ui_strings.h"
9 #include "ui/base/accessibility/accessible_view_state.h" 9 #include "ui/base/accessibility/accessible_view_state.h"
10 #include "ui/base/l10n/l10n_util.h" 10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/models/simple_menu_model.h" 11 #include "ui/base/models/simple_menu_model.h"
12 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/compositor/scoped_layer_animation_settings.h" 13 #include "ui/compositor/scoped_layer_animation_settings.h"
14 #include "ui/gfx/canvas.h" 14 #include "ui/gfx/canvas.h"
15 #include "ui/message_center/message_center.h" 15 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/message_center_style.h" 16 #include "ui/message_center/message_center_style.h"
17 #include "ui/message_center/message_center_tray.h"
18 #include "ui/message_center/message_center_util.h" 17 #include "ui/message_center/message_center_util.h"
19 #include "ui/message_center/views/padded_button.h" 18 #include "ui/message_center/views/padded_button.h"
20 #include "ui/views/context_menu_controller.h" 19 #include "ui/views/context_menu_controller.h"
21 #include "ui/views/controls/button/image_button.h" 20 #include "ui/views/controls/button/image_button.h"
22 #include "ui/views/controls/menu/menu_runner.h" 21 #include "ui/views/controls/menu/menu_runner.h"
23 #include "ui/views/controls/scroll_view.h" 22 #include "ui/views/controls/scroll_view.h"
24 #include "ui/views/shadow_border.h" 23 #include "ui/views/shadow_border.h"
25 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
26 25
27 namespace { 26 namespace {
28 27
29 const int kCloseIconTopPadding = 5; 28 const int kCloseIconTopPadding = 5;
30 const int kCloseIconRightPadding = 5; 29 const int kCloseIconRightPadding = 5;
31 30
32 const int kShadowOffset = 1; 31 const int kShadowOffset = 1;
33 const int kShadowBlur = 4; 32 const int kShadowBlur = 4;
34 33
35 // Menu constants 34 // Menu constants
36 const int kTogglePermissionCommand = 0; 35 const int kTogglePermissionCommand = 0;
37 const int kShowSettingsCommand = 1; 36 const int kShowSettingsCommand = 1;
38 37
39 // A dropdown menu for notifications. 38 // A dropdown menu for notifications.
40 class MenuModel : public ui::SimpleMenuModel, 39 class MenuModel : public ui::SimpleMenuModel,
41 public ui::SimpleMenuModel::Delegate { 40 public ui::SimpleMenuModel::Delegate {
42 public: 41 public:
43 MenuModel(message_center::MessageCenter* message_center, 42 MenuModel(message_center::MessageView* message_view,
44 message_center::MessageCenterTray* tray, 43 const string16& display_source);
45 const std::string& notification_id,
46 const string16& display_source,
47 const message_center::NotifierId& notifier_id);
48 virtual ~MenuModel(); 44 virtual ~MenuModel();
49 45
50 // Overridden from ui::SimpleMenuModel::Delegate: 46 // Overridden from ui::SimpleMenuModel::Delegate:
51 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; 47 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
52 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 48 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
53 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 49 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
54 virtual bool GetAcceleratorForCommandId( 50 virtual bool GetAcceleratorForCommandId(
55 int command_id, 51 int command_id,
56 ui::Accelerator* accelerator) OVERRIDE; 52 ui::Accelerator* accelerator) OVERRIDE;
57 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 53 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
58 54
59 private: 55 private:
60 message_center::MessageCenter* message_center_; // Weak reference. 56 message_center::MessageView* message_view_; // Weak, owns us.
61 message_center::MessageCenterTray* tray_; // Weak reference.
62 std::string notification_id_;
63 message_center::NotifierId notifier_id_;
64
65 DISALLOW_COPY_AND_ASSIGN(MenuModel); 57 DISALLOW_COPY_AND_ASSIGN(MenuModel);
66 }; 58 };
67 59
68 MenuModel::MenuModel(message_center::MessageCenter* message_center, 60 MenuModel::MenuModel(message_center::MessageView* message_view,
69 message_center::MessageCenterTray* tray, 61 const string16& display_source)
70 const std::string& notification_id,
71 const string16& display_source,
72 const message_center::NotifierId& notifier_id)
73 : ui::SimpleMenuModel(this), 62 : ui::SimpleMenuModel(this),
74 message_center_(message_center), 63 message_view_(message_view) {
75 tray_(tray),
76 notification_id_(notification_id),
77 notifier_id_(notifier_id) {
78 // Add 'disable notifications' menu item. 64 // Add 'disable notifications' menu item.
79 if (!display_source.empty()) { 65 if (!display_source.empty()) {
80 AddItem(kTogglePermissionCommand, 66 AddItem(kTogglePermissionCommand,
81 l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_NOTIFIER_DISABLE, 67 l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_NOTIFIER_DISABLE,
82 display_source)); 68 display_source));
83 } 69 }
84 // Add settings menu item. 70 // Add settings menu item.
85 AddItem(kShowSettingsCommand, 71 AddItem(kShowSettingsCommand,
86 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS)); 72 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS));
87 } 73 }
(...skipping 14 matching lines...) Expand all
102 } 88 }
103 89
104 bool MenuModel::GetAcceleratorForCommandId(int command_id, 90 bool MenuModel::GetAcceleratorForCommandId(int command_id,
105 ui::Accelerator* accelerator) { 91 ui::Accelerator* accelerator) {
106 return false; 92 return false;
107 } 93 }
108 94
109 void MenuModel::ExecuteCommand(int command_id, int event_flags) { 95 void MenuModel::ExecuteCommand(int command_id, int event_flags) {
110 switch (command_id) { 96 switch (command_id) {
111 case kTogglePermissionCommand: 97 case kTogglePermissionCommand:
112 message_center_->DisableNotificationsByNotifier(notifier_id_); 98 message_view_->DisableNotificationsFromThisSource();
113 break; 99 break;
114 case kShowSettingsCommand: 100 case kShowSettingsCommand:
115 // |tray_| may be NULL in tests. 101 message_view_->ShowNotifierSettingsBubble();
116 if (tray_)
117 tray_->ShowNotifierSettingsBubble();
118 break; 102 break;
119 default: 103 default:
120 NOTREACHED(); 104 NOTREACHED();
121 } 105 }
122 } 106 }
123 107
124 } // namespace 108 } // namespace
125 109
126 namespace message_center { 110 namespace message_center {
127 111
128 class MessageViewContextMenuController : public views::ContextMenuController { 112 class MessageViewContextMenuController : public views::ContextMenuController {
129 public: 113 public:
130 MessageViewContextMenuController( 114 MessageViewContextMenuController(MessageView* message_view,
131 MessageCenter* message_center, 115 const string16& display_source);
132 MessageCenterTray* tray,
133 const Notification& notification);
134 virtual ~MessageViewContextMenuController(); 116 virtual ~MessageViewContextMenuController();
135 117
136 protected: 118 protected:
137 // Overridden from views::ContextMenuController: 119 // Overridden from views::ContextMenuController:
138 virtual void ShowContextMenuForView(views::View* source, 120 virtual void ShowContextMenuForView(views::View* source,
139 const gfx::Point& point, 121 const gfx::Point& point,
140 ui::MenuSourceType source_type) OVERRIDE; 122 ui::MenuSourceType source_type) OVERRIDE;
141 123
142 MessageCenter* message_center_; // Weak reference. 124 MessageView* message_view_; // Weak, owns us.
143 MessageCenterTray* tray_; // Weak reference.
144 std::string notification_id_;
145 string16 display_source_; 125 string16 display_source_;
146 NotifierId notifier_id_;
147 }; 126 };
148 127
149 MessageViewContextMenuController::MessageViewContextMenuController( 128 MessageViewContextMenuController::MessageViewContextMenuController(
150 MessageCenter* message_center, 129 MessageView* message_view,
151 MessageCenterTray* tray, 130 const string16& display_source)
152 const Notification& notification) 131 : message_view_(message_view),
153 : message_center_(message_center), 132 display_source_(display_source) {
154 tray_(tray),
155 notification_id_(notification.id()),
156 display_source_(notification.display_source()),
157 notifier_id_(notification.notifier_id()) {
158 } 133 }
159 134
160 MessageViewContextMenuController::~MessageViewContextMenuController() { 135 MessageViewContextMenuController::~MessageViewContextMenuController() {
161 } 136 }
162 137
163 void MessageViewContextMenuController::ShowContextMenuForView( 138 void MessageViewContextMenuController::ShowContextMenuForView(
164 views::View* source, 139 views::View* source,
165 const gfx::Point& point, 140 const gfx::Point& point,
166 ui::MenuSourceType source_type) { 141 ui::MenuSourceType source_type) {
167 MenuModel menu_model(message_center_, tray_, notification_id_, 142 MenuModel menu_model(message_view_, display_source_);
168 display_source_, notifier_id_);
169 if (menu_model.GetItemCount() == 0) 143 if (menu_model.GetItemCount() == 0)
170 return; 144 return;
171 145
172 views::MenuRunner menu_runner(&menu_model); 146 views::MenuRunner menu_runner(&menu_model);
173 147
174 ignore_result(menu_runner.RunMenuAt( 148 ignore_result(menu_runner.RunMenuAt(
175 source->GetWidget()->GetTopLevelWidget(), 149 source->GetWidget()->GetTopLevelWidget(),
176 NULL, 150 NULL,
177 gfx::Rect(point, gfx::Size()), 151 gfx::Rect(point, gfx::Size()),
178 views::MenuItemView::TOPRIGHT, 152 views::MenuItemView::TOPRIGHT,
179 source_type, 153 source_type,
180 views::MenuRunner::HAS_MNEMONICS)); 154 views::MenuRunner::HAS_MNEMONICS));
181 } 155 }
182 156
183 MessageView::MessageView(const Notification& notification, 157 MessageView::MessageView(const string16& display_source)
184 MessageCenter* message_center, 158 : context_menu_controller_(
185 MessageCenterTray* tray) 159 new MessageViewContextMenuController(this, display_source)),
186 : message_center_(message_center),
187 notification_id_(notification.id()),
188 context_menu_controller_(new MessageViewContextMenuController(
189 message_center, tray, notification)),
190 scroller_(NULL) { 160 scroller_(NULL) {
191 set_focusable(true); 161 set_focusable(true);
192 set_context_menu_controller(context_menu_controller_.get()); 162 set_context_menu_controller(context_menu_controller_.get());
193 163
194 PaddedButton *close = new PaddedButton(this); 164 PaddedButton *close = new PaddedButton(this);
195 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding); 165 close->SetPadding(-kCloseIconRightPadding, kCloseIconTopPadding);
196 close->SetNormalImage(IDR_NOTIFICATION_CLOSE); 166 close->SetNormalImage(IDR_NOTIFICATION_CLOSE);
197 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER); 167 close->SetHoveredImage(IDR_NOTIFICATION_CLOSE_HOVER);
198 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED); 168 close->SetPressedImage(IDR_NOTIFICATION_CLOSE_PRESSED);
199 close->set_owned_by_client(); 169 close->set_owned_by_client();
200 close->set_animate_on_state_change(false); 170 close->set_animate_on_state_change(false);
201 close->SetAccessibleName(l10n_util::GetStringUTF16( 171 close->SetAccessibleName(l10n_util::GetStringUTF16(
202 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME)); 172 IDS_MESSAGE_CENTER_CLOSE_NOTIFICATION_BUTTON_ACCESSIBLE_NAME));
203 close_button_.reset(close); 173 close_button_.reset(close);
204 } 174 }
205 175
206 MessageView::MessageView() {
207 }
208
209 MessageView::~MessageView() { 176 MessageView::~MessageView() {
210 } 177 }
211 178
212 // static 179 // static
213 gfx::Insets MessageView::GetShadowInsets() { 180 gfx::Insets MessageView::GetShadowInsets() {
214 return gfx::Insets(kShadowBlur / 2 - kShadowOffset, 181 return gfx::Insets(kShadowBlur / 2 - kShadowOffset,
215 kShadowBlur / 2, 182 kShadowBlur / 2,
216 kShadowBlur / 2 + kShadowOffset, 183 kShadowBlur / 2 + kShadowOffset,
217 kShadowBlur / 2); 184 kShadowBlur / 2);
218 } 185 }
(...skipping 13 matching lines...) Expand all
232 void MessageView::RequestFocusOnCloseButton() { 199 void MessageView::RequestFocusOnCloseButton() {
233 close_button_->RequestFocus(); 200 close_button_->RequestFocus();
234 } 201 }
235 202
236 void MessageView::GetAccessibleState(ui::AccessibleViewState* state) { 203 void MessageView::GetAccessibleState(ui::AccessibleViewState* state) {
237 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 204 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
238 state->name = accessible_name_; 205 state->name = accessible_name_;
239 } 206 }
240 207
241 bool MessageView::OnMousePressed(const ui::MouseEvent& event) { 208 bool MessageView::OnMousePressed(const ui::MouseEvent& event) {
242 if (event.IsOnlyLeftMouseButton()) { 209 if (!event.IsOnlyLeftMouseButton())
243 message_center_->ClickOnNotification(notification_id_); 210 return false;
244 return true; 211
245 } 212 ClickOnNotification();
246 return false; 213 return true;
247 } 214 }
248 215
249 bool MessageView::OnKeyPressed(const ui::KeyEvent& event) { 216 bool MessageView::OnKeyPressed(const ui::KeyEvent& event) {
250 if (event.flags() != ui::EF_NONE) 217 if (event.flags() != ui::EF_NONE)
251 return false; 218 return false;
252 219
253 if (event.key_code() == ui::VKEY_RETURN) { 220 if (event.key_code() == ui::VKEY_RETURN) {
254 message_center_->ClickOnNotification(notification_id_); 221 ClickOnNotification();
255 return true; 222 return true;
256 } else if ((event.key_code() == ui::VKEY_DELETE || 223 } else if ((event.key_code() == ui::VKEY_DELETE ||
257 event.key_code() == ui::VKEY_BACK)) { 224 event.key_code() == ui::VKEY_BACK)) {
258 message_center_->RemoveNotification(notification_id_, true); // By user. 225 RemoveNotification(true); // By user.
259 return true; 226 return true;
260 } 227 }
261 228
262 return false; 229 return false;
263 } 230 }
264 231
265 bool MessageView::OnKeyReleased(const ui::KeyEvent& event) { 232 bool MessageView::OnKeyReleased(const ui::KeyEvent& event) {
266 // Space key handling is triggerred at key-release timing. See 233 // Space key handling is triggerred at key-release timing. See
267 // ui/views/controls/buttons/custom_button.cc for why. 234 // ui/views/controls/buttons/custom_button.cc for why.
268 if (event.flags() != ui::EF_NONE || event.flags() != ui::VKEY_SPACE) 235 if (event.flags() != ui::EF_NONE || event.flags() != ui::VKEY_SPACE)
269 return false; 236 return false;
270 237
271 message_center_->ClickOnNotification(notification_id_); 238 ClickOnNotification();
272 return true; 239 return true;
273 } 240 }
274 241
275 void MessageView::OnGestureEvent(ui::GestureEvent* event) { 242 void MessageView::OnGestureEvent(ui::GestureEvent* event) {
276 if (event->type() == ui::ET_GESTURE_TAP) { 243 if (event->type() == ui::ET_GESTURE_TAP) {
277 message_center_->ClickOnNotification(notification_id_); 244 ClickOnNotification();
278 event->SetHandled(); 245 event->SetHandled();
279 return; 246 return;
280 } 247 }
281 248
282 SlideOutView::OnGestureEvent(event); 249 SlideOutView::OnGestureEvent(event);
283 // Do not return here by checking handled(). SlideOutView calls SetHandled() 250 // Do not return here by checking handled(). SlideOutView calls SetHandled()
284 // even though the scroll gesture doesn't make no (or little) effects on the 251 // even though the scroll gesture doesn't make no (or little) effects on the
285 // slide-out behavior. See http://crbug.com/172991 252 // slide-out behavior. See http://crbug.com/172991
286 253
287 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent()) 254 if (!event->IsScrollGestureEvent() && !event->IsFlingScrollEvent())
288 return; 255 return;
289 256
290 if (scroller_) 257 if (scroller_)
291 scroller_->OnGestureEvent(event); 258 scroller_->OnGestureEvent(event);
292 event->SetHandled(); 259 event->SetHandled();
293 } 260 }
294 261
295 void MessageView::OnPaintFocusBorder(gfx::Canvas* canvas) { 262 void MessageView::OnPaintFocusBorder(gfx::Canvas* canvas) {
296 if (HasFocus()) { 263 if (HasFocus()) {
297 canvas->DrawRect(gfx::Rect(1, 0, width() - 2, height() - 2), 264 canvas->DrawRect(gfx::Rect(1, 0, width() - 2, height() - 2),
298 message_center::kFocusBorderColor); 265 message_center::kFocusBorderColor);
299 } 266 }
300 } 267 }
301 268
302 void MessageView::ButtonPressed(views::Button* sender, 269 void MessageView::ButtonPressed(views::Button* sender,
303 const ui::Event& event) { 270 const ui::Event& event) {
304 if (sender == close_button()) { 271 if (sender == close_button()) {
305 message_center_->RemoveNotification(notification_id_, true); // By user. 272 RemoveNotification(true); // By user.
306 } 273 }
307 } 274 }
308 275
309 void MessageView::OnSlideOut() { 276 void MessageView::OnSlideOut() {
310 message_center_->RemoveNotification(notification_id_, true); // By user. 277 RemoveNotification(true); // By user.
311 } 278 }
312 279
313 } // namespace message_center 280 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/message_view.h ('k') | ui/message_center/views/notification_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698