Comment dessiner un fond natif à la recherche de statusitem sur un CALayer
-
19-09-2019 - |
Question
En fait, je voudrais attirer l'arrière-plan d'un NSStatusItem
sélectionné sur la CALayer
de mon habitude statusItemView. Mais depuis
- (void)drawStatusBarBackgroundInRect:(NSRect)rect withHighlight:(BOOL)highlight
ne fonctionne pas (?) Sur les couches que je l'ai essayé de tirer la couleur avec la propriété backgroundColor. Mais la conversion du selectedMenuItemColor en RVB ne permet pas beaucoup. Il semble vraiment simple sans gradient. : - /
Je me suis converti [NSColor selectedMenuItemColor]
en CGColorRef
avec ce 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;
}
Toute idée comment dessiner une recherche native fond statusitem sur un CALayer?
La solution
Essayez et mettre en œuvre le sous-classement CALayer la méthode de drawInContext:
créer un NSGraphicsContext pour CGContext,
Autres conseils
NSImage *backgroundImage = [[NSImage alloc] initWithSize:self.frame.size]];
[backgroundImage lockFocus];
[self.statusItem drawStatusBarBackgroundInRect:self.bounds withHighlight:YES];
[backgroundImage unlockFocus];
[self.layer setContents:backgroundImage];
[backgroundImage release];
J'utilise ce code dans mon délégué de la couche:
- (void)drawLayer:(CALayer *)layer
inContext:(CGContextRef)context {
NSGraphicsContext* gc = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gc];
[self.statusItem drawStatusBarBackgroundInRect:self.frame
withHighlight:self.isHighlighted];
[NSGraphicsContext restoreGraphicsState];
}