Search code examples
cocoacore-graphicscalayernsstatusitemcgcolor

How to draw a native looking statusitem background on a CALayer


Actually I want to draw the background of a selected NSStatusItem on the CALayer of my custom statusItemView. But since

- (void)drawStatusBarBackgroundInRect:(NSRect)rect withHighlight:(BOOL)highlight

does not work (?) on layers I've tried it to draw the color with the backgroundColor property. But converting the selectedMenuItemColor into RGB doesn't help very much. It looks really plain without the gradient. :-/

I converted [NSColor selectedMenuItemColor] into a CGColorRef with this code:

- (CGColorRef)highlightColor {
    static CGColorRef highlight = NULL;
    if(highlight == NULL) {
        CGFloat red, green, blue, alpha;
        NSColor *hlclr = [[NSColor selectedMenuItemColor] colorUsingColorSpace:
                     [NSColorSpace genericRGBColorSpace]];
        [hlclr getRed:&red green:&green blue:&blue alpha:&alpha];
        CGFloat values[4] = {red, green, blue, alpha};
        highlight = CGColorCreate([self genericRGBSpace], values);
    }
    return highlight;
}

Any idea how to draw a native looking statusitem background on a CALayer?


Solution

  • Try subclassing CALayer and implementing the drawInContext: method to create an NSGraphicsContext for the CGContext, set the NSGraphicsContext as the current context, and then tell the status item to draw its background.