~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
128 lines · 4.4 KB Raw History
import HUDKit
import SwiftUI

/// HUD on iOS: HUD's views of atproto records and queries, drawn natively
/// by HUDKit. Signed out, the root is signing in, by handle
/// (`SignInScreen`), as an MCP client of the server's own. Signed in, the
/// root is the person's workspace, the same one the web page shows, live,
/// with Home and Settings over it from its tab bar; what opens from a tile
/// on a screen of its own goes on a navigation stack over it, as the MCP
/// App view's back stack (web/src). Several accounts may be signed in at
/// once, one in use, switched in Settings. The server is hud.place; in
/// Debug builds, `just serve` on this Mac, or the one launched with
/// `-server <url>`. What a player plays shows at the foot of the screens,
/// as the web view's now-playing bar, but on the screen whose player plays
/// it. In debug builds, launched with `-gallery [<entry id>]`, the root is
/// the gallery (`GalleryScreen`); with `-call '<a call as JSON>'`, that call
/// opens on a screen of its own, signed in or not; with `-theme '<a
/// place.hud.defs#theme as JSON>'`, the app is drawn in that theme where no
/// one signed in chose one, for development and screenshots.
@main
struct HUDApp: App {
	@State private var browser = Browser()
	@Environment(\.scenePhase) private var phase

	var body: some Scene {
		WindowGroup {
			NavigationStack(path: $browser.path) {
				root
					.navigationDestination(for: Step.self) { step in
						// Pushed over the root: no Tabs strip, whose tabs swap the screen in place.
						StepScreen(step: step, browser: browser)
							.environment(\.stackRoot, false)
					}
			}
			.hudSafeAreaBar(edge: .bottom) {
				// Not on the screen whose player plays it: that shows it already;
				// nor over the workspace, whose strip draws it beside its map.
				let onWorkspace = browser.workspace != nil && browser.path.isEmpty
				if browser.player.track != nil, !onWorkspace, browser.player.origin != browser.path.last.map({ AnyHashable($0) }) {
					NowPlayingBar(player: browser.player, open: { browser.goToPlaying() })
				}
			}
			.sheet(item: $browser.page) { page in
				SafariView(url: page.url).ignoresSafeArea()
			}
			.sheet(isPresented: $browser.showingHome) {
				NavigationStack {
					Home(browser: browser)
						.toolbar {
							ToolbarItem(placement: .confirmationAction) {
								Button("Done") { browser.showingHome = false }
							}
						}
				}
				// The app's own screen: the system's look, whatever the theme.
				.hudUnthemed()
			}
			.sheet(isPresented: $browser.showingSettings) {
				NavigationStack {
					SettingsScreen(browser: browser)
						.toolbar {
							ToolbarItem(placement: .confirmationAction) {
								Button("Done") { browser.showingSettings = false }
							}
						}
				}
				.hudUnthemed()
			}
			// Signed in, drawn in the theme the person chose (Settings), as the
			// web draws it; signed out, or with none chosen, the system's.
			.hudTheme(chosenTheme)
			// What feeds were told goes as the app is put away, as the web's
			// when its page is hidden; iOS gives it a moment to.
			.onChange(of: phase) { _, now in
				guard now == .background else { return }
				let moment = UIApplication.shared.beginBackgroundTask(withName: "Feed interactions")
				Task {
					await browser.feedback.flush()
					UIApplication.shared.endBackgroundTask(moment)
				}
			}
		}
	}

	private var chosenTheme: Theme? {
		#if DEBUG
		browser.workspace?.theme ?? Self.launchedTheme
		#else
		browser.workspace?.theme
		#endif
	}

	#if DEBUG
	/// The theme `-theme` names at launch, if it does.
	private static let launchedTheme: Theme? = {
		let arguments = ProcessInfo.processInfo.arguments
		guard let at = arguments.firstIndex(of: "-theme"), at + 1 < arguments.count,
			let found = try? JSONDecoder().decode(JSON.self, from: Data(arguments[at + 1].utf8))
		else { return nil }
		return Theme(found)
	}()
	#endif

	@ViewBuilder
	private var root: some View {
		#if DEBUG
		if let launch = GalleryScreen.launched {
			GalleryScreen(server: browser.serverURL, start: launch.start)
		} else {
			signedInOrOut
		}
		#else
		signedInOrOut
		#endif
	}

	@ViewBuilder
	private var signedInOrOut: some View {
		if let workspace = browser.workspace {
			WorkspaceScreen(workspace: workspace, browser: browser)
				.id(ObjectIdentifier(workspace))
		} else {
			SignInScreen(browser: browser)
				.toolbar(.hidden, for: .navigationBar)
				.hudUnthemed()
		}
	}
}