Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
51 lines
1.9 KiB
Swift
Executable File
51 lines
1.9 KiB
Swift
Executable File
// AppPush — "push this destination onto whatever stack hosts me".
|
|
//
|
|
// RwMenuItem carries an ACTION CLOSURE and nothing else: no NavigationLink,
|
|
// no submenu. That is fine for verbs (Play, Download, Like) but not for the
|
|
// menu rows that used to be links — Go to Artist, Go to Track Page — and a
|
|
// closure cannot reach the NavigationPath of the stack it happens to be in.
|
|
//
|
|
// So the hosting stack publishes a push function into the environment. Each
|
|
// tab stack publishes one that appends to ITS path, and the now-playing sheet
|
|
// publishes one that appends to the sheet's own path, so the same row pushes
|
|
// into the right stack wherever it is rendered. Nil (no hosting stack) makes
|
|
// the caller hide the row rather than offer a dead one.
|
|
|
|
import SwiftUI
|
|
|
|
public struct AppPushKey: EnvironmentKey {
|
|
public static let defaultValue: ((AppDestination) -> Void)? = nil
|
|
}
|
|
|
|
extension EnvironmentValues {
|
|
/// Set by whichever NavigationStack hosts this subtree; see AppShell.
|
|
public var appPush: ((AppDestination) -> Void)? {
|
|
get { self[AppPushKey.self] }
|
|
set { self[AppPushKey.self] = newValue }
|
|
}
|
|
}
|
|
|
|
/// A link to a page that works anywhere — including the Mac title bar, which
|
|
/// RedWhiteUI draws ABOVE the page stacks, where a NavigationLink(value:) has
|
|
/// no stack to push onto. With \.appPush around (every stack sets one, and so
|
|
/// does AppShell for the title bar) it pushes through that; without, it is a
|
|
/// plain NavigationLink. Style it like a NavigationLink: `.buttonStyle(...)`.
|
|
struct AppLink<Label: View>: View {
|
|
@Environment(\.appPush) private var push
|
|
let value: AppDestination
|
|
let label: Label
|
|
|
|
init(value: AppDestination, @ViewBuilder label: () -> Label) {
|
|
self.value = value
|
|
self.label = label()
|
|
}
|
|
|
|
var body: some View {
|
|
if let push {
|
|
Button { push(value) } label: { label }
|
|
} else {
|
|
NavigationLink(value: value) { label }
|
|
}
|
|
}
|
|
}
|