Skip to content

Instantly share code, notes, and snippets.

Avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
View SwiftUI.Toggle.sources.swift
// Backport SwiftUI.Toggle.init(_:sources:isOn:)
// https://developer.apple.com/documentation/swiftui/toggle/init(_:ison:)-8qx3l
@available(iOS, deprecated: 16.0)
@available(macOS, deprecated: 13.0)
@available(tvOS, deprecated: 16.0)
@available(watchOS, deprecated: 9.0)
private extension SwiftUI.Toggle where Label == SwiftUI.Text {
init<C>(_ titleKey: LocalizedStringKey, sources: C, isOn: KeyPath<C.Element, Binding<Bool>>) where C : RandomAccessCollection {
@krzyzanowskim
krzyzanowskim / swiftformat-github-action.yml
Last active Sep 27, 2022
Check if PR is missing formatting
View swiftformat-github-action.yml
name: auto-format
on: pull_request
jobs:
format:
# Check if the PR is not from a fork
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
@krzyzanowskim
krzyzanowskim / PastelColor.swift
Last active Jul 28, 2022
Random pastel colors
View PastelColor.swift
struct ContentView: View {
@State var color: Color = .Pastel.random()
var body: some View {
Rectangle()
.foregroundColor(color)
.onTapGesture {
color = .Pastel.random()
}
}
View AppSettings.swift
// Usage
@MainActor
final class AppSettings: ObservableObject {
static let standard = AppSettings()
@Published(preferenceKey: AppSettingsKey.editorHighlightSelectedLine)
var editorHighlightSelectedLine: Bool = true
}
View STPopUpMenu.swift
import AppKit
import SwiftUI
/*
Text("Menu")
.popUpMenu {
NSMenuItem(title: "One", action: nil, keyEquivalent: "")
NSMenuItem(title: "Two", action: nil, keyEquivalent: "")
}
*/
View VisualEffectView.swift
struct VisualEffectView: NSViewRepresentable {
let material: NSVisualEffectView.Material
let blendingMode: NSVisualEffectView.BlendingMode
init(material: NSVisualEffectView.Material, blendingMode: NSVisualEffectView.BlendingMode = .withinWindow) {
self.material = material
self.blendingMode = blendingMode
}
func makeNSView(context: Context) -> NSVisualEffectView {
View OptionSetIterator.swift
public struct OptionSetIterator<Element: OptionSet>: IteratorProtocol where Element.RawValue == Int {
private let value: Element
init(element: Element) {
self.value = element
}
private lazy var remainingBits = value.rawValue
private var bitMask = 1
View AsyncWaiter.swift
/// Wait for async operation to return value and call callback with the value
/// This class is intended to workaround/simplify async/await + actors isolation
/// https://twitter.com/krzyzanowskim/status/1523233140914876416
private class AsyncWaiter<T> {
var didReceiveValue: Bool = false
let value: (T) -> Void
let operation: () async throws -> T
init(_ value: @escaping (T) -> Void, operation: @escaping () async throws -> T) {
self.value = value
View String.stride.swift
extension String {
func stride(by distance: String.IndexDistance, substring: (String.SubSequence) -> Void) {
guard distance > 0, distance < count else {
substring(self[...])
return
}
var i = index(startIndex, offsetBy: distance, limitedBy: endIndex) ?? endIndex
var previ = startIndex
while i < endIndex {
View SharingServicePicker.swift
struct SharingServicePicker: NSViewRepresentable {
@Binding var isPresented: Bool
var items: [Any] = []
func makeNSView(context: Context) -> NSView {
let view = NSView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}