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

Side by Side Diff: ash/system/network/tray_sms.cc

Issue 10443004: Move common notification layout to base class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 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/network/tray_sms.h" 5 #include "ash/system/network/tray_sms.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/shell.h" 8 #include "ash/shell.h"
9 #include "ash/system/tray/system_tray.h" 9 #include "ash/system/tray/system_tray.h"
10 #include "ash/system/tray/tray_constants.h" 10 #include "ash/system/tray/tray_constants.h"
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 ViewType view_type, 176 ViewType view_type,
177 size_t index, 177 size_t index,
178 const std::string& number, 178 const std::string& number,
179 const std::string& message) 179 const std::string& message)
180 : tray_(tray), 180 : tray_(tray),
181 index_(index) { 181 index_(index) {
182 number_label_ = new views::Label( 182 number_label_ = new views::Label(
183 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_SMS_NUMBER, 183 l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_SMS_NUMBER,
184 UTF8ToUTF16(number))); 184 UTF8ToUTF16(number)));
185 number_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); 185 number_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
186 number_label_->SetFont(
187 number_label_->font().DeriveFont(0, gfx::Font::BOLD));
186 188
187 message_label_ = new views::Label(UTF8ToUTF16(message)); 189 message_label_ = new views::Label(UTF8ToUTF16(message));
188 message_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); 190 message_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
189 message_label_->SetMultiLine(true); 191 message_label_->SetMultiLine(true);
190 192
191 int msg_width;
192 if (view_type == VIEW_DETAILED) 193 if (view_type == VIEW_DETAILED)
193 msg_width = LayoutDetailedView(); 194 LayoutDetailedView();
194 else 195 else
195 msg_width = LayoutNotificationView(); 196 LayoutNotificationView();
196
197 message_label_->SizeToFit(msg_width);
198 } 197 }
199 198
200 virtual ~SmsMessageView() { 199 virtual ~SmsMessageView() {
201 } 200 }
202 201
203 // Overridden from ButtonListener. 202 // Overridden from ButtonListener.
204 virtual void ButtonPressed(views::Button* sender, 203 virtual void ButtonPressed(views::Button* sender,
205 const views::Event& event) OVERRIDE { 204 const views::Event& event) OVERRIDE {
206 tray_->sms_observer()->RemoveMessage(index_); 205 tray_->sms_observer()->RemoveMessage(index_);
207 tray_->Update(false); 206 tray_->Update(false);
208 } 207 }
209 208
210 private: 209 private:
211 int LayoutDetailedView() { 210 void LayoutDetailedView() {
212 views::ImageButton* close_button = new views::ImageButton(this); 211 views::ImageButton* close_button = new views::ImageButton(this);
213 close_button->SetImage(views::CustomButton::BS_NORMAL, 212 close_button->SetImage(views::CustomButton::BS_NORMAL,
214 ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 213 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
215 IDR_AURA_WINDOW_CLOSE)); 214 IDR_AURA_WINDOW_CLOSE));
216 215
217 int msg_width = kTrayPopupWidth - kNotificationCloseButtonWidth - 216 int msg_width = kTrayPopupWidth - kNotificationIconWidth -
218 kTrayPopupPaddingHorizontal * 2; 217 kTrayPopupPaddingHorizontal * 2;
218 message_label_->SizeToFit(msg_width);
219 219
220 views::GridLayout* layout = new views::GridLayout(this); 220 views::GridLayout* layout = new views::GridLayout(this);
221 SetLayoutManager(layout); 221 SetLayoutManager(layout);
222 222
223 views::ColumnSet* columns = layout->AddColumnSet(0); 223 views::ColumnSet* columns = layout->AddColumnSet(0);
224 224
225 // Message 225 // Message
226 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal); 226 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal);
227 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 227 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
228 0 /* resize percent */, 228 0 /* resize percent */,
229 views::GridLayout::FIXED, msg_width, msg_width); 229 views::GridLayout::FIXED, msg_width, msg_width);
230 230
231 // Close button 231 // Close button
232 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 232 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER,
233 0, /* resize percent */ 233 0, /* resize percent */
234 views::GridLayout::FIXED, 234 views::GridLayout::FIXED,
235 kNotificationCloseButtonWidth, 235 kNotificationIconWidth, kNotificationIconWidth);
236 kNotificationCloseButtonWidth);
237 236
238 237
239 layout->AddPaddingRow(0, kPaddingVertical); 238 layout->AddPaddingRow(0, kPaddingVertical);
240 layout->StartRow(0, 0); 239 layout->StartRow(0, 0);
241 layout->AddView(number_label_); 240 layout->AddView(number_label_);
242 layout->AddView(close_button, 1, 2); // 2 rows for icon 241 layout->AddView(close_button, 1, 2); // 2 rows for icon
243 layout->StartRow(0, 0); 242 layout->StartRow(0, 0);
244 layout->AddView(message_label_); 243 layout->AddView(message_label_);
245 244
246 layout->AddPaddingRow(0, kPaddingVertical); 245 layout->AddPaddingRow(0, kPaddingVertical);
247
248 return msg_width;
249 } 246 }
250 247
251 int LayoutNotificationView() { 248 void LayoutNotificationView() {
252 icon_ = new views::ImageView; 249 SetLayoutManager(
253 icon_->SetImage(ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 250 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
254 IDR_AURA_UBER_TRAY_SMS)); 251 AddChildView(number_label_);
255 252 message_label_->SizeToFit(kTrayNotificationContentsWidth);
256 int msg_width = kTrayPopupWidth - kNotificationCloseButtonWidth - 253 AddChildView(message_label_);
257 kTrayPopupPaddingHorizontal - kNotificationIconWidth;
258
259 views::GridLayout* layout = new views::GridLayout(this);
260 SetLayoutManager(layout);
261
262 views::ColumnSet* columns = layout->AddColumnSet(0);
263
264 // Icon
265 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
266 0 /* resize percent */,
267 views::GridLayout::FIXED,
268 kNotificationIconWidth, kNotificationIconWidth);
269
270 columns->AddPaddingColumn(0, kTrayPopupPaddingHorizontal/2);
271
272 // Message
273 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
274 0 /* resize percent */,
275 views::GridLayout::FIXED, msg_width, msg_width);
276
277 // Layout rows
278 layout->AddPaddingRow(0, kPaddingVertical);
279
280 layout->StartRow(0, 0);
281 layout->AddView(icon_, 1, 2); // 2 rows for icon
282 layout->AddView(number_label_);
283 layout->StartRow(0, 0);
284 layout->SkipColumns(1);
285 layout->AddView(message_label_);
286 layout->StartRow(0, 0);
287
288 layout->AddPaddingRow(0, kPaddingVertical);
289
290 return msg_width;
291 } 254 }
292 255
293 TraySms* tray_; 256 TraySms* tray_;
294 size_t index_; 257 size_t index_;
295 views::Label* number_label_; 258 views::Label* number_label_;
296 views::Label* message_label_; 259 views::Label* message_label_;
297 views::ImageView* icon_;
298 260
299 DISALLOW_COPY_AND_ASSIGN(SmsMessageView); 261 DISALLOW_COPY_AND_ASSIGN(SmsMessageView);
300 }; 262 };
301 263
302 class TraySms::SmsDetailedView : public TrayDetailsView, 264 class TraySms::SmsDetailedView : public TrayDetailsView,
303 public ViewClickListener { 265 public ViewClickListener {
304 public: 266 public:
305 explicit SmsDetailedView(TraySms* tray) 267 explicit SmsDetailedView(TraySms* tray)
306 : tray_(tray) { 268 : tray_(tray) {
307 Init(); 269 Init();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 } 322 }
361 323
362 TraySms* tray_; 324 TraySms* tray_;
363 325
364 DISALLOW_COPY_AND_ASSIGN(SmsDetailedView); 326 DISALLOW_COPY_AND_ASSIGN(SmsDetailedView);
365 }; 327 };
366 328
367 class TraySms::SmsNotificationView : public TrayNotificationView { 329 class TraySms::SmsNotificationView : public TrayNotificationView {
368 public: 330 public:
369 SmsNotificationView(TraySms* tray, 331 SmsNotificationView(TraySms* tray,
332 size_t message_index,
370 const std::string& number, 333 const std::string& number,
371 const std::string& text, 334 const std::string& text)
372 size_t message_index) 335 : TrayNotificationView(IDR_AURA_UBER_TRAY_SMS),
373 : tray_(tray), 336 tray_(tray),
374 message_index_(message_index) { 337 message_index_(message_index) {
375 SmsMessageView* message_view_ = new SmsMessageView( 338 SmsMessageView* message_view = new SmsMessageView(
376 tray_, SmsMessageView::VIEW_NOTIFICATION, message_index, number, text); 339 tray_, SmsMessageView::VIEW_NOTIFICATION, message_index_, number, text);
377 InitView(message_view_); 340 InitView(message_view);
341 }
342
343 void Update(size_t message_index,
344 const std::string& number,
345 const std::string& text) {
346 SmsMessageView* message_view = new SmsMessageView(
347 tray_, SmsMessageView::VIEW_NOTIFICATION, message_index_, number, text);
348 UpdateView(message_view);
378 } 349 }
379 350
380 // Overridden from views::View. 351 // Overridden from views::View.
381 bool OnMousePressed(const views::MouseEvent& event) { 352 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE {
382 tray_->PopupDetailedView(0, true); 353 tray_->PopupDetailedView(0, true);
383 return true; 354 return true;
384 } 355 }
385 356
386 // Overridden from TrayNotificationView: 357 // Overridden from TrayNotificationView:
387 virtual void OnClose() OVERRIDE { 358 virtual void OnClose() OVERRIDE {
388 tray_->sms_observer()->RemoveMessage(message_index_); 359 tray_->sms_observer()->RemoveMessage(message_index_);
389 tray_->HideNotificationView(); 360 tray_->HideNotificationView();
390 } 361 }
391 362
392 private: 363 private:
393 TraySms* tray_; 364 TraySms* tray_;
394 SmsMessageView* message_view_;
395 size_t message_index_; 365 size_t message_index_;
396 366
397 DISALLOW_COPY_AND_ASSIGN(SmsNotificationView); 367 DISALLOW_COPY_AND_ASSIGN(SmsNotificationView);
398 }; 368 };
399 369
400 TraySms::TraySms() 370 TraySms::TraySms()
401 : TrayImageItem(IDR_AURA_UBER_TRAY_SMS), 371 : default_(NULL),
402 default_(NULL),
403 detailed_(NULL), 372 detailed_(NULL),
404 notification_(NULL) { 373 notification_(NULL) {
405 sms_observer_.reset(new SmsObserver(this)); 374 sms_observer_.reset(new SmsObserver(this));
406 } 375 }
407 376
408 TraySms::~TraySms() { 377 TraySms::~TraySms() {
409 } 378 }
410 379
411 bool TraySms::GetInitialVisibility() { 380 bool TraySms::GetInitialVisibility() {
412 return !sms_observer()->messages().empty(); 381 return !sms_observer()->messages().empty();
(...skipping 12 matching lines...) Expand all
425 sms_observer()->RequestUpdate(); 394 sms_observer()->RequestUpdate();
426 HideNotificationView(); 395 HideNotificationView();
427 if (sms_observer()->messages().empty()) 396 if (sms_observer()->messages().empty())
428 return NULL; 397 return NULL;
429 detailed_ = new SmsDetailedView(this); 398 detailed_ = new SmsDetailedView(this);
430 return detailed_; 399 return detailed_;
431 } 400 }
432 401
433 views::View* TraySms::CreateNotificationView(user::LoginStatus status) { 402 views::View* TraySms::CreateNotificationView(user::LoginStatus status) {
434 CHECK(notification_ == NULL); 403 CHECK(notification_ == NULL);
435 const base::ListValue& messages = sms_observer()->messages(); 404 size_t index;
436 if (messages.empty())
437 return NULL;
438 DictionaryValue* message;
439 size_t message_index = messages.GetSize() - 1;
440 if (!messages.GetDictionary(message_index, &message))
441 return NULL;
442 std::string number, text; 405 std::string number, text;
443 if (!sms_observer()->GetMessageFromDictionary(message, &number, &text)) 406 if (GetLatestMessage(&index, &number, &text))
444 return NULL; 407 notification_ = new SmsNotificationView(this, index, number, text);
445 notification_ = new SmsNotificationView(this, number, text, message_index);
446 return notification_; 408 return notification_;
447 } 409 }
448 410
449 void TraySms::DestroyDefaultView() { 411 void TraySms::DestroyDefaultView() {
450 default_ = NULL; 412 default_ = NULL;
451 } 413 }
452 414
453 void TraySms::DestroyDetailedView() { 415 void TraySms::DestroyDetailedView() {
454 detailed_ = NULL; 416 detailed_ = NULL;
455 } 417 }
456 418
457 void TraySms::DestroyNotificationView() { 419 void TraySms::DestroyNotificationView() {
458 notification_ = NULL; 420 notification_ = NULL;
459 } 421 }
460 422
423 bool TraySms::GetLatestMessage(size_t* index,
424 std::string* number,
425 std::string* text) {
426 const base::ListValue& messages = sms_observer()->messages();
427 if (messages.empty())
428 return false;
429 DictionaryValue* message;
430 size_t message_index = messages.GetSize() - 1;
431 if (!messages.GetDictionary(message_index, &message))
432 return false;
433 if (!sms_observer()->GetMessageFromDictionary(message, number, text))
434 return false;
435 *index = message_index;
436 return true;
437 }
438
461 void TraySms::Update(bool notify) { 439 void TraySms::Update(bool notify) {
462 HideNotificationView();
463 if (sms_observer()->messages().empty()) { 440 if (sms_observer()->messages().empty()) {
464 if (tray_view())
465 tray_view()->SetVisible(false);
466 if (default_) 441 if (default_)
467 default_->SetVisible(false); 442 default_->SetVisible(false);
468 if (detailed_) 443 if (detailed_)
469 HideDetailedView(); 444 HideDetailedView();
445 HideNotificationView();
470 } else { 446 } else {
471 if (tray_view())
472 tray_view()->SetVisible(true);
473 if (default_) { 447 if (default_) {
474 default_->SetVisible(true); 448 default_->SetVisible(true);
475 default_->Update(); 449 default_->Update();
476 } 450 }
477 if (detailed_) 451 if (detailed_)
478 detailed_->Update(); 452 detailed_->Update();
479 else if (notify) 453 if (notification_) {
454 size_t index;
455 std::string number, text;
456 if (GetLatestMessage(&index, &number, &text))
457 notification_->Update(index, number, text);
458 } else if (notify) {
480 ShowNotificationView(); 459 ShowNotificationView();
460 }
481 } 461 }
482 } 462 }
483 463
484 } // namespace internal 464 } // namespace internal
485 } // namespace ash 465 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698