Skip to content

Instantly share code, notes, and snippets.

View iby's full-sized avatar
👻

Ian Bytchek iby

👻
View GitHub Profile
// This is a hideous hack for 10.11 where constraints are applied for detached view and the entire
// toolset panel looks like a huge cock…
if #available(macOS 10.13, *) {
} else if self.instructionView.isHidden && self.instructionView.superview != nil {
self.stackView.removeView(self.instructionView)
} else if !self.instructionView.isHidden && self.instructionView.superview == nil {
self.stackView.insertView(self.instructionView, at: 0, in: .center)
}
@iby
iby / Exclude.swift
Created September 5, 2021 09:22
Create a list of excluded paths at the base location keeping the specified paths.
private func exclude(paths: [String], at basePath: String) -> [String] {
let slash = CharacterSet(charactersIn: "/")
let baseURL = URL(fileURLWithPath: basePath, isDirectory: true)
var exclusions = try! FileManager.default.contentsOfDirectory(atPath: basePath).sorted()
var inclusions = paths
// Cleanup paths removing and expanding `/.`, `/..`, etc.
inclusions = inclusions.reduce(into: [], { inclusions, inclusion in
let path = URL(fileURLWithPath: inclusion, relativeTo: baseURL).standardized.relativePath.trimmingCharacters(in: slash)
if !path.isEmpty { inclusions.append(path) }
@iby
iby / main.swift
Created June 17, 2019 14:27
Slang Example
import Slang
let source: String = "import Foundation; class Foo { let bar = 1 }"
let file: File = File(source)
let disassembly: Disassembly = try! Disassembly(file)
var edits: [Edit] = []
// Change "Foundation" identifier to "AppKit".
edits.append(Edit(disassembly.query.syntax.first(of: .identifier).select(where: { $0.contents == "Foundation" }).one!, "AppKit"))
// Change "class" keyword to "struct".
@iby
iby / CALayer+CARenderer.swift
Last active June 6, 2023 12:20
Rendering animated CALayer off-screen using CARenderer with MTLTexture, https://stackoverflow.com/q/56150363/458356
import AppKit
import Metal
import QuartzCore
let view = NSView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))
let circle = NSView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
circle.wantsLayer = true
circle.layer?.backgroundColor = NSColor.red.cgColor
circle.layer?.cornerRadius = 25
let fooOne: Foo = Foo()
let fooTwo: Foo = Foo()
fooOne.reactive.signal.bar.observeValues({ Swift.print($0) })
fooOne.reactive.bind.bar <~ fooTwo.reactive.signal.bar
fooOne.bar = "Such b-a-r!"
fooTwo.bar = "Very BAR!"
class Foo: ReactiveExtensionsProvider {
let pipe = Signal<String, NoError>.pipe()
var bar: String = "Such bar!" {
didSet { self.pipe.input.send(value: self.bar) }
}
}
extension Reactive.Binds where Base: Foo {
var bar: BindingTarget<String> {
return self.reactive.makeBindingTarget({ $0.bar = $1 })
extension Reactive {
/// A proxy which holds reactive binding target extensions of `Base`.
struct Binds<Base> {
fileprivate init(_ base: Base) { self.base = base }
let base: Base
}
var bind: Binds<Base> { return Binds(self.base) }
@iby
iby / MLWAsyncAVPlayer.h
Created March 11, 2018 15:48 — forked from k06a/MLWAsyncAVPlayer.h
Awesome optimized AVPlayer for smooth scrolling AVPlayerLayer inside UICollectionView/UITableView (tested on iOS10+)
#import <AVFoundation/AVFoundation.h>
@interface MLWAsyncAVPlayer : AVPlayer
@end
@iby
iby / Foo.m
Last active May 4, 2019 08:01
Using NSObject’s +load and +initialize from Swift, checkout the full story – https://medium.com/post-mortem/using-nsobjects-load-and-initialize-from-swift-f6f2c6d9aad0
#import <Foundation/Foundation.h>
#import <MyProject/MyProject-Swift.h>
// This was a more concise solution in Swift 4.2 and earlier. If you try this with Swift 5 you'd get "Swift class
// extensions and categories on Swift classes are not allowed to have +load methods" runtime error.
// @implementation Foo (private)
// + (void)load { [self swiftyLoad]; }
// + (void)initialize { [self swiftyInitialize]; }
// @end
@iby
iby / NSViewOverlapTest.swift
Last active December 18, 2022 11:02
Overlap test for NSView with tracking areas
import AppKit
extension NSView
{
/// Checks if view overlaps with other views above it at specified **window** location.
func overlapTest(_ point: NSPoint) -> Bool {
// Perform hit test to get the view under mouse. Because it can be a view within