Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

66.7 KB
Raw Blame
import SwiftUI

/// One host: its terminal, the tab strip, the bars over the panes, the
/// key bar, and the sidebar and menus as sheets.
struct TerminalScreen: View {
    /// The host's session, from `Sessions`: the screen shows it and
    /// gives it back on the way out, connection and all.
    @ObservedObject var model: TerminalModel
    @ObservedObject private var settings = AppSettings.shared
    @ObservedObject private var lang = AppLanguage.shared
    @Environment(\.dismiss) private var dismiss
    @Environment(\.displayScale) private var displayScale
    /// The drawer is out (a narrow screen).
    @State private var showTree = false
    /// The screen is wide enough to keep the sidebar beside the terminal,
    /// and whether it is kept there.
    @State private var wide = false
    @AppStorage("sidebar.pinned") private var sidebarPinned = true
    @State private var showLog = false
    @State private var showSettings = false
    @State private var menu: MenuSheet?
    @State private var editingHost: Host?
    /// A bar being dragged moves the divider it hangs from.
    @State private var barDrag: CGPoint?
    /// The software keyboard is up for the terminal: the key bar shows
    /// with it.
    @State private var keyboardUp = false
    /// The drawer's rows are made while it is open and while it slides
    /// shut, not while it lies hidden under the screen.
    @State private var drawerShown = false
    /// How far a swipe from the left edge has pulled the screen off the
    /// drawer, as the Claude app's does; it springs back if let go short.
    @GestureState(resetTransaction: Transaction(animation: TerminalScreen.slide))
    private var edgePull: CGFloat = 0
    @State private var edgeDrag = EdgeDrag()
    /// Where the system puts a bar along the top (`barRoom`): on an
    /// unfolded Duo, below the rounded corners, not at the screen's top.
    @State private var topRoom = EdgeInsets()
    /// A name the sidebar asked for (`SidebarView.editing`), kept here so
    /// the question stands wherever the sidebar is.
    @State private var renaming: (kind: String, id: String)?
    @State private var renameText = ""
    /// The overview (`OverviewScreen`): the terminal shrinks into its
    /// thread's card and the cards fade in around it, as the desktop's Live
    /// Overview zooms out; a card tapped grows back into the terminal.
    /// `overviewOpen` while it is the screen's, `overviewVisible` while its
    /// cards are up (a close included).
    @State private var overviewOpen = false
    @State private var overviewVisible = false
    @State private var cardsAlpha: Double = 0
    /// Where the live card's picture is while the terminal is on its way
    /// into it; every card's is in `moves`.
    @State private var landingThumb: CGRect?
    @State private var terminalFrame: CGRect = .zero
    /// The terminal's top and bottom on the screen: the bar's buttons are
    /// above it and the keys below it scroll sideways, so a swipe from the
    /// edge there is theirs.
    @State private var terminalSpan: ClosedRange<CGFloat> = 0...CGFloat.infinity
    /// The terminal is hidden while a picture of it flies in its place.
    @State private var terminalAlpha: Double = 1
    @State private var flight = Flight()
    /// The thread the terminal went into the card of, and whether the
    /// card's picture shows in its place.
    @State private var liveThread: String?
    @State private var liveShown = false
    @State private var moves = OverviewMoves()
    /// Whether tabs lie past the start of the strip and past its end.
    @State private var stripEnds = StripEnds()

