Today we released an update for xScope that supports the Retina display. As I alluded to on Episode 14 of The Talk Show, this update was harder than most. The 68k to PowerPC, Carbon to Cocoa, and PowerPC to Intel transitions were no walk in the park, but this update really kicked my butt. Here’s hoping that sharing some of the things I learned along the way will help you with your own Retina work.
For most developers who are working strictly in window points, an update for the Retina display is a fairly straightforward process. xScope, however, does a lot of work with both pixels and points. And that’s where the fun begins…
The first gotcha I encountered while doing the Retina update was with mouse input using NSEvent’s
+mouseLocation. The team at Apple has done some amazing work making sure output looks stunning on the Retina display, but being able to get high-resolution input is definitely lacking.
There are two problems at play here. The first is that mouse coordinates can be reported for coordinates that do not exist on any attached screen. The second is that the NSPoint does not contain enough resolution to address every pixel on screen.
To deal with the first problem, I used an NSEvent category that clamps
+mouseLocation results to valid coordinates.
For the second problem, the only workable solution was to capture the
+mouseLocation and then track
-keyDown: events so the arrow keys can home in on the destination pixel. Yes, kids, that’s what we call a painful fricken’ hack.
The next big headache was caused because you can’t set a window’s frame using non-integral points.
xScope does this a lot. The best example is with the Ruler tool: the origin of the ruler can be positioned to both even and odd pixels on screen. The red position indicators are also small windows that point to individual pixels in the ruler window.
The workaround is to make an NSWindow that’s larger than you need and then adjust the bounds origin of the NSViews it contains. The pain here is that it immediately introduces a dependency between windows and views. For example, the position indicator windows need to know if the view they’re hovering over has had its bounds origin shifted.
There are many cases where xScope has to align to a pixel boundary. I found myself using this pattern many times throughout
CGFloat backingScaleFactor = [self.window backingScaleFactor]; CGFloat pixelWidth = 1.0 / backingScaleFactor; CGFloat pointOffset = lineWidth / 2.0;
pixelWidth tells you how wide a single pixel is in points, while the
pointOffset can be used to align a coordinate so that it straddles the Quartz drawing point:
NSBezierPath *line = [NSBezierPath bezierPath]; [line setLineWidth:pixelWidth]; [line moveToPoint:NSMakePoint(point.x + pointOffset, 0.0); [line lineToPoint:NSMakePoint(point.x + pointOffset, 100.0); [line stroke];
Another common pattern was to use the
backingScaleFactor to align a coordinate to the nearest pixel in the view:
NSPoint point; point.x = floor(x * backingScaleFactor) / backingScaleFactor; point.y = floor(y * backingScaleFactor) / backingScaleFactor;
Of course you can do much the same thing with NSView’s
-centerScanRect:, but in my experience it’s much more common to need aligned NSPoint values when you’re doing custom drawing. Creating an NSRect just to align the origin is a pain.
As Cocoa developers, we’re used to the pain and suffering caused by flipped view coordinates. Retina support can add a new dimension to this headache when you’re dealing with pixel coordinates.
Say you have a flipped NSView with an origin of 0,0 and dimensions of 1440 x 900 (e.g it covers the entire Retina screen in points.) Y coordinates on the screen can range in value from 0.0 to 899.5. When those coordinates are flipped, the range of values then become 0.5 to 900.0: which is off by a pixel (half point) if you’re trying to address the full range of view coordinates. The solution is to adjust the flipped coordinates like this:
NSRect windowRect = [self.window convertRectFromScreen:NSMakeRect(screenPoint.x, screenPoint.y, 0.0, 0.0)]; NSPoint viewPoint = [self convertPoint:windowRect.origin fromView:nil]; CGFloat backingScaleFactor = [self.window backingScaleFactor]; CGFloat pixelWidth = 1.0 / backingScaleFactor; viewPoint.y -= pixelWidth;
I’ve always wondered why there’s this odd note in the +mouseLocation documentation:
ycoordinate of the returned point will never be less than
Even though it’s a lie, the extra pixel does make the coordinate flip work correctly—and now you have to take care because that pixel can be a fractional point.
Hopefully my trials and tribulations will help you in your own development. The good news is that the beautiful results on the Retina display make all this hard work worthwhile.