Skip to content

Instantly share code, notes, and snippets.

// import https://gist.github.com/JadenGeller/2cdcc441f936b4b0a7b331ccf13a78ac#file-monitorevents-swift
import SwiftUI
enum ScrollPhase: Hashable, Sendable {
case interactive
case momentum
case ended
}
struct ScrollAreaModifier: ViewModifier {
struct HFold3DEffectModifier: ViewModifier {
var angle: Angle
var perspective: Double
var anchor: UnitPoint {
angle.radians >= 0 ? .leading : .trailing
}
func visualWidth(width: CGFloat) -> CGFloat {
width * cos(angle.radians) / (perspective * sin(abs(angle.radians)) + 1)
import SwiftUI
// FIXME: Is there value to defining this DynamicProperty vs. inlining all this into LazyBindingView?
@propertyWrapper
struct LazyBinding<Value>: DynamicProperty {
var initialValue: Value
var makeBinding: () -> Binding<Value>
@State var binding: Binding<Value>?
import SwiftUI
import OpenAI
struct OpenAIClient: ViewModifier {
@AppStorage("apiToken") var apiToken: String = ""
struct AccessTokenEntry: View {
var submit: (String) -> Void
@State var accessToken: String = ""
@JadenGeller
JadenGeller / DynamicSizeTextEditor.swift
Last active October 2, 2023 02:44
Dynamic width and height text editor — resizes to fit contents, like fixedSize should do
struct DynamicWidthTextEditor: View {
@Binding var text: String
// add spacing after each line to prevent flexible width text editor from reflowing before SwiftUI rerenders
var textWithTrailingBlocks: String {
text
.split(separator: "\n")
.map({ $0 + "█." })
.joined(separator: "\n")
}
@JadenGeller
JadenGeller / KeyPress+NSEvent.swift
Created October 1, 2023 06:04
Convert SwiftUI KeyPress to NSEvent — hopefully not useful for anything
extension NSEvent {
static func keyPress(_ keyPress: KeyPress) -> NSEvent? {
guard let eventType = EventType(keyPress.phase) else { return nil }
return .keyEvent(
with: eventType,
location: .zero,
modifierFlags: .init(keyPress.modifiers),
timestamp: ProcessInfo.processInfo.systemUptime,
windowNumber: 0, // TODO: Does it matter that this isn't accurate?
context: nil,
@JadenGeller
JadenGeller / BoundsLayout.swift
Last active October 14, 2023 02:30
`sizeThatFits` view modifier — not sure if this is useful
import SwiftUI
struct BoundsLayout: Layout {
var bounds: (ProposedViewSize, LayoutSubview) -> CGRect
func makeCache(subviews: Subviews) -> CGRect {
.zero
}
func sizeThatFits(
@JadenGeller
JadenGeller / PersistentFailureSharedAsyncThrowingSequence.swift
Created September 26, 2023 02:52
Useful for AsyncChannel that stays failed after a failure
struct PersistentFailureSharedAsyncThrowingSequence<Base: AsyncSequence>: AsyncSequence {
typealias Element = Base.Element
var iterator: AsyncIterator
init(_ base: Base) {
iterator = .init(base.makeAsyncIterator())
}
actor AsyncIterator: AsyncIteratorProtocol {
enum State {
@JadenGeller
JadenGeller / AsyncSequenceSplitAfterSeparator.swift
Last active September 21, 2023 22:25
Process stream by tying together iterator and continuation
struct AsyncIteratorSequence<AsyncIterator: AsyncIteratorProtocol>: AsyncSequence {
typealias Element = AsyncIterator.Element
var iterator: AsyncIterator
init(_ iterator: AsyncIterator) {
self.iterator = iterator
}
func makeAsyncIterator() -> AsyncIterator {
return iterator
@JadenGeller
JadenGeller / Deferred.swift
Last active September 21, 2023 22:02
Pretty sure there's no reason to use this... just use `CheckedContinuation`
actor Deferred<Value> {
enum State {
case waiting(Task<Value, Never>, CheckedContinuation<Value, Never>?)
case ready(Value)
}
var state: State?
init() async {
state = .waiting(Task { await withCheckedContinuation { continuation in
Task {