    /// On a phone held upright the sidebar is a drawer the whole screen
    /// slides off, rounding its corner -- by the button or a swipe from the
    /// left edge -- and a tap on what shows of it comes back; on a wide screen (an iPad, an unfolded Duo, landscape) it stays
    /// at the left at full height, and the bar runs over the terminal only,
    /// as on the desktop.
    var body: some View {
        GeometryReader { geo in
            // Decided in the pass that lays it out, so the terminal is sized
            // once; the state follows a pass later, for the buttons.
            let wide = geo.size.width >= Self.wideWidth
            let drawerOpen = showTree && !wide
            let drawerWidth = min(Self.columnWidth + 40, geo.size.width * 0.84)
            let pulled = drawerOpen || edgePull > 0
            ZStack(alignment: .topLeading) {
                if !wide {
                    // The drawer lies under the screen, which slides off it.
                    sidebar(live: pulled || drawerShown)
                        .frame(width: drawerWidth)
                        .opacity(pulled ? 1 : 0)
                        .allowsHitTesting(drawerOpen)
                        .accessibilityHidden(!drawerOpen)
                }
                HStack(spacing: 0) {
                    if wide && sidebarPinned {
                        sidebar(live: true).frame(width: Self.columnWidth)
                        Rectangle().fill(Color.white.opacity(0.08)).frame(width: 1).ignoresSafeArea()
                    }
                    screen
                        .clipShape(RoundedRectangle(cornerRadius: pulled ? 28 : 0, style: .continuous))
                        .overlay {
                            if drawerOpen {
                                // What shows of the screen takes it back.
                                RoundedRectangle(cornerRadius: 28, style: .continuous)
                                    .fill(Color.black.opacity(0.35))
                                    .ignoresSafeArea()
                                    .onTapGesture { showTree = false }
                            }
                        }
                        .shadow(color: .black.opacity(pulled ? 0.55 : 0), radius: 20)
                }
                .offset(x: drawerOpen ? drawerWidth : min(edgePull, drawerWidth))
            }
            .simultaneousGesture(
                // A swipe right from the left edge opens the drawer, the
                // screen following the finger; one from further in is the
                // terminal's (a selection, a scroll).
                DragGesture(minimumDistance: 10, coordinateSpace: .global)
                    .updating($edgePull) { drag, pull, transaction in
                        // Back once it is none: brought back past where it began.
                        let next = pullsDrawer(drag) ? max(0, drag.translation.width) : 0
                        if next == 0 && pull > 0 {
                            transaction.animation = Self.slide
                        }
                        pull = next
                    }
                    .onEnded { drag in
                        // Far enough, or flung far enough, and not flung back.
                        let far = drag.translation.width > drawerWidth / 3 || drag.predictedEndTranslation.width > drawerWidth / 2
                        if far && drag.velocity.width > -200 && pullsDrawer(drag) { showSidebar() }
                        edgeDrag.start = nil
                    },
                including: wide || drawerOpen ? .subviews : .all
            )
            .simultaneousGesture(
                DragGesture(minimumDistance: 24).onEnded { drag in
                    // A swipe to the left, not a scroll of the list that
                    // drifts.
                    let move = drag.translation
                    if move.width < -60 && abs(move.width) > abs(move.height) { showTree = false }
                },
                including: drawerOpen ? .all : .subviews
            )
            .animation(Self.slide, value: showTree)
            .animation(Self.slide, value: sidebarPinned)
            .onAppear {
                self.wide = wide
                model.edgePullsDrawer = !wide && !showTree
            }
            .onChange(of: wide) { _, wide in
                self.wide = wide
                if wide { showTree = false }
                model.edgePullsDrawer = !wide && !showTree
            }
        }
        // Under the status bar: the terminal's colour, or the sidebar's
        // beside an open drawer (the screen is clipped to its frame).
        .background((drawerOpen ? Color(white: 0.07) : cardsAlpha > 0 ? OverviewScreen.backdrop : model.background).ignoresSafeArea())
        .onChange(of: drawerOpen) { _, open in
            model.edgePullsDrawer = !wide && !open
            if open {
                drawerShown = true
            } else {
                // The only field it can leave focused is its search.
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    if !drawerOpen { drawerShown = false }
                }
            }
        }
        .toolbar(.hidden, for: .navigationBar)
        .toolbar(.hidden, for: .tabBar)
    }

    /// Wide enough for the sidebar to stay beside the terminal.
    private static let wideWidth: CGFloat = 700
    /// How near the left edge a swipe must start to open the drawer.
    static let edgeWidth: CGFloat = 24
    /// The screen sliding off the drawer and back, and the column coming
    /// and going.
    private static let slide = Animation.spring(response: 0.34, dampingFraction: 0.88)
    private static let columnWidth: CGFloat = 280
    private static let barHeight: CGFloat = 54
    /// How far in from an end of the tab strip its tabs fade.
    private static let stripFade: CGFloat = 28
    private var drawerOpen: Bool { showTree && !wide }

    /// A drag that pulls the drawer out: from the screen's left edge and
    /// sideways, over the terminal (the bar's buttons are above it, the
    /// keys under it scroll sideways), and not a selection's handle being
    /// moved. Decided as it sets off and kept, so a thumb's arc turning up
    /// or down on the way does not let go of the screen.
    private func pullsDrawer(_ drag: DragGesture.Value) -> Bool {
        if edgeDrag.start != drag.startLocation {
            let move = drag.translation
            edgeDrag.start = drag.startLocation
            edgeDrag.pulls = drag.startLocation.x < Self.edgeWidth && move.width > abs(move.height)
                && terminalSpan.contains(drag.startLocation.y) && model.inputView?.holdsSelection != true
        }
        return edgeDrag.pulls
    }

    /// The sidebar, opened (a drawer) or brought back (a column).
    private func showSidebar() {
        moves.waiting = false
        if wide { sidebarPinned = true } else { model.dismissKeyboard(); showTree = true }
    }

    /// A drawer covers the terminal and takes its keyboard down; a column
    /// leaves both be.
    private func toggleSidebar() {
        moves.waiting = false
        if wide { sidebarPinned.toggle() } else { model.dismissKeyboard(); showTree.toggle() }
    }

    /// Back to the hosts, an overview waiting to open left shut: the
    /// screen is gone only once it has slid away.
    private func leave() {
        moves.waiting = false
        dismiss()
    }

    private func threadName(_ id: String) -> String {
        for space in model.tree?.spaces ?? [] {
            for project in space.projects {
                if let thread = project.threads.first(where: { $0.id == id }) { return thread.name }
            }
        }
        return ""
    }

    private func sidebar(live: Bool) -> some View {
        SidebarPanel(
            model: model,
            hostName: hostName,
            connected: connected,
            done: { if !wide { showTree = false } },
            onEditHost: { showTree = false; editingHost = model.host },
            onSettings: { showSettings = true },
            onOverview: { openOverview() },
            overviewOn: overviewOpen,
            live: live
        )
    }

    /// The terminal's page: the bar, then the strip, the terminal, the
    /// status line and the keys. Every piece of chrome takes the
    /// terminal's own background.
    private var screen: some View {
        VStack(spacing: 0) {
            topBar
            content
        }
        .barRoom(.top, height: Self.barHeight, $topRoom)
        .background((cardsAlpha > 0 ? OverviewScreen.backdrop : model.background).ignoresSafeArea())
        .ignoresSafeArea(.container, edges: .bottom)
        // Another keyboard (the sidebar's search, a name being asked for)
        // is not the terminal's: it does not resize it.
        .ignoresSafeArea(keyboardUp ? [] : .keyboard, edges: .bottom)
    }

    /// The sidebar, back to the hosts, and the strip in the rest of the
    /// bar; the round buttons in the system's glass.
    private var topBar: some View {
        HStack(spacing: 8) {
            Button { toggleSidebar() } label: { barIcon("sidebar.left") }
                .buttonStyle(.plain)
                .glassCircle()
                .accessibilityLabel(tr("sidebar"))
            Button { leave() } label: { barIcon("chevron.left") }
                .buttonStyle(.plain)
                .glassCircle()
                .accessibilityLabel(tr("hosts"))
            if overviewOpen {
                // The overview's own bar: its name, and the way back.
                Text(tr("overview"))
                    .font(.headline)
                    .foregroundColor(.white)
                    .lineLimit(1)
                    .padding(.leading, 4)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Button { closeOverview() } label: { barIcon("xmark") }
                    .buttonStyle(.plain)
                    .glassCircle()
                    .accessibilityLabel(tr("done"))
            } else {
                tabStrip.frame(maxWidth: .infinity, alignment: .leading)
            }
        }
        // The screen's left side is the sidebar's while it stays there.
        .padding(.leading, max(10, wide && sidebarPinned ? 0 : topRoom.leading))
        .padding(.trailing, max(10, topRoom.trailing))
        .frame(height: Self.barHeight)
        .padding(.top, max(0, topRoom.top))
        .contentShape(Rectangle())
        // Pinching the bar zooms out to the overview.
        .gesture(MagnifyGesture().onEnded { value in
            if value.magnification < 0.8 { openOverview() }
        })
        // The text follows the system's size, as far as the bar holds it.
        .dynamicTypeSize(...DynamicTypeSize.xxLarge)
    }

    private func barIcon(_ name: String) -> some View {
        Image(systemName: name)
            .font(.body.weight(.semibold))
            .foregroundColor(cardsAlpha > 0 ? .white : model.ink)
            .frame(width: 42, height: 42)
            .contentShape(Circle())
    }

    private var content: some View {
        ZStack(alignment: .topLeading) {
            if overviewVisible {
                OverviewScreen(
                    model: model, stage: terminalFrame.size, liveThread: liveThread, liveShown: liveShown,
                    onThumb: { noteThumb($0, $1) }, onPick: { pick($0) }, onClose: { closeOverview() }
                )
                .opacity(cardsAlpha)
                .allowsHitTesting(overviewOpen)
            }
            // Above the cards, so the terminal shows on its way into its
            // card; untouchable meanwhile, so the cards get the taps.
            VStack(spacing: 0) {
                terminal
                    .opacity(terminalAlpha)
                    .onGeometryChange(for: CGRect.self) { $0.frame(in: .named("screen")) } action: { terminalFrame = $0 }
                    .onGeometryChange(for: ClosedRange<CGFloat>.self) { geo in
                        let frame = geo.frame(in: .global)
                        return frame.minY...max(frame.minY, frame.maxY)
                    } action: { terminalSpan = $0 }
                    .zIndex(2)
                statusLine.opacity(overviewOpen ? 0 : 1)
                if (keyboardUp || model.panelOpen) && !overviewOpen {
                    // With the keyboard down the bar sits on the home
                    // indicator's inset; with it up, on the keyboard.
                    KeyBar(model: model)
                        .padding(.bottom, keyboardUp ? 0 : Self.bottomInset)
                        .transition(.move(edge: .bottom).combined(with: .opacity))
                }
            }
            .animation(.easeOut(duration: 0.2), value: keyboardUp)
            .animation(.easeOut(duration: 0.2), value: model.panelOpen)
            .zIndex(1)
            .allowsHitTesting(!overviewVisible)
            // A picture of the terminal on its way into a card or out of one.
            Flyway(flight: flight)
                .allowsHitTesting(false)
                .zIndex(3)
        }
        .coordinateSpace(name: "screen")
        .background(cardsAlpha > 0 ? OverviewScreen.backdrop : model.background)
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { note in
            let frame = (note.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect) ?? .zero
            // The sidebar's search has a keyboard too, and no key bar.
            keyboardUp = frame.height > 0 && model.inputView?.isFirstResponder == true
            // The terminal's back before the overview it was put away for.
            if keyboardUp { moves.waiting = false }
        }
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
            keyboardUp = false
        }
        .onChange(of: landingThumb) { _, thumb in
            // The cards are laid out (or the card moved, scrolled into
            // sight), even after the picture got where they were thought to
            // be: it goes on to the card, and the cards come out from under
            // the cover.
            guard let thumb, overviewOpen, !liveShown, !moves.closing else { return }
            flyIn(to: thumb)
            flight.fadeCover(to: 0, duration: 0.3)
        }
        .onChange(of: livePicture) { _, _ in fadeIntoCard() }
        .onChange(of: currentThreadId) { _, now in
            if overviewOpen, !moves.closing, let now, now != liveThread,
               let thread = model.threads?.threads.first(where: { $0.id == now }) {
                // Another thread picked elsewhere (the sidebar): it is the
                // one the overview closes into.
                pick(thread)
            } else if moves.picked != nil {
                // The picked thread is the terminal's now; its first frame
                // follows.
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { revealPicked(force: false) }
            }
        }
        .sheet(item: $model.pendingHostKey) { challenge in
            ScrollView {
                VStack(alignment: .leading, spacing: 20) {
                    Text(tr("hostkey.approve.title")).font(.headline)
                    // Verbatim: an interpolated number is localised, "2,299".
                    Text(verbatim: "\(challenge.hostname):\(challenge.port)")
                    Text(challenge.fingerprint).font(.system(.body, design: .monospaced))
                        .textSelection(.enabled).fixedSize(horizontal: false, vertical: true)
                    Text(tr("hostkey.approve.body"))
                    HStack {
                        Button(tr("cancel"), role: .cancel) { model.disconnect() }
                        Spacer()
                        Button(tr("hostkey.approve.accept")) { model.approveHostKey(challenge) }
                    }
                }.padding(24)
            }.presentationDetents([.medium, .large])
        }
        .sheet(isPresented: $showSettings) {
            NavigationStack {
                SettingsView(showLog: $showLog)
                    .toolbar {
                        ToolbarItem(placement: .confirmationAction) { Button(tr("done")) { showSettings = false } }
                    }
            }
        }
        .onChange(of: showLog) { _, on in model.logShown = on }
        .onChange(of: model.sidebar?.editing) { _, editing in
            // A new project has no id yet: the same field, asking for the
            // name of what is about to exist.
            guard let editing, editing.kind != "none" else { return }
            if renaming == nil {
                renameText = ""
                renaming = (editing.kind, editing.id ?? "")
            }
        }
        .onChange(of: model.nameAsks) { _, _ in
            // "+" asks at once, even while the core still asks from before.
            renameText = ""
            renaming = ("new-project", "")
        }
        .onChange(of: model.sidebar?.new_project_error) { _, error in
            // A folder the core refused: asked again, with why.
            if error != nil, model.sidebar?.editing.kind == "new-project", renaming == nil {
                renaming = ("new-project", "")
            }
        }
        .alert(
            renaming?.kind == "new-project" ? tr("project.new") : tr("rename"),
            isPresented: Binding(get: { renaming != nil }, set: { if !$0 { renaming = nil } })
        ) {
            // A new project is a folder on the host, ~/dir or /dir.
            TextField(renaming?.kind == "new-project" ? "~/" : tr("f.name"), text: $renameText)
                .textInputAutocapitalization(.never)
                .autocorrectionDisabled()
            Button(tr("save")) {
                model.sideKey("Enter", value: renameText)
                renaming = nil
            }
            Button(tr("cancel"), role: .cancel) {
                model.sideKey("Escape", value: "")
                renaming = nil
            }
        } message: {
            if renaming?.kind == "new-project", let error = model.sidebar?.new_project_error {
                Text(error)
            }
        }
        .confirmationDialog(
            tr("thread.delete.title", threadName(model.confirming?.split(separator: ":", maxSplits: 1).last.map(String.init) ?? "")),
            isPresented: Binding(get: { model.confirming != nil }, set: { if !$0 { model.confirming = nil } }),
            titleVisibility: .visible
        ) {
            // Deleting ends every program in the thread: it is asked first.
            Button(tr("delete"), role: .destructive) {
                if let action = model.confirming { model.menuAction(action) }
                model.confirming = nil
            }
            Button(tr("cancel"), role: .cancel) { model.confirming = nil }
        } message: {
            Text(tr("thread.delete.body"))
        }
        .sheet(item: $menu) { sheet in
            MenuList(model: model, sheet: sheet) { menu = nil }
                .presentationDetents([.medium, .large])
        }
        .sheet(item: $editingHost) { host in
            HostEditView(host: host) { edited, secret, passphrase in
                if !secret.isEmpty { Keychain.save(secret, account: edited.id.uuidString) }
                if let passphrase { Keychain.save(passphrase, account: edited.id.uuidString + ".passphrase") }
                model.store?.upsert(edited)
                model.host = edited
                model.connect()
            }
        }
        .onAppear {
            moves.onScreen = true
            model.enterScreen()
            // The automated flows connect on their own schedule; a session
            // found open is simply shown.
            if !TerminalModel.automated, !model.isConnected {
                model.connect()
            }
            #if DEBUG
            let args = ProcessInfo.processInfo.arguments
            // When a flag fires: the seconds after it, if given.
            func when(_ flag: String, _ otherwise: Double) -> Double? {
                guard let at = args.firstIndex(of: flag) else { return nil }
                return at + 1 < args.count ? Double(args[at + 1]) ?? otherwise : otherwise
            }
            // On time: the system lets a long wait run late by up to a
            // tenth of it, which would run two flags a moment apart as one.
            // Not once the screen is gone.
            func after(_ seconds: Double, _ work: @escaping () -> Void) {
                let timer = DispatchSource.makeTimerSource(queue: .main)
                timer.schedule(deadline: .now() + seconds, leeway: .milliseconds(1))
                timer.setEventHandler {
                    if moves.onScreen { work() }
                    timer.cancel()
                }
                timer.resume()
            }
            // Screenshots of the sheets, which no script can tap open.
            if let t = when("--open-tree", 14) { after(t) { showSidebar() } }
            if let t = when("--open-overview", 14) { after(t) { openOverview() } }
            // The overview's ways back: a running thread's card by its
            // place among them, or the close button.
            if let at = args.firstIndex(of: "--overview-pick"), at + 1 < args.count, let n = Int(args[at + 1]) {
                after(17) {
                    let running = (model.threads?.threads ?? []).filter(\.live)
                    if running.indices.contains(n) { pick(running[n]) }
                }
            }
            if let t = when("--overview-close", 17) { after(t) { closeOverview() } }
            #endif
        }
        .onDisappear {
            // The picture goes, the connection stays (see Sessions), and an
            // overview left open, or on its way, lets its screens go.
            moves.onScreen = false
            overviewClosed()
            model.leaveScreen()
        }
    }

    // MARK: the bar under the terminal

    private var connected: Bool {
        !(model.connection.hasPrefix("disconnected") || model.connection.hasPrefix("failed") || model.connection.isEmpty)
    }

    private var hostName: String {
        model.host?.display ?? tr("thismac")
    }

    var statusColor: Color {
        if model.reconnecting { return .orange }
        if connected { return .green }
        if model.connection.hasPrefix("connecting") || model.connection.contains("reconnect") { return .orange }
        return .red
    }

    /// The current thread's tabs, as the desktop's bar has them; the
    /// threads are the sidebar's.
    private var tabStrip: some View {
        let current = model.tabs?.tabs.first(where: { $0.current })?.tab
        return ScrollViewReader { reader in
            ScrollView(.horizontal, showsIndicators: false) {
                // As tall as the round buttons beside them, in their glass.
                HStack(spacing: 8) {
                    tabPills(height: 42)
                    newTabButton
                }
                // A scroll comes to rest with a whole pill at the start.
                .scrollTargetLayout()
                .glassGroup(spacing: 8)
            }
            .scrollTargetBehavior(.viewAligned)
            // The glass swells as it is pressed, above and below the strip;
            // at its ends the tabs scrolled off it are cut, not drawn over
            // the buttons before it, and fade out on their way there.
            .scrollClipDisabled()
            .clipShape(SideClip())
            .onScrollGeometryChange(for: StripEnds.self) { geo in
                StripEnds(before: geo.visibleRect.minX > 1, after: geo.visibleRect.maxX < geo.contentSize.width - 1)
            } action: { _, ends in
                stripEnds = ends
            }
            .overlay { stripFades }
            .onAppear {
                // First shown, or back from the overview: the current tab
                // at the start, once the strip is laid out.
                DispatchQueue.main.async {
                    if let current { reader.scrollTo("tab:\(current)", anchor: .leading) }
                }
            }
            .onChange(of: current) { _, tab in
                // A new tab's pill is laid out in this same pass: at the
                // start once it is.
                DispatchQueue.main.async {
                    if let tab { reader.scrollTo("tab:\(tab)", anchor: .leading) }
                }
            }
        }
        .foregroundColor(model.ink)
    }

    /// The bar's ground over the strip's ends where tabs lie past them, so
    /// they fade out there rather than end in a cut; an end with nothing
    /// past it stays sharp.
    private var stripFades: some View {
        let ground = cardsAlpha > 0 ? OverviewScreen.backdrop : model.background
        return HStack(spacing: 0) {
            LinearGradient(colors: [ground, ground.opacity(0)], startPoint: .leading, endPoint: .trailing)
                .frame(width: Self.stripFade)
                .opacity(stripEnds.before ? 1 : 0)
            Spacer(minLength: 0)
            LinearGradient(colors: [ground.opacity(0), ground], startPoint: .leading, endPoint: .trailing)
                .frame(width: Self.stripFade)
                .opacity(stripEnds.after ? 1 : 0)
        }
        .animation(.easeOut(duration: 0.15), value: stripEnds)
        .allowsHitTesting(false)
    }

    /// The tabs as the desktop's capsules: the card of what each runs at
    /// the left, round within the capsule's round end, then its name.
    private func tabPills(height: CGFloat) -> some View {
        ForEach(model.tabs?.tabs ?? []) { tab in
            Button {
                model.chromeClick("pane", pane: tab.target)
            } label: {
                // A window tab wears the terminal's card, as on the
                // desktop; what each pane runs is on its capsule.
                let icon = model.tabs?.icon
                AtMost(width: 180) {
                    HStack(spacing: 8) {
                        TabIconCard(core: model.core, id: icon, size: height - 12, revision: model.iconsRevision)
                        Text(tab.label.isEmpty ? tr("tab.num", tab.tab) : tab.label)
                            .font(.subheadline.weight(tab.current ? .semibold : .regular))
                            .lineLimit(1)
                        if tab.current {
                            closeX { model.chromeClick("close-tab", tab: tab.tab) }
                        }
                    }
                    .padding(.leading, icon == nil ? 14 : 6)
                    .padding(.trailing, tab.current ? 6 : 14)
                }
                .frame(minWidth: 76, minHeight: height)
                .foregroundColor(tab.current ? model.ink : model.ink.opacity(0.62))
                .contentShape(Capsule())
            }
            .buttonStyle(.plain)
            .glassPill(lit: tab.current, ink: model.ink)
            .contextMenu {
                AppMenuItems(model: model, kind: "tab", id: String(tab.tab))
            }
            .id("tab:\(tab.tab)")
        }
    }

    /// The × at the end of the current pill: what the desktop's tabs carry.
    private func closeX(_ action: @escaping () -> Void) -> some View {
        Button(action: action) {
            Image(systemName: "xmark")
                .font(.system(size: 11, weight: .bold))
                .foregroundColor(model.ink.opacity(0.7))
                .frame(width: 30, height: 30)
                .contentShape(Circle())
        }
        .buttonStyle(.plain)
    }

    /// "+" opens a tab; held, it offers the splits too.
    private var newTabButton: some View {
        Button { model.chromeClick("new-tab") } label: { barIcon("plus") }
            .buttonStyle(.plain)
            .glassCircle()
            .accessibilityLabel(tr("newtab"))
            .contextMenu {
                Button(tr("newtab")) { model.chromeClick("new-tab") }
                Button(tr("split.right")) { model.chromeClick("split-right") }
                Button(tr("split.below")) { model.chromeClick("split-below") }
            }
    }

    // MARK: the overview

    private var currentThreadId: String? { model.threads?.threads.first(where: { $0.current })?.id }

    /// Where the live card's picture is, its pane, and whether its
    /// picture is drawn.
    private var liveThumb: CGRect? { liveThread.flatMap { moves.thumbs[$0] } }
    private var livePane: Int? {
        liveThread.flatMap { id in model.threads?.threads.first(where: { $0.id == id }) }.flatMap(OverviewScreen.pane(of:))
    }
    private var livePicture: Bool { livePane.flatMap { model.pictures[$0] } != nil }

    /// A card's picture moved (laid out, scrolled): kept for the flights,
    /// and the live one's followed while the terminal is on its way in.
    private func noteThumb(_ id: String, _ rect: CGRect) {
        moves.thumbs[id] = rect
        if id == liveThread, overviewOpen, !liveShown, !moves.closing { landingThumb = rect }
    }

    /// The home indicator's inset, for a bar that sits at the bottom
    /// while the keyboard is down.
    static var bottomInset: CGFloat {
        UIApplication.shared.connectedScenes
            .compactMap { ($0 as? UIWindowScene)?.keyWindow }
            .first?.safeAreaInsets.bottom ?? 0
    }

    /// Into the overview. The terminal as it is flies off at once, for
    /// where its card is bound to be, over a cover under which the cards
    /// are made; Core Animation keeps it going while they are, and the
    /// cards come out from under the cover once they are laid out.
    private func openOverview() {
        guard !overviewVisible, !flight.flying, !moves.waiting else {
            // Asked again from the drawer, or while it slides away: only
            // the drawer goes.
            if drawerOpen { showTree = false }
            return
        }
        guard drawerOpen || keyboardUp || model.panelOpen else { return zoomOut() }
        // The terminal back in its place, uncovered and at its full height,
        // first: the cards have its shape, and it flies from where it shows.
        model.dismissKeyboard()
        model.panelOpen = false
        moves.waiting = true
        let turn = moves.turn
        // Once; not if the drawer, the keyboard or the keys came back
        // meanwhile, or the screen went.
        let go = {
            guard turn == moves.turn, moves.waiting else { return }
            moves.waiting = false
            guard moves.onScreen, !drawerOpen, edgePull == 0, model.inputView?.isFirstResponder != true, !model.panelOpen else { return }
            zoomOut()
        }
        if drawerOpen {
            // Once the screen has slid back over the drawer, not while it
            // shows short of where it is laid out (a busy main thread
            // starts the slide late); a slide that never says it is done
            // waits no longer than a second.
            var back = Transaction(animation: Self.slide)
            back.addAnimationCompletion(criteria: .logicallyComplete, go)
            withTransaction(back) { showTree = false }
            DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: go)
        } else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4, execute: go)
        }
    }

    /// The terminal, where it shows uncovered, off into its card.
    private func zoomOut() {
        let running = (model.threads?.threads ?? []).filter(\.live)
        let live = running.first(where: \.current)
        moves.turn += 1
        moves.landed = false
        let turn = moves.turn
        if let live, let index = running.firstIndex(of: live), terminalFrame.height > 0,
           let shot = flight.snapshot(terminalFrame) {
            flight.cover(UIColor(OverviewScreen.backdrop), alpha: 1)
            flight.begin(with: shot, frame: terminalFrame, at: terminalFrame, radius: 0)
            flyIn(to: OverviewScreen.estimatedThumb(
                index: index, count: running.count, width: terminalFrame.width, aspect: terminalFrame.width / terminalFrame.height
            ))
            CATransaction.flush()
        }
        // The cards, after the flight is under way.
        DispatchQueue.main.async {
            guard turn == moves.turn else { return }
            model.refreshViews()
            model.keepScreens()
            liveThread = live?.id
            if let pane = live.flatMap(OverviewScreen.pane(of:)) { model.requestScreen(pane: pane) }
            moves.thumbs = [:]
            landingThumb = nil
            liveShown = false
            overviewOpen = true
            overviewVisible = true
            if flight.flying {
                // Under the cover already: gone at once, so the cover
                // fading off the cards shows no trace of it.
                terminalAlpha = 0
                withAnimation(.easeOut(duration: 0.3)) { cardsAlpha = 1 }
            } else {
                // Nothing flies over the live card: its picture shows.
                withAnimation(.easeOut(duration: 0.3)) {
                    terminalAlpha = 0
                    cardsAlpha = 1
                }
                moves.landed = true
                liveShown = true
            }
        }
        // Cards that never say where they are show all the same.
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) {
            guard turn == moves.turn, overviewOpen, !liveShown, landingThumb == nil else { return }
            moves.landed = true
            liveShown = true
            flight.fadeOut(duration: 0.2) {}
        }
    }

    private func flyIn(to thumb: CGRect) {
        let turn = moves.turn
        flight.move(to: thumb, radius: OverviewScreen.thumbRadius, duration: 0.45) {
            guard turn == moves.turn else { return }
            moves.landed = true
            fadeIntoCard()
            // A picture that never comes (the server could not say) leaves
            // the card's own background there.
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                if turn == moves.turn { fadeIntoCard(force: true) }
            }
        }
    }

    /// The card's picture under the terminal's, which fades off it.
    private func fadeIntoCard(force: Bool = false) {
        guard overviewOpen, moves.landed, !moves.closing, !liveShown, livePicture || force else { return }
        liveShown = true
        let turn = moves.turn
        DispatchQueue.main.async {
            if turn == moves.turn { flight.fadeOut(duration: 0.2) {} }
        }
    }

    /// A card's picture as a view to fly out of it, at once; drawn again
    /// at the terminal's size off the main thread and put in when it is,
    /// on the way.
    private func flyingPicture(of pane: Int) -> UIView? {
        guard let card = model.pictures[pane], terminalFrame.width > 0 else { return nil }
        let scale = displayScale
        let view = UIImageView(image: UIImage(cgImage: card, scale: scale, orientation: .up))
        view.contentMode = .scaleToFill
        view.backgroundColor = UIColor(model.screens[pane].flatMap { Color(hex: $0.background) } ?? model.background)
        model.paint(pane: pane, pixels: CGSize(width: terminalFrame.width * scale, height: terminalFrame.height * scale)) {
            [weak view] full in view?.image = UIImage(cgImage: full, scale: scale, orientation: .up)
        }
        return view
    }

    /// Back into the thread on show: its picture lifts off its card and
    /// grows to the screen as the cards go under the cover, and the
    /// terminal takes over (`flyBack`).
    private func closeOverview() {
        guard overviewOpen, !moves.closing else { return }
        moves.turn += 1
        moves.closing = true
        let turn = moves.turn
        let backdrop = UIColor(OverviewScreen.backdrop)
        if !moves.landed && flight.flying {
            // Still on its way in: it turns back.
            flight.fadeCover(to: 1, duration: 0.2)
            flight.move(to: terminalFrame, radius: 0, duration: 0.35) { flyBack(turn) }
        } else if let thumb = liveThumb, let pane = livePane, let picture = flyingPicture(of: pane) {
            flight.cover(backdrop, alpha: 0)
            flight.begin(with: picture, frame: terminalFrame, at: thumb, radius: OverviewScreen.thumbRadius)
            flight.fadeCover(to: 1, duration: 0.3)
            flight.move(to: terminalFrame, radius: 0, duration: 0.4) { flyBack(turn) }
        } else {
            fadeBack(turn)
            return
        }
        CATransaction.flush()
    }

    /// Nothing to fly: the terminal fades back over the cards, its bar
    /// back at once.
    private func fadeBack(_ turn: Int) {
        flight.end()
        overviewOpen = false
        withAnimation(.easeOut(duration: 0.25)) {
            terminalAlpha = 1
            cardsAlpha = 0
        } completion: {
            if turn == moves.turn { overviewClosed() }
        }
    }

    /// A card tapped. The thread on show comes back as `closeOverview`
    /// has it; another's picture leaves its card and grows to the screen,
    /// and the terminal takes over from it once it shows that thread
    /// (`revealPicked`).
    private func pick(_ thread: ThreadView) {
        guard overviewOpen, !moves.closing else { return }
        if thread.id == liveThread {
            closeOverview()
            return
        }
        if currentThreadId != thread.id { model.sideClick("thread", id: thread.id) }
        moves.turn += 1
        moves.closing = true
        moves.grown = false
        moves.picked = thread.id
        let turn = moves.turn
        guard let thumb = moves.thumbs[thread.id], let pane = OverviewScreen.pane(of: thread),
              let picture = flyingPicture(of: pane) else {
            // Its picture not drawn yet: none flies, not even the thread
            // on show's, and the terminal fades back once it shows this one.
            flight.end()
            moves.grown = true
            if currentThreadId == thread.id {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { revealPicked(force: false) }
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                if turn == moves.turn { revealPicked(force: true) }
            }
            return
        }
        flight.cover(UIColor(OverviewScreen.backdrop), alpha: 0)
        flight.begin(with: picture, frame: terminalFrame, at: thumb, radius: OverviewScreen.thumbRadius)
        flight.fadeCover(to: 1, duration: 0.3)
        flight.move(to: terminalFrame, radius: 0, duration: 0.42) {
            guard turn == moves.turn else { return }
            moves.grown = true
            revealPicked(force: false)
        }
        CATransaction.flush()
        // A switch that never shows ends the wait all the same.
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            if turn == moves.turn { revealPicked(force: true) }
        }
    }

    private func revealPicked(force: Bool) {
        guard let picked = moves.picked else { return }
        guard force || (moves.grown && currentThreadId == picked) else { return }
        moves.picked = nil
        flyBack(moves.turn)
    }

    /// The picture fills the screen: the terminal and its bar come back
    /// under it, the picture fades off it, and the cards are taken down
    /// after, out of sight under the terminal.
    private func flyBack(_ turn: Int) {
        guard turn == moves.turn else { return }
        guard flight.flying else { return fadeBack(turn) }
        terminalAlpha = 1
        cardsAlpha = 0
        overviewOpen = false
        DispatchQueue.main.async {
            guard turn == moves.turn else { return }
            flight.fadeOut(duration: 0.15) {
                if turn == moves.turn { overviewClosed() }
            }
        }
    }

    /// The cards are gone: the overview lets go of what it held, and what
    /// was set to run on its way in or out finds itself overtaken.
    private func overviewClosed() {
        moves.turn += 1
        moves.waiting = false
        flight.end()
        overviewOpen = false
        overviewVisible = false
        liveThread = nil
        liveShown = false
        moves.thumbs = [:]
        landingThumb = nil
        terminalAlpha = 1
        cardsAlpha = 0
        moves.landed = false
        moves.closing = false
        moves.grown = false
        moves.picked = nil
        model.releaseScreens()
    }

    // MARK: the terminal with its overlays

    private var terminal: some View {
        ZStack(alignment: .topLeading) {
            MetalView()
            TerminalInput()
            if model.navs.count > 1 && settings.paneBars {
                navBars
            }
            if settings.scrollbar && model.scroll.1 > 0 && model.scrollShown {
                scrollbar
            }
            if settings.devMode {
                Text(model.stats)
                    .font(.system(size: 8, design: .monospaced))
                    .foregroundColor(.white.opacity(0.7))
                    .padding(4)
                    .background(Color.black.opacity(0.5))
                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomLeading)
                    .allowsHitTesting(false)
            }
            if let composing = model.composing {
                Text(composing)
                    .font(.system(size: 14, design: .monospaced))
                    .padding(4)
                    .background(Color.yellow.opacity(0.9))
                    .foregroundColor(.black)
                    .padding(8)
            }
            if let card = model.status?.card {
                takeOverCard(card)
            }
            if showLog {
                logView
            }
            if model.reconnecting && !settings.autoReconnect && ConnectionPhase(status: model.connection) == nil {
                // Asked not to redial: the card offers it instead.
                ConnectionCard(phase: .disconnected, hostName: hostName, canEdit: model.host != nil) { action in
                    switch action {
                    case .back: leave()
                    default: model.connect()
                    }
                }
            }
            if let phase = ConnectionPhase(status: model.connection) {
                ConnectionCard(phase: phase, hostName: hostName, canEdit: model.host != nil) { action in
                    switch action {
                    case .retry: model.connect()
                    case .forgetKeyAndRetry:
                        if let host = model.host {
                            model.store?.forgetHostKey(for: host.id)
                            model.host?.knownHost = nil
                        }
                        model.connect()
                    case .edit: editingHost = model.host
                    case .back: leave()
                    }
                }
            }
        }
        .environmentObject(model)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(model.inset)
        .background(model.background)
    }

    /// A thin bar at the right: where the view is in the scrollback.
    private var scrollbar: some View {
        GeometryReader { geo in
            let (above, max) = model.scroll
            let total = CGFloat(max) + geo.size.height / Swift.max(model.cellHeight, 1)
            let visible = geo.size.height / Swift.max(model.cellHeight, 1)
            let thumb = Swift.max(geo.size.height * visible / Swift.max(total, 1), 24)
            let track = geo.size.height - thumb
            let y = track * (1 - CGFloat(above) / CGFloat(Swift.max(max, 1)))
            RoundedRectangle(cornerRadius: 2)
                .fill(Color.white.opacity(0.35))
                .frame(width: 3, height: thumb)
                .offset(x: geo.size.width - 6, y: y)
        }
        .allowsHitTesting(false)
        .transition(.opacity)
    }

    /// The App leaves rows above each pane for its bar; these draw it,
    /// where the App says (points, from the terminal's origin).
    private var navBars: some View {
        // A GeometryReader takes the terminal's box and never grows with
        // its children: a bar the App sizes to a wider tab (the server's,
        // until this phone's claim lands) must not widen the surface, or
        // the surface, the grid and the bars chase each other forever.
        GeometryReader { geo in
            ForEach(model.navs) { nav in
                navBar(nav, maxWidth: max(geo.size.width - nav.rect.left, 0))
            }
        }
    }

    private func navBar(_ nav: NavView, maxWidth: CGFloat) -> some View {
        HStack(spacing: 6) {
            ForEach(nav.members) { member in
                Button {
                    model.chromeClick("pane", pane: member.pane)
                } label: {
                    HStack(spacing: 4) {
                        if member.icon != nil {
                            TabIconCard(
                                core: model.core, id: member.icon, size: 15, busy: member.busy,
                                revision: model.iconsRevision
                            )
                        } else if member.busy {
                            ProgressView().controlSize(.mini)
                        }
                        Text(member.title.isEmpty ? "shell" : member.title)
                            .lineLimit(1)
                            .font(.caption2.weight(member.current ? .semibold : .regular))
                    }
                    .padding(.horizontal, 6)
                    .padding(.vertical, 2)
                    .background(member.current ? model.ink.opacity(nav.focused ? 0.22 : 0.12) : Color.clear)
                    .clipShape(Capsule())
                }
                .buttonStyle(.plain)
            }
            Spacer(minLength: 0)
            Button { model.chromeClick("new-pane", pane: nav.rect.pane) } label: {
                Image(systemName: "plus").font(.system(size: 11))
            }
            .buttonStyle(.plain)
            Button { model.chromeClick("close-pane", pane: nav.rect.pane) } label: {
                Image(systemName: "xmark").font(.system(size: 11))
            }
            .buttonStyle(.plain)
        }
        .foregroundColor(nav.focused ? model.ink : .gray)
        .padding(.horizontal, 6)
        .frame(width: min(nav.rect.width, maxWidth), height: nav.rect.height, alignment: .leading)
        // The bar's height is the core's: its text grows only so far.
        .dynamicTypeSize(...DynamicTypeSize.large)
        .background(model.background)
        .background(model.ink.opacity(nav.focused ? 0.1 : 0.04))
        .contentShape(Rectangle())
        .contextMenu {
            AppMenuItems(model: model, kind: "pane", id: String(nav.rect.pane))
        }
        .gesture(barDragGesture(nav))
        .offset(x: nav.rect.left, y: nav.rect.top)
    }

    /// Dragging a bar drags the divider it sits under (or beside, for a
    /// pane split to the right): the press lands on the divider's row or
    /// column, just outside the bar, and the App does the rest.
    private func barDragGesture(_ nav: NavView) -> some Gesture {
        DragGesture(minimumDistance: 10, coordinateSpace: .local)
            .onChanged { value in
                let r = nav.rect
                let origin: CGPoint
                if r.top > 0 {
                    origin = CGPoint(x: r.left + value.startLocation.x, y: r.top - 2)
                } else if r.left > 0 {
                    origin = CGPoint(x: r.left - 2, y: r.top + value.startLocation.y)
                } else {
                    return
                }
                if barDrag == nil {
                    barDrag = origin
                    model.pointer("down", at: origin)
                }
                model.pointer("move", at: CGPoint(x: origin.x + value.translation.width, y: origin.y + value.translation.height))
            }
            .onEnded { value in
                guard let origin = barDrag else { return }
                barDrag = nil
                model.pointer("up", at: CGPoint(x: origin.x + value.translation.width, y: origin.y + value.translation.height))
            }
    }

    private func takeOverCard(_ card: Card) -> some View {
        VStack(spacing: 8) {
            Text(card.title).font(.headline)
            Text(card.hint).font(.caption).multilineTextAlignment(.center)
            Button(card.action) { model.takeOver() }
                .buttonStyle(.borderedProminent)
                .disabled(card.state == "taking")
        }
        .padding(16)
        .frame(maxWidth: 320)
        .background(.ultraThinMaterial)
        .clipShape(RoundedRectangle(cornerRadius: 12))
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }

    private var logView: some View {
        ScrollView {
            Text(model.logText + "\n" + model.stats)
                .font(.system(size: 9, design: .monospaced))
                .foregroundColor(.white)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(6)
        }
        .frame(maxHeight: 220)
        .background(Color.black.opacity(0.8))
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomLeading)
    }

    // MARK: the status line

    @ViewBuilder
    private var statusLine: some View {
        let text = model.copied ? tr("copied") : (model.toastText ?? "")
        let showing = !text.isEmpty || model.connection.hasPrefix("connecting") || model.connection.hasPrefix("disconnected") || model.connection.hasPrefix("failed") || model.connection.contains("reconnect")
        if showing {
            Text(text.isEmpty ? model.connection : text)
                .font(.system(size: 11, design: .monospaced))
                .lineLimit(2)
                .foregroundColor(.white)
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(.horizontal, 8)
                .padding(.vertical, 4)
                .background(Color(white: 0.2))
        }
    }
}

