Skip to content

Instantly share code, notes, and snippets.

View importRyan's full-sized avatar
🪂

Ryan Ferrell importRyan

🪂
View GitHub Profile
@importRyan
importRyan / Problem.md
Last active September 19, 2022 16:55
iOS 16.0 bug: Unwanted vertical layout displacement (FB11545578)

Neighboring buttons dip down a few pixels in iOS 16 (but not 15.5), despite a ButtonStyle that changes only opacity.

Comparison

UnexplainedVerticalMicroMovement.mp4

iOS 16.0 Larger

ProblematicPress.mov

Xcode: Version 14.0 (14A309)

Keybase proof

I hereby claim:

  • I am importryan on github.
  • I am importryan (https://keybase.io/importryan) on keybase.
  • I have a public key ASBT4aNtB1NzRXBujBm8STwqXjSCaz7UlvEU8uSiWjtaBAo

To claim this, I am signing this object:

@importRyan
importRyan / UIActivityViewController.md
Last active January 7, 2022 22:36
UIActivityViewController in SwiftUI for iPad or iPhone

iPad-safe SwiftUI UIActivityViewController

  • Background preparation of UIActivityViewController to avoid stuttering UI
  • Situates popover for iPad modal presentation
  • Tolerant of SwiftUI's arbitrary timing of moving a view controller into view hierarchy

Some approaches to presenting a UIActivityViewController in SwiftUI work for some compositions, but sporadically fail for others. In those cases, your UI can appear to hang because the dismiss callback is never reached.

Since on iPad UIActivityController presentation is modal (and requires a valid origin rect), the host VC must be in the active view hierarchy before calling present. However, that insertion may not occur when expected (e.g., when updateUIViewController is called).

@importRyan
importRyan / Example 1.md
Last active June 10, 2021 21:50
WWDC SwiftUI Question

In D_Gen3.memgraph, Xcode memory debugger reports one leaked type, the Color for the bullseye in an HSV color picker, which adapts to brightness. Instruments would report thousands of leaks, 99% not referring to app code.

% leaks D_Gen3.memgraph

9   com.apple.SwiftUI                     0x1c94e6318 partial apply for closure #1 in ViewBodyAccessor.updateBody(of:changed:) + 44
8   Inclusivity                           0x102dc53e8 protocol witness for View.body.getter in conformance HSVHueSatWheelBullseye + 40  <compiler-generated>:0
7   Inclusivity                           0x102dc3594 HSVHueSatWheelBullseye.body.getter + 820  HSVHueSatWheelBullseye.swift:12
6   com.apple.SwiftUI                     0x1c9e748ac ZStack.init(alignment:content:) + 180
5   Inclusivity                           0x102dc409c closure #1 in HSVHueSatWheelBullseye.body.getter + 960  HSVHueSatWheelBullseye.swift:20
@importRyan
importRyan / Alerts in SwiftUI.md
Created March 2, 2021 22:55
Alerts for SwiftUI in macOS

Alerts for SwiftUI in macOS

Only one alert may be attached to a view. To avoid collisions and clutter, one could handle alerts centrally using the func alert<Item>(item: Binding<Item?>... modifier on a senior view.

The init above accepts a binding for any identifiable object and provides a closure to map it into multiple types of Alert views. To minimize code in that closure and simplify call sites, below is a minimalist example wrapper and implementation. This isn’t complex at all, but while the API is decent it could be simpler.

Presentation

@main
struct SomeApp: App {
    @StateObject var coordinator: AppCoordinator
@importRyan
importRyan / whenHovered.md
Last active April 6, 2024 06:54
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
@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 {