Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
- Added replacement methods for faulty CGSizeMakeWithDictionaryRepresentation
- Made DTVersion NSCopying-compliant
- Added category for NSWindowController to modally present a modal sheet, more like UIKit
  • Loading branch information
odrobnik committed Oct 1, 2012
1 parent 9d8a3d1 commit 63e4d1a
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Core/DTFoundation-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif

#import <Foundation/Foundation.h>
Expand Down
18 changes: 18 additions & 0 deletions Core/Source/DTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,21 @@

CGSize sizeThatFitsKeepingAspectRatio(CGSize originalSize, CGSize sizeToFit);


/**
Replacement for buggy CGSizeMakeWithDictionaryRepresentation
@param dict The dictionary containing an encoded `CGSize`
@param size The `CGSize` to decode from the dictionary
@see http://www.cocoanetics.com/2012/09/radar-cgrectmakewithdictionaryrepresentation/
*/
BOOL DTCGSizeMakeWithDictionaryRepresentation(NSDictionary *dict, CGSize *size);

/**
Replacement for buggy CGSizeCreateDictionaryRepresentation
@param size The `CGSize` to encode in the returned dictionary
@see http://www.cocoanetics.com/2012/09/radar-cgrectmakewithdictionaryrepresentation/
*/
NSDictionary *DTCGSizeCreateDictionaryRepresentation(CGSize size);



27 changes: 27 additions & 0 deletions Core/Source/DTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ CGSize sizeThatFitsKeepingAspectRatio(CGSize originalSize, CGSize sizeToFit)
CGSize scaledSize = CGSizeMake(roundf(originalSize.width*smallerZoom), roundf(originalSize.height*smallerZoom));
return scaledSize;
}

BOOL DTCGSizeMakeWithDictionaryRepresentation(NSDictionary *dict, CGSize *size)
{
NSNumber *widthNumber = dict[@"Width"];
NSNumber *heightNumber = dict[@"Height"];

if (!widthNumber || !heightNumber)
{
return NO;
}

if (size)
{
size->width = [widthNumber floatValue];
size->height = [heightNumber floatValue];
}

return YES;
}

NSDictionary *DTCGSizeCreateDictionaryRepresentation(CGSize size)
{
NSNumber *widthNumber = [NSNumber numberWithFloat:size.width];
NSNumber *heightNumber = [NSNumber numberWithFloat:size.height];

return @{@"Width":widthNumber, @"Height":heightNumber};
}
9 changes: 8 additions & 1 deletion Core/Source/DTVersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,14 @@ - (NSString *)description
return [NSString stringWithFormat:@"%d.%d", (int)_major, (int)_minor];
}

#pragma mark Properties
#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone
{
return [[DTVersion allocWithZone:zone] initWithMajor:_major minor:_minor maintenance:_maintenance build:_build];
}

#pragma mark - Properties

@synthesize major = _major;
@synthesize minor = _minor;
Expand Down
15 changes: 15 additions & 0 deletions Core/Source/NSDocument+DTFoundation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSDocument+DTFoundation.h
// DTFoundation
//
// Created by Oliver Drobnik on 10/1/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface NSDocument (DTFoundation)

- (NSWindowController *)mainDocumentWindowController;

@end
19 changes: 19 additions & 0 deletions Core/Source/NSDocument+DTFoundation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSDocument+DTFoundation.m
// DTFoundation
//
// Created by Oliver Drobnik on 10/1/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//

#import "NSDocument+DTFoundation.h"

@implementation NSDocument (DTFoundation)

- (NSWindowController *)mainDocumentWindowController
{
// TODO: what if there are more than one window for a document?
return [self.windowControllers objectAtIndex:0];
}

@end
4 changes: 2 additions & 2 deletions Core/Source/NSString+DTPaths.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ - (NSString *)pathByIncrementingSequenceNumber
NSString *nakedName = [baseName pathByDeletingSequenceNumber];

if ([extension isEqualToString:@""]) {
return [nakedName stringByAppendingFormat:@"(%d)", sequenceNumber+1];
return [nakedName stringByAppendingFormat:@"(%d)", (int)sequenceNumber+1];
}

return [[nakedName stringByAppendingFormat:@"(%d)", sequenceNumber+1] stringByAppendingPathExtension:extension];
return [[nakedName stringByAppendingFormat:@"(%d)", (int)sequenceNumber+1] stringByAppendingPathExtension:extension];
}

- (NSString *)pathByDeletingSequenceNumber
Expand Down
2 changes: 1 addition & 1 deletion Core/Source/NSString+DTUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (NSString *)md5Checksum
{
const char *cStr = [self UTF8String];
unsigned char result [CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
CC_MD5( cStr, (CC_LONG)strlen(cStr), result );

return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1],
Expand Down
19 changes: 19 additions & 0 deletions Core/Source/NSWindowController+DTPanelControllerPresenting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSWindowController+DTViewControllerPresenting.h
// DTFoundation
//
// Created by Oliver Drobnik on 10/1/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//

typedef void (^DTPanelControllerPresentingCompletionBlock)(NSUInteger result);

@interface NSWindowController (DTPanelControllerPresenting)

@property (nonatomic, readonly, strong) NSWindowController *modalPanelController;

- (void)presentModalPanelController:(NSWindowController *)panelController;

- (void)dismissModalPanelController;

@end
55 changes: 55 additions & 0 deletions Core/Source/NSWindowController+DTPanelControllerPresenting.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// NSWindow+DTViewControllerPresenting.m
// DTFoundation
//
// Created by Oliver Drobnik on 10/1/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//

#import "objc/runtime.h"

#import "NSWindowController+DTPanelControllerPresenting.h"

static char DTPresentedViewControllerKey;

@implementation NSWindowController (DTPanelControllerPresenting)

- (void)presentModalPanelController:(NSWindowController *)panelController
{
NSWindowController *windowController = self.modalPanelController;

if (windowController)
{
NSLog(@"Already presenting %@, cannot modally present another panel", NSStringFromClass([windowController class]));
return;
}

[NSApp beginSheet:panelController.window modalForWindow:self.window modalDelegate:nil didEndSelector:nil contextInfo:nil];

// retain the panel view controller
objc_setAssociatedObject(self, &DTPresentedViewControllerKey, panelController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)dismissModalPanelController
{
NSWindowController *windowController = self.modalPanelController;

if (!windowController)
{
return;
}

// slide out
[NSApp endSheet:windowController.window];
[windowController.window close];

// free the panel view controller
objc_setAssociatedObject(self, &DTPresentedViewControllerKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSWindowController *)modalPanelController
{
return objc_getAssociatedObject(self, &DTPresentedViewControllerKey);
}

@end
56 changes: 46 additions & 10 deletions DTFoundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
A73D5BAC155271FD0024BDB7 /* NSArray+DTError.h in Headers */ = {isa = PBXBuildFile; fileRef = A73D5BA9155271FD0024BDB7 /* NSArray+DTError.h */; settings = {ATTRIBUTES = (Public, ); }; };
A73D5BAD155271FD0024BDB7 /* NSArray+DTError.m in Sources */ = {isa = PBXBuildFile; fileRef = A73D5BAA155271FD0024BDB7 /* NSArray+DTError.m */; };
A73D5BAE155271FD0024BDB7 /* NSArray+DTError.m in Sources */ = {isa = PBXBuildFile; fileRef = A73D5BAA155271FD0024BDB7 /* NSArray+DTError.m */; };
A7437DE216147A450091C1A2 /* NSString+DTPaths.m in Sources */ = {isa = PBXBuildFile; fileRef = A7D8628014EBF65C001436AF /* NSString+DTPaths.m */; };
A747797215F78D630026ABA8 /* DTDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = A7D0AA43153C22B00020F18B /* DTDownload.m */; };
A747797415F78DEA0026ABA8 /* DTActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = A7EE2A0C1582CBFC00BB06DE /* DTActionSheet.m */; };
A747797515F78E3F0026ABA8 /* DTAsyncFileDeleter.m in Sources */ = {isa = PBXBuildFile; fileRef = A77DD39D14E648AD00F34B03 /* DTAsyncFileDeleter.m */; };
Expand Down Expand Up @@ -98,6 +99,11 @@
A79231CF157A0B9400C3ACBB /* NSURL+DTUnshorten.h in Headers */ = {isa = PBXBuildFile; fileRef = A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */; };
A79231D0157A0B9400C3ACBB /* NSURL+DTUnshorten.m in Sources */ = {isa = PBXBuildFile; fileRef = A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */; };
A79231D1157A0B9400C3ACBB /* NSURL+DTUnshorten.m in Sources */ = {isa = PBXBuildFile; fileRef = A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */; };
A79296811619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.h in Headers */ = {isa = PBXBuildFile; fileRef = A792967F1619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.h */; };
A79296821619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.m in Sources */ = {isa = PBXBuildFile; fileRef = A79296801619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.m */; };
A792968D1619D99400D5C979 /* NSDocument+DTFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = A792968B1619D99400D5C979 /* NSDocument+DTFoundation.h */; };
A792968E1619D99400D5C979 /* NSDocument+DTFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = A792968C1619D99400D5C979 /* NSDocument+DTFoundation.m */; };
A792968F1619F0FA00D5C979 /* DTUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = A7D0AA6C153C39920020F18B /* DTUtils.m */; };
A7949A3A14C963F500A8CCDE /* DTHTMLParser.h in Headers */ = {isa = PBXBuildFile; fileRef = A7949A3814C963F500A8CCDE /* DTHTMLParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7949A3B14C963F500A8CCDE /* DTHTMLParser.h in Headers */ = {isa = PBXBuildFile; fileRef = A7949A3814C963F500A8CCDE /* DTHTMLParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7A7CC7914866CAF00EC2EE4 /* DTVersion.m in Sources */ = {isa = PBXBuildFile; fileRef = A70B4CC81486621B00873A4A /* DTVersion.m */; };
Expand Down Expand Up @@ -292,6 +298,10 @@
A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+DTActionHandlers.m"; sourceTree = "<group>"; };
A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+DTUnshorten.h"; sourceTree = "<group>"; };
A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+DTUnshorten.m"; sourceTree = "<group>"; };
A792967F1619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSWindowController+DTPanelControllerPresenting.h"; sourceTree = "<group>"; };
A79296801619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSWindowController+DTPanelControllerPresenting.m"; sourceTree = "<group>"; };
A792968B1619D99400D5C979 /* NSDocument+DTFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDocument+DTFoundation.h"; sourceTree = "<group>"; };
A792968C1619D99400D5C979 /* NSDocument+DTFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDocument+DTFoundation.m"; sourceTree = "<group>"; };
A7949A3814C963F500A8CCDE /* DTHTMLParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTHTMLParser.h; sourceTree = "<group>"; };
A7949A3914C963F500A8CCDE /* DTHTMLParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTHTMLParser.m; sourceTree = "<group>"; };
A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+DTNetworkActivity.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -514,6 +524,34 @@
path = minizip;
sourceTree = "<group>";
};
A79296841619D37A00D5C979 /* OS X */ = {
isa = PBXGroup;
children = (
A792967F1619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.h */,
A79296801619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.m */,
A792968B1619D99400D5C979 /* NSDocument+DTFoundation.h */,
A792968C1619D99400D5C979 /* NSDocument+DTFoundation.m */,
);
name = "OS X";
sourceTree = "<group>";
};
A79296851619D38600D5C979 /* iOS */ = {
isa = PBXGroup;
children = (
A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */,
A7AB8BF9156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m */,
A7200AC9150917FD00D81719 /* UIImage+DTFoundation.h */,
A7200ACA150917FD00D81719 /* UIImage+DTFoundation.m */,
A72C110314A4946800F4EF69 /* UIView+DTFoundation.h */,
A72C110414A4946800F4EF69 /* UIView+DTFoundation.m */,
A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */,
A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */,
A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */,
A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */,
);
name = iOS;
sourceTree = "<group>";
};
A7AB49DB1483C47F00028FE8 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand All @@ -540,6 +578,8 @@
A7D0AA69153C39540020F18B /* Categories */ = {
isa = PBXGroup;
children = (
A79296851619D38600D5C979 /* iOS */,
A79296841619D37A00D5C979 /* OS X */,
A73D5BA9155271FD0024BDB7 /* NSArray+DTError.h */,
A73D5BAA155271FD0024BDB7 /* NSArray+DTError.m */,
A7D0AA23153C1B160020F18B /* NSDictionary+DTError.h */,
Expand All @@ -554,8 +594,6 @@
A7D0AA49153C233E0020F18B /* NSString+DTUtilities.m */,
A7D8627F14EBF65C001436AF /* NSString+DTPaths.h */,
A7D8628014EBF65C001436AF /* NSString+DTPaths.m */,
A7AB8BF8156AA00700CBAB7E /* UIApplication+DTNetworkActivity.h */,
A7AB8BF9156AA00700CBAB7E /* UIApplication+DTNetworkActivity.m */,
A7D0AA29153C1FE40020F18B /* NSString+DTURLEncoding.h */,
A7D0AA2A153C1FE40020F18B /* NSString+DTURLEncoding.m */,
A70B4CCC1486621B00873A4A /* NSURL+DTAppLinks.h */,
Expand All @@ -564,14 +602,6 @@
A70B4CCF1486621B00873A4A /* NSURL+DTPrefLinks.m */,
A79231CC157A0B9400C3ACBB /* NSURL+DTUnshorten.h */,
A79231CD157A0B9400C3ACBB /* NSURL+DTUnshorten.m */,
A7200AC9150917FD00D81719 /* UIImage+DTFoundation.h */,
A7200ACA150917FD00D81719 /* UIImage+DTFoundation.m */,
A72C110314A4946800F4EF69 /* UIView+DTFoundation.h */,
A72C110414A4946800F4EF69 /* UIView+DTFoundation.m */,
A7B57ED8157004630091D4EF /* UIWebView+DTFoundation.h */,
A7B57ED9157004630091D4EF /* UIWebView+DTFoundation.m */,
A78DF5C3157BBC5B00E360B5 /* UIView+DTActionHandlers.h */,
A78DF5C4157BBC5B00E360B5 /* UIView+DTActionHandlers.m */,
);
name = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -781,6 +811,8 @@
buildActionMask = 2147483647;
files = (
A766136216143F8A00DF6C2B /* NSMutableArray+DTMoving.h in Headers */,
A79296811619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.h in Headers */,
A792968D1619D99400D5C979 /* NSDocument+DTFoundation.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1139,6 +1171,10 @@
FACE18701612035600758319 /* DTPDFPage.m in Sources */,
A766136616143F8A00DF6C2B /* NSMutableArray+DTMoving.m in Sources */,
A76613E5161444FD00DF6C2B /* NSString+DTUtilities.m in Sources */,
A7437DE216147A450091C1A2 /* NSString+DTPaths.m in Sources */,
A79296821619C94100D5C979 /* NSWindowController+DTPanelControllerPresenting.m in Sources */,
A792968E1619D99400D5C979 /* NSDocument+DTFoundation.m in Sources */,
A792968F1619F0FA00D5C979 /* DTUtils.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 63e4d1a

Please sign in to comment.