Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
127 lines
4.7 KiB
Swift
Executable File
127 lines
4.7 KiB
Swift
Executable File
// SideScroller — a horizontal rail that can be driven by arrows on the Mac.
|
|
//
|
|
// Touch has a swipe; a pointer does not. A trackpad can scroll a rail
|
|
// sideways with two fingers, but a mouse cannot, and nothing on screen says
|
|
// the rail goes anywhere — so the Mac gets explicit arrows, the convention
|
|
// every desktop carousel uses.
|
|
//
|
|
// iOS gets the plain ScrollView, unchanged: the arrows would only cover the
|
|
// cards you are trying to tap.
|
|
//
|
|
// The arrows need to know where the rail currently sits, which is
|
|
// ScrollPosition + onScrollGeometryChange — both macOS 15. The package
|
|
// deployment target is macOS 14, so the whole affordance is behind an
|
|
// availability check and simply does not appear on 14. That is the right
|
|
// failure: a Mac without arrows still scrolls by trackpad, exactly as it did
|
|
// before this file existed.
|
|
|
|
import Inject
|
|
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
public struct SideScroller<Content: View>: View {
|
|
/// How far one arrow press travels. Roughly a card and a half at the
|
|
/// Discover rail's sizes, so a press always lands mid-card and makes it
|
|
/// obvious more is coming.
|
|
let step: CGFloat
|
|
@ViewBuilder let content: () -> Content
|
|
|
|
public init(step: CGFloat = 320, @ViewBuilder content: @escaping () -> Content) {
|
|
self.step = step
|
|
self.content = content
|
|
}
|
|
|
|
@ObserveInjection var inject
|
|
|
|
public var body: some View {
|
|
Group {
|
|
#if os(macOS)
|
|
if #available(macOS 15.0, *) {
|
|
ArrowRail(step: step, content: content)
|
|
} else {
|
|
plain
|
|
}
|
|
#else
|
|
plain
|
|
#endif
|
|
}
|
|
.enableInjection()
|
|
}
|
|
|
|
private var plain: some View {
|
|
ScrollView(.horizontal, showsIndicators: false) { content() }
|
|
}
|
|
}
|
|
|
|
#if os(macOS)
|
|
@available(macOS 15.0, *)
|
|
private struct ArrowRail<Content: View>: View {
|
|
let step: CGFloat
|
|
@ViewBuilder let content: () -> Content
|
|
|
|
@State private var position = ScrollPosition()
|
|
@State private var offsetX: CGFloat = 0
|
|
@State private var contentWidth: CGFloat = 0
|
|
@State private var containerWidth: CGFloat = 0
|
|
@State private var hovering = false
|
|
|
|
/// A pixel of slack: the offset lands on fractional values, and an arrow
|
|
/// that stays enabled at the very end is worse than one that disables a
|
|
/// pixel early.
|
|
private var canScrollLeft: Bool { offsetX > 1 }
|
|
private var canScrollRight: Bool { offsetX < contentWidth - containerWidth - 1 }
|
|
|
|
var body: some View {
|
|
ScrollView(.horizontal, showsIndicators: false) { content() }
|
|
.scrollPosition($position)
|
|
.onScrollGeometryChange(for: ScrollGeometry.self) { $0 } action: { _, geo in
|
|
offsetX = geo.contentOffset.x
|
|
contentWidth = geo.contentSize.width
|
|
containerWidth = geo.containerSize.width
|
|
}
|
|
.overlay(alignment: .leading) {
|
|
arrow("chevron.left", visible: hovering && canScrollLeft) {
|
|
position.scrollTo(x: max(0, offsetX - step))
|
|
}
|
|
}
|
|
.overlay(alignment: .trailing) {
|
|
arrow("chevron.right", visible: hovering && canScrollRight) {
|
|
position.scrollTo(x: min(contentWidth - containerWidth, offsetX + step))
|
|
}
|
|
}
|
|
// Arrows only while the pointer is over the rail — they are a
|
|
// pointer affordance, and parking them permanently over the first and
|
|
// last card taxes every glance for something used occasionally.
|
|
//
|
|
// AFTER the overlays, so the arrows are inside the hovered area. With
|
|
// .onHover on the ScrollView alone, moving onto an arrow (which tracks
|
|
// hover itself) counted as LEAVING the rail: the arrow faded out,
|
|
// stopped hit-testing, and the click fell through to the card under
|
|
// it — then hover returned and it faded back in, a flicker that left
|
|
// only a "perfect spot" clickable.
|
|
.onHover { hovering = $0 }
|
|
.animation(.easeOut(duration: 0.12), value: hovering)
|
|
}
|
|
|
|
private func arrow(
|
|
_ glyph: String, visible: Bool, action: @escaping () -> Void
|
|
) -> some View {
|
|
Button {
|
|
withAnimation(.easeOut(duration: 0.2)) { action() }
|
|
} label: {
|
|
Image(systemName: glyph)
|
|
.font(.system(size: 15, weight: .bold))
|
|
}
|
|
// The library's disc button — the same arrow RwRail draws: a square,
|
|
// opaque, page-coloured backdrop (the cards scroll UNDER this, so it
|
|
// has to be opaque to stay readable), no ring, and the style owns the
|
|
// colours so the chevron still inverts while pressed.
|
|
.buttonStyle(.bareIcon())
|
|
.padding(.horizontal, 4)
|
|
.opacity(visible ? 1 : 0)
|
|
// Invisible arrows must not eat clicks aimed at the card beneath them.
|
|
.allowsHitTesting(visible)
|
|
}
|
|
}
|
|
#endif
|