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
/// Signed in, the app's root: the workspace, as the web page has it. Its
/// canvases open as tabs along the top, with Home and Settings beside
/// them; the one on screen as its strip of columns. One stream hears every
/// open canvas, so a tile added on the web, or by an agent, shows at once;
/// everything is read again as the app comes back.
struct WorkspaceScreen: View {
let workspace: Workspace
let browser: Browser
@Environment(\.scenePhase) private var phase
@Environment(\.webAuthenticationSession) private var web
var body: some View {
VStack(spacing: 0) {
CanvasTabs(workspace: workspace, browser: browser)
signInAgain
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay(alignment: .top) { notice }
}
.hudText()
.background(.canvas)
.toolbar(.hidden, for: .navigationBar)
.sheet(item: Bindable(browser).sharing) { shared in
ShareSheet(canvas: shared.id, workspace: workspace)
.hudUnthemed()
}
.task { await workspace.listen() }
.task { await browser.identify() }
.onChange(of: phase) { _, now in
if now == .active { Task { await workspace.reload() } }
}
.onChange(of: tileIDs) { _, ids in browser.keepSteps(ids) }
}
private var tileIDs: Set<String> {
Set(workspace.tiles.values.flatMap { $0.map(\.id) })
}
@ViewBuilder
private var content: some View {
if let canvas = workspace.current, workspace.tiles[canvas] != nil {
StripView(canvas: canvas, workspace: workspace, browser: browser)
.id(canvas)
} else if let failed = workspace.failed {
ContentUnavailableView {
Label("Couldn't load your workspace", systemImage: "exclamationmark.triangle")
} description: {
Text(failed)
} actions: {
Button("Try again") { Task { await workspace.reload() } }
}
} else {
ProgressView()
}
}
/// While the person signed in before HUD asked for all it asks for now,
/// a bar under the tabs to sign in again, as the web page's: what needs
/// the rest is refused until they do.
@ViewBuilder
private var signInAgain: some View {
if workspace.signInAgain {
VStack(alignment: .leading, spacing: Space.s1) {
HStack(spacing: Space.s3) {
Text("HUD can do more now than when you signed in. Sign in again to allow it.")
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading)
Button("Sign in again") {
let who = workspace.me?.handle ?? browser.session?.did ?? ""
Task { await browser.signIn(who, using: web) }
}
.font(.subheadline.weight(.semibold))
.disabled(browser.signingIn)
}
if let error = browser.signInError {
Text(error).font(.footnote).foregroundStyle(.muted)
}
}
.padding(.horizontal, Space.s4)
.padding(.vertical, Space.s3)
.background(.panel)
}
}
/// What the workspace says a moment: a tile someone else added, with a
/// way to go to it, or what went wrong.
@ViewBuilder
private var notice: some View {
if let notice = workspace.notice {
HStack(spacing: Space.s3) {
Text(notice.text)
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading)
if let canvas = notice.canvas, let tile = notice.tile {
Button("Show") {
workspace.notice = nil
Task {
if canvas != workspace.current { await workspace.activate(canvas) }
workspace.show(canvas, tile)
}
}
.font(.subheadline.weight(.semibold))
}
}
.padding(.horizontal, Space.s4)
.padding(.vertical, Space.s3)
.hudGlass(in: .rect(cornerRadius: 14)) { card in
card
.background(.regularMaterial, in: .rect(cornerRadius: 14))
.shadow(color: .black.opacity(0.12), radius: 8, y: 2)
}
.padding(.horizontal, Space.s3)
.padding(.top, Space.s2)
.transition(.move(edge: .top).combined(with: .opacity))
.task(id: notice.id) {
try? await Task.sleep(for: .seconds(6))
if workspace.notice?.id == notice.id {
withAnimation { workspace.notice = nil }
}
}
}
}
}
/// The tabs: the canvases open, in their order, as words, the one on
/// screen on a soft pill; a tap shows one, a long press its menu (rename,
/// share, move, put away, delete or leave). Then the + menu (a new canvas,
/// one put away or shared, an invitation to open or decline), Home, and
/// the account, which opens Settings, each a control of its own. A quiet
/// bar on the canvas's own surface, not glass: what is under it never
/// scrolls beneath it.
struct CanvasTabs: View {
let workspace: Workspace
let browser: Browser
@State private var naming = false
@State private var name = ""
/// The canvas being renamed, and what it is being called.
@State private var renaming: String?
@State private var rename = ""
/// The canvas asked about deleting or leaving.
@State private var ending: String?
var body: some View {
HStack(spacing: Space.s2) {
ScrollViewReader { proxy in
ScrollView(.horizontal) {
HStack(spacing: Space.s1) {
ForEach(workspace.open, id: \.self) { id in
tab(id)
}
}
.padding(.horizontal, Space.s2)
}
.scrollIndicators(.hidden)
.onChange(of: workspace.current) { _, id in
if let id { withAnimation { proxy.scrollTo(id) } }
}
}
canvasMenu
Button {
browser.showingHome = true
} label: {
Image(systemName: "house")
.frame(minWidth: 44, minHeight: 44)
.contentShape(.rect)
}
.foregroundStyle(.secondary)
.accessibilityLabel("Home")
Button {
browser.showingSettings = true
} label: {
Image(systemName: "person.crop.circle")
.frame(minWidth: 44, minHeight: 44)
.contentShape(.rect)
}
.foregroundStyle(.secondary)
.accessibilityLabel("Accounts and settings")
}
.padding(.trailing, Space.s2)
.background(.canvas)
.alert("New canvas", isPresented: $naming) {
TextField("Name", text: $name)
Button("Create") {
let given = name.trimmingCharacters(in: .whitespaces)
Task { await workspace.create(given.isEmpty ? "Untitled canvas" : given) }
}
Button("Cancel", role: .cancel) {}
}
.alert("Rename canvas", isPresented: Binding(get: { renaming != nil }, set: { if !$0 { renaming = nil } })) {
TextField("Name", text: $rename)
Button("Rename") {
let given = rename.trimmingCharacters(in: .whitespaces)
if let id = renaming, !given.isEmpty { Task { await workspace.rename(id, given) } }
}
Button("Cancel", role: .cancel) {}
}
.confirmationDialog(
endingTitle,
isPresented: Binding(get: { ending != nil }, set: { if !$0 { ending = nil } }),
titleVisibility: .visible,
presenting: ending.flatMap { workspace.info($0) }
) { canvas in
if canvas.owned {
Button("Delete canvas", role: .destructive) { Task { await workspace.delete(canvas.id) } }
} else {
Button("Leave canvas", role: .destructive) {
Task {
do { try await workspace.leave(canvas.id) } catch { workspace.say(error.localizedDescription) }
}
}
}
} message: { canvas in
Text(canvas.owned ? "Its tiles go, for everyone it is shared with." : "It leaves your tabs. Its owner can invite you again.")
}
}
private var endingTitle: String {
guard let canvas = ending.flatMap({ workspace.info($0) }) else { return "" }
return canvas.owned ? "Delete “\(canvas.name)”?" : "Leave “\(canvas.name)”?"
}
private func tab(_ id: String) -> some View {
let on = id == workspace.current
return Button {
if !on { Task { await workspace.activate(id) } }
} label: {
Text(workspace.info(id)?.name ?? "Canvas")
.font(.subheadline.weight(on ? .medium : .regular))
.lineLimit(1)
.foregroundStyle(on ? HUDColor(.text, or: .primary) : .muted)
.padding(.horizontal, Space.s3)
.padding(.vertical, 6)
.background(on ? AnyShapeStyle(.field) : AnyShapeStyle(.clear), in: .capsule)
.frame(minHeight: 44)
.contentShape(.rect)
}
.buttonStyle(.plain)
.accessibilityAddTraits(on ? .isSelected : [])
.contextMenu {
let info = workspace.info(id)
let at = workspace.open.firstIndex(of: id) ?? 0
if info?.owned == true {
Button("Rename", systemImage: "pencil") {
rename = info?.name ?? ""
renaming = id
}
}
Button("Share", systemImage: "person.crop.circle.badge.plus") { browser.sharing = Shared(id: id) }
if workspace.open.count > 1 {
Section {
if at > 0 {
Button("Move left", systemImage: "arrow.left") { Task { await workspace.reorder(moved(id, by: -1)) } }
}
if at < workspace.open.count - 1 {
Button("Move right", systemImage: "arrow.right") { Task { await workspace.reorder(moved(id, by: 1)) } }
}
Button("Put away", systemImage: "archivebox") {
Task { await workspace.putAway(id) }
}
}
}
if let info {
Section {
Button(info.owned ? "Delete canvas" : "Leave canvas", systemImage: info.owned ? "trash" : "rectangle.portrait.and.arrow.right", role: .destructive) {
ending = id
}
}
}
}
.id(id)
}
/// The tabs with `id` moved a place left (-1) or right (1).
private func moved(_ id: String, by step: Int) -> [String] {
var order = workspace.open
guard let at = order.firstIndex(of: id), order.indices.contains(at + step) else { return order }
order.swapAt(at, at + step)
return order
}
private var canvasMenu: some View {
Menu {
Button("New canvas", systemImage: "plus") {
name = ""
naming = true
}
let others = workspace.canvases.filter { !workspace.open.contains($0.id) && $0.invitedBy == nil }
if !others.isEmpty {
Section("Open a canvas") {
ForEach(others) { canvas in
Button(canvas.name) {
Task { await workspace.activate(canvas.id) }
}
}
}
}
let invitations = workspace.canvases.filter { $0.invitedBy != nil }
if !invitations.isEmpty {
Section("Invitations") {
ForEach(invitations) { canvas in
Menu(canvas.name) {
Button("Open", systemImage: "checkmark") { Task { await workspace.activate(canvas.id) } }
Button("Decline", systemImage: "xmark", role: .destructive) { Task { await workspace.decline(canvas.id) } }
}
}
}
}
} label: {
Image(systemName: "plus")
.frame(minWidth: 44, minHeight: 44)
.contentShape(.rect)
}
.foregroundStyle(.secondary)
.accessibilityLabel("Canvases")
}
}