views:

315

answers:

1

Hi.

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?

+2  A: 

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.

Peter Hosey
This looks great. Do you know how to implement this? Thanks. Email me at http://j.mp/a2email. Thanks so much.
Alexsander Akers