Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / HoverView.swift
Last active March 21, 2022 17:24
NSView subclass that applies an effect when the mouse hovers over it.
import AppKit
class HoverView: NSView {
override var wantsUpdateLayer: Bool {
return true
}
private var isMouseOver = false {
didSet {
@mminer
mminer / NSButton+TextColor.swift
Last active September 10, 2022 23:24
NSButton extension that adds a method to change its text color.
import AppKit
extension NSButton {
func set(textColor color: NSColor) {
let newAttributedTitle = NSMutableAttributedString(attributedString: attributedTitle)
let range = NSRange(location: 0, length: attributedTitle.length)
newAttributedTitle.addAttributes([
.foregroundColor: color,
@mminer
mminer / String+Insert.swift
Last active October 18, 2017 04:45
String extension that adds a method to insert a string in the middle of it.
extension String {
func insert(_ string: String, at index: Int) -> String {
let prefix = String(characters.prefix(index))
let suffix = String(characters.suffix(characters.count - index))
return prefix + string + suffix
}
}
@mminer
mminer / NSImage+Base64.swift
Last active May 21, 2021 15:00
NSImage extension that allows initializing an image from a Base64 encoded string.
import AppKit
extension NSImage {
convenience init?(base64EncodedString: String) {
guard
let url = URL(string: base64EncodedString),
let data = try? Data(contentsOf: url)
else {
return nil
@mminer
mminer / formatBytes.swift
Last active April 19, 2023 00:59
Formats bytes into a more human-readable form (e.g. MB).
import Foundation
func format(bytes: Double) -> String {
guard bytes > 0 else {
return "0 bytes"
}
// Adapted from http://stackoverflow.com/a/18650828
let suffixes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
let k: Double = 1000
@mminer
mminer / switch.scss
Created August 23, 2016 03:15
An iOS-style toggle switch.
/*
<label className="switch">
<input type="checkbox" />
<div className="checkbox"></div>
</label>
*/
.switch {
$active-color: #27c940;
$switch-height: 24px;
@mminer
mminer / withHotkeys.js
Created September 9, 2016 18:21
Higher order component for React + Recompose allowing components to accept global hotkeys.
// Usage:
//
// const backspace = 8;
//
// export default withHotkeys({
// [backspace]: props => evt => {
// ...
// },
// })(YourComponent);
@mminer
mminer / parentPath.swift
Created September 16, 2016 18:10
Function that walks up directory tree to find given path's parent directory.
func parent(ofPath path: String, levels: Int) -> String {
var parentPath = path
for _ in 1...levels {
parentPath = (parentPath as NSString).deletingLastPathComponent
}
return parentPath
}
@mminer
mminer / SpriteKitPlaygroundStarter.swift
Last active September 29, 2016 04:27
Xcode playground starter code to experiment with SpriteKit.
import PlaygroundSupport
import SpriteKit
// Set up scene:
let size = CGSize(width: 480, height: 320)
let scene = SKScene(size: size)
scene.physicsBody = SKPhysicsBody(edgeLoopFrom: scene.frame)
scene.physicsWorld.gravity = CGVector.zero // Enabled later
@mminer
mminer / Date+Relative.swift
Last active October 12, 2016 17:32
Date extension to display the date in relative terms (e.g. "5 hours ago").
// Adapted from http://stackoverflow.com/a/1248
private let minuteInterval: TimeInterval = 60
private let hourInterval: TimeInterval = 60 * minuteInterval
private let dayInterval: TimeInterval = 24 * hourInterval
private let monthInterval: TimeInterval = 30 * dayInterval
extension Date {
var relative: String {