/// What a menu is about, for the sheet.
struct MenuSheet: Identifiable {
    var kind: String
    var id: String
    var title: String
    var key: String { kind + ":" + id }
}

/// A thread's status as the desktop's sidebar shows it: a spinner while
/// it works, an alert while it waits on someone, a check when it finished
/// unseen; otherwise the core's dot -- the one on show, open, pinned,
/// unread or quiet. Every state takes the same square, `size` × 2, so a
/// row or a chip keeps its width as the status changes.
struct ThreadMark: View {
    var status: String
    var dot: String
    var size: CGFloat

    // The web page's tokens (tokens.css), which follow the desktop's.
    static let open = Color(hex: "#61B8FF") ?? .blue
    static let done = Color(hex: "#7CE5A2") ?? .green
    static let warn = Color(hex: "#DB7320") ?? .orange

    var body: some View {
        Group {
            switch status {
            case "Running":
                Spinner(color: Self.open).padding(size * 0.15)
            case "NeedsAttention":
                Image(systemName: "exclamationmark.circle.fill")
                    .resizable().scaledToFit().foregroundColor(Self.warn)
            case "Done":
                Image(systemName: "checkmark.circle.fill")
                    .resizable().scaledToFit().foregroundColor(Self.done)
            default:
                Circle().fill(Self.dotColor(dot)).frame(width: size, height: size)
            }
        }
        .frame(width: size * 2, height: size * 2)
    }

