Skip to content

Instantly share code, notes, and snippets.

struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})
@haikusw
haikusw / KeyCommand.swift
Created May 24, 2022 23:25 — forked from steipete/KeyCommand.swift
Add Keyboard Shortcuts to SwiftUI on iOS 13 when using `UIHostingController`. Requires using KeyboardEnabledHostingController as hosting class) See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
//
// KeyCommand.swift
// Adds Keyboard Shortcuts to SwiftUI on iOS 13
// See https://steipete.com/posts/fixing-keyboardshortcut-in-swiftui/
// License: MIT
//
// Usage: (wrap view in `KeyboardEnabledHostingController`)
// Button(action: {
// print("Button Tapped!!")
// }) {
@haikusw
haikusw / StringGetSizeThatFits.swift
Created September 7, 2021 20:51 — forked from krzyzanowskim/StringGetSizeThatFits.swift
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski [email protected]
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000))
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil)
import SwiftUI
import Yams
extension String {
var yamlToJSON: String {
do {
guard let parsed = try Yams.load(yaml: self) else { return "" }
let data = try JSONSerialization.data(withJSONObject: parsed, options: [.sortedKeys, .prettyPrinted])
return String(decoding: data, as: UTF8.self)
} catch {
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
@haikusw
haikusw / ExampleApp.swift
Created April 8, 2021 17:02 — forked from helje5/SparkleCommands.swift
How to hookup Sparkle in SwiftUI
@main
struct PastApp: App { // https://zeezide.de/en/products/past/
var body: some Scene {
DocumentGroup(viewing: PastDocument.self) { file in
ContentView(document: file.$document)
}
.commands {
TextFormattingCommands()
import Foundation
import Combine
import PlaygroundSupport
func await<T>(_ body: @escaping (@escaping (T) -> Void) -> AnyCancellable) -> T {
return {
var result: T! = nil
let semaphore = DispatchSemaphore(value: 0)
body { value in
result = value
@haikusw
haikusw / AirplaneView.swift
Created February 18, 2021 09:44 — forked from CraigSiemens/AirplaneView.swift
Modified version if AirplaneView that tries to more closely match the behaviour in AutoLayout. https://talk.objc.io/episodes/S01E241-swiftui-layout-challenge-1
//
// ContentView.swift
// Airplane
//
// Created by Chris Eidhof on 08.02.21.
//
import SwiftUI
struct ContentView: View {
@haikusw
haikusw / DecodableRoot.swift
Created September 17, 2020 05:39 — forked from htinlinn/DecodableRoot.swift
Decode JSON at root level based on a key
extension JSONDecoder {
func decode<T: Decodable>(_ type: T.Type, from data: Data, keyedBy key: String?) throws -> T {
if let key = key {
// Pass the top level key to the decoder.
userInfo[.jsonDecoderRootKeyName] = key
let root = try decode(DecodableRoot<T>.self, from: data)
return root.value
} else {
@haikusw
haikusw / Tricubic.cginc
Created August 30, 2020 17:01 — forked from Fewes/Tricubic.cginc
Tricubic texture sampling for Unity
/*--------------------------------------------------------------------------*\
Copyright (c) 2008-2009, Danny Ruijters. All rights reserved.
http://www.dannyruijters.nl/cubicinterpolation/
This file is part of CUDA Cubic B-Spline Interpolation (CI).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the