Skip to content

Instantly share code, notes, and snippets.

@adamawolf
adamawolf / Apple_mobile_device_types.txt
Last active July 25, 2024 10:41
List of Apple's mobile device codes types a.k.a. machine ids (e.g. `iPhone1,1`, `Watch1,1`, etc.) and their matching product names
i386 : iPhone Simulator
x86_64 : iPhone Simulator
arm64 : iPhone Simulator
iPhone1,1 : iPhone
iPhone1,2 : iPhone 3G
iPhone2,1 : iPhone 3GS
iPhone3,1 : iPhone 4
iPhone3,2 : iPhone 4 GSM Rev A
iPhone3,3 : iPhone 4 CDMA
iPhone4,1 : iPhone 4S
@unnamedd
unnamedd / MacEditorTextView.swift
Last active May 26, 2024 17:49
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
*/
import Combine
import SwiftUI
@sindresorhus
sindresorhus / NativeButton.swift
Created October 11, 2019 08:58
Example of using NSButton in SwiftUI to access missing features like `keyEquivalent` (for example, to make the button the default and highlighted). Stack Overflow answer: https://stackoverflow.com/a/58337529/64949
/**
```
struct ContentView: View {
var body: some View {
NativeButton("Submit", keyEquivalent: .return) {
// Some action
}
.padding()
}
}
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
@mecid
mecid / AnimatableCGPointVector.swift
Created June 17, 2020 23:40
The type that holds array of CGPoints and conforms to VectorArithmetic
import SwiftUI
struct AnimatableCGPointVector: VectorArithmetic {
static var zero = AnimatableCGPointVector(values: [.zero])
static func - (lhs: AnimatableCGPointVector, rhs: AnimatableCGPointVector) -> AnimatableCGPointVector {
let values = zip(lhs.values, rhs.values)
.map { lhs, rhs in lhs.animatableData - rhs.animatableData }
.map { CGPoint(x: $0.first, y: $0.second) }
return AnimatableCGPointVector(values: values)
import SwiftUI
import PlaygroundSupport
// constants
let cardWidth: CGFloat = 343
let cardHeight: CGFloat = 212
let spacing = 36
let animation = Animation.spring()
let cardColors = [
Color(UIColor.systemRed),
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
/*Terminal*/
VStack {
@almonk
almonk / SnappyWindow.swift
Last active May 2, 2024 03:05
SnappyWindow – A NSWindow that acts like the PIP Video Window from Safari
//
// SnappyWindow.swift
// A NSWindow that snaps to corners
//
// Created by Alasdair Monk on 03/02/2021.
//
import Foundation
import Cocoa
@importRyan
importRyan / TextView.md
Last active May 10, 2023 14:15
Custom NSTextView Insertion Point

Custom NSTextView Insertion Point

While making the default insertion caret pop a little wider for better accessibility, I ran into an issue where the insertion point ghosted on arrow key presses. Christian Tietze observed the same issue.

My culprit was settings an x offset for the rect inside drawInsertionPoint. While Christian's blog code doesn't include an offset, there's otherwise no difference in our approach.

Below draws wider insertion caret with rounded corners.

class FatRoundInsertionCaretTextView: NSTextView {
@importRyan
importRyan / whenHovered.md
Last active July 9, 2024 19:51
Reliable SwiftUI mouse hover

Reliable mouseEnter/Exit for SwiftUI

Kapture 2021-03-01 at 14 43 39

On Mac, SwiftUI's .onHover closure is not always called on mouse exit, particularly with high cursor velocity. A grid of targets or with finer target shapes will often have multiple targets falsely active after the mouse has moved on.

It is easy to run back to AppKit's safety. Below is a SwiftUI-like modifier for reliable mouse-tracking. You can easily adapt it for other mouse tracking needs.

import SwiftUI