Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Willing jwilling

View GitHub Profile
@jwilling
jwilling / JNWThrottledBlock.h
Last active Oct 26, 2020
Simple throttling of blocks using dispatch sources.
View JNWThrottledBlock.h
#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 / gist:4186817
Last active Apr 24, 2020
Lets help improve AppKit.
View gist:4186817

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 / NSView+Animations.m
Created Jul 4, 2012
NSView add/removeSubview animated
View NSView+Animations.m
@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 / LBHView.h
Last active Feb 6, 2018
A terrible, terrible hack to toggle AppKit's destruction of layer modifications.
View LBHView.h
#import <Cocoa/Cocoa.h>
@interface LBHView : NSView
/// This property controls whether the view can indirectly control its
/// backing layer's properties, specifically the following:
/// `affineTransform`
/// `anchorPoint`
///
/// These properties cannot be modified on the layer during the time that
@jwilling
jwilling / NSString+IBNMetrics.h
Created Jan 3, 2014
Better string metrics on OS X.
View NSString+IBNMetrics.h
#import <Foundation/Foundation.h>
@interface NSString (IBNMetrics)
- (CGSize)ibn_sizeConstrainedToSize:(CGSize)size font:(NSFont *)font;
- (CGSize)ibn_sizeWithFont:(NSFont *)font;
@end
@jwilling
jwilling / gist:8160367
Created Dec 28, 2013
NSViewLayerContentsRedrawCrossfade on 10.8
View gist:8160367
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = 4.f;
context.allowsImplicitAnimation = YES;
[view.layer addAnimation:CATransition.animation forKey:@"contents"];
view.layer.contents = someImage;
} completionHandler:nil];
@jwilling
jwilling / QTMAsyncOperationQueue.h
Created Oct 29, 2013
Simple block-based serial queue for asynchronous operations.
View QTMAsyncOperationQueue.h
//
// QTMAsyncOperationQueue.h
// Quantum
//
// Created by Jonathan Willing on 10/28/13.
// Copyright (c) 2013 AppJon. All rights reserved.
//
#import <Foundation/Foundation.h>
View JNWAsyncGroupOperation.m
#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 / gist:5929321
Last active Dec 19, 2015
Handling errors when using toProperty:onObject:.
View gist:5929321
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 / CustomClipView.m
Last active Oct 12, 2015
NSTableView with custom clip view
View CustomClipView.m
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
self.layer = [CAScrollLayer layer];
self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
return self;
}
You can’t perform that action at this time.