Skip to content

Instantly share code, notes, and snippets.

View smic's full-sized avatar

Stephan Michels smic

View GitHub Profile
// TODO: FB8068393 Remove once this properly landed in Big Sur.
// These Strings are missing as of b2 (Linker Error)
extension NSToolbarItem.Identifier {
static let primarySeparator = NSToolbarItem.Identifier(rawValue: "NSToolbarPrimarySidebarTrackingSeparatorItem")
static let supplementarySeparator = NSToolbarItem.Identifier(rawValue: "NSToolbarSupplementarySidebarTrackingSeparatorItem")
}
@shaps80
shaps80 / Font.swift
Last active October 20, 2024 09:46
A set of UIFont/NSFont helpers that matches the equivalent SwiftUI Font API. (Supports iOS 13+ and macOS 10.15+)
import SwiftUI
#if os(macOS)
public typealias Font = NSFont
public typealias FontDescriptor = NSFontDescriptor
#else
public typealias Font = UIFont
public typealias FontDescriptor = UIFontDescriptor
#endif
@chriseidhof
chriseidhof / boilerplate.swift
Last active April 17, 2025 11:08
QuickMacApp
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
@Thomvis
Thomvis / Migrated.swift
Last active October 30, 2024 07:43
An approach towards migrating properties in a Codable struct without having to write a custom Codable implementation
import Cocoa
// Say we have a Person model
enum V1 {
struct Person: Codable {
let name: String
var age: Int
}
}
@dynamicMemberLookup
struct Template {
var template : String
private var data : [String:String]
var populatedTemplate : String { data.reduce(template) { $0.replacingOccurrences(of: "${\($1.key)}", with: $1.value) } }
init(template: String, data: [String:String] = [:]) {
self.template = template
self.data = data
}
@dabrahams
dabrahams / ConcurrentMap.swift
Last active April 3, 2024 04:59
Concurrent Map Implementations, Benchmarked
// See commentary below this gist.
import Foundation
import QuartzCore
// Implementation from https://talk.objc.io/episodes/S01E90-concurrent-map
public final class ThreadSafe<A> {
var _value: A
let queue = DispatchQueue(label: "ThreadSafe")
init(_ value: A) { self._value = value }
extension View {
func vertical(_ key: VerticalAlignment, relative: CGFloat) -> some View {
self.alignmentGuide(key, computeValue: { $0.height * relative })
}
func overlay<V: View>(alignment: Alignment, relative: UnitPoint = .center, _ other: V) -> some View {
self
.alignmentGuide(alignment.horizontal, computeValue: { $0.width * relative.x })
.alignmentGuide(alignment.vertical, computeValue: { $0.height * relative.y })
.overlay(other, alignment: alignment)
@AliSoftware
AliSoftware / Demo.swift
Last active October 31, 2023 12:25
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
@implementation UIImage (ResourceProxyHack)
+ (UIImage *)_iconForResourceProxy:(id)proxy format:(int)format {
// HACK: proxy seems garbage so we always show PDF for now.
let cgImage = [_bridge imageForFileType:@"pdf"];
// HACK: We use mainScreen here but could have multiple screens.
let image = [UIImage imageWithCGImage:cgImage scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
return image;
}
//
// ContentView.swift
// NavigationViewTest
//
// Created by Thomas Visser on 04/11/2019.
// Copyright © 2019 Thomas Visser. All rights reserved.
//
import SwiftUI