2010-07-21 19 views
5

Voglio replicare lo sfondo degli stack di dock in modalità griglia ed elenco. Lo sfondo è nero traslucido con un effetto di sfocatura:Applicare un filtro di background CIFilter quando la finestra dell'host è trasparente

Example of dock stack in grid mode http://www.thecustommac.com/wp-content/uploads/2009/09/stack-highlight.jpg

Il problema è che [CALayer backgroundFilters] si applica solo al contenuto della finestra, i filtri non sono applicati ai contenuti in altre finestre. Ecco il mio codice:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    //make window transparent 
    self.window.backgroundColor = [NSColor clearColor]; 
    [self.window setOpaque:NO]; 
    [self.window setHasShadow:NO]; 
    [self.window setStyleMask:NSBorderlessWindowMask]; 


    //make the content view layer hosting 
    CALayer *rootLayer = [CALayer layer]; 
    [[self.window contentView] setLayer:rootLayer]; 
    [[self.window contentView] setWantsLayer:YES]; 


    //blur the background contents - NOT WORKING! 
    [rootLayer setBackgroundColor:CGColorCreateGenericGray(0.0, .716)]; 

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
    [blurFilter setDefaults]; 
    [rootLayer setBackgroundFilters:[NSArray arrayWithObject: blurFilter]]; 
} 

Non riesco a pensare a come altro ottenere questo effetto. (Ho dato un'occhiata ai servizi di visualizzazione per vedere se ci sono funzioni utili ma non ne vedo nessuna.)

Qualche idea?

risposta

9

È disponibile un'API privata. Ecco il codice di esempio di Rob Keniger:

In 10.5 è possibile aggiungere qualsiasi filtro di immagine di base a una finestra utilizzando la funzione privata "CGSAddWindowFilter".

typedef void * CGSConnectionID; 

extern OSStatus CGSNewConnection(const void **attr, CGSConnectionID *id); 

- (void)enableBlurForWindow:(NSWindow *)window 
{ 

CGSConnectionID _myConnection; 
uint32_t __compositingFilter; 

int __compositingType = 1; // Apply filter to contents underneath the window, then draw window normally on top 

/* Make a new connection to CoreGraphics, alternatively you could use the main connection*/ 

CGSNewConnection(NULL , &_myConnection); 

/* The following creates a new CoreImage filter, then sets its options with a dictionary of values*/ 

CGSNewCIFilterByName (_myConnection, (CFStringRef)@"CIGaussianBlur", &__compositingFilter); 
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"]; 
CGSSetCIFilterValuesFromDictionary(_myConnection, __compositingFilter, (CFDictionaryRef)optionsDict); 

/* Now just switch on the filter for the window */ 

CGSAddWindowFilter(_myConnection, [window windowNumber], __compositingFilter, __compositingType); 
} 
+0

Fantastico, grazie mille! –

+0

FWIW, questo sembra non funzionare più in Yosemite. :-(CGSNewCIFilterByName() restituisce kCGErrorNotImplemented. –

1

Il codice ha qualche errore, qui è il codice che funziona:

typedef void * CGSConnection; 
extern OSStatus CGSNewConnection(const void **attributes, CGSConnection * id); 

-(void)enableBlurForWindow:(NSWindow *)window { 

    CGSConnection thisConnection; 
    NSUInteger compositingFilter; 
    NSInteger compositingType = 1 << 0; 

    // Make a new connection to Core Graphics 
    CGSNewConnection(NULL, &thisConnection); 

    // Create a Core Image filter and set it up 
    CGSNewCIFilterByName(thisConnection, (CFStringRef)@"CIGaussianBlur", &compositingFilter); 
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:2] forKey:@"inputRadius"]; 
    CGSSetCIFilterValuesFromDictionary(thisConnection, compositingFilter, (__bridge CFDictionaryRef)options); 

    // Apply the filter to the window 
    CGSAddWindowFilter(thisConnection, [window windowNumber], compositingFilter, compositingType); 
} 

Poi, usarlo:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    [_window setOpaque:NO]; 

    [_window setBackgroundColor: [NSColor colorWithCalibratedHue:0.568 saturation:0.388 brightness:0.941 alpha:0.6]]; 

    [self enableBlurForWindow:_window]; 
} 
+0

Questo non funziona più in XCode7.1 per quanto posso dire.Anche se ho aggiunto CoreGraphics.framework e '#import" CoreGraphics/CoreGraphics.h "', il tuo il codice non riesce a compilare perché non riesce a trovare 'CGSNewCIFilterByName',' CGSSetCIFilterValuesFromDictionary' e 'CGSAddWindowFilter'. Avevo anche il mio progetto impostato per l'esecuzione per 10.8 OSX, ma poi lo ho cambiato in 10.10 OSX per vedere se sarebbe stato compilato in quel momento, e anche questo è fallito. – Volomike

1

I filtri non vengono utilizzati dallo strato radice : Dalla documentazione:

/* An array of filters that are applied to the background of the layer. 
* The root layer ignores this property. Animatable. */ 

@property(copy) NSArray *backgroundFilters; 
Problemi correlati