How to use CIFilter on NSWindow in OSX 10.9 (Mavericks)?
Asked Answered
C

2

11

The old trick that involved using the private API CGSAddWindowFilter() seems to no longer work in Mavericks for some reason. I tried some code described in How does on-screen color inversion work in OS X? and the code below results in the following window.

#import "AppDelegate.h"

//Declarations to avoid compiler warnings (because of private APIs):
typedef void * CGSConnection;
typedef void * CGSWindowID;
extern OSStatus CGSNewConnection(const void **attributes, CGSConnection * id);
typedef void *CGSWindowFilterRef;
extern CGError CGSNewCIFilterByName(CGSConnection cid, CFStringRef filterName, CGSWindowFilterRef *outFilter);
extern CGError CGSAddWindowFilter(CGSConnection cid, CGSWindowID wid, CGSWindowFilterRef filter, int flags);
extern CGError CGSSetCIFilterValuesFromDictionary(CGSConnection cid, CGSWindowFilterRef filter, CFDictionaryRef filterValues);

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self.window setOpaque:NO];
    [self.window setAlphaValue:1.0];
    [self.window setBackgroundColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.1]];
    self.window.level = NSDockWindowLevel;

    CGSConnection thisConnection;
    CGSWindowFilterRef compositingFilter;
    int compositingType = 1; // under the window

    /* Make a new connection to CoreGraphics */
    CGSNewConnection(NULL, &thisConnection);

    /* Create a CoreImage filter and set it up */
    CGSNewCIFilterByName(thisConnection, CFSTR("CIColorInvert"), &compositingFilter);
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"];
    CGSSetCIFilterValuesFromDictionary(thisConnection, compositingFilter, (__bridge CFDictionaryRef)options);

    /* Now apply the filter to the window */
    CGSAddWindowFilter(thisConnection, (CGSWindowID)[self.window windowNumber], compositingFilter, compositingType);
}

@end

Anyone know a trick to make it apply the filter to the background too as it did in OSX 10.8?

I need this to be able to make MenuBarFilter work again in Mavericks.

Comity answered 24/10, 2013 at 20:9 Comment(0)
D
7

There you go:

typedef void * CGSConnection;
extern OSStatus CGSSetWindowBackgroundBlurRadius(CGSConnection connection, NSInteger   windowNumber, int radius);
extern CGSConnection CGSDefaultConnectionForThread();

- (void)enableBlurForWindow:(NSWindow *)window
{
    [window setOpaque:NO];
    window.backgroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];

    CGSConnection connection = CGSDefaultConnectionForThread();
    CGSSetWindowBackgroundBlurRadius(connection, [window windowNumber], 20);
}
Denman answered 8/1, 2014 at 11:21 Comment(4)
Came here 2 month ago for nothing. But not this time ! Just to know, how does this behave with 10.8 and below ?Maladapted
I came here yesterday for nothing, then I decided to reverse engineer Terminal.app and found this :) I don't know if It's compatible with older OSX versions, but I will test It and let you know.Denman
I am unable to test It on 10.8 because in Parallels these effects don't work, but Terminal.app from 10.8 uses this function so I guess It works.Denman
This does seem to add a blur filter. Is there a way to add other filter to the window? Thanks!Heinous
A
-3

CIFilter *filter = [CIFilter filterWithName:@""];

this is how to create a filter, write any filter type between the ""

Amnesty answered 11/11, 2013 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.