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

Side by Side Diff: trunk/src/ui/message_center/cocoa/notification_controller.mm

Issue 23769003: Revert 220500 "Adds the contextMessage field to notifications." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 3 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 #import "ui/message_center/cocoa/notification_controller.h" 5 #import "ui/message_center/cocoa/notification_controller.h"
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 196
197 // Initializes the closeButton_ ivar with the configured button. 197 // Initializes the closeButton_ ivar with the configured button.
198 - (void)configureCloseButtonInFrame:(NSRect)rootFrame; 198 - (void)configureCloseButtonInFrame:(NSRect)rootFrame;
199 199
200 // Initializes title_ in the given frame. 200 // Initializes title_ in the given frame.
201 - (void)configureTitleInFrame:(NSRect)rootFrame; 201 - (void)configureTitleInFrame:(NSRect)rootFrame;
202 202
203 // Initializes message_ in the given frame. 203 // Initializes message_ in the given frame.
204 - (void)configureBodyInFrame:(NSRect)rootFrame; 204 - (void)configureBodyInFrame:(NSRect)rootFrame;
205 205
206 // Initializes contextMessage_ in the given frame.
207 - (void)configureContextMessageInFrame:(NSRect)rootFrame;
208
209 // Creates a NSTextField that the caller owns configured as a label in a 206 // Creates a NSTextField that the caller owns configured as a label in a
210 // notification. 207 // notification.
211 - (NSTextField*)newLabelWithFrame:(NSRect)frame; 208 - (NSTextField*)newLabelWithFrame:(NSRect)frame;
212 209
213 // Gets the rectangle in which notification content should be placed. This 210 // Gets the rectangle in which notification content should be placed. This
214 // rectangle is to the right of the icon and left of the control buttons. 211 // rectangle is to the right of the icon and left of the control buttons.
215 // This depends on the icon_ and closeButton_ being initialized. 212 // This depends on the icon_ and closeButton_ being initialized.
216 - (NSRect)currentContentRect; 213 - (NSRect)currentContentRect;
217 214
218 // Returns the wrapped text that could fit within the given text field with not 215 // Returns the wrapped text that could fit within the given text field with not
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 [rootView addSubview:closeButton_]; 253 [rootView addSubview:closeButton_];
257 254
258 // Create the title. 255 // Create the title.
259 [self configureTitleInFrame:rootFrame]; 256 [self configureTitleInFrame:rootFrame];
260 [rootView addSubview:title_]; 257 [rootView addSubview:title_];
261 258
262 // Create the message body. 259 // Create the message body.
263 [self configureBodyInFrame:rootFrame]; 260 [self configureBodyInFrame:rootFrame];
264 [rootView addSubview:message_]; 261 [rootView addSubview:message_];
265 262
266 // Create the context message body.
267 [self configureContextMessageInFrame:rootFrame];
268 [rootView addSubview:contextMessage_];
269
270 // Populate the data. 263 // Populate the data.
271 [self updateNotification:notification_]; 264 [self updateNotification:notification_];
272 } 265 }
273 266
274 - (NSRect)updateNotification:(const message_center::Notification*)notification { 267 - (NSRect)updateNotification:(const message_center::Notification*)notification {
275 DCHECK_EQ(notification->id(), notificationID_); 268 DCHECK_EQ(notification->id(), notificationID_);
276 notification_ = notification; 269 notification_ = notification;
277 270
278 NSRect rootFrame = NSMakeRect(0, 0, 271 NSRect rootFrame = NSMakeRect(0, 0,
279 message_center::kNotificationPreferredImageSize, 272 message_center::kNotificationPreferredImageSize,
280 message_center::kNotificationIconSize); 273 message_center::kNotificationIconSize);
281 274
282 // Update the icon. 275 // Update the icon.
283 [icon_ setImage:notification_->icon().AsNSImage()]; 276 [icon_ setImage:notification_->icon().AsNSImage()];
284 277
285 // The message_center:: constants are relative to capHeight at the top and 278 // The message_center:: constants are relative to capHeight at the top and
286 // relative to the baseline at the bottom, but NSTextField uses the full line 279 // relative to the baseline at the bottom, but NSTextField uses the full line
287 // height for its height. 280 // height for its height.
288 CGFloat titleTopGap = 281 CGFloat titleTopGap =
289 roundf([[title_ font] ascender] - [[title_ font] capHeight]); 282 roundf([[title_ font] ascender] - [[title_ font] capHeight]);
290 CGFloat titleBottomGap = roundf(fabs([[title_ font] descender])); 283 CGFloat titleBottomGap = roundf(fabs([[title_ font] descender]));
291 CGFloat titlePadding = message_center::kTextTopPadding - titleTopGap; 284 CGFloat titlePadding = message_center::kTextTopPadding - titleTopGap;
292 285
293 CGFloat messageTopGap = 286 CGFloat messageTopGap =
294 roundf([[message_ font] ascender] - [[message_ font] capHeight]); 287 roundf([[message_ font] ascender] - [[message_ font] capHeight]);
295 CGFloat messageBottomGap = roundf(fabs([[message_ font] descender]));
296 CGFloat messagePadding = 288 CGFloat messagePadding =
297 message_center::kTextTopPadding - titleBottomGap - messageTopGap; 289 message_center::kTextTopPadding - titleBottomGap - messageTopGap;
298 290
299 CGFloat contextMessageTopGap = roundf(
300 [[contextMessage_ font] ascender] - [[contextMessage_ font] capHeight]);
301 CGFloat contextMessagePadding =
302 message_center::kTextTopPadding - messageBottomGap - contextMessageTopGap;
303
304 // Set the title and recalculate the frame. 291 // Set the title and recalculate the frame.
305 [title_ setStringValue:base::SysUTF16ToNSString( 292 [title_ setStringValue:base::SysUTF16ToNSString(
306 [self wrapText:notification_->title() 293 [self wrapText:notification_->title()
307 forField:title_ 294 forField:title_
308 maxNumberOfLines:message_center::kTitleLineLimit])]; 295 maxNumberOfLines:message_center::kTitleLineLimit])];
309 [title_ sizeToFit]; 296 [title_ sizeToFit];
310 NSRect titleFrame = [title_ frame]; 297 NSRect titleFrame = [title_ frame];
311 titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame); 298 titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame);
312 299
313 // Set the message and recalculate the frame. 300 // Set the message and recalculate the frame.
314 [message_ setStringValue:base::SysUTF16ToNSString( 301 [message_ setStringValue:base::SysUTF16ToNSString(
315 [self wrapText:notification_->message() 302 [self wrapText:notification_->message()
316 forField:message_ 303 forField:title_
317 maxNumberOfLines:message_center::kMessageExpandedLineLimit])]; 304 maxNumberOfLines:message_center::kMessageExpandedLineLimit])];
305 [message_ setHidden:NO];
318 [message_ sizeToFit]; 306 [message_ sizeToFit];
319 NSRect messageFrame = [message_ frame]; 307 NSRect messageFrame = [message_ frame];
308 messageFrame.origin.y =
309 NSMinY(titleFrame) - messagePadding - NSHeight(messageFrame);
310 messageFrame.size.height = NSHeight([message_ frame]);
320 311
321 // If there are list items, then the message_ view should not be displayed. 312 // Create the list item views (up to a maximum).
313 [listItemView_ removeFromSuperview];
322 const std::vector<message_center::NotificationItem>& items = 314 const std::vector<message_center::NotificationItem>& items =
323 notification->items(); 315 notification->items();
316 NSRect listFrame = NSZeroRect;
324 if (items.size() > 0) { 317 if (items.size() > 0) {
318 // If there are list items, then the message_ view should not be displayed.
325 [message_ setHidden:YES]; 319 [message_ setHidden:YES];
326 messageFrame.origin.y = titleFrame.origin.y; 320 messageFrame.origin.y = titleFrame.origin.y;
327 messageFrame.size.height = 0; 321 messageFrame.size.height = 0;
328 } else {
329 [message_ setHidden:NO];
330 messageFrame.origin.y =
331 NSMinY(titleFrame) - messagePadding - NSHeight(messageFrame);
332 messageFrame.size.height = NSHeight([message_ frame]);
333 }
334 322
335 // Set the context message and recalculate the frame.
336 [contextMessage_ setStringValue:base::SysUTF16ToNSString(
337 [self wrapText:notification_->context_message()
338 forField:contextMessage_
339 maxNumberOfLines:message_center::kContextMessageLineLimit])];
340 [contextMessage_ sizeToFit];
341 NSRect contextMessageFrame = [contextMessage_ frame];
342
343 if (notification_->context_message().empty()) {
344 [contextMessage_ setHidden:YES];
345 contextMessageFrame.origin.y = messageFrame.origin.y;
346 contextMessageFrame.size.height = 0;
347 } else {
348 [contextMessage_ setHidden:NO];
349 contextMessageFrame.origin.y =
350 NSMinY(messageFrame) -
351 contextMessagePadding -
352 NSHeight(contextMessageFrame);
353 contextMessageFrame.size.height = NSHeight([contextMessage_ frame]);
354 }
355
356 // Create the list item views (up to a maximum).
357 [listItemView_ removeFromSuperview];
358 NSRect listFrame = NSZeroRect;
359 if (items.size() > 0) {
360 listFrame = [self currentContentRect]; 323 listFrame = [self currentContentRect];
361 listFrame.origin.y = 0; 324 listFrame.origin.y = 0;
362 listFrame.size.height = 0; 325 listFrame.size.height = 0;
363 listItemView_.reset([[NSView alloc] initWithFrame:listFrame]); 326 listItemView_.reset([[NSView alloc] initWithFrame:listFrame]);
364 [listItemView_ accessibilitySetOverrideValue:NSAccessibilityListRole 327 [listItemView_ accessibilitySetOverrideValue:NSAccessibilityListRole
365 forAttribute:NSAccessibilityRoleAttribute]; 328 forAttribute:NSAccessibilityRoleAttribute];
366 [listItemView_ 329 [listItemView_
367 accessibilitySetOverrideValue:NSAccessibilityContentListSubrole 330 accessibilitySetOverrideValue:NSAccessibilityContentListSubrole
368 forAttribute:NSAccessibilitySubroleAttribute]; 331 forAttribute:NSAccessibilitySubroleAttribute];
369 CGFloat y = 0; 332 CGFloat y = 0;
370 333
371 NSFont* font = [NSFont systemFontOfSize:message_center::kMessageFontSize]; 334 NSFont* font = [NSFont systemFontOfSize:message_center::kMessageFontSize];
372 CGFloat lineHeight = roundf(NSHeight([font boundingRectForFont])); 335 CGFloat lineHeight = roundf(NSHeight([font boundingRectForFont]));
373 336
374 const int kNumNotifications = 337 const int kNumNotifications =
375 std::min(items.size(), message_center::kNotificationMaximumItems); 338 std::min(items.size(), message_center::kNotificationMaximumItems);
376 for (int i = kNumNotifications - 1; i >= 0; --i) { 339 for (int i = kNumNotifications - 1; i >= 0; --i) {
377 NSTextField* field = [self newLabelWithFrame: 340 NSTextField* field = [self newLabelWithFrame:
378 NSMakeRect(0, y, NSWidth(listFrame), lineHeight)]; 341 NSMakeRect(0, y, NSWidth(listFrame), lineHeight)];
379 [[field cell] setUsesSingleLineMode:YES]; 342 [[field cell] setUsesSingleLineMode:YES];
380 [field setAttributedStringValue: 343 [field setAttributedStringValue:
381 [MCNotificationController attributedStringForItem:items[i] 344 [MCNotificationController attributedStringForItem:items[i]
382 font:font]]; 345 font:font]];
383 [listItemView_ addSubview:field]; 346 [listItemView_ addSubview:field];
384 y += lineHeight; 347 y += lineHeight;
385 } 348 }
386 // TODO(thakis): The spacing is not completely right. 349 // TODO(thakis): The spacing is not completely right.
387 CGFloat listTopPadding = 350 CGFloat listTopPadding =
388 message_center::kTextTopPadding - contextMessageTopGap; 351 message_center::kTextTopPadding - messageTopGap;
389 listFrame.size.height = y; 352 listFrame.size.height = y;
390 listFrame.origin.y = 353 listFrame.origin.y =
391 NSMinY(contextMessageFrame) - listTopPadding - NSHeight(listFrame); 354 NSMinY(titleFrame) - listTopPadding - NSHeight(listFrame);
392 [listItemView_ setFrame:listFrame]; 355 [listItemView_ setFrame:listFrame];
393 [[self view] addSubview:listItemView_]; 356 [[self view] addSubview:listItemView_];
394 } 357 }
395 358
396 // Create the progress bar view if needed. 359 // Create the progress bar view if needed.
397 [progressBarView_ removeFromSuperview]; 360 [progressBarView_ removeFromSuperview];
398 NSRect progressBarFrame = NSZeroRect; 361 NSRect progressBarFrame = NSZeroRect;
399 if (notification->type() == message_center::NOTIFICATION_TYPE_PROGRESS) { 362 if (notification->type() == message_center::NOTIFICATION_TYPE_PROGRESS) {
400 progressBarFrame = [self currentContentRect]; 363 progressBarFrame = [self currentContentRect];
401 progressBarFrame.origin.y = NSMinY(contextMessageFrame) - 364 progressBarFrame.origin.y = NSMinY(messageFrame) -
402 message_center::kProgressBarTopPadding - 365 message_center::kProgressBarTopPadding -
403 message_center::kProgressBarThickness; 366 message_center::kProgressBarThickness;
404 progressBarFrame.size.height = message_center::kProgressBarThickness; 367 progressBarFrame.size.height = message_center::kProgressBarThickness;
405 progressBarView_.reset( 368 progressBarView_.reset(
406 [[MCNotificationProgressBar alloc] initWithFrame:progressBarFrame]); 369 [[MCNotificationProgressBar alloc] initWithFrame:progressBarFrame]);
407 // Setting indeterminate to NO does not work with custom drawRect. 370 // Setting indeterminate to NO does not work with custom drawRect.
408 [progressBarView_ setIndeterminate:YES]; 371 [progressBarView_ setIndeterminate:YES];
409 [progressBarView_ setStyle:NSProgressIndicatorBarStyle]; 372 [progressBarView_ setStyle:NSProgressIndicatorBarStyle];
410 [progressBarView_ setDoubleValue:notification->progress()]; 373 [progressBarView_ setDoubleValue:notification->progress()];
411 [[self view] addSubview:progressBarView_]; 374 [[self view] addSubview:progressBarView_];
412 } 375 }
413 376
414 // If the bottom-most element so far is out of the rootView's bounds, resize 377 // If the bottom-most element so far is out of the rootView's bounds, resize
415 // the view. 378 // the view.
416 CGFloat minY = NSMinY(contextMessageFrame); 379 CGFloat minY = NSMinY(messageFrame);
417 if (listItemView_ && NSMinY(listFrame) < minY) 380 if (listItemView_ && NSMinY(listFrame) < minY)
418 minY = NSMinY(listFrame); 381 minY = NSMinY(listFrame);
419 if (progressBarView_ && NSMinY(progressBarFrame) < minY) 382 if (progressBarView_ && NSMinY(progressBarFrame) < minY)
420 minY = NSMinY(progressBarFrame); 383 minY = NSMinY(progressBarFrame);
421 if (minY < messagePadding) { 384 if (minY < messagePadding) {
422 CGFloat delta = messagePadding - minY; 385 CGFloat delta = messagePadding - minY;
423 rootFrame.size.height += delta; 386 rootFrame.size.height += delta;
424 titleFrame.origin.y += delta; 387 titleFrame.origin.y += delta;
425 messageFrame.origin.y += delta; 388 messageFrame.origin.y += delta;
426 contextMessageFrame.origin.y += delta;
427 listFrame.origin.y += delta; 389 listFrame.origin.y += delta;
428 progressBarFrame.origin.y += delta; 390 progressBarFrame.origin.y += delta;
429 } 391 }
430 392
431 // Add the bottom container view. 393 // Add the bottom container view.
432 NSRect frame = rootFrame; 394 NSRect frame = rootFrame;
433 frame.size.height = 0; 395 frame.size.height = 0;
434 [bottomView_ removeFromSuperview]; 396 [bottomView_ removeFromSuperview];
435 bottomView_.reset([[NSView alloc] initWithFrame:frame]); 397 bottomView_.reset([[NSView alloc] initWithFrame:frame]);
436 CGFloat y = 0; 398 CGFloat y = 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 frame.size.height += NSHeight(imageFrame); 449 frame.size.height += NSHeight(imageFrame);
488 [bottomView_ addSubview:imageView]; 450 [bottomView_ addSubview:imageView];
489 } 451 }
490 452
491 [bottomView_ setFrame:frame]; 453 [bottomView_ setFrame:frame];
492 [[self view] addSubview:bottomView_]; 454 [[self view] addSubview:bottomView_];
493 455
494 rootFrame.size.height += NSHeight(frame); 456 rootFrame.size.height += NSHeight(frame);
495 titleFrame.origin.y += NSHeight(frame); 457 titleFrame.origin.y += NSHeight(frame);
496 messageFrame.origin.y += NSHeight(frame); 458 messageFrame.origin.y += NSHeight(frame);
497 contextMessageFrame.origin.y += NSHeight(frame);
498 listFrame.origin.y += NSHeight(frame); 459 listFrame.origin.y += NSHeight(frame);
499 progressBarFrame.origin.y += NSHeight(frame); 460 progressBarFrame.origin.y += NSHeight(frame);
500 461
501 // Make sure that there is a minimum amount of spacing below the icon and 462 // Make sure that there is a minimum amount of spacing below the icon and
502 // the edge of the frame. 463 // the edge of the frame.
503 CGFloat bottomDelta = NSHeight(rootFrame) - NSHeight([icon_ frame]); 464 CGFloat bottomDelta = NSHeight(rootFrame) - NSHeight([icon_ frame]);
504 if (bottomDelta > 0 && bottomDelta < message_center::kIconBottomPadding) { 465 if (bottomDelta > 0 && bottomDelta < message_center::kIconBottomPadding) {
505 CGFloat bottomAdjust = message_center::kIconBottomPadding - bottomDelta; 466 CGFloat bottomAdjust = message_center::kIconBottomPadding - bottomDelta;
506 rootFrame.size.height += bottomAdjust; 467 rootFrame.size.height += bottomAdjust;
507 titleFrame.origin.y += bottomAdjust; 468 titleFrame.origin.y += bottomAdjust;
508 messageFrame.origin.y += bottomAdjust; 469 messageFrame.origin.y += bottomAdjust;
509 contextMessageFrame.origin.y += bottomAdjust;
510 listFrame.origin.y += bottomAdjust; 470 listFrame.origin.y += bottomAdjust;
511 progressBarFrame.origin.y += bottomAdjust; 471 progressBarFrame.origin.y += bottomAdjust;
512 } 472 }
513 473
514 [[self view] setFrame:rootFrame]; 474 [[self view] setFrame:rootFrame];
515 [title_ setFrame:titleFrame]; 475 [title_ setFrame:titleFrame];
516 [message_ setFrame:messageFrame]; 476 [message_ setFrame:messageFrame];
517 [contextMessage_ setFrame:contextMessageFrame];
518 [listItemView_ setFrame:listFrame]; 477 [listItemView_ setFrame:listFrame];
519 [progressBarView_ setFrame:progressBarFrame]; 478 [progressBarView_ setFrame:progressBarFrame];
520 479
521 return rootFrame; 480 return rootFrame;
522 } 481 }
523 482
524 - (void)close:(id)sender { 483 - (void)close:(id)sender {
525 [closeButton_ setTarget:nil]; 484 [closeButton_ setTarget:nil];
526 messageCenter_->RemoveNotification([self notificationID], /*by_user=*/true); 485 messageCenter_->RemoveNotification([self notificationID], /*by_user=*/true);
527 } 486 }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 message_center::kRegularTextColor)]; 604 message_center::kRegularTextColor)];
646 [title_ setFont:[NSFont messageFontOfSize:message_center::kTitleFontSize]]; 605 [title_ setFont:[NSFont messageFontOfSize:message_center::kTitleFontSize]];
647 } 606 }
648 607
649 - (void)configureBodyInFrame:(NSRect)rootFrame { 608 - (void)configureBodyInFrame:(NSRect)rootFrame {
650 NSRect frame = [self currentContentRect]; 609 NSRect frame = [self currentContentRect];
651 frame.size.height = 0; 610 frame.size.height = 0;
652 message_.reset([self newLabelWithFrame:frame]); 611 message_.reset([self newLabelWithFrame:frame]);
653 [message_ setAutoresizingMask:NSViewMinYMargin]; 612 [message_ setAutoresizingMask:NSViewMinYMargin];
654 [message_ setTextColor:gfx::SkColorToCalibratedNSColor( 613 [message_ setTextColor:gfx::SkColorToCalibratedNSColor(
655 message_center::kRegularTextColor)]; 614 message_center::kDimTextColor)];
656 [message_ setFont: 615 [message_ setFont:
657 [NSFont messageFontOfSize:message_center::kMessageFontSize]]; 616 [NSFont messageFontOfSize:message_center::kMessageFontSize]];
658 } 617 }
659 618
660 - (void)configureContextMessageInFrame:(NSRect)rootFrame {
661 NSRect frame = [self currentContentRect];
662 frame.size.height = 0;
663 contextMessage_.reset([self newLabelWithFrame:frame]);
664 [contextMessage_ setAutoresizingMask:NSViewMinYMargin];
665 [contextMessage_ setTextColor:gfx::SkColorToCalibratedNSColor(
666 message_center::kDimTextColor)];
667 [contextMessage_ setFont:
668 [NSFont messageFontOfSize:message_center::kMessageFontSize]];
669 }
670
671 - (NSTextField*)newLabelWithFrame:(NSRect)frame { 619 - (NSTextField*)newLabelWithFrame:(NSRect)frame {
672 NSTextField* label = [[NSTextField alloc] initWithFrame:frame]; 620 NSTextField* label = [[NSTextField alloc] initWithFrame:frame];
673 [label setDrawsBackground:NO]; 621 [label setDrawsBackground:NO];
674 [label setBezeled:NO]; 622 [label setBezeled:NO];
675 [label setEditable:NO]; 623 [label setEditable:NO];
676 [label setSelectable:NO]; 624 [label setSelectable:NO];
677 return label; 625 return label;
678 } 626 }
679 627
680 - (NSRect)currentContentRect { 628 - (NSRect)currentContentRect {
(...skipping 26 matching lines...) Expand all
707 if (font.GetStringWidth(last) > width) 655 if (font.GetStringWidth(last) > width)
708 last = ui::ElideText(last, font, width, ui::ELIDE_AT_END); 656 last = ui::ElideText(last, font, width, ui::ELIDE_AT_END);
709 wrapped.resize(lines - 1); 657 wrapped.resize(lines - 1);
710 wrapped.push_back(last); 658 wrapped.push_back(last);
711 } 659 }
712 660
713 return JoinString(wrapped, '\n'); 661 return JoinString(wrapped, '\n');
714 } 662 }
715 663
716 @end 664 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698