Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using magic strings in CoreUI renderer. #172

Merged
merged 2 commits into from Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/SampleApp/SampleAppAppDelegate.h
Expand Up @@ -26,7 +26,7 @@
@property (assign) IBOutlet NSSlider *titleBarHeight;
@property (assign) IBOutlet NSSlider *bottomBarHeight;
@property (assign) IBOutlet NSButton *showsBaselineSeparator;
@property (assign) IBOutlet NSButton *showsStatusSeparator;
@property (assign) IBOutlet NSButton *showsBottomBarSeparator;
@property (assign) IBOutlet NSButton *texturedWindow;
@property (nonatomic, retain) NSMutableArray *windowControllers;

Expand Down
2 changes: 1 addition & 1 deletion Examples/SampleApp/SampleAppAppDelegate.m
Expand Up @@ -106,7 +106,7 @@ - (IBAction)checkboxAction:(id)sender
self.window.verticallyCenterTitle = [sender state];
} else if ([sender isEqual:self.showsBaselineSeparator]) {
self.window.showsBaselineSeparator = [sender state];
} else if ([sender isEqual:self.showsStatusSeparator]) {
} else if ([sender isEqual:self.showsBottomBarSeparator]) {
self.window.showsBottomBarSeparator = [sender state];
} else if ([sender isEqual:self.texturedWindow]) {
if ([sender state] == NSOnState)
Expand Down
14 changes: 7 additions & 7 deletions Examples/SampleApp/en.lproj/MainMenu.xib
Expand Up @@ -1470,7 +1470,7 @@
<object class="NSButtonCell" key="NSCell" id="699158758">
<int key="NSCellFlags">-2080374784</int>
<int key="NSCellFlags2">0</int>
<string key="NSContents">Shows Status Separator</string>
<string key="NSContents">Shows Bottom Bar Separator</string>
<reference key="NSSupport" ref="323712760"/>
<reference key="NSControlView" ref="289461751"/>
<int key="NSButtonFlags">1211912448</int>
Expand Down Expand Up @@ -1744,7 +1744,7 @@
<object class="NSTextFieldCell" key="NSCell" id="807199571">
<int key="NSCellFlags">68157504</int>
<int key="NSCellFlags2">272630784</int>
<string key="NSContents">Status Bar Height</string>
<string key="NSContents">Bottom Bar Height</string>
<reference key="NSSupport" ref="323712760"/>
<reference key="NSControlView" ref="127786433"/>
<reference key="NSBackgroundColor" ref="636289225"/>
Expand Down Expand Up @@ -2734,15 +2734,15 @@
</object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">showsStatusSeparator</string>
<string key="label">showsBottomBarSeparator</string>
<reference key="source" ref="621444262"/>
<reference key="destination" ref="289461751"/>
</object>
<string key="id">hGb-jz-Jo2</string>
</object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">statusBarHeight</string>
<string key="label">bottomBarHeight</string>
<reference key="source" ref="621444262"/>
<reference key="destination" ref="1071848139"/>
</object>
Expand Down Expand Up @@ -4665,7 +4665,7 @@
<string key="fullScreenRightMarginSlider">NSSlider</string>
<string key="sheet">NSPanel</string>
<string key="showsBaselineSeparator">NSButton</string>
<string key="showsStatusSeparator">NSButton</string>
<string key="showsBottomBarSeparator">NSButton</string>
<string key="texturedWindow">NSButton</string>
<string key="titleBarHeight">NSSlider</string>
<string key="trafficLightLeftMargin">NSSlider</string>
Expand Down Expand Up @@ -4699,8 +4699,8 @@
<string key="name">showsBaselineSeparator</string>
<string key="candidateClassName">NSButton</string>
</object>
<object class="IBToOneOutletInfo" key="showsStatusSeparator">
<string key="name">showsStatusSeparator</string>
<object class="IBToOneOutletInfo" key="showsBottomBarSeparator">
<string key="name">showsBottomBarSeparator</string>
<string key="candidateClassName">NSButton</string>
</object>
<object class="IBToOneOutletInfo" key="texturedWindow">
Expand Down
Expand Up @@ -18,13 +18,20 @@
typedef void (*_CUIDraw)(CUIRendererRef renderer, CGRect frame, CGContextRef context, CFDictionaryRef options, CFDictionaryRef *result);
static _CUIDraw CUIDraw = 0;

static const NSString *kCUIWidgetWindowFrame = @"kCUIWidgetWindowFrame";
static const NSString *kCUIWindowFrameBottomBarHeightKey = @"kCUIWindowFrameBottomBarHeightKey";
static const NSString *kCUIWindowFrameDrawBottomBarSeparatorKey = @"kCUIWindowFrameDrawBottomBarSeparatorKey";
static const NSString *kCUIWindowFrameDrawTitleSeparatorKey = @"kCUIWindowFrameDrawTitleSeparatorKey";
static const NSString *kCUIWindowFrameRoundedBottomCornersKey = @"kCUIWindowFrameRoundedBottomCornersKey";
static const NSString *kCUIWindowFrameRoundedTopCornersKey = @"kCUIWindowFrameRoundedTopCornersKey";
static const NSString *kCUIWindowFrameUnifiedTitleBarHeightKey = @"kCUIWindowFrameUnifiedTitleBarHeightKey";
static NSString * const kCUIIsFlippedKey = @"is.flipped";
static NSString * const kCUIStateActive = @"normal";
static NSString * const kCUIStateInactive = @"inactive";
static NSString * const kCUIStateKey = @"state";
static NSString * const kCUIWidgetKey = @"widget";
static NSString * const kCUIWidgetWindowFrame = @"kCUIWidgetWindowFrame";
static NSString * const kCUIWindowFrameBottomBarHeightKey = @"kCUIWindowFrameBottomBarHeightKey";
static NSString * const kCUIWindowFrameDrawBottomBarSeparatorKey = @"kCUIWindowFrameDrawBottomBarSeparatorKey";
static NSString * const kCUIWindowFrameDrawTitleSeparatorKey = @"kCUIWindowFrameDrawTitleSeparatorKey";
static NSString * const kCUIWindowFrameRoundedBottomCornersKey = @"kCUIWindowFrameRoundedBottomCornersKey";
static NSString * const kCUIWindowFrameRoundedTopCornersKey = @"kCUIWindowFrameRoundedTopCornersKey";
static NSString * const kCUIWindowFrameUnifiedTitleBarHeightKey = @"kCUIWindowFrameUnifiedTitleBarHeightKey";
static NSString * const kCUIWindowTypeDocument = @"regularwin";
static NSString * const kCUIWindowTypeKey = @"windowtype";

@interface NSWindow (NSWindowPrivate)

Expand All @@ -46,27 +53,50 @@ @implementation INWindowBackgroundView (CoreUIRendering)

+ (void)load
{
NSBundle *coreUIBundle = [NSBundle bundleWithIdentifier:@"com.apple.coreui"];
CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, (__bridge CFURLRef)coreUIBundle.bundleURL);
if ((CUIDraw = CFBundleGetFunctionPointerForName(bundle, (__bridge CFStringRef)@"CUIDraw")) &&
CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.coreui"));
if ((CUIDraw = CFBundleGetFunctionPointerForName(bundle, CFSTR("CUIDraw"))) &&
[NSWindow respondsToSelector:@selector(coreUIRenderer)]) {
INAppStoreWindowSwizzle(self,
@selector(drawWindowBackgroundLayersInRect:forEdge:showsSeparator:clippingPath:),
@selector(drawCUIWindowBackgroundLayersInRect:forEdge:showsSeparator:clippingPath:));

[self loadConstant:&kCUIIsFlippedKey named:@"kCUIIsFlippedKey" fromBundle:bundle];
[self loadConstant:&kCUIStateActive named:@"kCUIStateActive" fromBundle:bundle];
[self loadConstant:&kCUIStateInactive named:@"kCUIStateInactive" fromBundle:bundle];
[self loadConstant:&kCUIStateKey named:@"kCUIStateKey" fromBundle:bundle];
[self loadConstant:&kCUIWidgetKey named:@"kCUIWidgetKey" fromBundle:bundle];
[self loadConstant:&kCUIWidgetWindowFrame named:@"kCUIWidgetWindowFrame" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameBottomBarHeightKey named:@"kCUIWindowFrameBottomBarHeightKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameDrawBottomBarSeparatorKey named:@"kCUIWindowFrameDrawBottomBarSeparatorKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameDrawTitleSeparatorKey named:@"kCUIWindowFrameDrawTitleSeparatorKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameRoundedBottomCornersKey named:@"kCUIWindowFrameRoundedBottomCornersKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameRoundedTopCornersKey named:@"kCUIWindowFrameRoundedTopCornersKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowFrameUnifiedTitleBarHeightKey named:@"kCUIWindowFrameUnifiedTitleBarHeightKey" fromBundle:bundle];
[self loadConstant:&kCUIWindowTypeDocument named:@"kCUIWindowTypeDocument" fromBundle:bundle];
[self loadConstant:&kCUIWindowTypeKey named:@"kCUIWindowTypeKey" fromBundle:bundle];
} else {
NSLog(@"Failed to load CoreUI, falling back to custom drawing");
}
CFRelease(bundle);
}

+ (void)loadConstant:(NSString * const *)constant named:(NSString *)constantName fromBundle:(CFBundleRef)bundle
{
NSString * const *_constant = (NSString * const *)(CFBundleGetDataPointerForName(bundle, (__bridge CFStringRef)(constantName)));
if (_constant) {
*((NSString **)constant) = *_constant;
} else {
NSLog(@"Failed to load %@, using fallback value %@", constantName, *constant);
}
}

- (void)drawCUIWindowBackgroundLayersInRect:(CGRect)drawingRect forEdge:(CGRectEdge)edge showsSeparator:(BOOL)showsSeparator clippingPath:(CGPathRef)clippingPath
{
INAppStoreWindow *window = (INAppStoreWindow *)self.window;
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithDictionary:
@{@"state": (window.isMainWindow ? @"normal" : @"inactive"),
@"widget": kCUIWidgetWindowFrame,
@"windowtype": @"regularwin",
@"is.flipped": @(self.isFlipped)}];
@{kCUIStateKey: (window.isMainWindow ? kCUIStateActive : kCUIStateInactive),
kCUIWidgetKey: kCUIWidgetWindowFrame,
kCUIWindowTypeKey: kCUIWindowTypeDocument,
kCUIIsFlippedKey: @(self.isFlipped)}];
if (edge == CGRectMaxYEdge) {
options[kCUIWindowFrameUnifiedTitleBarHeightKey] = @(window.titleBarHeight + window.toolbarHeight);
options[kCUIWindowFrameDrawTitleSeparatorKey] = @(showsSeparator);
Expand Down