Skip to content

Instantly share code, notes, and snippets.

@emin-grbo
emin-grbo / decodeOrReport.swift
Created February 13, 2024 10:39
DecodeOrReport
// Used to detect specific issue with JSON decoding
func decodeOrReport(data: Data) {
do {
let _ = try JSONDecoder().decode(WidgetResponse.self, from: data)
print("\n\n👍ALL GOOD\n\n")
} catch DecodingError.keyNotFound( let key, let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
print("could not find key \(key) in JSON: \(context.debugDescription)")
} catch DecodingError.valueNotFound( let type, let context) {
print("\n\n⛔️FAILED TO DECODE\n\n")
@drewolbrich
drewolbrich / View+WindowGeometryPreferences.swift
Last active June 3, 2024 12:08
A visionOS SwiftUI view modifier that can be used to hide a window's resize handles or to constrain a window's aspect ratio
//
// View+WindowGeometryPreferences.swift
//
// Created by Drew Olbrich on 1/30/24.
// Copyright © 2024 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@drewolbrich
drewolbrich / SIMD+LookAt.swift
Created November 17, 2023 05:30
SIMD look-at functions
import simd
/// Creates a 4x4 matrix that orients the +Z axis in the direction of `at`, and the
/// +Y axis toward `up`.
func simd_look(at: SIMD3<Float>, up: SIMD3<Float> = SIMD3<Float>(0, 1, 0)) -> simd_float4x4 {
let zAxis = normalize(at)
let xAxis = normalize(cross(up, zAxis))
let yAxis = normalize(cross(zAxis, xAxis))
return simd_float4x4(
@scornflake
scornflake / CALayerToMetalRenderer.swift
Created October 19, 2023 02:18
CALayer -> MTLTexture renderer (via CARemderer)
import SWBShared2
import Metal
import AppKit
import CoreImage
import CoreGraphics
import QuartzCore
@globalActor
public struct CALayerToMetalRendererActor {
@amirdew
amirdew / View+MinimumPadding.swift
Created December 22, 2022 13:20
Minimum paddings for SwiftUI views
extension View {
func minimumPadding(edges: Edge.Set = .all, _ length: CGFloat = 8) -> some View {
GeometryReader { geo in
padding(.bottom, edges.contains(.bottom) ? max(length, geo.safeAreaInsets.bottom) : 0)
.padding(.top, edges.contains(.top) ? max(length, geo.safeAreaInsets.top) : 0)
.padding(.leading, edges.contains(.leading) ? max(length, geo.safeAreaInsets.leading) : 0)
.padding(.trailing, edges.contains(.trailing) ? max(length, geo.safeAreaInsets.trailing) : 0)
.ignoresSafeArea(edges: edges)
}
}
@Starmel
Starmel / commonprofile_frag.metal
Created May 4, 2020 19:17
SceneKit Shader fragment functions
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
@warrenm
warrenm / CommonProfile.metal
Created November 14, 2019 01:29
SceneKit's CommonProfile Shader v2 (macOS 10.15)
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
@aronskaya
aronskaya / catalyst_notes.md
Created September 3, 2019 16:51
Notes on Catalyst (macOS)

Toolbar

NSToolbar (Mac API) UIWindowScene.titlebar.toolbar — access

func scene(_ scene: UIScene,
			willConnectTo session: UISceneSession,
			options connectionOptions: 				UIScene.ConnectionOptions) {
		// setup the window
@mhamilt
mhamilt / metal_compute.md
Created August 23, 2019 11:00
Metal Compute Shader

Paralell Data Processing with Metal

Build Instructions

Create a Swift Command Line Tool in Xcode. You can then copy and paste the below code into the .swift and .metal files respectively

Source

main.swift

//
// TYAudioVideoManager.swift
// Yash Thaker
//
// Created by Yash Thaker on 05/12/18.
// Copyright © 2018 Yash Thaker. All rights reserved.
//
/*
1) mergeVideos(videoUrls: [URL], exportUrl: URL, preset: String? = nil, progress: @escaping Progress, completion: @escaping Completion)