    static func dotColor(_ dot: String) -> Color {
        switch dot {
        case "Active": return done
        case "Open": return open
        case "Pinned": return Color(hex: "#C7C7CC") ?? .gray
        case "Unread": return Color(hex: "#8E8E93") ?? .gray
        default: return Color.white.opacity(0.16)
        }
    }
}

/// The desktop's and the page's busy mark: an open ring turning once in
/// 0.96 s, drawn to the size it is given.
struct Spinner: View {
    var color: Color
    @State private var turning = false

    var body: some View {
        Circle()
            .trim(from: 0, to: 0.75)
            .stroke(color, style: StrokeStyle(lineWidth: 1.6, lineCap: .round))
            .rotationEffect(.degrees(turning ? 360 : 0))
            // Started again each time it shows: a lazy list's row comes
            // back with its state, and an animation that ran out.
            .onAppear {
                turning = false
                withAnimation(.linear(duration: 0.96).repeatForever(autoreverses: false)) { turning = true }
            }
            .onDisappear { turning = false }
    }
}

/// The App's context menu for something, as the system menu that a long
/// press opens: its items, checks, separators and submenus, built when
/// the menu shows.
struct AppMenuItems: View {
    @ObservedObject var model: TerminalModel
    var kind: String
    var id: String

    var body: some View {
        let items = model.contextMenu(kind, id: id)
        ForEach(items, id: \.rowId) { item in
            Self.row(item, model: model)
        }
    }

