主机窗口透明时应用CIFilter背景过滤器

时间:2010-07-21 11:02:16

标签: cocoa macos core-animation

我想在网格和列表模式中复制停靠堆栈的背景。背景是半透明的黑色,具有模糊效果:

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

问题是[CALayer backgroundFilters]仅适用于窗口中的内容,过滤器不适用于其他窗口中的内容。这是我的代码:

- (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]];
}

我无法想到如何实现这种效果。 (我已经看过显示服务,看看是否有任何有用的功能,但我看不到任何功能。)

有什么想法吗?

4 个答案:

答案 0 :(得分:9)

这里有私有API。以下是Rob Keniger的示例代码:

在10.5中,您可以使用。将任何核心图像过滤器添加到窗口 私人函数'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 );
}

答案 1 :(得分:1)

您的代码有一些错误,以下是正常运行的代码:

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);
}

然后,使用它:

- (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];
}

答案 2 :(得分:1)

根层未使用过滤器: 来自文档:

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

@property(copy) NSArray *backgroundFilters;

答案 3 :(得分:-2)

https://github.com/dsns/GaussianBlur_NSWindow_os_x_mavericks 它看起来像小牛应用程序的拐杖代码 但它工作