Skip to content

Instantly share code, notes, and snippets.

View martinhoeller's full-sized avatar

Martin Höller martinhoeller

View GitHub Profile
@martinhoeller
martinhoeller / NSColor+Values.swift
Last active April 8, 2024 08:45
NSColor Catalog Colors
import Cocoa
import Foundation
var colors: [NSColor] = [.labelColor, .secondaryLabelColor, .tertiaryLabelColor, .quaternaryLabelColor, .textColor, .placeholderTextColor, .selectedTextColor, .textBackgroundColor, .selectedTextBackgroundColor, .keyboardFocusIndicatorColor, .unemphasizedSelectedTextColor, .unemphasizedSelectedTextBackgroundColor, .linkColor, .separatorColor, .selectedContentBackgroundColor, .unemphasizedSelectedContentBackgroundColor, .selectedMenuItemTextColor, .gridColor, .headerTextColor, .controlAccentColor, .controlColor, .controlBackgroundColor, .controlTextColor, .disabledControlTextColor, .selectedControlColor, .selectedControlTextColor, .alternateSelectedControlTextColor, .windowBackgroundColor, .windowFrameTextColor, .underPageBackgroundColor, .findHighlightColor, .highlightColor, .shadowColor
]
for color in colors {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
import Foundation
extension Array where Element: Hashable {
/**
Returns a new array containing only the unique elements of the receiver.
- warning: There are no guarantees about the order of the returned array.
*/
var uniqued: Self { Array(Set(self)) }
}
/**
Decodes a value of the given type for the given key, providing a transform closure that converts the decoded value into another type.
- parameter type: The type of value to decode.
- parameter key: The key that the decoded value is associated with.
- parameter transform: A closure that transforms the decoded value into a value of a different type.
- returns: The value that was returned by the `transform` closure.
*/
func decode<T, R>(_ type: T.Type, forKey key: Self.Key, transform: (T) throws -> R) throws -> R where T : Decodable {
import Foundation
extension KeyedDecodingContainerProtocol {
/**
Decodes a value of the given type for the given key, providing a default value if the value could not be decoded.
- parameter type: The type of value to decode.
- parameter key: The key that the decoded value is associated with.
- parameter defaultValue: The default value that is used if the a value for `key` could not be decoded.
@martinhoeller
martinhoeller / BoundingBox.swift
Last active April 28, 2020 12:19
A bounding box implementation that can be initialized either with NE/SE or with an array of coordinates.
import CoreLocation
/**
A bounding box implementation that can be initialized either with NE/SE or with an array of coordinates.
*/
struct BoundingBox {
let ne: CLLocationCoordinate2D
let sw: CLLocationCoordinate2D
let center: CLLocationCoordinate2D
let latitudeSpread: CLLocationDegrees
@martinhoeller
martinhoeller / Images.swift
Last active May 10, 2020 09:16
A Swift property wrapper for retreiving images from asset bundles
// inspired by https://stackoverflow.com/a/49205781/379776 and https://swiftbysundell.com/articles/property-wrappers-in-swift/
import UIKit
/*
Add something like this to your podspec
s.resource_bundles = {
'Assets' => ['MyPod/Assets/**/*.{xcassets}']
}
@martinhoeller
martinhoeller / LinkDetectingTextView.swift
Created April 18, 2020 13:43
An NSTextView subclass that automatically highlights links
import AppKit
// based on https://www.hackingwithswift.com/example-code/strings/how-to-detect-a-url-in-a-string-using-nsdatadetector
// and https://stackoverflow.com/a/14604456/379776
class LinkDetectingTextView: NSTextView {
override var string: String {
didSet {
highlightLinks()
}
}
@martinhoeller
martinhoeller / URL+Timestamp.swift
Created April 18, 2020 08:13
A URL extension to insert a timestamp in the URL's file name
extension URL {
// Inserts a timestamp into a URL's filename
// e.g. "/path/to/file.ext" -> "/path/to/file~1587197384.ext"
func addingTimestamp(separator: String = "~") -> URL {
let timestamp = Int(Date().timeIntervalSince1970)
let filename = deletingPathExtension().lastPathComponent + "\(separator)\(timestamp).\(pathExtension)"
return deletingLastPathComponent().appendingPathComponent(filename)
}
}
@martinhoeller
martinhoeller / NSStackView+Animations.swift
Last active June 13, 2023 15:15
An NSStackView extension for animated hiding/showing of views
//
// NSStackView+Animations.swift
//
// Created by Martin Höller on 18.06.16.
// Copyright © 2016 blue banana software. All rights reserved.
//
// Based on http://prod.lists.apple.com/archives/cocoa-dev/2015/Jan/msg00314.html
import Cocoa