Skip to content

Instantly share code, notes, and snippets.

View pilky's full-sized avatar

Martin Pilkington pilky

View GitHub Profile
--indent 4
--self insert
--extensionacl on-declarations
--modifierorder required,dynamic,override,public,private,private(set),lazy
--ranges no-space
--patternlet inline
--disable consecutiveBlankLines, strongOutlets, blankLinesAroundMark, unusedArguments
--disable spaceInsideComments, redundantParens, andOperator, consecutiveSpaces, redundantReturn
--disable indent, redundantNilInit, redundantType, redundantObjc, enumNamespaces
@pilky
pilky / SourceListTableCellView.swift
Created July 6, 2020 19:06
A source list cell that correct colours the label text of a drag image when selected
class SourceListTableCellView: NSTableCellView {
override var draggingImageComponents: [NSDraggingImageComponent] {
let components = super.draggingImageComponents
guard
self.backgroundStyle == .emphasized, //emphasized = selected
let textField = self.textField,
let newStyle = textField.attributedStringValue.mutableCopy() as? NSMutableAttributedString,
let labelIndex = components.firstIndex(where: { $0.key == .label })
else {
return components
@pilky
pilky / ExtendableFilePromiseProvider.swift
Created May 25, 2020 12:28
NSFilePromiseProvider that supports additional types
class ExtendableFilePromiseProvider: NSFilePromiseProvider {
var additionalItems: [NSPasteboard.PasteboardType: Any] = [:]
override func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
var types = super.writableTypes(for: pasteboard)
if self.additionalItems.count > 0 {
types.append(contentsOf: self.additionalItems.keys)
}
return types
}
@pilky
pilky / CalculatingCellSize.swift
Created March 16, 2020 17:22
Dynamic NSOutlineView row heights
class MyOutlineDelegate: NSObject, NSOutlineViewDelegate {
//The cell we'll use for calculating sizes, should be the same cell used in the outline
lazy var sizingCell: MyCell = {
let cell = MyCell(frame: .zero)
cell.translatesAutoresizingMaskIntoConstraints = false
return cell
}()
lazy var sizingCellWidthAnchor: NSLayoutConstraint = {
return self.sizingCell.widthAnchor.constraint(equalToConstant: 1000)
@pilky
pilky / optional-weirdness.swift
Created October 28, 2019 14:30
Optional KeyPath Weirdness
import Cocoa
class Foo {
var string = "test"
var optionalString: String?
}
let foo = Foo()
foo[keyPath: \.string] == nil //false
class Foo<T> {
struct Bar {
let handler: (T) -> Void
}
var bars: [Bar]
}
protocol Test {
var foo: String { get set }
func updateFoo()
}
extension Test where Self: NSObject {
func updateFoo() {
self.foo = "Bar" // <- Error: Cannot assign to property: 'self' is immutable
}
@pilky
pilky / gist:1b562464f0ad3a5f268bd0bee8581c63
Created July 27, 2019 15:12
Xcode 11 Playground Crash
import Cocoa
class Foo: NSObject {
var objectID = UUID()
}
let foo = Foo() // Crashes with "error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x20)."
//Obj-C
typedef NSString * ABCMessageField NS_STRING_ENUM;
extern ABCMessageField const ABCMessageFieldSubject;
extern ABCMessageField const ABCMessageFieldFrom;
//Expected Swift (according to https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17)
struct ABCMessageField: RawRepresentable {
typealias RawValue = String
init(rawValue: RawValue)
@property (assign, nonatomic) CGSize preferredMaxSize;
- (CGSize)intrinsicContentSize {
CGSize imageSize = self.image.size;
CGSize maxSize = self.preferredMaxSize;
if (imageSize.height > maxSize.height) {
imageSize.width *= maxSize.height / imageSize.height;
imageSize.height = maxSize.height;
}