Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
/// NavigationSplitView default a List in left view to Sidebar style, and then swipeActions modifier stop working. It's a multi level regression. The swipeActions has been broken for a while, but in macOS 12 it didn't work in .sidebar list style neither
NavigationSplitView {
List {
ForEach(list, id: \.self) { item in
HStack {
Text(item.title)
}
.contentShape(Rectangle())
}
/// This code defines an init method for the Binding type. This method accepts three parameters:
///
/// An object of any type T
/// A key path to a property of type Value on that object
/// An optional UndoManager object
///
/// The init method sets the Binding value to the value of the property specified by the key path. It also sets the set value of the Binding to a closure that updates the value of the property at the key path and registers an undo operation with the provided UndoManager, if one is given.
///
/// This allows the Binding object to be used to access and update the value of the specified property on the provided object, and to register undo operations for those updates with the UndoManager.
// 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 September 27, 2022 19:17
Check if PR is missing formatting
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 February 10, 2024 10:27
Random pastel colors
struct ContentView: View {
@State var color: Color = .Pastel.random()
var body: some View {
Rectangle()
.foregroundColor(color)
.onTapGesture {
color = .Pastel.random()
}
}
// Usage
@MainActor
final class AppSettings: ObservableObject {
static let standard = AppSettings()
@Published(preferenceKey: AppSettingsKey.editorHighlightSelectedLine)
var editorHighlightSelectedLine: Bool = true
}
import AppKit
import SwiftUI
/*
Text("Menu")
.popUpMenu {
NSMenuItem(title: "One", action: nil, keyEquivalent: "")
NSMenuItem(title: "Two", action: nil, keyEquivalent: "")
}
*/
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 {
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
@krzyzanowskim
krzyzanowskim / AsyncWaiter.swift
Last active June 25, 2022 12:25
Synchronously (well) wait for async Task value update https://twitter.com/krzyzanowskim/status/1523233140914876416
/// 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