Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / message_center / cocoa / notification_controller.mm
index 5f642eb..369b121 100644 (file)
 // Returns the wrapped text that could fit within the content rect with not
 // more than the given number of lines. The wrapped text would be painted using
 // the given font. The Ellipsis could be added at the end of the last line if
-// it is too long.
+// it is too long. Outputs the number of lines computed in the actualLines
+// parameter.
+- (base::string16)wrapText:(const base::string16&)text
+                   forFont:(NSFont*)font
+          maxNumberOfLines:(size_t)lines
+               actualLines:(size_t*)actualLines;
+
+// Same as above without outputting the lines formatted.
 - (base::string16)wrapText:(const base::string16&)text
                    forFont:(NSFont*)font
           maxNumberOfLines:(size_t)lines;
+
 @end
 
 ////////////////////////////////////////////////////////////////////////////////
       message_center::kTextTopPadding - messageBottomGap - contextMessageTopGap;
 
   // Set the title and recalculate the frame.
+  size_t actualTitleLines = 0;
   [title_ setString:base::SysUTF16ToNSString(
       [self wrapText:notification_->title()
-             forFont:[title_ font]
-       maxNumberOfLines:message_center::kTitleLineLimit])];
+                forFont:[title_ font]
+       maxNumberOfLines:message_center::kMaxTitleLines
+            actualLines:&actualTitleLines])];
   [title_ sizeToFit];
   NSRect titleFrame = [title_ frame];
   titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame);
 
+  // The number of message lines depends on the number of context message lines
+  // and the lines within the title, and whether an image exists.
+  int messageLineLimit = message_center::kMessageExpandedLineLimit;
+  if (actualTitleLines > 1)
+    messageLineLimit -= (actualTitleLines - 1) * 2;
+  if (!notification_->image().IsEmpty()) {
+    messageLineLimit /= 2;
+    if (!notification_->context_message().empty())
+      messageLineLimit -= message_center::kContextMessageLineLimit;
+  }
+  if (messageLineLimit < 0)
+    messageLineLimit = 0;
+
   // Set the message and recalculate the frame.
   [message_ setString:base::SysUTF16ToNSString(
       [self wrapText:notification_->message()
              forFont:[message_ font]
-       maxNumberOfLines:message_center::kMessageExpandedLineLimit])];
+      maxNumberOfLines:messageLineLimit])];
   [message_ sizeToFit];
   NSRect messageFrame = [message_ frame];
 
   // If there are list items, then the message_ view should not be displayed.
   const std::vector<message_center::NotificationItem>& items =
       notification->items();
-  if (items.size() > 0) {
+  // If there are list items, don't show the main message.  Also if the message
+  // is empty, mark it as hidden and set 0 height, so it doesn't take up any
+  // space (size to fit leaves it 15 px tall.
+  if (items.size() > 0 || notification_->message().empty()) {
     [message_ setHidden:YES];
     messageFrame.origin.y = titleFrame.origin.y;
     messageFrame.size.height = 0;
 
 - (base::string16)wrapText:(const base::string16&)text
                    forFont:(NSFont*)nsfont
-    maxNumberOfLines:(size_t)lines {
-  if (text.empty())
-    return text;
+          maxNumberOfLines:(size_t)lines
+               actualLines:(size_t*)actualLines {
+  *actualLines = 0;
+  if (text.empty() || lines == 0)
+    return base::string16();
   gfx::FontList font_list((gfx::Font(nsfont)));
   int width = NSWidth([self currentContentRect]);
   int height = (lines + 1) * font_list.GetHeight();
     wrapped.push_back(last);
   }
 
+  *actualLines = wrapped.size();
   return lines == 1 ? wrapped[0] : JoinString(wrapped, '\n');
 }
 
+- (base::string16)wrapText:(const base::string16&)text
+                   forFont:(NSFont*)nsfont
+          maxNumberOfLines:(size_t)lines {
+  size_t unused;
+  return [self wrapText:text
+                forFont:nsfont
+       maxNumberOfLines:lines
+            actualLines:&unused];
+}
+
 @end