OLD | NEW |
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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 220 |
221 // Gets the rectangle in which notification content should be placed. This | 221 // Gets the rectangle in which notification content should be placed. This |
222 // rectangle is to the right of the icon and left of the control buttons. | 222 // rectangle is to the right of the icon and left of the control buttons. |
223 // This depends on the icon_ and closeButton_ being initialized. | 223 // This depends on the icon_ and closeButton_ being initialized. |
224 - (NSRect)currentContentRect; | 224 - (NSRect)currentContentRect; |
225 | 225 |
226 // Returns the wrapped text that could fit within the content rect with not | 226 // Returns the wrapped text that could fit within the content rect with not |
227 // more than the given number of lines. The wrapped text would be painted using | 227 // more than the given number of lines. The wrapped text would be painted using |
228 // the given font. The Ellipsis could be added at the end of the last line if | 228 // the given font. The Ellipsis could be added at the end of the last line if |
229 // it is too long. | 229 // it is too long. |
230 - (string16)wrapText:(const string16&)text | 230 - (string16)wrapText:(const base::string16&)text |
231 forFont:(NSFont*)font | 231 forFont:(NSFont*)font |
232 maxNumberOfLines:(size_t)lines; | 232 maxNumberOfLines:(size_t)lines; |
233 @end | 233 @end |
234 | 234 |
235 //////////////////////////////////////////////////////////////////////////////// | 235 //////////////////////////////////////////////////////////////////////////////// |
236 | 236 |
237 @implementation MCNotificationController | 237 @implementation MCNotificationController |
238 | 238 |
239 - (id)initWithNotification:(const message_center::Notification*)notification | 239 - (id)initWithNotification:(const message_center::Notification*)notification |
240 messageCenter:(message_center::MessageCenter*)messageCenter { | 240 messageCenter:(message_center::MessageCenter*)messageCenter { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 for (int i = kNumNotifications - 1; i >= 0; --i) { | 385 for (int i = kNumNotifications - 1; i >= 0; --i) { |
386 NSTextView* itemView = [self newLabelWithFrame: | 386 NSTextView* itemView = [self newLabelWithFrame: |
387 NSMakeRect(0, y, NSWidth(listFrame), lineHeight)]; | 387 NSMakeRect(0, y, NSWidth(listFrame), lineHeight)]; |
388 [itemView setFont:font]; | 388 [itemView setFont:font]; |
389 | 389 |
390 // Disable the word-wrap in order to show the text in single line. | 390 // Disable the word-wrap in order to show the text in single line. |
391 [[itemView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; | 391 [[itemView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; |
392 [[itemView textContainer] setWidthTracksTextView:NO]; | 392 [[itemView textContainer] setWidthTracksTextView:NO]; |
393 | 393 |
394 // Construct the text from the title and message. | 394 // Construct the text from the title and message. |
395 string16 text = | 395 base::string16 text = |
396 items[i].title + base::UTF8ToUTF16(" ") + items[i].message; | 396 items[i].title + base::UTF8ToUTF16(" ") + items[i].message; |
397 string16 ellidedText = | 397 base::string16 ellidedText = |
398 [self wrapText:text forFont:font maxNumberOfLines:1]; | 398 [self wrapText:text forFont:font maxNumberOfLines:1]; |
399 [itemView setString:base::SysUTF16ToNSString(ellidedText)]; | 399 [itemView setString:base::SysUTF16ToNSString(ellidedText)]; |
400 | 400 |
401 // Use dim color for the title part. | 401 // Use dim color for the title part. |
402 NSColor* titleColor = | 402 NSColor* titleColor = |
403 gfx::SkColorToCalibratedNSColor(message_center::kRegularTextColor); | 403 gfx::SkColorToCalibratedNSColor(message_center::kRegularTextColor); |
404 NSRange titleRange = NSMakeRange( | 404 NSRange titleRange = NSMakeRange( |
405 0, | 405 0, |
406 std::min(ellidedText.size(), items[i].title.size())); | 406 std::min(ellidedText.size(), items[i].title.size())); |
407 [itemView setTextColor:titleColor range:titleRange]; | 407 [itemView setTextColor:titleColor range:titleRange]; |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 DCHECK(closeButton_); | 717 DCHECK(closeButton_); |
718 | 718 |
719 NSRect iconFrame, contentFrame; | 719 NSRect iconFrame, contentFrame; |
720 NSDivideRect([[self view] bounds], &iconFrame, &contentFrame, | 720 NSDivideRect([[self view] bounds], &iconFrame, &contentFrame, |
721 NSWidth([icon_ frame]) + message_center::kIconToTextPadding, | 721 NSWidth([icon_ frame]) + message_center::kIconToTextPadding, |
722 NSMinXEdge); | 722 NSMinXEdge); |
723 contentFrame.size.width -= NSWidth([closeButton_ frame]); | 723 contentFrame.size.width -= NSWidth([closeButton_ frame]); |
724 return contentFrame; | 724 return contentFrame; |
725 } | 725 } |
726 | 726 |
727 - (string16)wrapText:(const string16&)text | 727 - (string16)wrapText:(const base::string16&)text |
728 forFont:(NSFont*)nsfont | 728 forFont:(NSFont*)nsfont |
729 maxNumberOfLines:(size_t)lines { | 729 maxNumberOfLines:(size_t)lines { |
730 if (text.empty()) | 730 if (text.empty()) |
731 return text; | 731 return text; |
732 gfx::FontList font_list((gfx::Font(nsfont))); | 732 gfx::FontList font_list((gfx::Font(nsfont))); |
733 int width = NSWidth([self currentContentRect]); | 733 int width = NSWidth([self currentContentRect]); |
734 int height = (lines + 1) * font_list.GetHeight(); | 734 int height = (lines + 1) * font_list.GetHeight(); |
735 | 735 |
736 std::vector<string16> wrapped; | 736 std::vector<base::string16> wrapped; |
737 gfx::ElideRectangleText(text, font_list, width, height, | 737 gfx::ElideRectangleText(text, font_list, width, height, |
738 gfx::WRAP_LONG_WORDS, &wrapped); | 738 gfx::WRAP_LONG_WORDS, &wrapped); |
739 | 739 |
740 // This could be possible when the input text contains only spaces. | 740 // This could be possible when the input text contains only spaces. |
741 if (wrapped.empty()) | 741 if (wrapped.empty()) |
742 return string16(); | 742 return base::string16(); |
743 | 743 |
744 if (wrapped.size() > lines) { | 744 if (wrapped.size() > lines) { |
745 // Add an ellipsis to the last line. If this ellipsis makes the last line | 745 // Add an ellipsis to the last line. If this ellipsis makes the last line |
746 // too wide, that line will be further elided by the gfx::ElideText below. | 746 // too wide, that line will be further elided by the gfx::ElideText below. |
747 string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); | 747 base::string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); |
748 if (gfx::GetStringWidth(last, font_list) > width) | 748 if (gfx::GetStringWidth(last, font_list) > width) |
749 last = gfx::ElideText(last, font_list, width, gfx::ELIDE_AT_END); | 749 last = gfx::ElideText(last, font_list, width, gfx::ELIDE_AT_END); |
750 wrapped.resize(lines - 1); | 750 wrapped.resize(lines - 1); |
751 wrapped.push_back(last); | 751 wrapped.push_back(last); |
752 } | 752 } |
753 | 753 |
754 return lines == 1 ? wrapped[0] : JoinString(wrapped, '\n'); | 754 return lines == 1 ? wrapped[0] : JoinString(wrapped, '\n'); |
755 } | 755 } |
756 | 756 |
757 @end | 757 @end |
OLD | NEW |