Chromium Code Reviews
[email protected] (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: ash/system/web_notification/web_notification_view.cc

Issue 11154022: Re-factor Ash Message Center code part 3/4 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 2 months 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
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 "ash/system/web_notification/web_notification_view.h" 5 #include "ash/system/web_notification/web_notification_view.h"
6 6
7 #include "ash/system/tray/tray_constants.h"
8 #include "ash/system/tray/tray_views.h"
9 #include "ash/system/web_notification/web_notification.h"
10 #include "ash/system/web_notification/web_notification_tray.h"
11 #include "grit/ash_resources.h" 7 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h" 8 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h" 9 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/models/simple_menu_model.h" 10 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
16 #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"
14 #include "ui/views/controls/image_view.h"
15 #include "ui/views/controls/scroll_view.h"
17 #include "ui/views/controls/label.h" 16 #include "ui/views/controls/label.h"
18 #include "ui/views/controls/menu/menu_model_adapter.h" 17 #include "ui/views/controls/menu/menu_model_adapter.h"
19 #include "ui/views/controls/menu/menu_runner.h" 18 #include "ui/views/controls/menu/menu_runner.h"
20 #include "ui/views/layout/grid_layout.h" 19 #include "ui/views/layout/grid_layout.h"
21 #include "ui/views/widget/widget.h" 20 #include "ui/views/widget/widget.h"
22 21
23 namespace ash { 22 namespace {
23 const int kPaddingHorizontal = 18;
24 const int kPaddingBetweenItems = 10;
25 }
24 26
25 namespace message_center { 27 namespace message_center {
26 28
27 const SkColor kNotificationColor = SkColorSetRGB(0xfe, 0xfe, 0xfe); 29 const SkColor kNotificationColor = SkColorSetRGB(0xfe, 0xfe, 0xfe);
28 const SkColor kNotificationReadColor = SkColorSetRGB(0xfa, 0xfa, 0xfa); 30 const SkColor kNotificationReadColor = SkColorSetRGB(0xfa, 0xfa, 0xfa);
29 31
30 // Menu constants 32 // Menu constants
31 const int kTogglePermissionCommand = 0; 33 const int kTogglePermissionCommand = 0;
32 const int kToggleExtensionCommand = 1; 34 const int kToggleExtensionCommand = 1;
33 const int kShowSettingsCommand = 2; 35 const int kShowSettingsCommand = 2;
34 36
35 // A dropdown menu for notifications. 37 // A dropdown menu for notifications.
36 class WebNotificationMenuModel : public ui::SimpleMenuModel, 38 class WebNotificationMenuModel : public ui::SimpleMenuModel,
37 public ui::SimpleMenuModel::Delegate { 39 public ui::SimpleMenuModel::Delegate {
38 public: 40 public:
39 explicit WebNotificationMenuModel(WebNotificationTray* tray, 41 explicit WebNotificationMenuModel(
miket_OOO 2012/10/16 01:05:42 This guy doesn't need to be explicit (not sure why
stevenjb 2012/10/16 19:16:06 Probably used to take one arg, and more likely my
40 const WebNotification& notification) 42 WebNotificationList::Delegate* list_delegate,
43 const WebNotification& notification)
41 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 44 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
42 tray_(tray), 45 list_delegate_(list_delegate),
43 notification_(notification) { 46 notification_(notification) {
44 // Add 'disable notifications' menu item. 47 // Add 'disable notifications' menu item.
45 if (!notification.extension_id.empty()) { 48 if (!notification.extension_id.empty()) {
46 AddItem(kToggleExtensionCommand, 49 AddItem(kToggleExtensionCommand,
47 GetLabelForCommandId(kToggleExtensionCommand)); 50 GetLabelForCommandId(kToggleExtensionCommand));
48 } else if (!notification.display_source.empty()) { 51 } else if (!notification.display_source.empty()) {
49 AddItem(kTogglePermissionCommand, 52 AddItem(kTogglePermissionCommand,
50 GetLabelForCommandId(kTogglePermissionCommand)); 53 GetLabelForCommandId(kTogglePermissionCommand));
51 } 54 }
52 // Add settings menu item. 55 // Add settings menu item.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 92
90 virtual bool GetAcceleratorForCommandId( 93 virtual bool GetAcceleratorForCommandId(
91 int command_id, 94 int command_id,
92 ui::Accelerator* accelerator) OVERRIDE { 95 ui::Accelerator* accelerator) OVERRIDE {
93 return false; 96 return false;
94 } 97 }
95 98
96 virtual void ExecuteCommand(int command_id) OVERRIDE { 99 virtual void ExecuteCommand(int command_id) OVERRIDE {
97 switch (command_id) { 100 switch (command_id) {
98 case kToggleExtensionCommand: 101 case kToggleExtensionCommand:
99 tray_->DisableByExtension(notification_.id); 102 list_delegate_->DisableNotificationByExtension(notification_.id);
100 break; 103 break;
101 case kTogglePermissionCommand: 104 case kTogglePermissionCommand:
102 tray_->DisableByUrl(notification_.id); 105 list_delegate_->DisableNotificationByUrl(notification_.id);
103 break; 106 break;
104 case kShowSettingsCommand: 107 case kShowSettingsCommand:
105 tray_->ShowSettings(notification_.id); 108 list_delegate_->ShowNotificationSettings(notification_.id);
106 break; 109 break;
107 default: 110 default:
108 NOTREACHED(); 111 NOTREACHED();
109 } 112 }
110 } 113 }
111 114
112 private: 115 private:
113 WebNotificationTray* tray_; 116 WebNotificationList::Delegate* list_delegate_;
114 WebNotification notification_; 117 WebNotification notification_;
115 118
116 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel); 119 DISALLOW_COPY_AND_ASSIGN(WebNotificationMenuModel);
117 }; 120 };
118 121
119 WebNotificationView::WebNotificationView(WebNotificationTray* tray, 122 WebNotificationView::WebNotificationView(
120 const WebNotification& notification, 123 WebNotificationList::Delegate* list_delegate,
121 int scroll_bar_width) 124 const WebNotification& notification,
122 : tray_(tray), 125 int scroll_bar_width)
126 : list_delegate_(list_delegate),
123 notification_(notification), 127 notification_(notification),
124 icon_(NULL), 128 icon_(NULL),
125 close_button_(NULL), 129 close_button_(NULL),
126 scroller_(NULL), 130 scroller_(NULL),
127 gesture_scroll_amount_(0.f) { 131 gesture_scroll_amount_(0.f) {
128 SkColor bg_color = notification.is_read ? 132 SkColor bg_color = notification.is_read ?
129 kNotificationReadColor : kNotificationColor; 133 kNotificationReadColor : kNotificationColor;
130 set_background(views::Background::CreateSolidBackground(bg_color)); 134 set_background(views::Background::CreateSolidBackground(bg_color));
131 SetPaintToLayer(true); 135 SetPaintToLayer(true);
132 SetFillsBoundsOpaquely(false); 136 SetFillsBoundsOpaquely(false);
(...skipping 16 matching lines...) Expand all
149 ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 153 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
150 IDR_AURA_UBER_TRAY_NOTIFY_CLOSE)); 154 IDR_AURA_UBER_TRAY_NOTIFY_CLOSE));
151 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, 155 close_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
152 views::ImageButton::ALIGN_MIDDLE); 156 views::ImageButton::ALIGN_MIDDLE);
153 157
154 views::GridLayout* layout = new views::GridLayout(this); 158 views::GridLayout* layout = new views::GridLayout(this);
155 SetLayoutManager(layout); 159 SetLayoutManager(layout);
156 160
157 views::ColumnSet* columns = layout->AddColumnSet(0); 161 views::ColumnSet* columns = layout->AddColumnSet(0);
158 162
159 const int padding_width = kTrayPopupPaddingHorizontal/2; 163 const int padding_width = kPaddingHorizontal/2;
miket_OOO 2012/10/16 01:05:42 whitespace around the /
stevenjb 2012/10/16 19:16:06 Done.
160 columns->AddPaddingColumn(0, padding_width); 164 columns->AddPaddingColumn(0, padding_width);
161 165
162 // Notification Icon. 166 // Notification Icon.
163 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING, 167 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
164 0, /* resize percent */ 168 0, /* resize percent */
165 views::GridLayout::FIXED, 169 views::GridLayout::FIXED,
166 kWebNotificationIconSize, kWebNotificationIconSize); 170 kWebNotificationIconSize, kWebNotificationIconSize);
167 171
168 columns->AddPaddingColumn(0, padding_width); 172 columns->AddPaddingColumn(0, padding_width);
169 173
170 // Notification message text. 174 // Notification message text.
171 const int message_width = kWebNotificationWidth - kWebNotificationIconSize - 175 const int message_width = kWebNotificationWidth - kWebNotificationIconSize -
172 kWebNotificationButtonWidth - (padding_width * 3) - scroll_bar_width; 176 kWebNotificationButtonWidth - (padding_width * 3) - scroll_bar_width;
173 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 177 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
174 100, /* resize percent */ 178 100, /* resize percent */
175 views::GridLayout::FIXED, 179 views::GridLayout::FIXED,
176 message_width, message_width); 180 message_width, message_width);
177 181
178 columns->AddPaddingColumn(0, padding_width); 182 columns->AddPaddingColumn(0, padding_width);
179 183
180 // Close button. 184 // Close button.
181 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING, 185 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::LEADING,
182 0, /* resize percent */ 186 0, /* resize percent */
183 views::GridLayout::FIXED, 187 views::GridLayout::FIXED,
184 kWebNotificationButtonWidth, 188 kWebNotificationButtonWidth,
185 kWebNotificationButtonWidth); 189 kWebNotificationButtonWidth);
186 190
187 // Layout rows 191 // Layout rows
188 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems); 192 layout->AddPaddingRow(0, kPaddingBetweenItems);
189 193
190 layout->StartRow(0, 0); 194 layout->StartRow(0, 0);
191 layout->AddView(icon_, 1, 2); 195 layout->AddView(icon_, 1, 2);
192 layout->AddView(title, 1, 1); 196 layout->AddView(title, 1, 1);
193 layout->AddView(close_button_, 1, 1); 197 layout->AddView(close_button_, 1, 1);
194 198
195 layout->StartRow(0, 0); 199 layout->StartRow(0, 0);
196 layout->SkipColumns(2); 200 layout->SkipColumns(2);
197 layout->AddView(message, 1, 1); 201 layout->AddView(message, 1, 1);
198 layout->AddPaddingRow(0, kTrayPopupPaddingBetweenItems); 202 layout->AddPaddingRow(0, kPaddingBetweenItems);
199 } 203 }
200 204
201 WebNotificationView::~WebNotificationView() { 205 WebNotificationView::~WebNotificationView() {
202 } 206 }
203 207
204 bool WebNotificationView::OnMousePressed(const ui::MouseEvent& event) { 208 bool WebNotificationView::OnMousePressed(const ui::MouseEvent& event) {
205 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) { 209 if (event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) {
206 ShowMenu(event.location()); 210 ShowMenu(event.location());
207 return true; 211 return true;
208 } 212 }
209 tray_->OnClicked(notification_.id); 213 list_delegate_->OnNotificationClicked(notification_.id);
210 return true; 214 return true;
211 } 215 }
212 216
213 ui::EventResult WebNotificationView::OnGestureEvent( 217 ui::EventResult WebNotificationView::OnGestureEvent(
214 const ui::GestureEvent& event) { 218 const ui::GestureEvent& event) {
215 if (event.type() == ui::ET_GESTURE_TAP) { 219 if (event.type() == ui::ET_GESTURE_TAP) {
216 tray_->OnClicked(notification_.id); 220 list_delegate_->OnNotificationClicked(notification_.id);
217 return ui::ER_CONSUMED; 221 return ui::ER_CONSUMED;
218 } 222 }
219 223
220 if (event.type() == ui::ET_GESTURE_LONG_PRESS) { 224 if (event.type() == ui::ET_GESTURE_LONG_PRESS) {
221 ShowMenu(event.location()); 225 ShowMenu(event.location());
222 return ui::ER_CONSUMED; 226 return ui::ER_CONSUMED;
223 } 227 }
224 228
225 if (event.type() == ui::ET_SCROLL_FLING_START) { 229 if (event.type() == ui::ET_SCROLL_FLING_START) {
226 // The threshold for the fling velocity is computed empirically. 230 // The threshold for the fling velocity is computed empirically.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 } 265 }
262 266
263 if (scroller_) 267 if (scroller_)
264 scroller_->OnGestureEvent(event); 268 scroller_->OnGestureEvent(event);
265 return ui::ER_CONSUMED; 269 return ui::ER_CONSUMED;
266 } 270 }
267 271
268 void WebNotificationView::ButtonPressed(views::Button* sender, 272 void WebNotificationView::ButtonPressed(views::Button* sender,
269 const ui::Event& event) { 273 const ui::Event& event) {
270 if (sender == close_button_) 274 if (sender == close_button_)
271 tray_->SendRemoveNotification(notification_.id); 275 list_delegate_->SendRemoveNotification(notification_.id);
272 } 276 }
273 277
274 void WebNotificationView::OnImplicitAnimationsCompleted() { 278 void WebNotificationView::OnImplicitAnimationsCompleted() {
275 tray_->SendRemoveNotification(notification_.id); 279 list_delegate_->SendRemoveNotification(notification_.id);
276 } 280 }
277 281
278 void WebNotificationView::ShowMenu(gfx::Point screen_location) { 282 void WebNotificationView::ShowMenu(gfx::Point screen_location) {
279 WebNotificationMenuModel menu_model(tray_, notification_); 283 WebNotificationMenuModel menu_model(list_delegate_, notification_);
280 if (menu_model.GetItemCount() == 0) 284 if (menu_model.GetItemCount() == 0)
281 return; 285 return;
282 286
283 views::MenuModelAdapter menu_model_adapter(&menu_model); 287 views::MenuModelAdapter menu_model_adapter(&menu_model);
284 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); 288 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
285 289
286 views::View::ConvertPointToScreen(this, &screen_location); 290 views::View::ConvertPointToScreen(this, &screen_location);
287 ignore_result(menu_runner.RunMenuAt( 291 ignore_result(menu_runner.RunMenuAt(
288 GetWidget()->GetTopLevelWidget(), 292 GetWidget()->GetTopLevelWidget(),
289 NULL, 293 NULL,
(...skipping 20 matching lines...) Expand all
310 base::TimeDelta::FromMilliseconds(swipe_out_duration)); 314 base::TimeDelta::FromMilliseconds(swipe_out_duration));
311 settings.AddObserver(this); 315 settings.AddObserver(this);
312 316
313 gfx::Transform transform; 317 gfx::Transform transform;
314 transform.SetTranslateX(direction == SLIDE_LEFT ? -width() : width()); 318 transform.SetTranslateX(direction == SLIDE_LEFT ? -width() : width());
315 layer()->SetTransform(transform); 319 layer()->SetTransform(transform);
316 layer()->SetOpacity(0.f); 320 layer()->SetOpacity(0.f);
317 } 321 }
318 322
319 } // namespace message_center 323 } // namespace message_center
320
321 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698