    static func row(_ item: MenuItem, model: TerminalModel) -> AnyView {
        switch item.kind {
        case "separator":
            return AnyView(Divider())
        case "header":
            return AnyView(Text(item.label))
        default:
            if !item.submenu.isEmpty {
                let subs = item.submenu
                return AnyView(Menu(item.label) {
                    ForEach(subs, id: \.rowId) { sub in row(sub, model: model) }
                })
            }
            // Deleting a thread ends its programs: it is asked first.
            let deletes = item.id.hasPrefix("delete-thread:")
            return AnyView(
                Button(role: deletes ? .destructive : nil) {
                    if deletes { model.confirming = item.id } else { model.menuAction(item.id) }
                } label: {
                    if item.checked {
                        Label(item.label, systemImage: "checkmark")
                    } else {
                        Text(item.label)
                    }
                }
                .disabled(!item.enabled)
            )
        }
    }
}

struct MenuList: View {
    @ObservedObject var model: TerminalModel
    @ObservedObject private var lang = AppLanguage.shared
    var sheet: MenuSheet
    var dismiss: () -> Void
    @State private var items: [MenuItem] = []

    var body: some View {
        NavigationStack {
            List {
                ForEach(items, id: \.rowId) { item in
                    row(item)
                }
            }
            .navigationTitle(sheet.title)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) { Button(tr("done"), action: dismiss) }
            }
        }
        .onAppear { items = model.contextMenu(sheet.kind, id: sheet.id) }
    }

    /// Rows nest (a submenu is a row that pushes more rows), so the view
    /// is erased rather than recursively opaque.
    private func row(_ item: MenuItem) -> AnyView {
        switch item.kind {
        case "separator":
            return AnyView(Divider())
        case "header":
            return AnyView(Text(item.label).font(.caption).foregroundColor(.secondary))
        default:
            if !item.submenu.isEmpty {
                let subs = item.submenu
                return AnyView(
                    NavigationLink(item.label) {
                        List {
                            ForEach(subs, id: \.rowId) { sub in row(sub) }
                        }
                        .navigationTitle(item.label)
                    }
                )
            }
            return AnyView(
                Button {
                    model.menuAction(item.id)
                    dismiss()
                } label: {
                    HStack {
                        Text(item.label)
                        Spacer()
                        if item.checked { Image(systemName: "checkmark") }
                    }
                }
                .disabled(!item.enabled)
            )
        }
    }
}


