I am working on implementing tabs for my application. Everything is working well except I can't figure out how to draw the tabs in a lighter shade of gray when the window resigns main status. I am successfully receiving a NSWindowDidBecomeMainNotification and redrawing the tab bar but I am not sure how to calculate the new gray color for the tabs. I am currently using images for all drawing. I tried drawing a semi-transparent layer above to the entire tab bar to make it lighter but it didn't seem to work. I could probably set an opacity value for each image that I use but that seems less than ideal. I also couldn't find any information in the Apple docs about this. What is the best practice in this case?
3 Answers
What you could do is make the tabs draw in code, and then simply not draw a background. Let the window's background, active or inactive, show through.
You might also consider switching to PSMTabBarControl, a reusable tab bar class that's already written and in use in several apps. (And I assume that you've dismissed NSTabView as not the look you're going for.)
You can try drawing the images using a semi-transparent mask. The code I generally use simply draws an image and then uses Quartz's Transparency Layer feature to fill a rect over the image's actual bounds. I do this as a category on NSImage:
@implementation NSImage (ShadedImageAddition)
- (void)drawAtPoint:(NSPoint)point shadeWithColor:(NSColor *)color intensity:(CGFloat)alpha
{
NSRect imageRect;
CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
imageRect.origin = point;
imageRect.size = [self size];
CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(imageRect), NULL);
[self drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[[color colorWithAlphaComponent:alpha] setFill];
NSRectFillUsingOperation(imageRect, NSCompositeSourceAtop);
CGContextEndTransparencyLayer(context);
}
@end
Then when the image is to be drawn in a non-key window, I generally draw a white mask over it at 50%. Of course you'll have to choose a color and alpha value that looks good with your bitmaps.
[myImage drawAtPoint:NSZeroPoint shadeWithColor:[NSColor whiteColor] intensity:0.50];