Skip to content

Instantly share code, notes, and snippets.

View steipete's full-sized avatar

Peter Steinberger steipete

View GitHub Profile
@schwa
schwa / environment.swift
Created March 27, 2021 15:49
Dash Snippet for SwiftUI Environments & Modifiers
struct __ValueType__ {
}
struct __ValueType__Key: EnvironmentKey {
static var defaultValue = __ValueType__()
}
extension EnvironmentValues {
var __KeyName__: __ValueType__ {
get {
extension Result {
public func `catch`(_ handler: () throws -> Success) -> Result<Success, Error> {
flatMapError { _ in
.init { try handler() }
}
}
public func `catch`(_ handler: (Failure) throws -> Success) -> Result<Success, Error> {
flatMapError { error in
.init { try handler(error) }
// Copyright © 2020 Michael Gubik. All rights reserved. Created on 2020-05-16.
import Foundation
#if TEST
import Sentry
#endif
// maybe look into os_log: https://stackoverflow.com/a/25951564/11588848
private enum LogLevel: String {
@Amzd
Amzd / ColorPicker.swift
Last active April 19, 2024 18:42
What SwiftUI's ColorPicker should have been.
import SwiftUI
@available(iOS 14.0, *)
public struct ColorPickerWithoutLabel: UIViewRepresentable {
@Binding var selection: Color
var supportsAlpha: Bool = true
public init(selection: Binding<Color>, supportsAlpha: Bool = true) {
self._selection = selection
self.supportsAlpha = supportsAlpha
struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})
@rnystrom
rnystrom / example.swift
Created January 18, 2021 00:30
Show the keyboard as a UISearchController in a modal is displayed
class ViewController: UIViewController {
@IBAction func onSearch(_ sender: Any) {
let fakeWindow = UIWindow(windowScene: view.window!.windowScene!)
let fakeTextField = UITextField()
fakeTextField.autocorrectionType = .no
fakeWindow.addSubview(fakeTextField)
fakeWindow.makeKeyAndVisible()
fakeTextField.becomeFirstResponder()
@DougGregor
DougGregor / SwiftConcurrencyDependencies.svg
Created December 2, 2020 00:39
Swift Concurrency Proposal Dependencies
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@steipete
steipete / Architecture.swift
Last active December 8, 2022 15:47
Detect if a process runs under Rosetta 2 on Apple Silicon M1 or native. Works for macOS and iOS.
@objc(PSTArchitecture) class Architecture: NSObject {
/// Check if process runs under Rosetta 2.
///
/// Use to disable tests that use WebKit when running on Apple Silicon
/// FB8920323: Crash in WebKit memory allocator on Apple Silicon when iOS below 14
/// Crash is in JavaScriptCore: bmalloc::HeapConstants::HeapConstants(std::__1::lock_guard<bmalloc::Mutex> const&)
@objc class var isRosettaEmulated: Bool {
// Issue is specific to Simulator, not real devices
#if targetEnvironment(simulator)
return processIsTranslated() == EMULATED_EXECUTION
@fruitcoder
fruitcoder / CA+Extensions.swift
Last active September 17, 2021 16:23
Returns the simulator drag coefficient when Slow Animations are enabled, otherwise (and non simulator builds) 1.0
import UIKit
#if targetEnvironment(simulator)
@_silgen_name("UIAnimationDragCoefficient") func UIAnimationDragCoefficient() -> Float
func simulatorDragCoefficient() -> CFTimeInterval {
let drag = UIAnimationDragCoefficient()
if drag > 1 {
return CFTimeInterval(drag)
}
return 1
@Amzd
Amzd / View+NSCursor.swift
Last active February 9, 2024 17:34
Set the cursor that is displayed when hovering a View. (macOS, SwiftUI)
import SwiftUI
extension View {
/// https://stackoverflow.com/a/61985678/3393964
public func cursor(_ cursor: NSCursor) -> some View {
self.onHover { inside in
if inside {
cursor.push()
} else {
NSCursor.pop()