/// Where a connection is, read off the core's status line.
enum ConnectionPhase: Equatable {
    case connecting(String)
    case attaching
    case reconnecting
    case failed(String)
    case disconnected

    init?(status: String) {
        if status.hasPrefix("pane ") { return nil }
        if status.hasPrefix("connecting") {
            self = .connecting(String(status.dropFirst("connecting to ".count)))
        } else if status.hasPrefix("connected") {
            self = .attaching
        } else if status.contains("reconnect") {
            self = .reconnecting
        } else if status.hasPrefix("failed: ") {
            self = .failed(String(status.dropFirst("failed: ".count)))
        } else if status.hasPrefix("attach failed: ") {
            self = .failed(String(status.dropFirst("attach failed: ".count)))
        } else if status.hasPrefix("disconnected: ") {
            self = .failed(String(status.dropFirst("disconnected: ".count)))
        } else if status.hasPrefix("disconnected") {
            self = .disconnected
        } else {
            // "idle", or nothing yet: the connection is about to start.
            self = .connecting("")
        }
    }
}

/// The card over the terminal while there is no terminal to show: the
/// steps of a connection, or what stopped it and what to do about it.
struct ConnectionCard: View {
    enum Action { case retry, forgetKeyAndRetry, edit, back }

    @ObservedObject private var lang = AppLanguage.shared

