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 canvas's strip of columns, in the server's order, a column's stacked
/// tiles one under another. On a phone each column takes the screen's
/// width, swiped between, its tiles reaching under the map at the foot,
/// what they scroll going under it; on an iPad, its tile's width of it (a
/// third, a half, two thirds, all), under the map. The column asked for
/// (`Workspace.reveal`), as a tile opened, slides into view.
struct StripView: View {
let canvas: String
let workspace: Workspace
let browser: Browser
@State private var visible: String?
/// What the map and the home indicator cover at the foot, on a phone.
@State private var foot: CGFloat = 0
@Environment(\.horizontalSizeClass) private var size
var body: some View {
let columns = workspace.columns(canvas)
if columns.isEmpty {
ContentUnavailableView {
Label("Nothing on this canvas", systemImage: "rectangle.split.3x1")
} description: {
Text("Open something from Home, or ask your AI to add a tile.")
} actions: {
Button("Open Home") { browser.showingHome = true }
}
} else {
strip(columns)
}
}
private func strip(_ columns: [StripColumn]) -> some View {
let compact = size != .regular
return ScrollView(.horizontal) {
LazyHStack(spacing: Space.s2) {
ForEach(columns) { column in
VStack(spacing: Space.s2) {
ForEach(column.tiles) { tile in
TileView(tile: tile, canvas: canvas, workspace: workspace, browser: browser, under: compact && tile.id == column.tiles.last?.id ? foot : 0)
}
}
.containerRelativeFrame(.horizontal, count: compact ? 1 : 6, span: compact ? 1 : column.width.sixths, spacing: Space.s2)
.ignoresSafeArea(.container, edges: compact ? .bottom : [])
}
}
.scrollTargetLayout()
}
.scrollIndicators(.hidden)
.scrollTargetBehavior(.viewAligned)
.scrollPosition(id: $visible)
.contentMargins(.horizontal, Space.s3, for: .scrollContent)
.contentMargins(.vertical, Space.s2, for: .scrollContent)
.onGeometryChange(for: CGFloat.self) { $0.safeAreaInsets.bottom } action: { foot = $0 }
// On a phone the map is at the foot, in the thumb's reach, what plays
// over it; on an iPad under the tabs, as the web's, and what plays at
// the foot.
.hudSafeAreaBar(edge: compact ? .bottom : .top) {
VStack(spacing: Space.s2) {
if compact { playing(columns, compact: compact) }
StripMap(
columns: columns,
shown: shown(columns, compact: compact),
arrived: workspace.arrived,
readonly: workspace.info(canvas)?.readonly ?? false,
visible: $visible,
add: { browser.showingHome = true },
others: max((workspace.info(canvas)?.members.count ?? 1) - 1, 0),
share: { browser.sharing = Shared(id: canvas) },
edge: compact ? .bottom : .top
)
}
}
.hudSafeAreaBar(edge: .bottom) {
if !compact {
playing(columns, compact: compact)
.padding(.bottom, Space.s2)
}
}
.onAppear {
visible = workspace.focus[canvas].flatMap { key in columns.contains { $0.id == key } ? key : nil } ?? columns.first?.id
see(columns, compact: compact)
}
.onChange(of: visible) { _, key in
if let key { workspace.focus[canvas] = key }
see(columns, compact: compact)
}
.onChange(of: workspace.arrived) { see(columns, compact: compact) }
.onChange(of: workspace.reveal) { _, ask in
guard let ask, ask.canvas == canvas else { return }
withAnimation { visible = ask.key }
}
}
/// The columns on screen: on a phone the one in view; on an iPad those
/// from it that fit the screen's six sixths.
private func shown(_ columns: [StripColumn], compact: Bool) -> Set<String> {
guard let at = columns.firstIndex(where: { $0.id == visible }) else { return [] }
if compact { return [columns[at].id] }
var room = 6
var out: Set<String> = []
for column in columns[at...] {
guard out.isEmpty || column.width.sixths <= room else { break }
room -= column.width.sixths
out.insert(column.id)
}
return out
}
/// What arrived in the columns on screen is seen.
private func see(_ columns: [StripColumn], compact: Bool) {
let on = shown(columns, compact: compact)
workspace.seen(columns.filter { on.contains($0.id) }.flatMap { $0.tiles.map(\.id) })
}
/// What plays, as a piece of the strip's bar: none while its tile is on
/// screen (its player shows it), else a tap on it goes there.
@ViewBuilder
private func playing(_ columns: [StripColumn], compact: Bool) -> some View {
let at = browser.player.track == nil ? nil : browser.playingTile()
let onScreen = at.map { at in
at.canvas == canvas && columns.contains { shown(columns, compact: compact).contains($0.id) && $0.tiles.contains { $0.id == at.tile } }
} ?? false
if browser.player.track != nil, !onScreen {
NowPlayingBar(player: browser.player, open: { browser.goToPlaying() }, inBar: true)
.padding(.horizontal, Space.s2)
}
}
}
/// The strip map, as the web's: a chip a column, as wide as its share of
/// the strip, a segment for each tile stacked in it with its app's icon,
/// its chain's colour along its top (tiles opened one from another), a dot
/// while something someone else or an agent added in it is unseen; the
/// columns on screen ringed. A chip tapped goes to its column; the chips
/// scroll to keep the ringed one in view. + before them opens Home to add
/// a tile; a canvas only to look at says so instead; Share after them. A
/// bar of glass at the screen's `edge`: on a phone its foot, in the
/// thumb's reach; on an iPad under the tabs, as the web has it.
struct StripMap: View {
let columns: [StripColumn]
/// The columns on screen.
let shown: Set<String>
/// Tiles added by someone else or an agent, not yet seen.
let arrived: Set<String>
let readonly: Bool
@Binding var visible: String?
let add: () -> Void
/// How many besides the owner are in the canvas.
let others: Int
let share: () -> Void
let edge: VerticalEdge
/// Each tile's chain colour from before, so a chain keeps it as it grows;
/// a reference, as reading the colours updates it.
@State private var kept = Kept()
@ScaledMetric(relativeTo: .caption) private var unit: CGFloat = 7
@ScaledMetric(relativeTo: .caption) private var height: CGFloat = 26
@Environment(\.accessibilityReduceMotion) private var still
final class Kept {
var colours: [String: Int] = [:]
}
/// The chains' colours, 1 to 6, the system's own.
private static let chain: [Color] = [.blue, .pink, .green, .orange, .purple, .teal]
var body: some View {
let colours = chainColours(columns, kept: &kept.colours)
HUDGlassGroup {
HStack(spacing: Space.s2) {
if readonly {
Label("View only", systemImage: "eye")
.font(.caption)
.foregroundStyle(.secondary)
.labelStyle(.iconOnly)
.frame(minWidth: 44, minHeight: 44)
.hudGlass(in: .circle)
.accessibilityLabel("View only: its owner and editors change this canvas")
} else {
Button(action: add) {
Image(systemName: "plus")
.frame(minWidth: 44, minHeight: 44)
.hudGlass(in: .circle, interactive: true)
}
.accessibilityLabel("New tile")
}
ScrollViewReader { proxy in
ScrollView(.horizontal) {
HStack(spacing: 4) {
ForEach(columns) { column in
chip(column, chain: colours[column.tiles[0].id], on: shown.contains(column.id))
.id(column.id)
}
}
.padding(.vertical, 6)
.padding(.horizontal, Space.s3)
.frame(minHeight: 44)
}
.scrollIndicators(.hidden)
.hudGlass(in: .capsule)
.onChange(of: visible) { _, key in
if let key { withAnimation { proxy.scrollTo(key, anchor: .center) } }
}
}
Button(action: share) {
HStack(spacing: 3) {
Image(systemName: others > 0 ? "person.2" : "person.crop.circle.badge.plus")
if others > 0 { Text("\(others)").monospacedDigit() }
}
.font(.subheadline)
.padding(.horizontal, Space.s2)
.frame(minWidth: 44, minHeight: 44)
.hudGlass(in: .capsule, interactive: true)
}
.accessibilityLabel(others > 0 ? "Share, with \(others) other\(others == 1 ? "" : "s")" : "Share")
}
}
.padding(.horizontal, Space.s3)
.padding(.vertical, Space.s1)
.hudBar(at: edge)
}
private func chip(_ column: StripColumn, chain: Int?, on: Bool) -> some View {
let icon = min(14, height / CGFloat(column.tiles.count) - 2)
let fresh = column.tiles.contains { arrived.contains($0.id) }
return Button {
withAnimation { visible = column.id }
} label: {
VStack(spacing: 1) {
ForEach(column.tiles) { tile in
AppIcon(url: tile.icon, symbol: tile.symbol, size: icon, relativeTo: .caption)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.field)
}
}
.frame(width: max(unit * CGFloat(column.width.sixths), unit * 2.5), height: height)
.overlay(alignment: .top) {
if let chain { Self.chain[(chain - 1) % 6].frame(height: 2) }
}
.clipShape(.rect(cornerRadius: 4))
.overlay {
if on {
RoundedRectangle(cornerRadius: 6).strokeBorder(.accent, lineWidth: 1.5).padding(-3)
}
}
.overlay(alignment: .topTrailing) {
if fresh {
Circle()
.fill(HUDColor(.danger, or: .red))
.frame(width: 7, height: 7)
.offset(x: 2, y: -2)
.phaseAnimator(still ? [1.0] : [1.0, 0.3]) { dot, o in dot.opacity(o) } animation: { _ in .easeInOut(duration: 1) }
}
}
}
.buttonStyle(.plain)
.accessibilityLabel(column.tiles.map(\.heading).joined(separator: " / ") + (fresh ? ", new" : ""))
.accessibilityAddTraits(on ? .isSelected : [])
}
}