// SPDX-License-Identifier: AGPL-3.0-only // Copyright (C) 2026 Neo-Desktop import DModemShared import SwiftUI import UIKit /// The terminal: the screen drawn run by run in its cells, the keyboard /// on a tap (KeyInput), the keys a phone's keyboard lacks below it. struct TerminalScreen: View { @ObservedObject var model: TerminalModel @State private var focused = true @AppStorage("terminalFontSize") private var fontSize: Double = 12 var body: some View { VStack(spacing: 0) { GeometryReader { geo in let cell = CellMetrics(fontSize: fontSize) ScreenCanvas(model: model, revision: model.revision, cell: cell) .background(Color(argb: model.bridge.background)) .onAppear { fit(geo.size, cell) } .onChange(of: geo.size) { fit($0, cell) } .onChange(of: fontSize) { _ in fit(geo.size, CellMetrics(fontSize: fontSize)) } .overlay(KeyInput(model: model, focused: $focused).frame(width: 1, height: 1).opacity(0.01), alignment: .topLeading) .contentShape(Rectangle()) .onTapGesture { focused = true } .gesture(MagnificationGesture().onEnded { s in fontSize = min(24, max(7, fontSize * Double(s))) }) } ExtraKeys(model: model) } .navigationTitle(model.bridge.title.isEmpty ? "Terminal" : model.bridge.title) .navigationBarTitleDisplayMode(.inline) } /// as many cells as fit private func fit(_ size: CGSize, _ cell: CellMetrics) { model.resize(cols: max(20, Int(size.width / cell.width)), rows: max(5, Int(size.height / cell.height))) } } /// a cell's size in the terminal's font struct CellMetrics { let font: UIFont let width: CGFloat let height: CGFloat init(fontSize: Double) { font = UIFont.monospacedSystemFont(ofSize: fontSize, weight: .regular) width = ceil(("M" as NSString).size(withAttributes: [.font: font]).width) height = ceil(font.lineHeight) } } /// the screen: each row's runs, their backgrounds, their text, the cursor private struct ScreenCanvas: View { @ObservedObject var model: TerminalModel /// (read here so the canvas redraws with each change) let revision: Int64 let cell: CellMetrics var body: some View { Canvas { ctx, _ in let regular = Font(cell.font as CTFont) let bold = Font(UIFont.monospacedSystemFont(ofSize: cell.font.pointSize, weight: .bold) as CTFont) for row in 0.. Void) -> some View { Button(action: action) { Text(label).frame(minWidth: 32, minHeight: 32) } .buttonStyle(.bordered) } } /// a view the keyboard types into: its text and keys to the terminal /// (UIKeyInput), the hardware keyboard's arrows and Esc too struct KeyInput: UIViewRepresentable { let model: TerminalModel @Binding var focused: Bool func makeUIView(context: Context) -> KeyInputView { let v = KeyInputView() v.model = model return v } func updateUIView(_ v: KeyInputView, context: Context) { v.model = model if focused && !v.isFirstResponder { DispatchQueue.main.async { v.becomeFirstResponder() } } } } /// (a UIView: on the main actor, as the terminal's model is) final class KeyInputView: UIView, UIKeyInput { weak var model: TerminalModel? override var canBecomeFirstResponder: Bool { true } var hasText: Bool { true } // (a terminal's keyboard: nothing corrected, capitalised or made smart) var autocorrectionType: UITextAutocorrectionType = .no var autocapitalizationType: UITextAutocapitalizationType = .none var spellCheckingType: UITextSpellCheckingType = .no var smartQuotesType: UITextSmartQuotesType = .no var smartDashesType: UITextSmartDashesType = .no var smartInsertDeleteType: UITextSmartInsertDeleteType = .no var keyboardType: UIKeyboardType = .asciiCapable func insertText(_ text: String) { model?.typed(text) } func deleteBackward() { model?.key("BACKSPACE") } override var keyCommands: [UIKeyCommand]? { [ (UIKeyCommand.inputUpArrow, "UP"), (UIKeyCommand.inputDownArrow, "DOWN"), (UIKeyCommand.inputLeftArrow, "LEFT"), (UIKeyCommand.inputRightArrow, "RIGHT"), (UIKeyCommand.inputEscape, "ESCAPE"), ("\t", "TAB"), ].map { input, name in let c = UIKeyCommand(input: input, modifierFlags: [], action: #selector(keyCommand(_:))) c.wantsPriorityOverSystemBehavior = true c.title = name return c } } @objc private func keyCommand(_ c: UIKeyCommand) { guard let name = c.title.isEmpty ? nil : c.title else { return } model?.key(name) } }