Bug 784909 - Add support for app-provided HiDPI mouse pointer/cursor. r=josh
authorJustin Dolske <dolske@mozilla.com>
Fri, 28 Sep 2012 01:27:11 -0700
changeset 108531 85a545c8cd49e795b56c604a9ed99917665966f0
parent 108530 96faff88d7928f0c85de918de6a5e1b8677fc31c
child 108532 a3b635cad2d878edc319607f862cb4c393f753ae
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersjosh
bugs784909
milestone18.0a1
Bug 784909 - Add support for app-provided HiDPI mouse pointer/cursor. r=josh
widget/cocoa/nsMacCursor.mm
--- a/widget/cocoa/nsMacCursor.mm
+++ b/widget/cocoa/nsMacCursor.mm
@@ -144,38 +144,54 @@
 }
 
 + (NSCursor *) cocoaCursorWithImageNamed: (NSString *) imageName hotSpot: (NSPoint) aPoint
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
 
   nsCOMPtr<nsIFile> resDir;
   nsAutoCString resPath;
-  NSString* pathToImage;
-  NSImage* cursorImage;
+  NSString* pathToImage, *pathToHiDpiImage;
+  NSImage* cursorImage, *hiDpiCursorImage;
 
   nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(resDir));
   if (NS_FAILED(rv))
     goto INIT_FAILURE;
   resDir->AppendNative(NS_LITERAL_CSTRING("res"));
   resDir->AppendNative(NS_LITERAL_CSTRING("cursors"));
 
   rv = resDir->GetNativePath(resPath);
   if (NS_FAILED(rv))
     goto INIT_FAILURE;
 
   pathToImage = [NSString stringWithUTF8String:(const char*)resPath.get()];
   if (!pathToImage)
     goto INIT_FAILURE;
   pathToImage = [pathToImage stringByAppendingPathComponent:imageName];
-  pathToImage = [pathToImage stringByAppendingPathExtension:@"png"];
+  pathToHiDpiImage = [pathToImage stringByAppendingString:@"@2x"];
+  // Add same extension to both image paths.
+  pathToImage      = [pathToImage      stringByAppendingPathExtension:@"png"];
+  pathToHiDpiImage = [pathToHiDpiImage stringByAppendingPathExtension:@"png"];
 
   cursorImage = [[[NSImage alloc] initWithContentsOfFile:pathToImage] autorelease];
   if (!cursorImage)
     goto INIT_FAILURE;
+
+  // Note 1: There are a few different ways to get a hidpi image via
+  // initWithContentsOfFile. We let the OS handle this here: when the
+  // file basename ends in "@2x", it will be displayed at native resolution
+  // instead of being pixel-doubled. See bug 784909 comment 7 for alternates ways.
+  //
+  // Note 2: The OS is picky, and will ignore the hidpi representation
+  // unless it is exactly twice the size of the lowdpi image.
+  hiDpiCursorImage = [[[NSImage alloc] initWithContentsOfFile:pathToHiDpiImage] autorelease];
+  if (hiDpiCursorImage) {
+    NSImageRep *imageRep = [[hiDpiCursorImage representations] objectAtIndex:0];
+    [cursorImage addRepresentation: imageRep];
+  }
   return [[[NSCursor alloc] initWithImage:cursorImage hotSpot:aPoint] autorelease];
 
 INIT_FAILURE:
   NS_WARNING("Problem getting path to cursor image file!");
   [self release];
   return nil;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL;