Skip to content

Instantly share code, notes, and snippets.

View jwilling's full-sized avatar

Jonathan Willing jwilling

View GitHub Profile
@jwilling
jwilling / gist:2292277
Created April 3, 2012 14:08
Drawing with shadow offset
- (void)drawInRect:(CGRect)rect {
CGSize shadowOffset = CGSizeMake(0, -1);
float colorValues[] = {0, 0, 0, .9};
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadow (ctx, shadowOffset, 1);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(colorSpace, colorValues);
@jwilling
jwilling / gist:2773202
Created May 23, 2012 04:03
Simple category for changing window size to fit content
- (void)resizeWindowForContentSize:(NSSize)size animated:(BOOL)animated {
NSRect windowFrame = [self contentRectForFrameRect:[self frame]];
NSRect newWindowFrame = [self frameRectForContentRect:
NSMakeRect( NSMinX( windowFrame ), NSMaxY( windowFrame ) - size.height, size.width, size.height )];
[self setFrame:newWindowFrame display:YES animate:animated];
}
@jwilling
jwilling / NSView+Animations.m
Created July 4, 2012 22:28
NSView add/removeSubview animated
@implementation NSView (Animations)
- (void)addSubview:(NSView *)aView animated:(BOOL)animated {
[aView setAlphaValue:0.f];
[aView setFrameOrigin:NSZeroPoint];
CGFloat duration = animated ? (([[[self window] currentEvent] modifierFlags] & NSShiftKeyMask) ? 1.f : 0.25f ) : 0.f;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:duration];
@jwilling
jwilling / JWLocationManager.h
Created July 31, 2012 20:02
Block-based wrapper around Core Location
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef void (^JWLocationManagerCallback)(CLLocation *location);
typedef void (^JWLocationManagerErrorCallback)(NSError *error);
@interface JWLocationManager : NSObject <CLLocationManagerDelegate>
@property (nonatomic, copy) NSString *purpose;
@property (nonatomic, copy) JWLocationManagerCallback locationUpdatedBlock;
@jwilling
jwilling / CustomClipView.m
Last active October 12, 2015 09:08
NSTableView with custom clip view
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
self.layer = [CAScrollLayer layer];
self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
return self;
}
@jwilling
jwilling / gist:4186817
Last active March 5, 2023 22:01
Lets help improve AppKit.

Is AppKit causing you frustration? Instead of just complaining about it, lets try to make it better by compiling a list of specific problems with AppKit. Leave a comment below, and I'll include it in the list.


##NSView##

  • NSView does not have the ability to set an affine transform (rdar://15608609)
  • Controls that have cells (such as NSTextField) do not respond properly when layer-backed. Take NSButton as an example. When not layer-backed, it will animate properly using the animator proxy. When layer-backed, using the animator proxy breaks focus rings (they jump to destination immediately). Currently, directly manipulating the layer of a cell-based control is not supported (and will lead to a broken interface), but is much needed. (rdar://15608822)

##NSViewController##

  • NSViewController could be more useful. It has -loadView but no other lifecycle methods. (rdar://15608948)
@jwilling
jwilling / gist:5929321
Last active December 19, 2015 08:59
Handling errors when using toProperty:onObject:.
RAC(self.someOrderedSet) = [[[[[[RACAble(self.searchField.text)
filter:^BOOL(NSString *text) {
return (text.length > 0);
}]
map:^id(NSString *text) {
return [Something searchForAutocomplete:text]; // this signal can send errors
}]
switchToLatest]
catchTo:[RACSignal return:[NSArray array]]]
map:^id(NSArray *objects) {
@jwilling
jwilling / JNWAsyncGroupOperation.m
Created July 11, 2013 00:55
basic async operations
#import <Foundation/Foundation.h>
typedef void (^JNWGroupAsyncOperationCompletion)();
@interface JNWGroupAsyncOperation : NSObject
// The completion block that will be fired when all operations have completed.
@property (nonatomic, copy) void(^groupCompletion)();
// Adds an asynchronous operation and starts it on the main thread. The completion block _must_
@jwilling
jwilling / JNWThrottledBlock.h
Last active October 26, 2020 17:02
Simple throttling of blocks using dispatch sources.
#import <Foundation/Foundation.h>
@interface JNWThrottledBlock : NSObject
// Runs the block after the buffer time _only_ if another call with the same identifier is not received
// within the buffer time. If a new call is received within that time period the buffer will be reset.
// The block will be run on the main queue.
//
// Identifier and block must not be nil.
+ (void)runBlock:(void (^)())block withIdentifier:(NSString *)identifier throttle:(CFTimeInterval)bufferTime;
@jwilling
jwilling / QTMAsyncOperationQueue.h
Created October 29, 2013 04:17
Simple block-based serial queue for asynchronous operations.
//
// QTMAsyncOperationQueue.h
// Quantum
//
// Created by Jonathan Willing on 10/28/13.
// Copyright (c) 2013 AppJon. All rights reserved.
//
#import <Foundation/Foundation.h>