Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
struct SharingServicePicker: NSViewRepresentable {
@Binding var isPresented: Bool
var items: [Any] = []
func makeNSView(context: Context) -> NSView {
let view = NSView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
@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:
private struct PopUpButton: NSViewRepresentable {
@Binding var selection: String
var content: [String]
func makeNSView(context: Context) -> NSPopUpButton {
let button = NSPopUpButton()
button.imagePosition = .imageLeading
button.usesSingleLineMode = true
button.autoenablesItems = false
// 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: "")
}
*/
@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
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
import Cocoa
// for-in
func checkForIn(array: [Int], dict: [Int: String]) {
for num in array where dict[num] != nil {
num
}
}
checkForIn([1,2,3,4], dict: [1:"one", 2:"two"])
@krzyzanowskim
krzyzanowskim / String.stride.swift
Last active April 15, 2022 19:50
Stride String
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 {