Skip to content

Instantly share code, notes, and snippets.

View konstantinpavlikhin's full-sized avatar

Konstantin Pavlikhin konstantinpavlikhin

View GitHub Profile
@mayoff
mayoff / NSBezierPath-CGPath.swift
Last active September 20, 2022 11:00 — forked from juliensagot/NSBezierPath+cgPath.swift
Convert NSBezierPath to CGPath (Swift 3.0 / Xcode 8b4 syntax)
import AppKit
public extension NSBezierPath {
public var CGPath: CGPath {
let path = CGMutablePath()
var points = [CGPoint](repeating: .zero, count: 3)
for i in 0 ..< self.elementCount {
let type = self.element(at: i, associatedPoints: &points)
switch type {
@soffes
soffes / BaseTextStorage.h
Last active July 1, 2023 10:14
BaseTextStorage (Objective-C)
@import Darwin.TargetConditionals;
#if TARGET_OS_IPHONE
@import UIKit;
#else
@import AppKit;
#endif
/// Concrete text storage intended to be subclassed.
@interface BaseTextStorage : NSTextStorage
@jspahrsummers
jspahrsummers / main.m
Created November 24, 2015 16:11
[NSThread isMainThread] is probably not what you want!
#import <Foundation/Foundation.h>
int main (int argc, const char **argv) {
@autoreleasepool {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"is main thread? %i", (int)[NSThread isMainThread]);
});
});
@chadseld
chadseld / OPTestKeyLoopView.m
Created July 16, 2015 18:32
OPTestKeyLoopView.m
//
// OPTestKeyLoopView.m
//
// Copyright (c) 2013 AgileBits Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
@chadseld
chadseld / OPTestKeyLoopView.h
Created July 16, 2015 18:31
OPTestKeyLoopView.h
//
// OPTestKeyLoopView.h
//
// Copyright (c) 2013 AgileBits Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
@boredzo
boredzo / PRHFrameGrabber+DraggingSource.m
Last active March 11, 2016 19:26
Dragging source code for promising files from my never-released movie player app.
//PRHFrameGrabber grabs frame images from the movie.
//It wraps a couple of AVAssetImageGenerators, one of which is allowed to round to a more convenient time (e.g., a keyframe).
//Thus, the temporally lax generator will return an image first, while we wait for the temporally strict generator, which may take a significant fraction of a second.
//PRHFrameGrabber is also an NSPasteboardWriter. It's what the movie view feeds to the dragging session when the user tries to drag a frame; the frame grabber fulfills its NSPasteboardWriter duties by returning the grabbed frame.
@interface PRHFrameGrabber ()
@property(readwrite, copy) NSImage *image;
@end
@implementation PRHFrameGrabber
@steipete
steipete / PSPDFEnvironment.m
Last active March 5, 2024 09:15
Example for DISPATCH_SOURCE_TYPE_MEMORYPRESSURE (Mac only, since 10.9) ... Since we share code between iOS and mac, I'm trying to be a good system citizen and reimplement the equivalent of UIApplicationDidReceiveMemoryWarningNotification on the Mac.
NSString *const PSPDFApplicationDidReceiveMemoryWarningNotification = @"PSPDFApplicationDidReceiveMemoryWarningNotification";
// Test with sudo memory_pressure -l critical. Don't forget to re-set afterwards!
__attribute__((constructor)) static void PSPDFInstallLowMemoryNotificationWarningMac(void) {
static dispatch_source_t source;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source);
@jspahrsummers
jspahrsummers / GHRunLoopWatchdog.h
Created January 28, 2015 20:50
A class for logging excessive blocking on the main thread
/// Observes a run loop to detect any stalling or blocking that occurs.
///
/// This class is thread-safe.
@interface GHRunLoopWatchdog : NSObject
/// Initializes the receiver to watch the specified run loop, using a default
/// stalling threshold.
- (id)initWithRunLoop:(CFRunLoopRef)runLoop;
/// Initializes the receiver to detect when the specified run loop blocks for
@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@jaredsinclair
jaredsinclair / set-build-number.sh
Last active January 1, 2020 15:41
Git-friendly run script for automated build numbering in Xcode
# Set the build number to the current git commit count.
#
# Permanent home:
# https://gist.github.com/jaredsinclair/af6898f93674ee5923f3
#
# Based on: http://w3facility.info/question/how-do-i-force-xcode-to-rebuild-the-info-plist-file-in-my-project-every-time-i-build-the-project/
# Updated with dSYM handling from http://yellowfeather.co.uk/blog/auto-incrementing-build-number-in-xcode-revisited/
git=`sh /etc/profile; which git`
appBuild=`"$git" rev-list HEAD --count`