    var phase: ConnectionPhase
    var hostName: String
    var canEdit: Bool
    var act: (Action) -> Void

    var body: some View {
        VStack(spacing: 14) {
            switch phase {
            case .connecting(let target):
                ProgressView().controlSize(.large)
                Text(tr("card.connecting", hostName)).font(.headline)
                if !target.isEmpty {
                    Text(target).font(.caption.monospaced()).foregroundColor(.secondary)
                }
                steps(done: 0)
            case .attaching:
                ProgressView().controlSize(.large)
                Text(tr("card.attaching", hostName)).font(.headline)
                steps(done: 1)
            case .reconnecting:
                ProgressView().controlSize(.large)
                Text(tr("card.reconnecting", hostName)).font(.headline)
            case .disconnected:
                Image(systemName: "bolt.slash").font(.system(size: 34)).foregroundColor(.secondary)
                Text(tr("conn.disconnected")).font(.headline)
                HStack {
                    Button(tr("back")) { act(.back) }.buttonStyle(.bordered)
                    Button(tr("reconnect")) { act(.retry) }.buttonStyle(.borderedProminent)
                }
            case .failed(let reason):
                Image(systemName: "exclamationmark.triangle").font(.system(size: 34)).foregroundColor(.orange)
                Text(tr("card.failed", hostName)).font(.headline).multilineTextAlignment(.center)
                Text(reason)
                    .font(.caption.monospaced())
                    .foregroundColor(.secondary)
                    .multilineTextAlignment(.center)
                    .lineLimit(6)
                if let hint = Self.hint(for: reason) {
                    Text(hint).font(.footnote).multilineTextAlignment(.center)
                }
                HStack {
                    Button(tr("back")) { act(.back) }.buttonStyle(.bordered)
                    if canEdit {
                        Button(tr("edithost")) { act(.edit) }.buttonStyle(.bordered)
                    }
                    if reason.contains("key changed") && canEdit {
                        Button(tr("forgetretry")) { act(.forgetKeyAndRetry) }.buttonStyle(.borderedProminent)
                    } else {
                        Button(tr("retry")) { act(.retry) }.buttonStyle(.borderedProminent)
                    }
                }
            }
        }
        .padding(20)
        .frame(maxWidth: 340)
        .background(.regularMaterial)
        .clipShape(RoundedRectangle(cornerRadius: 16))
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black.opacity(0.4))
    }

    private func steps(done: Int) -> some View {
        VStack(alignment: .leading, spacing: 6) {
            step(tr("conn.step1"), state: done > 0 ? 2 : 1)
            step(tr("conn.step2"), state: done > 1 ? 2 : (done == 1 ? 1 : 0))
        }
        .font(.footnote)
        .padding(.top, 4)
    }

    private func step(_ label: String, state: Int) -> some View {
        HStack(spacing: 8) {
            Image(systemName: state == 2 ? "checkmark.circle.fill" : (state == 1 ? "circle.dotted" : "circle"))
                .foregroundColor(state == 2 ? .green : .secondary)
            Text(label).foregroundColor(state == 0 ? .secondary : .primary)
        }
    }

    /// What the reason usually means, in plain words.
    static func hint(for reason: String) -> String? {
        let r = reason.lowercased()
        if r.contains("refused the login") || r.contains("auth") {
            return tr("hint.auth")
        }
        if r.contains("key changed") {
            return tr("hint.keychanged")
        }
        if r.contains("timed out") || r.contains("connection refused") || r.contains("unreachable") || r.contains("no route") {
            return tr("hint.unreachable")
        }
        if r.contains("not found") || r.contains("exit 127") || r.contains("no such file") {
            return tr("hint.notfound")
        }
        if r.contains("codec") || r.contains("version") {
            return tr("hint.version")
        }
        return nil
    }
}

