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

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

Issue 1292003004: Elide origins displayed on web notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove the use_origin flag in Notification do everything in NotificationView Created 5 years, 4 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
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 <algorithm> 7 #include <algorithm>
8 8
9 #include "base/mac/foundation_util.h" 9 #include "base/mac/foundation_util.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "components/url_formatter/elide_url.h"
13 #include "skia/ext/skia_utils_mac.h" 14 #include "skia/ext/skia_utils_mac.h"
14 #import "ui/base/cocoa/hover_image_button.h" 15 #import "ui/base/cocoa/hover_image_button.h"
15 #include "ui/base/l10n/l10n_util_mac.h" 16 #include "ui/base/l10n/l10n_util_mac.h"
16 #include "ui/base/resource/resource_bundle.h" 17 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/font_list.h" 18 #include "ui/gfx/font_list.h"
18 #include "ui/gfx/text_elider.h" 19 #include "ui/gfx/text_elider.h"
19 #include "ui/gfx/text_utils.h" 20 #include "ui/gfx/text_utils.h"
20 #include "ui/message_center/message_center.h" 21 #include "ui/message_center/message_center.h"
21 #include "ui/message_center/message_center_style.h" 22 #include "ui/message_center/message_center_style.h"
22 #include "ui/message_center/notification.h" 23 #include "ui/message_center/notification.h"
23 #include "ui/resources/grit/ui_resources.h" 24 #include "ui/resources/grit/ui_resources.h"
24 #include "ui/strings/grit/ui_strings.h" 25 #include "ui/strings/grit/ui_strings.h"
25 26 #include "url/gurl.h"
26 27
27 @interface MCNotificationProgressBar : NSProgressIndicator 28 @interface MCNotificationProgressBar : NSProgressIndicator
28 @end 29 @end
29 30
30 @implementation MCNotificationProgressBar 31 @implementation MCNotificationProgressBar
31 - (void)drawRect:(NSRect)dirtyRect { 32 - (void)drawRect:(NSRect)dirtyRect {
32 NSRect sliceRect, remainderRect; 33 NSRect sliceRect, remainderRect;
33 double progressFraction = ([self doubleValue] - [self minValue]) / 34 double progressFraction = ([self doubleValue] - [self minValue]) /
34 ([self maxValue] - [self minValue]); 35 ([self maxValue] - [self minValue]);
35 NSDivideRect(dirtyRect, &sliceRect, &remainderRect, 36 NSDivideRect(dirtyRect, &sliceRect, &remainderRect,
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 CGFloat messagePadding = 374 CGFloat messagePadding =
374 message_center::kTextTopPadding - titleBottomGap - messageTopGap; 375 message_center::kTextTopPadding - titleBottomGap - messageTopGap;
375 376
376 CGFloat contextMessageTopGap = roundf( 377 CGFloat contextMessageTopGap = roundf(
377 [[contextMessage_ font] ascender] - [[contextMessage_ font] capHeight]); 378 [[contextMessage_ font] ascender] - [[contextMessage_ font] capHeight]);
378 CGFloat contextMessagePadding = 379 CGFloat contextMessagePadding =
379 message_center::kTextTopPadding - messageBottomGap - contextMessageTopGap; 380 message_center::kTextTopPadding - messageBottomGap - contextMessageTopGap;
380 381
381 // Set the title and recalculate the frame. 382 // Set the title and recalculate the frame.
382 size_t actualTitleLines = 0; 383 size_t actualTitleLines = 0;
383 [title_ setString:base::SysUTF16ToNSString( 384 [title_ setString:base::SysUTF16ToNSString([self
384 [self wrapText:notification_->title() 385 wrapText:notification_->title()
385 forFont:[title_ font] 386 forFont:[title_ font]
386 maxNumberOfLines:message_center::kMaxTitleLines 387 maxNumberOfLines:message_center::kMaxTitleLines
387 actualLines:&actualTitleLines])]; 388 actualLines:&actualTitleLines])];
Nico 2015/08/19 17:58:12 Huh, is this what clang-format does? The formattin
Miguel Garcia 2015/08/19 19:52:46 Acknowledged.
388 [title_ sizeToFit]; 389 [title_ sizeToFit];
389 NSRect titleFrame = [title_ frame]; 390 NSRect titleFrame = [title_ frame];
390 titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame); 391 titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame);
391 392
392 // The number of message lines depends on the number of context message lines 393 // The number of message lines depends on the number of context message lines
393 // and the lines within the title, and whether an image exists. 394 // and the lines within the title, and whether an image exists.
394 int messageLineLimit = message_center::kMessageExpandedLineLimit; 395 int messageLineLimit = message_center::kMessageExpandedLineLimit;
395 if (actualTitleLines > 1) 396 if (actualTitleLines > 1)
396 messageLineLimit -= (actualTitleLines - 1) * 2; 397 messageLineLimit -= (actualTitleLines - 1) * 2;
397 if (!notification_->image().IsEmpty()) { 398 if (!notification_->image().IsEmpty()) {
398 messageLineLimit /= 2; 399 messageLineLimit /= 2;
399 if (!notification_->context_message().empty()) 400
401 if (!notification_->context_message().empty() &&
402 ![self useOriginAsContextMessage:notification])
400 messageLineLimit -= message_center::kContextMessageLineLimit; 403 messageLineLimit -= message_center::kContextMessageLineLimit;
401 } 404 }
402 if (messageLineLimit < 0) 405 if (messageLineLimit < 0)
403 messageLineLimit = 0; 406 messageLineLimit = 0;
404 407
405 // Set the message and recalculate the frame. 408 // Set the message and recalculate the frame.
406 [message_ setString:base::SysUTF16ToNSString( 409 [message_ setString:base::SysUTF16ToNSString(
407 [self wrapText:notification_->message() 410 [self wrapText:notification_->message()
408 forFont:[message_ font] 411 forFont:[message_ font]
409 maxNumberOfLines:messageLineLimit])]; 412 maxNumberOfLines:messageLineLimit])];
(...skipping 11 matching lines...) Expand all
421 messageFrame.origin.y = titleFrame.origin.y; 424 messageFrame.origin.y = titleFrame.origin.y;
422 messageFrame.size.height = 0; 425 messageFrame.size.height = 0;
423 } else { 426 } else {
424 [message_ setHidden:NO]; 427 [message_ setHidden:NO];
425 messageFrame.origin.y = 428 messageFrame.origin.y =
426 NSMinY(titleFrame) - messagePadding - NSHeight(messageFrame); 429 NSMinY(titleFrame) - messagePadding - NSHeight(messageFrame);
427 messageFrame.size.height = NSHeight([message_ frame]); 430 messageFrame.size.height = NSHeight([message_ frame]);
428 } 431 }
429 432
430 // Set the context message and recalculate the frame. 433 // Set the context message and recalculate the frame.
431 [contextMessage_ setString:base::SysUTF16ToNSString( 434 base::string16 message;
432 [self wrapText:notification_->context_message() 435 if ([self useOriginAsContextMessage:notification]) {
433 forFont:[contextMessage_ font] 436 gfx::FontList font_list((gfx::Font([message_ font])));
434 maxNumberOfLines:message_center::kContextMessageLineLimit])]; 437 message =
438 url_formatter::ElideHost(notification->origin_url(), font_list,
439 message_center::kContextMessageViewWidth);
440 } else {
441 message = notification_->context_message();
442 }
443
444 [contextMessage_
445 setString:
446 base::SysUTF16ToNSString([self
447 wrapText:message
448 forFont:[contextMessage_ font]
449 maxNumberOfLines:message_center::kContextMessageLineLimit])];
Nico 2015/08/19 17:58:11 This might look a bit less awkward with a temp var
Miguel Garcia 2015/08/19 19:52:46 Done.
435 [contextMessage_ sizeToFit]; 450 [contextMessage_ sizeToFit];
436 NSRect contextMessageFrame = [contextMessage_ frame]; 451 NSRect contextMessageFrame = [contextMessage_ frame];
437 452
438 if (notification_->context_message().empty()) { 453 if (notification_->context_message().empty() &&
454 ![self useOriginAsContextMessage:notification]) {
439 [contextMessage_ setHidden:YES]; 455 [contextMessage_ setHidden:YES];
440 contextMessageFrame.origin.y = messageFrame.origin.y; 456 contextMessageFrame.origin.y = messageFrame.origin.y;
441 contextMessageFrame.size.height = 0; 457 contextMessageFrame.size.height = 0;
442 } else { 458 } else {
443 [contextMessage_ setHidden:NO]; 459 [contextMessage_ setHidden:NO];
444 contextMessageFrame.origin.y = 460 contextMessageFrame.origin.y =
445 NSMinY(messageFrame) - 461 NSMinY(messageFrame) -
446 contextMessagePadding - 462 contextMessagePadding -
447 NSHeight(contextMessageFrame); 463 NSHeight(contextMessageFrame);
448 contextMessageFrame.size.height = NSHeight([contextMessage_ frame]); 464 contextMessageFrame.size.height = NSHeight([contextMessage_ frame]);
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 [[self view] setFrame:rootFrame]; 651 [[self view] setFrame:rootFrame];
636 [title_ setFrame:titleFrame]; 652 [title_ setFrame:titleFrame];
637 [message_ setFrame:messageFrame]; 653 [message_ setFrame:messageFrame];
638 [contextMessage_ setFrame:contextMessageFrame]; 654 [contextMessage_ setFrame:contextMessageFrame];
639 [listView_ setFrame:listFrame]; 655 [listView_ setFrame:listFrame];
640 [progressBarView_ setFrame:progressBarFrame]; 656 [progressBarView_ setFrame:progressBarFrame];
641 657
642 return rootFrame; 658 return rootFrame;
643 } 659 }
644 660
661 - (bool)useOriginAsContextMessage:
662 (const message_center::Notification*)notification {
663 return (notification->context_message().empty() &&
664 notification->origin_url().is_valid() &&
665 notification->origin_url().SchemeIsHTTPOrHTTPS());
Nico 2015/08/19 17:58:12 This kind of looks like model code; why isn't this
Jun Mukai 2015/08/19 18:06:02 +1 (to be clear, I objected the idea to introduce
Miguel Garcia 2015/08/19 19:52:46 Done On 2015/08/19 18:06:02, Jun Mukai wrote:
666 }
667
645 - (void)close:(id)sender { 668 - (void)close:(id)sender {
646 [closeButton_ setTarget:nil]; 669 [closeButton_ setTarget:nil];
647 messageCenter_->RemoveNotification([self notificationID], /*by_user=*/true); 670 messageCenter_->RemoveNotification([self notificationID], /*by_user=*/true);
648 } 671 }
649 672
650 - (void)buttonClicked:(id)button { 673 - (void)buttonClicked:(id)button {
651 messageCenter_->ClickOnNotificationButton([self notificationID], 674 messageCenter_->ClickOnNotificationButton([self notificationID],
652 [button tag]); 675 [button tag]);
653 } 676 }
654 677
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 forFont:(NSFont*)nsfont 914 forFont:(NSFont*)nsfont
892 maxNumberOfLines:(size_t)lines { 915 maxNumberOfLines:(size_t)lines {
893 size_t unused; 916 size_t unused;
894 return [self wrapText:text 917 return [self wrapText:text
895 forFont:nsfont 918 forFont:nsfont
896 maxNumberOfLines:lines 919 maxNumberOfLines:lines
897 actualLines:&unused]; 920 actualLines:&unused];
898 } 921 }
899 922
900 @end 923 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698