Skip to content

Instantly share code, notes, and snippets.

View dezinezync's full-sized avatar
🏠
Working from home

Nikhil Nigade dezinezync

🏠
Working from home
View GitHub Profile
@phillipcaudell
phillipcaudell / Confirmation.swift
Created September 1, 2023 09:50
Present a confirmation dialogue from a swipeAction, contextMenu or other transient view.
import SwiftUI
/// Describe a potentially destructive action to the user.
///
/// A confirmation describes the action to the user and provides a mechanism for confirming the action in the
/// form of a `Button`. You present a confirmation by calling the environment's confirm action:
///
/// ```swift
/// @Environment(\.confirm) private var confirm
///
@sophiateutschler
sophiateutschler / Color+Lch.swift
Created February 8, 2023 19:31
Lch color space initializer for SwiftUI Color
//
// MIT License
//
// Copyright (c) 2023 Sophiestication Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@chriseidhof
chriseidhof / ViewToPDF.swift
Created January 6, 2023 15:46
Image Rendering
import SwiftUI
extension View {
@MainActor
func pdf(size: ProposedViewSize) -> Data {
let renderer = ImageRenderer(content: self)
renderer.proposedSize = size
var pdfData = NSMutableData()
renderer.render { size, render in
var mediaBox = CGRect(origin: .zero, size: size)
@cerupcat
cerupcat / Tappable UIView
Last active January 29, 2022 16:09
Turn any UIView into a tappable button that animates and calls a selector
import Foundation
import UIKit
@propertyWrapper
class Tappable<T: UIView>: NSObject, UIGestureRecognizerDelegate {
// MARK: - Public Properties
public var wrappedValue: T? {
willSet {
removeGesture()
@lukaskubanek
lukaskubanek / Bundle+TestFlight.swift
Last active April 26, 2024 08:48
A code snippet for detecting the TestFlight environment for a macOS app at runtime
/// MIT License
///
/// Copyright (c) 2021 Lukas Kubanek, Structured Path GmbH
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
@samwarnick
samwarnick / migrate.swift
Last active January 25, 2023 16:11
Migrate to app group
// In my core data stack
let container = NSPersistentCloudKitContainer(name: "DataModel")
let storeUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)!.appendingPathComponent("DataModel.sqlite")
// Enable history tracking and remote notifications
guard let description = container.persistentStoreDescriptions.first else {
fatalError("###\(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
@bobergj
bobergj / ToolbarItemWithControlValidating.swift
Created December 4, 2020 13:13
ToolbarItemWithControlValidating
// View-based toolbar items do not auto-validate by default, since the toolbar item view may
// be of any type. This subclass validates a toolbar item NSControl view against its action target.
class ToolbarItemWithControlValidating: NSToolbarItem {
override func validate() {
// validate with views view
if let action = self.action {
let validator = NSApp.target(forAction: action, to: self.target, from: self) as AnyObject?
switch validator {
case let validator as NSToolbarItemValidation:
isEnabled = validator.validateToolbarItem(self)
@lukaskubanek
lukaskubanek / NSView Drawing Issue on macOS Big Sur.md
Last active July 18, 2022 08:09
NSView Drawing Issue on macOS Big Sur

This is an excerpt from our internal documentation describing an issue with drawing in NSViews on macOS Big Sur.

1️⃣ Introduction

In macOS Big Sur (probably starting with β9), Apple changed the default contents format for backing layers of NSViews. Instead of an explicit CALayerContentsFormat.RGBA8Uint value, an „Automatic“ value is now used. Even though it also resolves into „RGBA8“ in our testing, it has some serious implications as it breaks assumptions our code relies on.

I first stumbled upon this issue in this tweet by Frank. It links to a thread on Apple Forums by Mark that contains valuable information as well as ideas for workarounds. The changed behavior was also confirmed by Marcin in this tweet.

2️⃣ Impact on Diagrams

@cdevroe
cdevroe / PhotographyBlogs.opml
Last active September 15, 2020 11:41
An OPML file of Photography related blogs - Inspired by Jim Grey
<?xml version="1.0" encoding="UTF-8"?>
<!-- OPML generated by NetNewsWire -->
<opml version="1.1">
<head>
<title>PhotographyBlogs.opml</title>
</head>
<body>
<outline text="Photography" title="Photography">
<outline text="Colin Devroe" title="Colin Devroe" description="" type="rss" version="RSS" htmlUrl="http://cdevroe.com/" xmlUrl="http://cdevroe.com/feed/"/>
<outline text="Christopher May Fine Art Photography" title="Christopher May Fine Art Photography" description="" type="rss" version="RSS" htmlUrl="https://christophermayphotography.com/" xmlUrl="https://christophermayphotography.com/feed/"/>
//
// ContentView.swift
//
//
// Created by Bernstein, Joel on 7/4/20.
//
import SwiftUI
struct ElementModel: Identifiable