Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
import SwiftUI
/// The collection row's scroll state, read from its NSScrollView: SwiftUI's horizontal ScrollView does not
/// report its offset on macOS 13, so the pager chevrons follow the clip view's bounds instead.
package final class RowScroller: ObservableObject {
package init() {}
@Published package private(set) var canBack = false
@Published package private(set) var canForward = false
private weak var scrollView: NSScrollView?
private var observers: [NSObjectProtocol] = []
package func attach(_ view: NSScrollView) {
guard view !== scrollView else { return update() }
observers.forEach(NotificationCenter.default.removeObserver)
scrollView = view
let clip = view.contentView
clip.postsBoundsChangedNotifications = true
clip.postsFrameChangedNotifications = true
view.documentView?.postsFrameChangedNotifications = true
observers = [(NSView.boundsDidChangeNotification, clip), (NSView.frameDidChangeNotification, clip),
(NSView.frameDidChangeNotification, view.documentView as Any)].map { name, object in
NotificationCenter.default.addObserver(forName: name, object: object, queue: .main) { [weak self] _ in self?.update() }
}
update()
}
deinit { observers.forEach(NotificationCenter.default.removeObserver) }
private func update() {
guard let view = scrollView, let document = view.documentView else { return }
let x = view.contentView.bounds.minX, end = document.frame.width - view.contentView.bounds.width
if canBack != (x > 1) { canBack = x > 1 }
if canForward != (x < end - 1) { canForward = x < end - 1 }
}
/// Scroll by the whole cards that fit in the row.
package func page(_ direction: CGFloat, step: CGFloat) {
guard let view = scrollView, let document = view.documentView else { return }
let clip = view.contentView
let distance = max(step, (clip.bounds.width / step).rounded(.down) * step)
let target = min(max(0, clip.bounds.minX + direction * distance), max(0, document.frame.width - clip.bounds.width))
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.35
context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
clip.animator().setBoundsOrigin(NSPoint(x: target, y: clip.bounds.minY))
}
view.reflectScrolledClipView(clip)
}
}
/// Hands the NSScrollView around this background view to the row's scroller once it is in a window.
package struct EnclosingScrollView: NSViewRepresentable {
let row: RowScroller
package init(row: RowScroller) { self.row = row }
package final class Probe: NSView {
package var row: RowScroller?
package override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
DispatchQueue.main.async { [weak self] in
if let view = self?.enclosingScrollView { self?.row?.attach(view) }
}
}
}
package func makeNSView(context: Context) -> Probe { let probe = Probe(); probe.row = row; return probe }
package func updateNSView(_ probe: Probe, context: Context) {
probe.row = row
DispatchQueue.main.async { if let view = probe.enclosingScrollView { row.attach(view) } }
}
}
/// The library's collection row reports its height, so the hero above it fills exactly what is left.
package struct CollectionHeightKey: PreferenceKey {
package static var defaultValue: CGFloat = 0
package static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = max(value, nextValue()) }
}