/// The tab strip's clip: its own width, with room above and below for the
/// glass to swell into as it is pressed.
private struct SideClip: Shape {
    func path(in rect: CGRect) -> Path { Path(rect.insetBy(dx: 0, dy: -16)) }
}

/// Its content at most `width` across, however much is offered: in a
/// scroll view, which offers none, a frame's limit alone sizes the frame
/// but leaves a long name whole and spilling out of it.
private struct AtMost: Layout {
    var width: CGFloat

    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
        guard let content = subviews.first else { return .zero }
        let ideal = content.sizeThatFits(.unspecified).width
        return content.sizeThatFits(ProposedViewSize(width: min(proposal.width ?? ideal, ideal, width), height: proposal.height))
    }

    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
        subviews.first?.place(at: bounds.origin, proposal: ProposedViewSize(bounds.size))
    }
}

/// What the overview's moves keep to themselves, out of SwiftUI's state so
/// that keeping it redraws nothing: which move is the latest (a late end
/// belongs to an older one), whether the terminal's picture is in its
/// card, and the thread a tapped card is bringing back.
final class OverviewMoves {
    var turn = 0
    /// Each card's picture, by thread, in the screen's space: read as a
    /// flight sets off, not drawn from.
    var thumbs: [String: CGRect] = [:]
    var landed = false
    /// On the way back (`closeOverview`, `pick`): taps wait.
    var closing = false
    var grown = false
    var picked: String?
    /// An open waits for the drawer or the keyboard to go first.
    var waiting = false
    /// The screen is up: what was set to run on it runs only then.
    var onScreen = false
}

/// Whether tabs are scrolled off the strip's start, and lie past its end.
private struct StripEnds: Equatable {
    var before = false
    var after = false
}

/// Where the drag from the left edge under way set off, and whether it
/// pulls the drawer: decided once, as it sets off.
final class EdgeDrag {
    var start: CGPoint?
    var pulls = false
}