chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
import HUDKit
import SwiftUI
/// A tile: its header (its app's icon, which opens the app's page, its
/// title as people read it, the account it shows by @handle, the tile it
/// was opened from, and a menu to take it off the canvas), part of the
/// tile, over a hairline; and its body under it, its call's answer drawn
/// as a screen of the stack draws it. What opens from it is put on the
/// canvas right of it (`addTile` with `from`), and slides into view; a tab
/// chosen in it draws its call in this tile, in its place (`updateTile`,
/// every page told), or, on a canvas this person only views, on a screen
/// of its own.
struct TileView: View {
let tile: Tile
let canvas: String
let workspace: Workspace
let browser: Browser
/// What covers its foot (on a phone, the strip map): its body is laid
/// out above it and scrolls under it.
var under: CGFloat = 0
@Environment(\.horizontalSizeClass) private var size
var body: some View {
let workspace = workspace
let canvas = canvas
let id = tile.id
VStack(spacing: 0) {
header
Divider()
StepView(
step: browser.step(for: tile),
browser: browser,
open: { call in Task { await workspace.add(call, from: id, on: canvas) } },
replace: { call in
Task {
if !(await workspace.replace(id, with: call, on: canvas)) { browser.go(call) }
}
}
)
.safeAreaPadding(.bottom, under)
.environment(\.shown, Shown(title: tile.heading, handle: handle))
}
.tileChrome()
}
/// The tile it was opened from, while it is on the canvas.
private var source: Tile? {
tile.from.flatMap { workspace.tile($0, on: canvas) }
}
/// The handle of the account it shows: as its call names it, else as Bluesky knows its DID.
private var handle: String? {
guard let account = tile.account else { return nil }
if account == "me" { return workspace.me?.handle }
if account.hasPrefix("did:") { return workspace.handles[account] }
return account
}
/// Its header: the workspace's tile header for this tile, its icon
/// opening its app's page beside it (but on an app's own page, the icon
/// alone), its source going to the tile it was opened from.
private var header: some View {
let (workspace, canvas, id) = (workspace, canvas, tile.id)
let source = source
let domain = tile.tool == "describe_app" ? nil : tile.domain
return TileHeader(
title: tile.heading,
icon: tile.icon,
symbol: tile.symbol,
handle: handle,
mine: tile.account == "me" || (tile.account != nil && tile.account == workspace.me?.did) || (handle != nil && handle == workspace.me?.handle),
from: source?.heading,
// Its handle opens the account across its apps, but on that page itself.
openWho: tile.account.flatMap { account in
tile.tool == "describe_repo" ? nil : {
Task { await workspace.add(Call("describe_repo", ["actor": .string(account == "me" ? (workspace.me?.did ?? account) : account)]), from: id, on: canvas) }
}
},
goFrom: { if let source { workspace.show(canvas, source.id) } },
byAgent: tile.byAgent,
app: domain.map { domain in browser.apps?.first { $0.domain == domain }?.name ?? domain },
openApp: {
guard let domain else { return }
Task { await workspace.add(Call("describe_app", ["domain": .string(domain)]), from: id, on: canvas) }
},
added: added
) {
if workspace.info(canvas)?.readonly == false {
arrange
Button("Remove from canvas", systemImage: "xmark", role: .destructive) {
Task { await workspace.remove(id, on: canvas) }
}
}
}
}
/// Where it stands on the strip, as the web's tile menu moves it: its
/// column left or right, its width, stacked under the column on its
/// left or out of its stack.
@ViewBuilder
private var arrange: some View {
let strip = workspace.columns(canvas)
let at = strip.firstIndex { $0.tiles.contains { $0.id == tile.id } } ?? 0
let column = strip.indices.contains(at) ? strip[at] : nil
let (workspace, canvas, id) = (workspace, canvas, tile.id)
Section {
if at > 0 {
Button("Move left", systemImage: "arrow.left") { Task { await workspace.move(id, -1, on: canvas) } }
}
if at < strip.count - 1 {
Button("Move right", systemImage: "arrow.right") { Task { await workspace.move(id, 1, on: canvas) } }
}
// A phone's column fills the screen whatever its width: set it where it shows.
if let column, size == .regular {
Picker(selection: Binding(get: { column.width }, set: { width in Task { await workspace.setWidth(id, width, on: canvas) } })) {
Text("A third").tag(TileWidth.third)
Text("Half").tag(TileWidth.half)
Text("Two thirds").tag(TileWidth.twoThirds)
Text("Full").tag(TileWidth.full)
} label: {
Label("Width", systemImage: "arrow.left.and.right")
}
.pickerStyle(.menu)
}
if at > 0 {
Button("Stack under the column on its left", systemImage: "square.stack") { Task { await workspace.stack(id, on: canvas) } }
}
if tile.stack != nil, (column?.tiles.count ?? 0) > 1 {
Button("Take out of stack", systemImage: "square.split.1x2") { Task { await workspace.unstack(id, on: canvas) } }
}
}
}
/// Who added it.
private var added: String {
let who = tile.author == workspace.me?.did ? "you" : workspace.handles[tile.author].map { "@\($0)" } ?? "a member"
return tile.byAgent ? "Added by an agent for \(who)" : "Added by \(who)"
}
}
/// A tile's header, drawn from plain values so the workspace and the
/// gallery draw the same one: its app's icon, a 44-point target, a button
/// to its app's page where `app` names it, else the icon alone; what it
/// shows, then on a line under it whose, in what app, and where it was
/// opened from ("@dan · Bluesky · from Posts"), every header as tall; and
/// its menu, who added it over its `actions`. In a compact width (a
/// phone), one line: a way back to the tile it was opened from, what it
/// shows, then the handle ("You", an account of yours), the title giving
/// way before it.
/// Content, not a floating bar, so nothing scrolls under it.
struct TileHeader<Actions: View>: View {
let title: String
let icon: URL?
let symbol: String
var handle: String?
/// Whether the account it shows is the person's own: "You", not their handle.
var mine = false
/// The heading of the tile it was opened from.
var from: String?
/// Opens the account it shows across its apps, from its handle.
var openWho: (() -> Void)?
var goFrom: () -> Void = {}
var byAgent = false
/// Its app's name, where its icon opens its app's page.
var app: String?
var openApp: () -> Void = {}
/// Who added it, as its menu says.
let added: String
@ViewBuilder let actions: () -> Actions
@Environment(\.horizontalSizeClass) private var size
var body: some View {
HStack(spacing: Space.s2) {
if size == .compact {
oneLine
} else {
twoLines
}
Spacer(minLength: 0)
menu
}
.padding(.leading, Space.s1)
.padding(.vertical, Space.s1)
.background(.tileSurface)
}
private var twoLines: some View {
HStack(spacing: Space.s2) {
appIcon
VStack(alignment: .leading, spacing: 1) {
HStack(spacing: Space.s1) {
Text(title)
.font(.headline)
.lineLimit(1)
if byAgent {
Label("Agent", systemImage: "sparkles")
.font(.caption2.weight(.medium))
.padding(.horizontal, 6)
.padding(.vertical, 1)
.background(HUDColor(.attention, or: .purple).opacity(0.12), in: .capsule)
.foregroundStyle(HUDColor(.attention, or: .purple))
.fixedSize()
.accessibilityLabel("Added by an agent")
}
}
about
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
}
/// Whose, its app, where it was opened from, apart by dots; whose
/// ("You" too) opening the account across its apps.
private var about: some View {
let who = mine ? "You" : handle.map { "@\($0)" }
return HStack(spacing: 0) {
if let who, let openWho {
Button(action: openWho) { Text(who) }
.buttonStyle(.plain)
.layoutPriority(1)
.accessibilityHint(mine ? "Opens your profile across your apps" : "Opens their profile across their apps")
if let app { Text(" · \(app)").layoutPriority(1) }
} else {
Text([who, app].compactMap { $0 }.joined(separator: " · "))
.layoutPriority(1)
}
if let from {
if who != nil || app != nil { Text(" · ") }
Button(action: goFrom) {
Text("from ") + Text(from).underline()
}
.buttonStyle(.plain)
.accessibilityLabel("Opened from \(from)")
.accessibilityHint("Goes to the tile it was opened from")
}
}
}
private var oneLine: some View {
HStack(spacing: Space.s1) {
if let from {
Button(action: goFrom) {
Image(systemName: "chevron.backward")
.foregroundStyle(.secondary)
.frame(minWidth: 32, minHeight: 44)
.contentShape(.rect)
}
.buttonStyle(.plain)
.accessibilityLabel("Back to \(from)")
}
appIcon
HStack(alignment: .firstTextBaseline, spacing: Space.s1) {
Text(title)
.font(.headline)
.lineLimit(1)
// Whose: "You", an account of yours; opening it across its apps, but on that page itself.
if let who = mine ? "You" : handle.map({ "@\($0)" }) {
Button { openWho?() } label: {
Text(who)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
}
.buttonStyle(.plain)
.disabled(openWho == nil)
.layoutPriority(1)
.accessibilityHint(openWho == nil ? "" : mine ? "Opens your profile across your apps" : "Opens their profile across their apps")
}
}
if byAgent {
Image(systemName: "sparkles")
.font(.caption)
.foregroundStyle(HUDColor(.attention, or: .purple))
.accessibilityLabel("Added by an agent")
}
}
}
@ViewBuilder
private var appIcon: some View {
if let app {
Button(action: openApp) {
AppIcon(url: icon, symbol: symbol)
.frame(minWidth: 44, minHeight: 44)
.contentShape(.rect)
}
.buttonStyle(.plain)
.accessibilityLabel("Open \(app)")
} else {
AppIcon(url: icon, symbol: symbol)
.frame(minWidth: 44, minHeight: 44)
}
}
private var menu: some View {
Menu {
Section(added) {
actions()
}
} label: {
Image(systemName: "ellipsis")
.foregroundStyle(.secondary)
.frame(minWidth: 44, minHeight: 44)
}
.accessibilityLabel("Tile actions")
}
}
extension View {
/// A tile's frame: filling its place on the strip, rounded, over a hairline.
func tileChrome() -> some View {
frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.tileSurface)
.clipShape(.rect(cornerRadius: 16))
.overlay(RoundedRectangle(cornerRadius: 16).strokeBorder(.hairline, lineWidth: 0.5))
}
}
/// An app's icon: its favicon, HUD's /icon/<domain>; where it has none, or
/// it fails to load, the symbol of what it shows. `size` points at the
/// default text size, scaled with Dynamic Type.
struct AppIcon: View {
let url: URL?
let symbol: String
@ScaledMetric private var side: CGFloat
init(url: URL?, symbol: String, size: CGFloat = 20, relativeTo style: Font.TextStyle = .headline) {
self.url = url
self.symbol = symbol
_side = ScaledMetric(wrappedValue: size, relativeTo: style)
}
var body: some View {
Group {
if let url {
AsyncImage(url: url) { phase in
if let image = phase.image {
image.resizable().scaledToFill()
} else {
fallback
}
}
} else {
fallback
}
}
.frame(width: side, height: side)
.clipShape(.rect(cornerRadius: side * 0.22))
.accessibilityHidden(true)
}
private var fallback: some View {
Image(systemName: symbol)
.font(.system(size: side * 0.8))
.foregroundStyle(.secondary)
}
}